diff --git a/catholic/core/canon/canon_law.py b/catholic/core/canon/canon_law.py index 917e230..93e590c 100644 --- a/catholic/core/canon/canon_law.py +++ b/catholic/core/canon/canon_law.py @@ -1,12 +1,12 @@ from catholic.core.canon.services import get_canon_law_by_id, get_canon_laws_with_given_substring, \ get_canon_law_paragraphs_by_paragraph_ids -from catholic.core.utils.files import load_pickle +from catholic.core.utils.files import load_pickle_by_name from catholic.core.utils.console import error, markdown, blue_text, red_text from catholic.core.utils.query import decode_query def execute_canon_command(law, search): - canon_law_dict = load_pickle("pickles/canon.pickle") + canon_law_dict = load_pickle_by_name("canon.pickle") if law: if law.isdigit(): _display_canon_law(canon_law_dict, law) diff --git a/catholic/core/catechism/catechism.py b/catholic/core/catechism/catechism.py index 7c9ec6c..772b391 100644 --- a/catholic/core/catechism/catechism.py +++ b/catholic/core/catechism/catechism.py @@ -1,6 +1,6 @@ from catholic.core.catechism.services import get_catechism_by_paragraph, \ get_catechism_paragraphs_with_given_substring, get_catechism_paragraphs_by_paragraph_ids -from catholic.core.utils.files import load_pickle +from catholic.core.utils.files import load_pickle_by_name from catholic.core.utils.console import markdown, error, blue_text, red_text from catholic.core.utils.query import decode_query @@ -13,7 +13,7 @@ def execute_catechism_command(paragraph, search): :param paragraph: Paragraph_number :return: None """ - catechism_dict = load_pickle("pickles/catechism.pickle") + catechism_dict = load_pickle_by_name("catechism.pickle") if paragraph: if paragraph.isdigit(): _display_catechism_paragraph(catechism_dict, paragraph) diff --git a/catholic/core/missal/missal.py b/catholic/core/missal/missal.py index ceefea8..64b98f7 100644 --- a/catholic/core/missal/missal.py +++ b/catholic/core/missal/missal.py @@ -1,12 +1,12 @@ from catholic.core.missal.services import get_roman_missal_by_number, \ get_roman_missal_paragraphs_with_given_substring, get_roman_missal_paragraphs_by_numbers -from catholic.core.utils.files import load_pickle +from catholic.core.utils.files import load_pickle_by_name from catholic.core.utils.console import markdown, error, blue_text, red_text from catholic.core.utils.query import decode_query def execute_missal_command(missal_id, search): - missal_dict = load_pickle("pickles/girm.pickle") + missal_dict = load_pickle_by_name("girm.pickle") if missal_id: if missal_id.isdigit(): _display_missal_paragraph(missal_dict, missal_id) diff --git a/catholic/core/utils/files.py b/catholic/core/utils/files.py index 072f2e1..be070ea 100644 --- a/catholic/core/utils/files.py +++ b/catholic/core/utils/files.py @@ -1,11 +1,24 @@ +import os import pickle +from pickles import read_pickle + def load_pickle(pickle_name: str): """ + TIP: DO NOT use this Reads the contents of the given pickle, and returns a dict. :param pickle_name: Name of the pickle file :return: dict """ with open(pickle_name, 'rb') as handle: return pickle.load(handle) + + +def load_pickle_by_name(name: str): + """ + Reads the contents of the given pickle, and returns a dict. + :param name: Name of the pickle file + :return: dict + """ + return read_pickle(name) diff --git a/pickles/__init__.py b/pickles/__init__.py new file mode 100644 index 0000000..17c9f44 --- /dev/null +++ b/pickles/__init__.py @@ -0,0 +1,14 @@ +import os +import pickle + + +def read_pickle(name: str) -> dict: + """ + Reads pickles by name + :param name: + :return: + """ + this_dir, _ = os.path.split(__file__) + data_path = os.path.join(this_dir, name) + with open(data_path, 'rb') as handle: + return pickle.load(handle)