2from .imports
import dependsOnPackage
3from ..debugging
import get_varname, called_as_decorator, debug, get_metadata
7from os.path
import join
8from random
import randint
13def replace_line(line, offset=0, keepTabs=True, convertTabs=True, calls=0):
14 """ Replaces the line of code this is called from with the give line parameter.
15 This is a very bad idea
and you should
not use it
16 Automatically adds a newline to the end, but does
not automatically add tabs.
18 meta = get_metadata(calls=calls+2)
20 with open(meta.filename,
'r')
as f:
24 if file[meta.lineno-1] == meta.code_context[0]:
26 tabs = re.match(
r'([\t ]+)', file[meta.lineno-1 + offset])
28 line = tabs.group(1) + line
31 line = line.replace(
'\t',
' ')
33 file[meta.lineno-1 + offset] = line +
'\n'
36 debug(f
"Error: lines don't match, not replacing line.\n\tMetadata: \"{meta.code_context}\"\n\tFile: \"{file[meta.lineno-1]}\"", clr=-1)
39 with open(meta.filename,
'w')
as f:
43def comment(comment='', line_limit=80, char='=', start='', end='#', capitalize=False):
44 """ Replaces the call with a nicely formatted comment line next time the line is run
45 NOTE: This is a terrible, terrible function that you should NOT use.
46 I
'm pretty confident it won't overwrite your source code.
47 And it
's surprisingly useful.
48 But still, use at your own risk.
51 comment = comment.upper()
53 meta = get_metadata(calls=2)
55 with open(meta.filename,
'r')
as f:
59 if file[meta.lineno-1] == meta.code_context[0]:
60 tabs = re.match(
r'([\t ]+)', file[meta.lineno-1])
67 replace_line(
'# ' + comment, calls=1)
75 half = (((line_limit // len(char)) - len(comment) - (len(tabs) // 2) - 1 - (2
if len(comment)
else 0) - len(end)) // 2) - 1
77 seperateChar =
' ' if len(comment)
else ''
79 c =
'#' + start + (char * half) + seperateChar + comment + seperateChar + (char * half)
81 c += ((line_limit - len(c) - len(end)) // len(char)) * char
83 file[meta.lineno-1] = c +
'\n'
86 debug(f
"Error: lines don't match, not replacing line.\n\tMetadata: \"{meta.code_context}\"\n\tFile: \"{file[meta.lineno-1]}\"", clr=-1)
89 with open(meta.filename,
'w')
as f:
95 """ Run a command and terminate if it fails. """
97 ec = subprocess.call(
' '.join(args), shell=
True)
99 print(
"Execution failed:", e, file=sys.stderr)
105 """ Centers a string for printing in the terminal """
106 from os
import get_terminal_size
107 for _
in range(int((get_terminal_size().columns - len(string)) / 2)): string =
' ' + string
112def insert_newlines(string:str, max_line_length:int) -> str:
113 """ Inserts newline characters into `string` in order to keep `string` under `max_line_length`
114 characters long, while not inserting a newline
in the middle of a word
117 words = string.split()
121 current_line = words[0]
123 for word
in words[1:]:
126 if len(current_line) + 1 + len(word) > max_line_length:
127 result += current_line +
"\n"
131 current_line +=
" " + word
134def assertValue(param, *values, blocking=True):
135 paramName = get_varname(param)
136 if not called_as_decorator(
'assertValue'):
137 if param
not in values:
138 err = ValueError(f
"Invalid value for {paramName}, must be one of: {values}")
144 print(
'TODO: AssertValue usage as a decorator')