3from typing
import Callable
11IGNORE_KEYWORD =
"PRE_BUILD_IGNORE"
25 """Represents a source file to map log calls with"""
35 def __init__(self, RawPath, FilePath, FileName, Offset):
36 if not os.path.exists(FilePath):
37 raise FileNotFoundError(FilePath)
46 Util.touch(f
"{Offset}{RawPath}")
48 def new_error(self, exception: Exception, name: str, tag: str) ->
None:
49 """Store an error to display later
52 exception (Exception): Exception to store
53 name (str): Name of this error
54 tag (str): Additional tag for this error
56 self.
errorserrors.append(f
" {name}:{tag}\n {Text.red(type(exception).__name__)}\n > {Error.error_to_string(exception)}\n")
59 """Get a new unique mapping to both a TAG and ID
62 tag_str (str): raw tag string to store
63 id_str (str): raw id string to store
66 int: uid to replace these strings with
69 tag_str = tag_str.strip(
'"')
70 tstring = f
"[{tag_str}]"
71 istring = id_str.strip(
'"')
72 number_id = await IDMatch.map_unique_pair(tstring, istring, map_range)
76 """Get a new unique TAG mapping
79 tag_str (str): raw string to store
82 int: uid to replace this string with
85 state_match = re.findall(REGEX.STATE_PASS, tag_str)
87 tag_str = tag_str.strip(
'"')
88 tstring = f
"[{tag_str}]"
90 number_id = await IDMatch.map_unique_tag(tstring, IDMatch.TAG_RANGE_STATE
if len(state_match) != 0
else IDMatch.TAG_RANGE_ELSE)
93 async def get_new_id(self, raw_log_level: str, id_str: str) -> int:
94 """Get a new unique ID mapping
97 raw_log_level (str): Logging level of this tag
98 id_str (str): raw string to store
101 int: uid to replace this string with
103 istring = FILE_LOG_LEVELS[raw_log_level] + id_str.strip(
'"')
104 number_id = await IDMatch.map_unique_id(istring)
107 async def line_special(self, line: str, matches: list[str]) -> str:
108 """Format special string msg calls
111 line (str): The line that matched this type
112 reMatch (list[str]): The resulting regex list
115 str: Reformatted line
118 return line.replace(matches, str(uid), 1)
120 async def line_tag(self, line: str, matches: list[str]) -> str:
121 """Format defined log tags
124 line (str): The line that matched this type
125 reMatch (list[str]): The resulting regex list
128 str: Reformatted line
131 return line.replace(matches, str(tag), 1)
133 async def line_vsx(self, line: str, matches: list[str]) -> str:
134 """Format 'variable string' type log calls
137 line (str): The line that matched this type
138 reMatch (list[str]): The resulting regex list
141 str: Reformatted line
143 uid = await self.
get_new_id(matches[0], matches[2])
144 return line.replace(matches[2], str(uid), 1)
146 async def line_ssx(self, line: str, matches: list[str]) -> str:
147 """Format 'string string' type log calls
150 line (str): The line that matched this type
151 reMatch (list[str]): The resulting regex list
154 str: Reformatted line
160 if matches[0] ==
"p":
165 uid = await self.
get_new_id(matches[0], matches[2])
167 return line.replace(matches[1], str(tag), 1).replace(matches[2], str(uid), 1)
169 async def map_lines(self, function: Callable[[str], str]) ->
None:
170 """Map a function to each line of this file
173 function (Callable[[str], None]): Function that takes in a str and returns a str
179 with open(self.
pathpath,
"r", encoding=
"utf-8")
as original, open(temp_path,
"w", encoding=
"utf-8")
as new_file:
180 for line
in original:
182 newline = await function(line)
183 new_file.buffer.write(newline.encode(
"utf-8"))
184 except Exception
as err:
186 new_file.buffer.write(line.encode(
"utf-8"))
192 async def match_log_mapping(self, line: str) -> str:
193 """Maps a source file line to Log related syntax
196 line (str): The line to scan
199 ScriptException: On invalid Log related syntax
202 str: Reformatted line
206 if IGNORE_KEYWORD
in newline:
218 for reg, func
in scan_sequence:
219 matches = re.findall(reg, line)
220 if len(matches) != 0:
221 return await func(line, matches[0])
224 (REGEX.TAG_FAIL, Error.MalformedTAGDefinitionException),
225 (REGEX.CALL_ERR_LITERAL, Error.MalformedLogCallException),
226 (REGEX.CALL_ERR_BLANK, Error.BlankTAGException),
229 for reg, exception
in fail_sequence:
230 err = re.findall(reg, line)
233 raise exception(err[0] + Text.error(err[1]) + err[2])
238 """Begin replacing lines for mapping log calls"""
Represents a source file to map log calls with.
int get_new_pair_mapping(self, str tag_str, str id_str, range map_range)
Get a new unique mapping to both a TAG and ID.
str line_special(self, str line, list[str] matches)
Format special string msg calls.
int get_new_tag(self, str tag_str)
Get a new unique TAG mapping.
str line_vsx(self, str line, list[str] matches)
Format 'variable string' type log calls.
None scan(self)
Begin replacing lines for mapping log calls.
str match_log_mapping(self, str line)
Maps a source file line to Log related syntax.
None map_lines(self, Callable[[str], str] function)
Map a function to each line of this file.
int get_new_id(self, str raw_log_level, str id_str)
Get a new unique ID mapping.
str line_tag(self, str line, list[str] matches)
Format defined log tags.
None new_error(self, Exception exception, str name, str tag)
Store an error to display later.
str line_ssx(self, str line, list[str] matches)
Format 'string string' type log calls.
Regex definitions for logging.