Skip to content

Commit

Permalink
Refactor (#45)
Browse files Browse the repository at this point in the history
* `overwrite_metadata` function implemented

* `update` & `clear` functions refactored to use `overwrite_metadata` function

* `CHANGELOG.md` updated

* apply feedbacks
  • Loading branch information
AHReccese authored Jan 9, 2025
1 parent 9e88132 commit 444e81c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `extract_namespaces` function in `util.py`
### Added
- `DMetaBaseError` added to `dmeta/__init__.py`
- `overwrite_metadata` function added to `functions.py`
### Changed
- `update` function in `functions.py` refactored
- `clear` function in `functions.py` refactored
- `README.md` updated
- GitHub actions are limited to the `dev` and `main` branches
- `Python 3.13` added to `test.yml`
Expand Down
69 changes: 34 additions & 35 deletions dmeta/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@
NOT_IMPLEMENTED_ERROR, FILE_FORMAT_DOES_NOT_EXIST_ERROR


def overwrite_metadata(
xml_path,
metadata=None,
is_core=True):
"""
Overwrite metadata in an XML file based on a predefined mapping.
:param xml_path: path to the XML file to be updated
:type xml_path: str
:param metadata: a dictionary containing metadata to overwrite the XML elements with, or `None`
to reset
:type metadata: dict
:param is_core: a flag that indicates whether the given XML file is the core.xml file
:type is_core: bool
:return: None
"""
xml_map = CORE_XML_MAP if is_core else APP_XML_MAP
if os.path.exists(xml_path):
e_core = lxml.parse(xml_path)
for xml_element in e_core.iter():
for personal_field in xml_map if metadata is None else metadata:
associated_xml_tag = xml_map[personal_field]
if (associated_xml_tag in xml_element.tag):
xml_element.text = "" if metadata is None else metadata[personal_field]
e_core.write(xml_path)


def clear(microsoft_file_name, in_place=False):
"""
Clear all the editable metadata in the given microsoft file.
Expand All @@ -28,22 +55,9 @@ def clear(microsoft_file_name, in_place=False):
core_xml_path = os.path.join(doc_props_dir, "core.xml")
app_xml_path = os.path.join(doc_props_dir, "app.xml")

if os.path.exists(core_xml_path):
e_core = lxml.parse(core_xml_path)
for xml_element in e_core.iter():
for personal_field in CORE_XML_MAP.values():
if (personal_field in xml_element.tag):
xml_element.text = ""
e_core.write(core_xml_path)

if os.path.exists(app_xml_path):
e_app = lxml.parse(app_xml_path)
for xml_element in e_app.iter():
for personal_field in APP_XML_MAP.values():
if (personal_field in xml_element.tag):
xml_element.text = ""
e_app.write(app_xml_path)

overwrite_metadata(core_xml_path)
overwrite_metadata(app_xml_path,is_core=False)

modified = microsoft_file_name
if not in_place:
modified = microsoft_file_name[:microsoft_file_name.rfind('.')] + "_cleared" + "." + microsoft_format
Expand Down Expand Up @@ -95,8 +109,8 @@ def update(config_file_name, microsoft_file_name, in_place=False):
:return: None
"""
config = read_json(config_file_name)
personal_fields_core_xml = [e for e in CORE_XML_MAP.keys() if e in config]
personal_fields_app_xml = [e for e in APP_XML_MAP.keys() if e in config]
personal_fields_core_xml = {e:v for e,v in CORE_XML_MAP.items() if e in config}
personal_fields_app_xml = {e:v for e,v in APP_XML_MAP.items() if e in config}

has_core_tags = len(personal_fields_core_xml) > 0
has_app_tags = len(personal_fields_core_xml) > 0
Expand All @@ -112,24 +126,9 @@ def update(config_file_name, microsoft_file_name, in_place=False):
app_xml_path = os.path.join(doc_props_dir, "app.xml")

if has_core_tags:
if os.path.exists(core_xml_path):
e_core = lxml.parse(core_xml_path)
for xml_element in e_core.iter():
for personal_field in personal_fields_core_xml:
associated_xml_tag = CORE_XML_MAP[personal_field]
if (associated_xml_tag in xml_element.tag):
xml_element.text = config[personal_field]
e_core.write(core_xml_path)

overwrite_metadata(core_xml_path, personal_fields_core_xml)
if has_app_tags:
if os.path.exists(app_xml_path):
e_app = lxml.parse(app_xml_path)
for xml_element in e_app.iter():
for personal_field in personal_fields_app_xml:
associated_xml_tag = APP_XML_MAP[personal_field]
if (associated_xml_tag in xml_element.tag):
xml_element.text = config[personal_field]
e_app.write(app_xml_path)
overwrite_metadata(app_xml_path, personal_fields_app_xml, is_core=False)

modified = microsoft_file_name
if not in_place:
Expand Down

0 comments on commit 444e81c

Please sign in to comment.