Skip to content

Commit

Permalink
Merge pull request #1 from icaruseu/0.1.6
Browse files Browse the repository at this point in the history
0.1.6
  • Loading branch information
yngwi authored Mar 15, 2023
2 parents cbb6253 + aff391a commit 50322f3
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 23 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified conftest.py
100644 → 100755
Empty file.
Empty file modified pytest_helpers.py
100644 → 100755
Empty file.
Empty file modified requirements.txt
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion setup.py
100644 → 100755
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.5",
version="0.1.6",
description="to-CEI",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
12 changes: 11 additions & 1 deletion test/test_charter.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from pytest_helpers import xp, xps
from to_cei.charter import NO_DATE_TEXT, NO_DATE_VALUE, Charter
from to_cei.config import CEI, CHARTER_NSS
from to_cei.config import (CEI, CHARTER_NSS, SCHEMA_LOCATION,
SCHEMA_LOCATION_QNAME)
from to_cei.helpers import ln
from to_cei.seal import Seal
from to_cei.validator import Validator
Expand Down Expand Up @@ -88,6 +89,15 @@ def test_writes_correct_file(tmp_path):
Validator().validate_cei(written.getroot())


def test_add_schema_location_is_respected():
charter = Charter("1A")
assert (
charter.to_xml(add_schema_location=True).get(SCHEMA_LOCATION_QNAME)
== SCHEMA_LOCATION
)
assert charter.to_xml(add_schema_location=False).get(SCHEMA_LOCATION_QNAME) == None


# --------------------------------------------------------------------#
# Charter abstract #
# --------------------------------------------------------------------#
Expand Down
10 changes: 10 additions & 0 deletions test/test_charter_group.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from to_cei.charter import Charter
from to_cei.charter_group import CharterGroup
from to_cei.config import SCHEMA_LOCATION, SCHEMA_LOCATION_QNAME
from to_cei.validator import Validator


Expand All @@ -23,6 +24,15 @@ def test_writes_correct_file(tmp_path):
Validator().validate_cei(written.getroot())


def test_add_schema_location_is_respected():
group = CharterGroup("Charter group", [Charter("1A"), Charter("1b")])
assert (
group.to_xml(add_schema_location=True).get(SCHEMA_LOCATION_QNAME)
== SCHEMA_LOCATION
)
assert group.to_xml(add_schema_location=False).get(SCHEMA_LOCATION_QNAME) == None


def test_raises_exception_for_empty_name():
with pytest.raises(ValueError):
CharterGroup("")
Empty file modified test/test_helpers.py
100644 → 100755
Empty file.
Empty file modified test/test_seal.py
100644 → 100755
Empty file.
Empty file modified test/test_validator.py
100644 → 100755
Empty file.
32 changes: 24 additions & 8 deletions to_cei/charter.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from astropy.time import Time
from lxml import etree

from to_cei.config import CEI
from to_cei.config import CEI, CEI_SCHEMA_LOCATION_ATTRIBUTE
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
Expand Down Expand Up @@ -967,13 +967,16 @@ def _create_cei_tenor(self) -> Optional[etree._Element]:
else CEI.tenor(self.transcription)
)

