-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
135 lines (104 loc) · 4.23 KB
/
config.py
File metadata and controls
135 lines (104 loc) · 4.23 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
#BA modules
import template
class ConfigFileParser(object):
def _parse_line_option(self, line, line_no, file_path):
spl = [field.strip() for field in line.split("=")]
if len(spl) != 2:
raise ValueError("WARNING ConfigFileParser::parse in config file", file_path,"line: ", line_no," not correctly formated")
return spl
def _parse_template_option(self, line, line_no, file_path):
spl = [field.strip() for field in line.split()]
return spl
def parse(self,file_path,warnings=0):
general = {}
commands = {}
templates = {}
ignores = []
f = open(file_path)
lines = f.readlines()
f.close()
if warnings:
print "WARNING ConfigFileParser::parse cannot open config file ",file_path
return None
curr_template = None
curr_section = ""
curr_section_name = ""
for line_no, line in enumerate(lines):
line = line.strip()
if len(line) == 0 or line[0] == "#":
continue
if line[0] == "[":
# This means we are openning a new section
if curr_template is not None:
templates[curr_section_name] = curr_template
curr_template = None
curr_section = line.strip("[]")
if " " in curr_section:
# Means that the tag in this section has a name e.g. for command or template tags
curr_section, curr_section_name = curr_section.split()
continue
if curr_section == "ignore":
ignores.append(line)
if curr_section == "general":
option, value = self._parse_line_option(line, line_no, file_path)
general[option] = value
if curr_section == "command":
if not curr_section_name in commands:
commands[curr_section_name] = {}
option, value = self._parse_line_option(line, line_no, file_path)
commands[curr_section_name][option] = value
if curr_section == "template":
if not curr_section_name in commands:
templates[curr_section_name] = {}
fields = self._parse_template_option(line, line_no, file_path)
if curr_template == None:
curr_template = template.Template()
curr_template.add_section(fields)
if curr_template is not None:
templates[curr_section_name] = curr_template
config = Configurations()
config.update_options("general", general)
config.update_options("template", templates)
config.update_options("command", commands)
config.update_options("ignore", ignores)
return config
class Configurations(object):
def __init__(self):
self.general_options = self._get_default_general_options()
self.ignores = set()
self.templates = {}
self.commands = {}
def check_if_ignore(self, name):
return name in self.ignores
def get_command_template(self, command):
return self.templates[command]
def check_if_needs_push(self):
#TODO
return False
def filter_ignore(self, log_entry):
#TODO
return False
def update_options(self, option_type, new_options):
if option_type == "general":
self.general_options.update(new_options)
elif option_type == "template":
self.templates.update(new_options)
elif option_type == "command":
self.commands.update(new_options)
elif option_type == "ignore":
for name in new_options:
self.ignores.add(name)
else:
raise ValueError("Unrecognized option type %s" % option_type)
def _get_default_general_options(self):
general_options = {
"git_repo" : ".",
"get_log_repo" : ".",
"files" : []
}
return general_options
def get_configurations(current_path):
config_parser = ConfigFileParser()
config = config_parser.parse(current_path + "/.ba_config")
return config