|
| 1 | +from .d_exceptions import * |
| 2 | + |
| 3 | + |
| 4 | +def get_logger(file_name: str, level: int) -> logging.Logger: |
| 5 | + logger = logging.getLogger(file_name) |
| 6 | + handler = logging.StreamHandler() |
| 7 | + formatter = logging.Formatter( |
| 8 | + "%(asctime)s - %(name)s - %(levelname)s - %(message)s" |
| 9 | + ) |
| 10 | + handler.setFormatter(formatter) |
| 11 | + logger.addHandler(handler) |
| 12 | + logger.setLevel(level) |
| 13 | + return logger |
| 14 | + |
| 15 | + |
| 16 | +logger = get_logger(__file__, LOGGING_LEVEL) |
| 17 | + |
| 18 | + |
| 19 | +def run_cmd( |
| 20 | + cmd: Command, |
| 21 | + stdin=bytes(), |
| 22 | + raise_on_error=True, |
| 23 | +) -> Tuple[StdOut, StdErr]: |
| 24 | + print(f"Running {cmd}...") |
| 25 | + process = subprocess.Popen( |
| 26 | + cmd, |
| 27 | + stdout=subprocess.PIPE, |
| 28 | + stderr=subprocess.PIPE, |
| 29 | + stdin=subprocess.PIPE, |
| 30 | + shell=True, |
| 31 | + ) |
| 32 | + stdout, stderr = process.communicate(input=stdin) |
| 33 | + process.wait() |
| 34 | + if raise_on_error and process.returncode != 0: |
| 35 | + raise AdocMathException(stdout.decode() + stderr.decode()) |
| 36 | + else: |
| 37 | + return StdOut(stdout.decode()), StdErr(stderr.decode()) |
| 38 | + |
| 39 | + |
| 40 | +def for_each_apply_method( |
| 41 | + ps: Iterable[Union[str, plib.Path]], # path strs |
| 42 | + method: Callable[[plib.Path], None], |
| 43 | +): |
| 44 | + """Calls a method with an argument of all of ps. |
| 45 | + If a p is a directory, it searches it recursively. |
| 46 | + The idea is that the method is bound to some object (such as set), and this function allows easy updating of that set over a tree of files. |
| 47 | + """ |
| 48 | + for p_path_or_str in ps: |
| 49 | + p = plib.Path(p_path_or_str).resolve(strict=True) |
| 50 | + if p.is_dir(): |
| 51 | + for_each_apply_method( |
| 52 | + ps=sorted(p.glob("*")), |
| 53 | + method=method, |
| 54 | + ) |
| 55 | + elif p.is_file(): |
| 56 | + method(p) |
| 57 | + else: |
| 58 | + raise AdocMathException(DEAD_CODE_MSG) |
| 59 | + |
| 60 | + |
| 61 | +def log(*args): |
| 62 | + """Useful for debuggin :-P |
| 63 | +
|
| 64 | + https://stackoverflow.com/a/2749857/4204961""" |
| 65 | + |
| 66 | + frame = inspect.currentframe() |
| 67 | + frame = inspect.getouterframes(frame)[1] |
| 68 | + string = inspect.getframeinfo(frame[0]).code_context[0].strip() # type: ignore |
| 69 | + params = string[string.find("(") + 1 : -1].split(",") |
| 70 | + |
| 71 | + names = [] |
| 72 | + for i in params: |
| 73 | + if i.find("=") != -1: |
| 74 | + names.append(i.split("=")[1].strip()) |
| 75 | + |
| 76 | + else: |
| 77 | + names.append(i) |
| 78 | + |
| 79 | + for name, val in zip(names, args): |
| 80 | + logger.debug(f"\n {name} =\n{' ' * 14}{pprint.pformat(val)}") |
| 81 | + |
| 82 | + |
| 83 | +def join_with( |
| 84 | + it: Iterable[str], |
| 85 | + joiner: str, |
| 86 | +) -> str: |
| 87 | + """Reverses the arguments of x.join(y)""" |
| 88 | + |
| 89 | + return joiner.join(it) |
0 commit comments