-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_bash.py
67 lines (47 loc) · 1.6 KB
/
to_bash.py
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
"""
provides string_to_bash utilities
"""
from . import core
from . import utils
_get_function_name = utils.get_function_name_fun()
def atom_to_code(in_atom: core.Atom) -> str:
"""
returns a string/piece of bash code resulting in printing the
in_atom.atom_char to the standard output
"""
special_chars = {
r'"': r"\"",
"\\": "\\\\",
"`": "\\`",
"\n": "\\n",
"\t": "\\t",
"%": "%%",
}
res_char = special_chars.get(in_atom.atom_char, in_atom.atom_char)
return f'printf "{res_char}"'
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "", "")
_call_function_or_atom = utils.get_call_function_or_atom(
atom_to_code, _function_call_str
)
_body_to_str = utils.get_body_to_str(
"", "\n ", _call_function_or_atom, "", "\n true"
)
def _merge_to_full_function(in_function_name, in_function_body):
return f"{in_function_name} ()\n{{{in_function_body}\n}}\n"
_function_to_code = utils.get_function_to_code(
_get_function_name, _body_to_str, _merge_to_full_function
)
def _main_call_to_code(in_initial_call, **kwargs):
res = ""
if in_initial_call is not None:
res = _call_function_or_atom(in_initial_call, **kwargs)
return res
def _join_to_final(main_call, function_definitions, **_kwargs):
res = "\n\n".join(function_definitions + [main_call])
if res:
res = "\n\n" + res
res += "\n"
return "#!/usr/bin/env bash\n\nset -euo pipefail" + res
proc_printer_program, proc = utils.get_all_proc_functions(
_main_call_to_code, _function_to_code, _join_to_final
)