-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocess_common.py
More file actions
66 lines (53 loc) · 1.85 KB
/
process_common.py
File metadata and controls
66 lines (53 loc) · 1.85 KB
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
import string
import re
import header_operations
full_table = string.maketrans(" '`()-\t", "_______")
def convert_to_identifier(s):
return string.translate(s, full_table, ",|")
part_table = string.maketrans(" \t\n", "__^")
def replace_spaces(s):
return string.translate(s, part_table)
is_valid_regex = re.compile("[a-z0-9_]+$")
def is_valid_identifier(s):
try:
return is_valid_regex.match(s)
except TypeError:
return False
def assert_valid_identifier(s, entry=None, tag=None):
if not is_valid_identifier(s):
ERROR("identifier %s is invalid: must be a string of lowercase, digit, or underscore characters" % repr(s), entry=entry, tag=tag)
def format_error_message(prefix, msg, entry=None, opcode=None, tag=None):
message = [prefix, msg]
info = []
if tag:
info.extend([",", " type tag: ", tag])
if opcode:
info.extend([",", " operation: ", header_operations.get_opcode_name(opcode)])
if entry:
entry_string = repr(entry)
if len(entry_string) > 100:
entry_string = entry_string[:100] + " ... <snipped>"
info.extend([",", " entry: ", entry_string])
if len(info):
info[0] = ";"
return "".join(message + info)
class ModuleSystemError(Exception):
def __init__(self, msg, entry=None, opcode=None, tag=None):
self.msg = msg
self.entry = entry
self.opcode = opcode
self.tag = tag
def __str__(self):
return format_error_message("ERROR: ", msg=self.msg, entry=self.entry, opcode=self.opcode, tag=self.tag)
def ERROR(*a, **k):
raise ModuleSystemError(*a, **k)
def WARNING(*a, **k):
print format_error_message("WARNING: ", *a, **k)
def set_warnings_as_errors():
global WARNING
WARNING = ERROR
def print_error(*a, **k):
print format_error_message("ERROR: ", *a, **k)
def set_errors_non_fatal():
global ERROR
ERROR = print_error