67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
from entry import Entry
|
|
from actions import *
|
|
from conditions import *
|
|
|
|
|
|
class Config:
|
|
|
|
def __init__(self):
|
|
self._entries = []
|
|
|
|
def add_entry(self, entry: Entry):
|
|
self._entries.append(entry)
|
|
|
|
def get_next_entry(self):
|
|
return self._entries.pop()
|
|
|
|
def __next__(self):
|
|
self.get_next_entry()
|
|
|
|
def __iter__(self):
|
|
return iter(self._entries)
|
|
|
|
def read_config(self, *args, **kwargs):
|
|
with open('config.txt', 'r') as file:
|
|
for line in file:
|
|
if len(line) < 2:
|
|
continue
|
|
if line.startswith('#'):
|
|
continue
|
|
entry = self._create_entry(line)
|
|
self._entries.append(entry)
|
|
|
|
def _create_entry(self, line):
|
|
splits = line.split(';')
|
|
assert(len(splits) == 4)
|
|
name = splits[0]
|
|
condition_list = self._split_conditions(splits[1], name)
|
|
success_list = self._split_actions(splits[2], name)
|
|
fail_list = self._split_actions(splits[3], name)
|
|
return Entry(name, condition_list, success_list, fail_list)
|
|
|
|
def _split_conditions(self, split, entry_name):
|
|
splits = list()
|
|
split.strip()
|
|
for s in split.split(','):
|
|
s = s.strip()
|
|
ss = s.split(' ', 1)
|
|
condition = conditions[ss[0]]
|
|
argument = None
|
|
if len(ss) > 1:
|
|
argument = str(ss[1])
|
|
splits.append(condition(argument, entry=entry_name))
|
|
return splits
|
|
|
|
def _split_actions(self, split, entry_name):
|
|
splits = list()
|
|
split.strip()
|
|
for s in split.split(','):
|
|
s = s.strip()
|
|
ss = s.split(' ', 1)
|
|
action = actions[ss[0]]
|
|
argument = None
|
|
if len(ss) > 1:
|
|
argument = str(ss[1])
|
|
splits.append(action(argument, entry=entry_name))
|
|
return splits
|