Skip to content

Commit

Permalink
Merge branch 'dev/fixes-for-new-xsd_1' into dev/flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrpfn committed Sep 29, 2021
2 parents d091bb1 + f14874a commit b97cdd1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
51 changes: 43 additions & 8 deletions ascmhl/hashlist_xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ def write_hash_list(hash_list: MHLHashList, file_path: str):
current_indent += " "

for media_hash in hash_list.media_hashes:
_write_xml_element_to_file(file, _media_hash_xml_element(media_hash), current_indent)
if media_hash.is_directory:
_write_xml_element_to_file(file, _directory_hash_xml_element(media_hash), current_indent)
else:
_write_xml_element_to_file(file, _media_hash_xml_element(media_hash), current_indent)

current_indent = current_indent[:-2]
_write_xml_string_to_file(file, "</hashes>\n", current_indent)
Expand Down Expand Up @@ -214,13 +217,8 @@ def _media_hash_xml_element(media_hash: MHLMediaHash):
path_element.attrib["lastmodificationdate"] = datetime_isostring(media_hash.last_modification_date)

hash_element = E.hash(path_element)
if media_hash.is_directory:
hash_element.attrib["directory"] = "true"

for hash_entry in media_hash.hash_entries:
entry_element = E(hash_entry.hash_format)
if hash_entry.structure_hash_string is not None:
entry_element.attrib["structure"] = hash_entry.structure_hash_string
entry_element.text = hash_entry.hash_string
if hash_entry.action:
entry_element.attrib["action"] = hash_entry.action
Expand All @@ -230,6 +228,43 @@ def _media_hash_xml_element(media_hash: MHLMediaHash):

return hash_element

def _directory_hash_xml_element(media_hash: MHLMediaHash, skipPath = False):
"""builds and returns one <hash> element for a given MediaHash object"""

content_element = E.content()
structure_element = E.structure()

for hash_entry in media_hash.hash_entries:
entry_element_content = E(hash_entry.hash_format)
entry_element_content.text = hash_entry.hash_string

entry_element_structure = E(hash_entry.hash_format)
entry_element_structure.text = hash_entry.structure_hash_string

if hash_entry.action:
entry_element_content.attrib["action"] = hash_entry.action
entry_element_structure.attrib["action"] = hash_entry.action
if hash_entry.hash_date:
entry_element_content.attrib["hashdate"] = datetime_isostring(hash_entry.hash_date, True)
entry_element_structure.attrib["hashdate"] = datetime_isostring(hash_entry.hash_date, True)

content_element.append(entry_element_content)
structure_element.append(entry_element_structure)

hash_element = E.directoryhash()

if skipPath == False:
path_element = E.path(media_hash.path)
if media_hash.file_size:
path_element.attrib["size"] = str(media_hash.file_size)
if media_hash.last_modification_date:
path_element.attrib["lastmodificationdate"] = datetime_isostring(media_hash.last_modification_date)
hash_element.append(path_element)

hash_element.append(content_element)
hash_element.append(structure_element)

return hash_element

def _ascmhlreference_xml_element(hash_list: MHLHashList, file_path: str):
"""builds and returns one <hashlistreference> element for a given HashList object"""
Expand Down Expand Up @@ -267,8 +302,8 @@ def _process_info_xml_element(hash_list: MHLHashList):
root_hash.path = hash_list.get_root_path()

info_element = E.processinfo(
_root_media_hash_xml_element(root_hash),
E.process(process_info.process.process_type),
_root_media_hash_xml_element(root_hash),
_ignorespec_xml_element(hash_list.process_info.ignore_spec),
)
return info_element
Expand All @@ -288,6 +323,6 @@ def _ignore_xml_element(ignore_pattern: str):


def _root_media_hash_xml_element(root_media_hash: MHLMediaHash):
element = _media_hash_xml_element(root_media_hash)
element = _directory_hash_xml_element(root_media_hash, skipPath=True)
element.tag = "roothash"
return element
52 changes: 45 additions & 7 deletions xsd/ASCMHL.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<enumeration value="failed"/>
</restriction>
</simpleType>
<simpleType name="RelativePathType">
<restriction base="string"/>
</simpleType>
<complexType name="HashListType">
<sequence>
<element name="creatorinfo" type="ascmhl:CreatorInfoType"/>
Expand All @@ -36,8 +39,8 @@

