diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff475a..c12869f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/dmeta/errors.py b/dmeta/errors.py new file mode 100644 index 0000000..642df25 --- /dev/null +++ b/dmeta/errors.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +"""DMeta errors.""" + + +class DMetaBaseError(Exception): + """DMeta base error class.""" + + pass diff --git a/dmeta/functions.py b/dmeta/functions.py index c777bf9..24f39cc 100644 --- a/dmeta/functions.py +++ b/dmeta/functions.py @@ -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): @@ -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): @@ -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) @@ -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(): @@ -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): @@ -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: diff --git a/dmeta/params.py b/dmeta/params.py index 7e60d6e..38b034e 100644 --- a/dmeta/params.py +++ b/dmeta/params.py @@ -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." diff --git a/dmeta/util.py b/dmeta/util.py index d56c9eb..6f3be99 100644 --- a/dmeta/util.py +++ b/dmeta/util.py @@ -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): @@ -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 @@ -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)