-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_common.py
116 lines (93 loc) · 3.48 KB
/
util_common.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from modmerger_options import *
try:
DEBUG_MODE
except NameError:
DEBUG_MODE = 0
try:
logger
except NameError:
import logging
logger = logging.getLogger("mnb_warband")
if( DEBUG_MODE == 1):
logger.setLevel(logging.DEBUG)
elif( DEBUG_MODE == -1):
logger.setLevel(logging.WARN)
elif( DEBUG_MODE == -2):
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.INFO)
h = logging.StreamHandler()
#f = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
f = logging.Formatter("%(levelname)s %(message)s")
h.setFormatter(f)
logger.addHandler(h)
# add mode constants
ADDMODE_APPEND = 0 # adds an element to a list regardless of whether it already exists in the list
ADDMODE_REPLACE_EXIST = 1 # replaces existing element in a list
ADDMODE_IGNORE_IF_EXIST = 2 # if element key is found in list, it will be dropped
# copied from find_object in header_common.py to make this module independent
# does a case-insensitive search over a list of tuple/list which first elements matches obj_id
def list_find_first_match_i(objlist,obj_id):
result = -1
num_objects = len(objlist)
i_object = 0
object_id_lowercase = obj_id.lower()
while (i_object < num_objects) and (result == -1):
object = objlist[i_object]
if (object[0].lower() == object_id_lowercase):
result = i_object
i_object += 1
return result
# add a list of tuple/array objects to another, with first element as key
# dst_objects : destination object list
# add_objects : list of objects to add
# add_mode : see add mode constants above
# Return : num_appended, num_replaced, num_ignored which are the number of objects being appended, replaced and ignored respectively
def add_objects(dst_objects, add_objects, add_mode = ADDMODE_REPLACE_EXIST):
num_appended=0
num_replaced=0
num_ignored=0
if(add_mode == ADDMODE_APPEND):
dst_objects.extend(add_objects) # Use this only if there are no replacements (i.e. no duplicated item names)
num_appended = len(add_objects)
else:
# Use the following loop to replace existing entries with same id
for i in range (0,len(add_objects)):
find_index = list_find_first_match_i(dst_objects, add_objects[i][0]);
if( find_index == -1 ):
dst_objects.append(add_objects[i])
num_appended+=1
elif(add_mode == ADDMODE_REPLACE_EXIST):
dst_objects[find_index] = add_objects[i]
num_replaced=0
else:
num_ignored=0
return num_appended, num_replaced, num_ignored
# move this to util_strings later
# removes vowels from a string
# Note: often used in place to "shorten" certain well known strings
def devowel(word, table=''.join(chr(i) for i in range(256)), vowels="aeiou"):
return word.translate(table, vowels)
# inverts a dictionary with value as list of keys of original dictionary
def inverted_list_dict(d):
inv = {}
for k, v in d.iteritems():
keys = inv.setdefault(v, [])
keys.append(k)
return inv
# inverts a dictionary with value as a key of original dictionary
# assumes 1-1 mapping between keys and values
def inverted_dict(d):
return dict([v,k] for k,v in d.iteritems())
##Caba'drin Addition
def list_find_first_containing_i(objlist,obj_id):
result = -1
num_objects = len(objlist)
i_object = 0
object_id_lowercase = obj_id.lower()
while (i_object < num_objects) and (result == -1):
object = objlist[i_object]
if (object_id_lowercase in object[0].lower()):
result = i_object
i_object += 1
return result