Skip to content

Commit

Permalink
Use functions in setters to simplify them
Browse files Browse the repository at this point in the history
  • Loading branch information
yngwi committed Mar 22, 2022
1 parent c74373e commit ca5e819
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 88 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# This call to setup() does all the work
setup(
name="to_cei",
version="0.1.4",
version="0.1.5",
description="to-CEI",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
117 changes: 37 additions & 80 deletions to_cei/charter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from lxml import etree

from to_cei.config import CEI
from to_cei.helpers import join, validate_element
from to_cei.helpers import (get_str, get_str_list, get_str_or_element,
get_str_or_element_list, join)
from to_cei.seal import Seal
from to_cei.xml_assembler import XmlAssembler

Expand Down Expand Up @@ -187,7 +188,7 @@ def __init__(
witnesses: Optional[List[str | etree._Element]] = [],
) -> None:
"""
Creates a new charter object.
Creates a new charter object. Empty strings in the parameters are treated similar to None values.
Args:
id_text: The human readable id of the charter. If id_norm is not present, id_text will be used in a normalized form. If it is missing or empty, an exception will be raised.
Expand Down Expand Up @@ -286,75 +287,63 @@ def abstract(self, value: Optional[str | etree._Element] = None):
raise ValueError(
"XML element content for both issuer and abstract is not allowed, please join the issuer in the XML abstract yourself"
)
self._abstract = validate_element(value, "abstract")
self._abstract = get_str_or_element(value, "abstract")

@property
def abstract_sources(self):
return self._abstract_sources

@abstract_sources.setter
def abstract_sources(self, value: Optional[str | List[str]] = []):
self._abstract_sources = (
[]
if value is None or (isinstance(value, str) and not len(value))
else (value if isinstance(value, List) else [value])
)
self._abstract_sources = get_str_list(value)

@property
def archive(self):
return self._archive

@archive.setter
def archive(self, value: Optional[str] = None):
self._archive = value if value is not None and len(value) else None
self._archive = get_str(value)

@property
def chancellary_remarks(self):
return self._chancellary_remarks

@chancellary_remarks.setter
def chancellary_remarks(self, value: Optional[str | List[str]] = []):
self._chancellary_remarks = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._chancellary_remarks = get_str_list(value)

@property
def comments(self):
return self._comments

@comments.setter
def comments(self, value: Optional[str | List[str]] = []):
self._comments = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._comments = get_str_list(value)

@property
def condition(self):
return self._condition

@condition.setter
def condition(self, value: Optional[str] = None):
self._condition = value if isinstance(value, str) and len(value) else None
self._condition = get_str(value)

@property
def date(self):
return self._date

@date.setter
def date(self, value: Optional[str | etree._Element] = None):
self._date = validate_element(value, "date", "dateRange")
self._date = get_str_or_element(value, "date", "dateRange")

@property
def date_quote(self):
return self._date_quote

@date_quote.setter
def date_quote(self, value: Optional[str | etree._Element] = None):
self._date_quote = validate_element(value, "quoteOriginaldatierung")
self._date_quote = get_str_or_element(value, "quoteOriginaldatierung")

@property
def date_value(self):
Expand Down Expand Up @@ -423,7 +412,7 @@ def dimensions(self):

@dimensions.setter
def dimensions(self, value: Optional[str] = None):
self._dimensions = value if isinstance(value, str) and len(value) else None
self._dimensions = get_str(value)

@property
def external_link(self):
Expand All @@ -447,39 +436,31 @@ def footnotes(self):

@footnotes.setter
def footnotes(self, value: Optional[str | List[str]] = []):
self._footnotes = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._footnotes = get_str_list(value)

@property
def graphic_urls(self):
return self._graphic_urls

@graphic_urls.setter
def graphic_urls(self, value: Optional[str | List[str]] = []):
self._graphic_urls = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._graphic_urls = get_str_list(value)

@property
def id_norm(self):
return quote(self._id_norm if self._id_norm else self.id_text)

@id_norm.setter
def id_norm(self, value: Optional[str] = None):
self._id_norm = value if isinstance(value, str) and len(value) else None
self._id_norm = get_str(value)

@property
def id_old(self):
return self._id_old

@id_old.setter
def id_old(self, value: Optional[str] = None):
self._id_old = value if isinstance(value, str) and len(value) else None
self._id_old = get_str(value)

@property
def id_text(self):
Expand All @@ -495,47 +476,47 @@ def index(self):

@index.setter
def index(self, value: Optional[List[str | etree._Element]] = []):
self._index = [validate_element(item, "index") for item in value] if isinstance(value, List) else [] # type: ignore
self._index = get_str_or_element_list(value, "index")

@property
def index_geo_features(self):
return self._index_geo_features

@index_geo_features.setter
def index_geo_features(self, value: Optional[List[str | etree._Element]] = []):
self._index_geo_features = [validate_element(item, "geogName") for item in value] if isinstance(value, List) else [] # type: ignore
self._index_geo_features = get_str_or_element_list(value, "geogName")

@property
def index_organizations(self):
return self._index_organizations

@index_organizations.setter
def index_organizations(self, value: Optional[List[str | etree._Element]] = []):
self._index_organizations = [validate_element(item, "orgName") for item in value] if isinstance(value, List) else [] # type: ignore
self._index_organizations = get_str_or_element_list(value, "orgName")

@property
def index_persons(self):
return self._index_persons

@index_persons.setter
def index_persons(self, value: Optional[List[str | etree._Element]] = []):
self._index_persons = [validate_element(item, "persName") for item in value] if isinstance(value, List) else [] # type: ignore
self._index_persons = get_str_or_element_list(value, "persName")

@property
def index_places(self):
return self._index_places

@index_places.setter
def index_places(self, value: Optional[List[str | etree._Element]] = []):
self._index_places = [validate_element(item, "placeName") for item in value] if isinstance(value, List) else [] # type: ignore
self._index_places = get_str_or_element_list(value, "placeName")

@property
def issued_place(self):
return self._issued_place

@issued_place.setter
def issued_place(self, value: Optional[str | etree._Element] = None):
self._issued_place = validate_element(value, "placeName")
self._issued_place = get_str_or_element(value, "placeName")

@property
def issuer(self):
Expand All @@ -547,91 +528,71 @@ def issuer(self, value: Optional[str | etree._Element] = None):
raise ValueError(
"XML element content for both issuer and abstract is not allowed, please join the issuer in the XML abstract yourself"
)
self._issuer = validate_element(value, "issuer")
self._issuer = get_str_or_element(value, "issuer")

@property
def language(self):
return self._language

@language.setter
def language(self, value: Optional[str] = None):
self._language = value if isinstance(value, str) and len(value) else None
self._language = get_str(value)

@property
def literature(self):
return self._literature

@literature.setter
def literature(self, value: Optional[str | List[str]] = []):
self._literature = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._literature = get_str_list(value)

@property
def literature_abstracts(self):
return self._literature_abstracts

@literature_abstracts.setter
def literature_abstracts(self, value: Optional[str | List[str]] = []):
self._literature_abstracts = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._literature_abstracts = get_str_list(value)

@property
def literature_depictions(self):
return self._literature_depictions

@literature_depictions.setter
def literature_depictions(self, value: Optional[str | List[str]] = []):
self._literature_depictions = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._literature_depictions = get_str_list(value)

@property
def literature_editions(self):
return self._literature_editions

@literature_editions.setter
def literature_editions(self, value: Optional[str | List[str]] = []):
self._literature_editions = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._literature_editions = get_str_list(value)

@property
def literature_secondary(self):
return self._literature_secondary

@literature_secondary.setter
def literature_secondary(self, value: Optional[str | List[str]] = []):
self._literature_secondary = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._literature_secondary = get_str_list(value)

@property
def material(self):
return self._material

@material.setter
def material(self, value: Optional[str] = None):
self._material = value if isinstance(value, str) and len(value) else None
self._material = get_str(value)

@property
def notarial_authentication(self):
return self._notarial_authentication

@notarial_authentication.setter
def notarial_authentication(self, value: Optional[str | etree._Element] = None):
self._notarial_authentication = validate_element(value, "notariusDesc")
self._notarial_authentication = get_str_or_element(value, "notariusDesc")

@property
def recipient(self):
Expand All @@ -643,7 +604,7 @@ def recipient(self, value: Optional[str | etree._Element] = None):
raise ValueError(
"XML element content for both recipient and abstract is not allowed, please join the recipient in the XML abstract yourself"
)
self._recipient = validate_element(value, "recipient")
self._recipient = get_str_or_element(value, "recipient")

@property
def seals(self):
Expand All @@ -657,7 +618,7 @@ def seals(
if value is None or isinstance(value, str) and len(value) == 0:
return None
validated = (
validate_element(value, "sealDesc")
get_str_or_element(value, "sealDesc")
if isinstance(value, etree._Element)
else value
)
Expand All @@ -672,35 +633,31 @@ def tradition(self):

@tradition.setter
def tradition(self, value: Optional[str] = None):
self._tradition = value if isinstance(value, str) and len(value) else None
self._tradition = get_str(value)

@property
def transcription(self):
return self._transcription

@transcription.setter
def transcription(self, value: Optional[str | etree._Element] = None):
self._transcription = validate_element(value, "tenor")
self._transcription = get_str_or_element(value, "tenor")

@property
def transcription_sources(self):
return self._transcription_sources

@transcription_sources.setter
def transcription_sources(self, value: Optional[str | List[str]] = []):
self._transcription_sources = (
[]
if value is None or (isinstance(value, str) and not len(value))
else ([value] if isinstance(value, str) else value)
)
self._transcription_sources = get_str_list(value)

@property
def witnesses(self):
return self._witnesses

@witnesses.setter
def witnesses(self, value: Optional[List[str | etree._Element]] = []):
self._witnesses = [validate_element(item, "persName") for item in value] if isinstance(value, List) else [] # type: ignore
self._witnesses = get_str_or_element_list(value, "persName")

# --------------------------------------------------------------------#
# Private CEI creators #
Expand Down
Loading

0 comments on commit ca5e819

Please sign in to comment.