diff --git a/api/s1_title.py b/api/s1_title.py index f49b889..4a74810 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -33,11 +33,11 @@ class InpTitle: def inp_in_title(section: list[str]) -> ChangeSet: - title = InpTitle(section) - cs = ChangeSet({'operation' : API_ADD, 'type': 'title', 'value' : title.value}) + obj = InpTitle(section) + cs = ChangeSet({'operation' : API_ADD, 'type': 'title', 'value' : obj.value}) return cs def inp_out_title(name: str) -> list[str]: - title = str(get_title(name)['value']) - return title.split('\n') + obj = str(get_title(name)['value']) + return obj.split('\n') diff --git a/api/s2_junctions.py b/api/s2_junctions.py index 88ff1a0..7607263 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -138,19 +138,19 @@ class InpJunction: def inp_in_junction(section: list[str]) -> ChangeSet: cs = ChangeSet() for s in section: - junction = InpJunction(s) - cs.append({'operation': API_ADD, 'type': 'junction', 'id': junction.id, 'elevation': junction.elevation, 'demand': junction.demand, 'pattern': junction.pattern}) + obj = InpJunction(s) + cs.append({'operation': API_ADD, 'type': 'junction', 'id': obj.id, 'elevation': obj.elevation, 'demand': obj.demand, 'pattern': obj.pattern}) return cs def inp_out_junction(name: str) -> list[str]: lines = [] - junctions = read_all(name, 'select * from junctions') - for j in junctions: - id = j['id'] - elev = j['elevation'] - demand = j['demand'] if j['demand'] != None else '' - pattern = j['pattern'] if j['pattern'] != None else '' + objs = read_all(name, 'select * from junctions') + for obj in objs: + id = obj['id'] + elev = obj['elevation'] + demand = obj['demand'] if obj['demand'] != None else '' + pattern = obj['pattern'] if obj['pattern'] != None else '' desc = ';' lines.append(f'{id} {elev} {demand} {pattern} {desc}') return lines diff --git a/api/s3_reservoirs.py b/api/s3_reservoirs.py index 541af9d..821b57a 100644 --- a/api/s3_reservoirs.py +++ b/api/s3_reservoirs.py @@ -114,3 +114,37 @@ def delete_reservoir_cache(name: str, cs: ChangeSet) -> SqlChangeSet: def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet: return execute_command(name, delete_reservoir_cache(name, cs)) + + +class InpReservoir: + def __init__(self, line: str) -> None: + tokens = line.split() + + num = len(tokens) + has_desc = tokens[-1].startswith(';') + num_without_desc = (num - 1) if has_desc else num + + self.id = str(tokens[0]) + self.head = float(tokens[1]) + self.pattern = float(tokens[2]) if num_without_desc >= 3 else None + self.desc = str(tokens[-1]) if has_desc else None + + +def inp_in_reservoir(section: list[str]) -> ChangeSet: + cs = ChangeSet() + for s in section: + obj = InpReservoir(s) + cs.append({'operation': API_ADD, 'type': 'reservoir', 'id': obj.id, 'head': obj.head, 'pattern': obj.pattern}) + return cs + + +def inp_out_junction(name: str) -> list[str]: + lines = [] + objs = read_all(name, 'select * from junctions') + for obj in objs: + id = obj['id'] + head = obj['head'] + pattern = obj['pattern'] if obj['pattern'] != None else '' + desc = ';' + lines.append(f'{id} {head} {pattern} {desc}') + return lines