<complexType name="ProcessInfoType">
<sequence>
<element name="roothash" type="ascmhl:HashType"/>
<element name="process" type="ascmhl:ProcessType"/>
<element name="roothash" type="ascmhl:RootDirectoryHashType" minOccurs="0"/>
<element name="ignore" type="ascmhl:IgnoreType" minOccurs="0"/>
</sequence>
</complexType>
Expand All @@ -62,6 +65,7 @@
<restriction base="string">
<enumeration value="in-place"/>
<enumeration value="transfer"/>
<enumeration value="flatten"/>
</restriction>
</simpleType>
<complexType name="IgnoreType">
Expand All @@ -70,16 +74,17 @@
</sequence>
</complexType>
<complexType name="HashesType">
<sequence>
<choice minOccurs="1" maxOccurs="unbounded">
<element name="hash" type="ascmhl:HashType" maxOccurs="unbounded"/>
</sequence>
<element name="directoryhash" type="ascmhl:DirectoryHashType"/>
</choice>
</complexType>
<complexType name="HashType">
<sequence>
<element name="path">
<complexType>
<simpleContent>
<extension base="string">
<extension base="ascmhl:RelativePathType">
<attribute name="size" type="integer"/>
<attribute name="creationdate" type="dateTime"/>
<attribute name="lastmodificationdate" type="dateTime"/>
Expand All @@ -96,10 +101,33 @@
<element name="xxh3" type="ascmhl:HashFormatType" maxOccurs="1" minOccurs="0"/>
<element name="xxh128" type="ascmhl:HashFormatType" maxOccurs="1" minOccurs="0"/>
</choice>
<element name="previousPath" type="string" minOccurs="0"/>
<element name="previousPath" type="ascmhl:RelativePathType" minOccurs="0"/>
<element minOccurs="0" name="metadata" type="ascmhl:MetadataType"/>
</sequence>
<attribute name="directory" type="boolean"/>
</complexType>
<complexType name="DirectoryHashType">
<sequence>
<element name="path">
<complexType>
<simpleContent>
<extension base="ascmhl:RelativePathType">
<attribute name="creationdate" type="dateTime"/>
<attribute name="lastmodificationdate" type="dateTime"/>
</extension>
</simpleContent>
</complexType>
</element>
<element name="content" type="ascmhl:DirectoryHashFormatContainerType" minOccurs="0"/>
<element name="structure" type="ascmhl:DirectoryHashFormatContainerType" minOccurs="0"/>
<element name="previousPath" type="ascmhl:RelativePathType" minOccurs="0"/>
<element name="metadata" type="ascmhl:MetadataType" minOccurs="0"/>
</sequence>
</complexType>
<complexType name="RootDirectoryHashType">
<sequence>
<element name="content" type="ascmhl:DirectoryHashFormatContainerType"/>
<element name="structure" type="ascmhl:DirectoryHashFormatContainerType"/>
</sequence>
</complexType>
<complexType name="HashFormatType">
<simpleContent>
Expand All @@ -110,6 +138,16 @@
</extension>
</simpleContent>
</complexType>
<complexType name="DirectoryHashFormatContainerType">
<choice minOccurs="1" maxOccurs="unbounded">
<element name="md5" type="ascmhl:HashFormatType"/>
<element name="sha1" type="ascmhl:HashFormatType"/>
<element name="c4" type="ascmhl:HashFormatType"/>
<element name="xxh3" type="ascmhl:HashFormatType"/>
<element name="xxh64" type="ascmhl:HashFormatType"/>
<element name="xxh128" type="ascmhl:HashFormatType"/>
</choice>
</complexType>
<complexType name="ReferencesType">
<sequence>
<element maxOccurs="unbounded" name="hashlistreference"
Expand All @@ -118,7 +156,7 @@
</complexType>
<complexType name="HashListReferenceType">
<sequence>
<element name="path" type="string"/>
<element name="path" type="ascmhl:RelativePathType"/>
<element name="c4" type="ascmhl:HashFormatType"/>
</sequence>
</complexType>
Expand Down

0 comments on commit b97cdd1

Please sign in to comment.