2 @package libtextworker.general
3 @brief Utilities (unable to describe)
18 from importlib
import import_module
19 from typing
import Literal
23 available_toolkits = Literal[
"tk",
"wx"]
33 A logging class for GUIs.
34 Available toolkits can be checked/set by the UseToolKit attribute.
37 UseToolKit: available_toolkits | bool =
False
41 Set's the GUI toolkit to use.
42 Currently there is no support for Tkinter yet.
46 def CallGUILog(self, name: Literal[
"CRITICAL",
"DEBUG",
"ERROR",
"EXCEPTION",
"NORMAL",
"WARNING"],
47 msg: object, *args: object):
49 Call GUI toolkit logging function.
50 Do nothing if not able to.
53 do = import_module(f
"libtextworker.interface.{self.UseToolKit}.constants")
54 if args: msg = msg % args
55 getattr(do,
"LOG_" + name)(msg)
57 def critical(self, msg: object, *args: object, **kwds):
58 super().critical(msg, *args, **kwds)
59 self.
CallGUILogCallGUILog(
"CRITICAL", msg, *args)
61 def debug(self, msg: object, *args: object, **kwds):
62 super().debug(msg, *args, **kwds)
65 def error(self, msg: object, *args: object, **kwds):
66 super().error(msg, *args, **kwds)
69 def exception(self, msg: object, *args: object, **kwds):
70 super().exception(msg, *args, **kwds)
71 self.
CallGUILogCallGUILog(
"EXCEPTION", msg, *args)
73 def log(self, level: int, msg: object, *args: object, **kwds):
74 super().log(level, msg, *args, **kwds)
75 self.
CallGUILogCallGUILog(
"NORMAL", msg, *args)
77 def warning(self, msg: object, *args: object, **kwds):
78 super().warning(msg, *args, **kwds)
79 self.
CallGUILogCallGUILog(
"WARNING", msg, *args)
84 Common exception class for libtextworker
87 def __init__(this, msg: str):
88 logger.error(msg, exc_info=1)
89 Exception.__init__(this, msg)
95 Craft any >=2 paths, together.
96 @param *args (str|pathlib.Path)
98 @raise Exception: not enough arguments (must be >=2)
101 raise Exception(
"Not enough arguments")
103 final = pathlib.Path(args[0])
105 for i
in range(1, len(args)):
106 final /= str(args[i])
111 return os.path.normpath(final)
116 Create a directory with optional sub-folders.
117 @param directory (str): Directory to create
118 @param childs (list of str): Sub-dirs under $directory
119 @throws Exception: Directory creation failed
121 if not os.path.isdir(directory):
124 for folder
in childs:
126 if not os.path.isdir(folder):
132 Create directory layer-to-layer.
133 How to understand this? Try this path: path1/path2/path3.
134 WalkCreation will create path1 first, then path1/path2 and path1/path2/path3. Skip existing dirs, of course.
136 @param directory (str): Directory to create
137 @throws Exception: Directory creation failed
139 directory = directory.replace(
"\\",
"/")
140 splits = directory.split(
"/")
142 for item
in range(1, len(splits)):
143 firstdir +=
"/" + splits[item]
144 if not os.path.isdir(firstdir):
150 Get the current directory path.
151 @param file (str): File path
152 @param aspathobj (bool): Return a pathlib.Path object instead of a string.
153 @return pathlib.Path | str
155 result = pathlib.Path(file).parent
158 return result.__str__()
163 Reset every configurations under $TOPLV_DIR to default.
164 Will close the app after completed.
167 if os.path.isdir(TOPLV_DIR):
168 shutil.rmtree(TOPLV_DIR)
175 Tests if the specified module name is importable.
176 Results is stored in Importable (dict[str, bool]).
180 import_module(pkgname)
182 Importable[pkgname] =
False
183 warnings.warn(
"%s not found" % pkgname)
186 Importable[pkgname] =
True
191 logger =
Logger(
"libtextworker", logging.INFO)
192 logging.captureWarnings(
True)
193 formatter = logging.Formatter(
"[%(asctime)s %(levelname)s] %(message)s")
196 strhdlr = logging.StreamHandler()
197 strhdlr.setFormatter(formatter)
200 from time
import strftime, localtime
201 logpath = os.path.expanduser(f
"~/.logs/libtextworker-{strftime(r'%Y-%m-%d', localtime())}.log")
203 if not os.path.isfile(logpath): open(logpath,
"w").write(
"")
205 filehdlr = logging.FileHandler(logpath)
206 filehdlr.setFormatter(formatter)
208 logger.addHandler(strhdlr)
209 logger.addHandler(filehdlr)
bool test_import(str pkgname)
str CraftItems(*str|pathlib.Path args)
def WalkCreation(str directory)
def GetCurrentDir(str file, bool aspathobj=False)
def CreateDirectory(str directory, list[str] childs=[])
def UseGUIToolKit(self, available_toolkits toolkit)
def CallGUILog(self, Literal["CRITICAL", "DEBUG", "ERROR", "EXCEPTION", "NORMAL", "WARNING"] name, object msg, *object args)