Skip to content

Commit

Permalink
update & clear functions refactored to use overwrite_metadata f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
AHReccese committed Jan 2, 2025
1 parent d6c4299 commit a9f63d5
Showing 1 changed file with 8 additions and 37 deletions.
45 changes: 8 additions & 37 deletions dmeta/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,8 @@ 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:
Expand Down Expand Up @@ -123,11 +109,11 @@ 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
has_core_tags = len(personal_fields_core_xml.keys()) > 0
has_app_tags = len(personal_fields_core_xml.keys()) > 0

if not (has_core_tags or has_app_tags):
print("There isn't any chosen personal field to remove.")
Expand All @@ -140,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 a9f63d5

Please sign in to comment.