-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_utils.py
65 lines (51 loc) · 2 KB
/
metadata_utils.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
import xmlutils as xu
#element,
def read_metadata(metadata_fn):
if xu.check_if_xml_tree(metadata_fn):
return metadata_fn
return xu.read_file(metadata_fn)
def set_model_name(metadata_root, model_name):
model_name_elem = xu.get_elems(metadata_root, 'model', uniq=True)
model_name_elem.text = model_name
def get_model_name(metadata_root):
#<model>modelname</model>
model_name=xu.get_value_by_attr(metadata_root, 'model')
return model_name
def get_columns_and_type(metadata_root):
#<columns><column></column>
# <column></column>
#</columns>
all_columns=[]
for col_elem in xu.get_elems_by_parent_child(metadata_root, 'columns', 'column'):
col_name=xu.get_value_by_attr(col_elem,'name')
col_type=xu.get_value_by_attr(col_elem,'type')
all_columns.append([col_name, col_type])
return all_columns
def is_fkey(metadata_root, column_name):
xroot=xu.get_elems_by_parent_child_key_value(metadata_root, 'columns', 'column', 'name', column_name, True)
if 'fkey' in xu.get_attr_list(xroot):
return True
return False
def get_primary_keys(metadata_root):
all_primary_keys=[]
key_xroots=xu.get_elems_by_parent_child(metadata_root, 'primarykey', 'key')
for key_xroot in key_xroots:
pk_text=xu.get_value(key_xroot)
all_primary_keys.append(pk_text)
return all_primary_keys
def get_foreign_keys(metadata_root):
all_foreign_keys=[]
for col_name, col_type in get_columns_and_type(metadata_root):
if is_fkey(metadata_root, col_name):
all_foreign_keys.append(col_name)
return all_foreign_keys
def get_delimiter(metadata_root):
elem = xu.get_elems(metadata_root, 'delimiter', uniq=True)
if elem is None:
return None
return xu.get_value_elem(metadata_root, 'delimiter')
def get_header(metadata_root):
elem = xu.get_elems(metadata_root, 'header', uniq=True)
if elem is None:
return False
return xu.get_value_elem(metadata_root, 'header')