-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.py
More file actions
106 lines (85 loc) · 3.97 KB
/
template.py
File metadata and controls
106 lines (85 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TODO: What the module is doing
"""
__version__ = "2024-02-08 21:29:39"
__author__ = "Harding"
__description__ = __doc__
__copyright__ = "Copyright 2025"
__credits__ = ["Other projects"]
__license__ = "GPL"
__maintainer__ = "Harding"
__email__ = "not.at.the.moment@example.com"
__status__ = "Development"
from typing import Union, Any, Dict, List
import logging # TODO: Change to loguru? https://github.com/Delgan/loguru
from types import ModuleType
from pydantic import validate_call
import harding_utils as hu
_g_logger = logging.getLogger(__name__)
_g_logger.setLevel(logging.DEBUG) # This is the level that is actually used
_console_handler = logging.StreamHandler()
_console_handler.setLevel(logging.DEBUG)
_console_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(module)s.%(funcName)s:%(lineno)d - %(message)s'))
if _g_logger.handlers:
_g_logger.removeHandler(_g_logger.handlers[0]) # When you importlib.reload() a module, we need to clear out the old logger
_g_logger.addHandler(_console_handler)
@validate_call(config={"arbitrary_types_allowed": True, "strict": True, "validate_return": True})
def _reload(arg_module: Union[str, ModuleType, None] = None):
''' Internal function. During development, this is nice to have '''
import importlib
import sys
l_module: str = arg_module if isinstance(arg_module, str) else getattr(arg_module, '__name__', __name__)
return importlib.reload(sys.modules[l_module])
@validate_call(config={"arbitrary_types_allowed": True, "strict": True, "validate_return": True})
def file_work(arg_file: str, arg_update: bool = False) -> str:
''' This is all the work done on each file '''
_g_logger.debug("TODO: Starting work on %s", arg_file)
if arg_update:
_g_logger.debug("TODO: it should be an update")
_g_logger.debug("TODO: Ending work on %s", arg_file)
return f"{arg_file} is done!"
@validate_call(config={"arbitrary_types_allowed": True, "strict": True, "validate_return": True})
def module_work(arg_files: List[str], arg_update: bool = False) -> List[str]:
''' This is all the work the module is doing '''
_g_logger.info("Welcome to TODO: Template!")
res: List[str] = []
for file in arg_files:
res.append(file_work(file, arg_update))
return res
@validate_call(config={"arbitrary_types_allowed": True, "strict": True, "validate_return": True})
def module_main(arg_argv: Union[Dict[str, Any], None] = None) -> List[str]:
''' This function can be used from an interactive prompt such as Ipython or Jupyter '''
if arg_argv is None:
arg_argv = {}
debug_mode: bool = arg_argv.get("debug_mode", False)
_g_logger.debug("TODO: Sanity check on the keys in the arg_argv")
_g_logger.debug("arg_argv looks like:")
_g_logger.debug(hu.dict_to_json_string_pretty(arg_argv))
l_files = []
if "-" == arg_argv.get('files', [""])[0]:
l_files.append("-")
else:
l_files = hu.adv_glob(arg_argv.get('files', ""), arg_argv.get('check_subfolders', False))
if not l_files:
error_msg: str = "arg_files[] is empty!"
_g_logger.critical(error_msg)
return [error_msg]
if debug_mode:
_g_logger.debug("Entering debug mode")
return module_work(arg_files=l_files, arg_update=arg_argv.get('update', False))
if __name__ == "__main__":
import argparse
version = f"version {__version__} by {__author__} {__email__}"
parser = argparse.ArgumentParser(
description=f"{__description__} {version}")
parser.add_argument("-s", "--subfolders", action="store_true",
dest="check_subfolders", help="Look in subfolders", default=False)
parser.add_argument("-u", "--update", action="store_true",
dest="update", help="Update the file", default=False)
parser.add_argument("files", nargs="+")
args = parser.parse_args()
l_main_res: List[str] = module_main(args.__dict__)
for r in l_main_res:
_g_logger.info(r)