|
|
@@ -0,0 +1,111 @@
|
|
|
+import os.path
|
|
|
+
|
|
|
+from engine.log import debug
|
|
|
+
|
|
|
+
|
|
|
+class FlatFileHandler:
|
|
|
+ def __init__(self, filename, directory='config/', base='/etc/hometv/'):
|
|
|
+ self.filename = filename
|
|
|
+ self.directory = directory
|
|
|
+ self.base = base
|
|
|
+
|
|
|
+ self.attributes = {}
|
|
|
+ self.cache = {}
|
|
|
+
|
|
|
+ if os.path.exists(self.base + self.directory + self.filename):
|
|
|
+ content = self.read_file()
|
|
|
+ debug('Loading config')
|
|
|
+ self.parse_config(content)
|
|
|
+
|
|
|
+ else:
|
|
|
+ self.create_file()
|
|
|
+
|
|
|
+ def handle(self, mode='r'):
|
|
|
+ return open(self.base + self.directory + self.filename, mode)
|
|
|
+
|
|
|
+ def is_empty(self):
|
|
|
+ return len(self.attributes) == 0
|
|
|
+
|
|
|
+ def create_file(self):
|
|
|
+ self.handle(mode='x').close()
|
|
|
+
|
|
|
+ def write_to_file(self):
|
|
|
+ file = self.handle('w+')
|
|
|
+
|
|
|
+ for attribute in self.attributes:
|
|
|
+ file.write(str(self.attributes[attribute]) + '\n')
|
|
|
+
|
|
|
+ def read_file(self):
|
|
|
+ file = self.handle()
|
|
|
+ content = None
|
|
|
+ if file.mode == 'r':
|
|
|
+ content = file.readlines()
|
|
|
+ file.close()
|
|
|
+ return content
|
|
|
+
|
|
|
+ def parse_config(self, lines):
|
|
|
+ debug('Parse settings. {} lines'.format(len(lines)))
|
|
|
+ for line in lines:
|
|
|
+ content = line.split('=')
|
|
|
+ debug('{}=>{}'.format(content[0], content[1].strip()))
|
|
|
+ self.cache[content[0]] = content[1]
|
|
|
+
|
|
|
+ def set_attribute(self, name, _type=str, value=None, default=None, raw=None):
|
|
|
+ self.attributes[name] = self.Attribute(name, _type=_type, value=value, default=default, raw=(self.cache[name] if name in self.cache else raw))
|
|
|
+
|
|
|
+ def remove_attribute(self, name):
|
|
|
+ self.attributes.pop(name)
|
|
|
+
|
|
|
+ def get(self, key):
|
|
|
+ debug(key)
|
|
|
+ return self.attributes[key].get()
|
|
|
+
|
|
|
+ class Attribute:
|
|
|
+ def __init__(self, name, _type=str, value=None, default=None, raw=None):
|
|
|
+ self.name = name
|
|
|
+ self.value = value
|
|
|
+ self.default = default
|
|
|
+ self.type = _type
|
|
|
+
|
|
|
+ self.raw = raw
|
|
|
+
|
|
|
+ if raw and not value:
|
|
|
+ self.set_from_raw(self.raw)
|
|
|
+
|
|
|
+ def __str__(self):
|
|
|
+ output = self.get()
|
|
|
+ if self.type is list:
|
|
|
+ output = ','.join(str(x) for x in output)
|
|
|
+ return '{}={}'.format(self.name, output)
|
|
|
+
|
|
|
+ def parse(self):
|
|
|
+ if self.type is str:
|
|
|
+ self.value = str(self.raw)
|
|
|
+ if self.type is bool:
|
|
|
+ self.value = self.raw.upper() in ['TRUE', 'YES', 'Y', 'T', '1']
|
|
|
+ if self.type is int:
|
|
|
+ self.value = int(self.raw)
|
|
|
+ if self.type is float:
|
|
|
+ self.value = float(self.raw)
|
|
|
+ if self.type is list:
|
|
|
+ self.value = self.raw.split(',')
|
|
|
+ if self.type is dict:
|
|
|
+ self.value = {}
|
|
|
+ attributes = self.raw.split(',')
|
|
|
+ for attribute in attributes:
|
|
|
+ attr = attribute.split('>')
|
|
|
+ self.value[attr[0]] = attr[1]
|
|
|
+
|
|
|
+ def set_from_raw(self, raw):
|
|
|
+ self.raw = raw.strip()
|
|
|
+ self.parse()
|
|
|
+
|
|
|
+ def set(self, value):
|
|
|
+ if type(value) is self.type:
|
|
|
+ self.value = value
|
|
|
+ else:
|
|
|
+ raise TypeError('{} expected but {} given'.format(self.type.__name__, type(value).__name__))
|
|
|
+
|
|
|
+ def get(self):
|
|
|
+ debug('Get value: {}'.format(self.value))
|
|
|
+ return self.value if self.value else self.default
|