Skip to content

Commit

Permalink
Enhance/errors (#33)
Browse files Browse the repository at this point in the history
* custom DMeta error type added

* add DMetaBaseError instead of printing the error message

* add error messages to `params.py`

* add DMetaBaseError handlings

* `CHANGELOG.md` updated

* add exception handling to clear_all

* add exception handling to update_all

* update error messages

* update error messages

* add counter to `update-all` and `clear-all` functions, enhance exception handlings

* `autopep8.sh` applied

* enhance exception handlings
  • Loading branch information
AHReccese authored Jul 25, 2024
1 parent 050cb99 commit 4c7d883
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- `dmeta/errors.py`
- `pptx` and `xlsx` support
- `get_microsoft_format` function in `util.py`
### Changed
- `run_dmeta` in `functions.py`
- `read_json` in `util.py`
- `get_microsoft_format` in `util.py`
- error messages in `params.py`
- `clear` function in `functions.py`
- `extract` function in `util.py`
- `remove_format` function in `util.py`
- `clear` function in `functions.py`
Expand Down
8 changes: 8 additions & 0 deletions dmeta/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
"""DMeta errors."""


class DMetaBaseError(Exception):
"""DMeta base error class."""

pass
61 changes: 41 additions & 20 deletions dmeta/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import shutil
import zipfile
from art import tprint
from .util import get_microsoft_format, extract, read_json
import defusedxml.lxml as lxml


from .params import CORE_XML_MAP, APP_XML_MAP, OVERVIEW, DMETA_VERSION
from .errors import DMetaBaseError
from .util import get_microsoft_format, extract, read_json
from .params import CORE_XML_MAP, APP_XML_MAP, OVERVIEW, DMETA_VERSION, \
UPDATE_COMMAND_WITH_NO_CONFIG_FILE_ERROR, SUPPORTED_MICROSOFT_FORMATS, \
NOT_IMPLEMENTED_ERROR, FILE_FORMAT_DOES_NOT_EXIST_ERROR


def clear(microsoft_file_name):
Expand Down Expand Up @@ -57,12 +58,22 @@ def clear_all():
"""
path = os.getcwd()
dir_list = os.listdir(path)
microsoft_files = []
for item in dir_list:
if get_microsoft_format(item) is not None:
microsoft_files.append(item)
for microsoft_file in microsoft_files:
clear(microsoft_file)
counter = {
format: 0 for format in SUPPORTED_MICROSOFT_FORMATS
}
for file in dir_list:
try:
format = get_microsoft_format(file)
clear(file)
counter[format] += 1
except DMetaBaseError as e:
e = e.__str__()
if e == NOT_IMPLEMENTED_ERROR:
print("DMeta couldn't clear the metadata of {} since {}".format(file, NOT_IMPLEMENTED_ERROR))
if e == FILE_FORMAT_DOES_NOT_EXIST_ERROR:
print("Clearing the metadata of {} failed because DMeta {}".format(file, FILE_FORMAT_DOES_NOT_EXIST_ERROR))
for format in counter.keys():
print("Metadata of {} files with the format of {} has been cleared.".format(counter[format], format))


def update(config_file_name, microsoft_file_name):
Expand All @@ -83,7 +94,7 @@ def update(config_file_name, microsoft_file_name):
has_app_tags = len(personal_fields_core_xml) > 0

if not (has_core_tags or has_app_tags):
print("There isn't any chosen personal field to remove")
print("There isn't any chosen personal field to remove.")
return

microsoft_format = get_microsoft_format(microsoft_file_name)
Expand Down Expand Up @@ -130,12 +141,22 @@ def update_all(config_file_name):
"""
path = os.getcwd()
dir_list = os.listdir(path)
microsoft_files = []
for item in dir_list:
if get_microsoft_format(item) is not None:
microsoft_files.append(item)
for microsoft_file in microsoft_files:
update(config_file_name, microsoft_file)
counter = {
format: 0 for format in SUPPORTED_MICROSOFT_FORMATS
}
for file in dir_list:
try:
format = get_microsoft_format(file)
update(config_file_name, file)
counter[format] += 1
except DMetaBaseError as e:
e = e.__str__()
if e == NOT_IMPLEMENTED_ERROR:
print("DMeta couldn't update the metadata of {} since {}".format(file, NOT_IMPLEMENTED_ERROR))
if e == FILE_FORMAT_DOES_NOT_EXIST_ERROR:
print("Updating the metadata of {} failed because DMeta {}".format(file, FILE_FORMAT_DOES_NOT_EXIST_ERROR))
for format in counter.keys():
print("Metadata of {} files with the format of {} has been updated.".format(counter[format], format))


def dmeta_help():
Expand All @@ -146,7 +167,7 @@ def dmeta_help():
"""
print(OVERVIEW)
print("Repo : https://github.com/openscilab/dmeta")
print("Webpage : https://openscilab.com/")
print("Webpage : https://openscilab.com")


def run_dmeta(args):
Expand All @@ -163,12 +184,12 @@ def run_dmeta(args):
clear_all()
elif args.update:
if not args.config:
print("when using the `update` command, you should set the .json config file through the --config command")
raise DMetaBaseError(UPDATE_COMMAND_WITH_NO_CONFIG_FILE_ERROR)
else:
update(args.config[0], args.update[0])
elif args.update_all:
if not args.config:
print("when using the `update-all` command, you should set the .json config file through the --config command")
raise DMetaBaseError(UPDATE_COMMAND_WITH_NO_CONFIG_FILE_ERROR)
else:
update_all(args.config[0])
else:
Expand Down
5 changes: 5 additions & 0 deletions dmeta/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@
"pptx",
"xlsx"
]
NOT_IMPLEMENTED_ERROR = "The file format is not supported."
FILE_FORMAT_DOES_NOT_EXIST_ERROR = "Failed to detect the file format. Make sure associated file has a proper extension (.docx, .pptx, etc.)."
INVALID_CONFIG_FILE_NAME_ERROR = "Config file name is not a string."
CONFIG_FILE_DOES_NOT_EXIST_ERROR = "Given config file doesn't exist."
UPDATE_COMMAND_WITH_NO_CONFIG_FILE_ERROR = "No config file provided. Set the .json config file with --config command."
12 changes: 7 additions & 5 deletions dmeta/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from shutil import rmtree
from zipfile import ZipFile
import defusedxml.ElementTree as ET
from .params import SUPPORTED_MICROSOFT_FORMATS
from .params import SUPPORTED_MICROSOFT_FORMATS, NOT_IMPLEMENTED_ERROR, \
FILE_FORMAT_DOES_NOT_EXIST_ERROR, INVALID_CONFIG_FILE_NAME_ERROR, CONFIG_FILE_DOES_NOT_EXIST_ERROR
from .errors import DMetaBaseError


def extract_namespaces(xml_file_path):
Expand Down Expand Up @@ -39,10 +41,10 @@ def get_microsoft_format(file_name):
"""
last_dot_index = file_name.rfind('.')
if (last_dot_index == -1):
return None
raise DMetaBaseError(FILE_FORMAT_DOES_NOT_EXIST_ERROR)
format = file_name[last_dot_index + 1:]
if format not in SUPPORTED_MICROSOFT_FORMATS:
return None
raise DMetaBaseError(NOT_IMPLEMENTED_ERROR)
return format


Expand Down Expand Up @@ -71,11 +73,11 @@ def read_json(config_file_name):
:return: obj
"""
if not isinstance(config_file_name, str):
raise ("Given config file name should be str not the other type.")
raise DMetaBaseError(INVALID_CONFIG_FILE_NAME_ERROR)
if ".json" not in config_file_name:
config_file_name = config_file_name + ".json"
if os.path.isfile(config_file_name):
config_file = open(config_file_name)
return json.load(config_file)
else:
raise ("Given config file doesn't exist.")
raise DMetaBaseError(CONFIG_FILE_DOES_NOT_EXIST_ERROR)

0 comments on commit 4c7d883

Please sign in to comment.