def _create_cei_text(self) -> etree._Element:
return CEI.text(
def _create_cei_text(self, add_schema_location: bool = False) -> etree._Element:
text = CEI.text(
self._create_cei_front(),
self._create_cei_body(),
self._create_cei_back(),
type="charter",
)
if add_schema_location:
text.attrib.update(CEI_SCHEMA_LOCATION_ATTRIBUTE)
return text

def _create_cei_traditio_form(self) -> Optional[etree._Element]:
return None if not self._tradition else CEI.traditioForm(self._tradition)
Expand All @@ -993,13 +996,26 @@ def _create_cei_witness_orig(self) -> Optional[etree._Element]:
# Public methods #
# --------------------------------------------------------------------#

def to_xml(self) -> etree._Element:
return self._create_cei_text()
def to_xml(self, add_schema_location: bool = False) -> etree._Element:
"""Creates an xml representation of the charter.
def to_file(self, folder: Optional[str] = None):
Args:
add_schema_location: If True, the CEI schema location is added to the root element.
Returns:
An etree Element object representing the charter.
"""
return self._create_cei_text(add_schema_location)

def to_file(self, folder: Optional[str] = None, add_schema_location: bool = False):
"""Writes the xml representation of the charter to a file. The filename is generated from the normalized charter id.
Args:
folder (str): The folder to write the file to. If this is ommitted, the file is written to the place where the script is
folder (str): The folder to write the file to. If this is ommitted, the file is written to the place where the script is executed from.
add_schema_location (bool): If True, the CEI schema location is added to the root element. Defaults to False.
"""
return super(Charter, self).to_file(self.id_norm + ".cei", folder=folder)
return super(Charter, self).to_file(
self.id_norm + ".cei",
folder=folder,
add_schema_location=add_schema_location,
)
30 changes: 24 additions & 6 deletions to_cei/charter_group.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from lxml import etree

from to_cei.charter import Charter
from to_cei.config import CEI
from to_cei.config import CEI, CEI_SCHEMA_LOCATION_ATTRIBUTE
from to_cei.xml_assembler import XmlAssembler


Expand Down Expand Up @@ -39,14 +39,32 @@ def name(self, value: str):
raise ValueError("Group name cannot be empty")
self._name = value

def to_xml(self) -> etree._Element:
xml = CEI.cei(
def to_xml(self, add_schema_location: bool = False) -> etree._Element:
"""Creates an xml representation of the charter group.
Args:
add_schema_location: If True, the CEI schema location is added to the root element.
Returns:
An etree Element object representing the charter group.
"""
cei = CEI.cei(
CEI.teiHeader(CEI.fileDesc(CEI.titleStmt(CEI.title(self.name)))),
CEI.text(CEI.group(*[charter.to_xml() for charter in self.charters])),
)
return xml
if add_schema_location:
cei.attrib.update(CEI_SCHEMA_LOCATION_ATTRIBUTE)
return cei

def to_file(self, folder: Optional[str] = None):
def to_file(self, folder: Optional[str] = None, add_schema_location: bool = False):
"""Writes the xml representation of the charter group to a file. The filename is generated from a normalization of the group name.
Args:
folder (str): The folder to write the file to. If this is ommitted, the file is written to the place where the script is executed from.
add_schema_location (bool): If True, the CEI schema location is added to the root element. Defaults to False.
"""
return super(CharterGroup, self).to_file(
self.name.lower().replace(" ", "_") + ".cei.group", folder=folder
self.name.lower().replace(" ", "_") + ".cei.group",
folder=folder,
add_schema_location=add_schema_location,
)
16 changes: 15 additions & 1 deletion to_cei/config.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from lxml import etree
from lxml.builder import ElementMaker # type: ignore

from to_cei import filecache
Expand All @@ -8,6 +9,19 @@

CEI_PREFIX: str = "cei"

CHARTER_NSS = {CEI_PREFIX: CEI_NS}
CHARTER_NSS = {
CEI_PREFIX: CEI_NS,
}

SCHEMA_LOCATION_QNAME = etree.QName(
"http://www.w3.org/2001/XMLSchema-instance", "schemaLocation"
)

SCHEMA_LOCATION = f"{CEI_NS} {CEI_NS}"

CEI_SCHEMA_LOCATION_ATTRIBUTE = {SCHEMA_LOCATION_QNAME: SCHEMA_LOCATION}

XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"


CEI = ElementMaker(namespace=CEI_NS, nsmap=CHARTER_NSS)
Empty file modified to_cei/filecache.py
100644 → 100755
Empty file.
Empty file modified to_cei/helpers.py
100644 → 100755
Empty file.
Empty file modified to_cei/seal.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion to_cei/validator.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class Schema(str, Enum):
CEI = "https://raw.githubusercontent.com/icaruseu/mom-ca/master/my/XRX/src/mom/app/cei/xsd/cei.xsd"
CEI = config.CEI_NS


class Validator:
Expand Down
27 changes: 22 additions & 5 deletions to_cei/xml_assembler.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,39 @@

class XmlAssembler(ABC):
@abstractmethod
def to_xml(self) -> Optional[etree._Element]:
def to_xml(self, add_schema_location: bool = False) -> Optional[etree._Element]:
pass

def to_string(self) -> str:
xml = self.to_xml()
def to_string(self, add_schema_location: bool = False) -> str:
"""Serializes the xml representation of the object to a string.
Args:
add_schema_location: If True, the CEI schema location is added to the root element.
Returns:
A string representation of the object.
"""
xml = self.to_xml(add_schema_location)
return (
""
if xml is None
else str(etree.tostring(xml, encoding="unicode", pretty_print=True))
else str(
etree.tostring(
xml,
encoding="unicode",
pretty_print=True,
)
)
)

def to_file(
self,
name: str,
folder: Optional[str | Path] = None,
inclusive_ns_prefixes: List[str] = [],
add_schema_location: bool = False,
):
xml = self.to_xml()
xml = self.to_xml(add_schema_location)
if xml is None:
raise Exception("Failed to read xml")
folder = (
Expand All @@ -45,4 +60,6 @@ def to_file(
encoding="UTF-8",
pretty_print=True,
inclusive_ns_prefixes=[CEI_PREFIX] + inclusive_ns_prefixes,
xml_declaration=True,
standalone=False,
)

0 comments on commit 50322f3

Please sign in to comment.