city_retrofit/exports/insel/insel.py

77 lines
2.0 KiB
Python
Raw Normal View History

"""
Insel export models to insel format
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import os
from pathlib import Path
from abc import ABC
class Insel(ABC):
def __init__(self, path, name, new_content="", mode=1, keep_files=False):
self._path = path
self._name = name
self._full_path = None
self._content = None
self._results = None
self._keep_files = keep_files
self.add_content(new_content, mode)
self.save()
self.run()
def save(self):
with open(self.full_path, 'w') as insel_file:
insel_file.write(self.content)
return
@property
def full_path(self):
if self._full_path is None:
self._full_path = (Path(self._path) / 'tests/tmp' / self._name).resolve()
print(self._full_path)
return self._full_path
@property
def content(self):
if self._content is None:
if os.path.exists(self.full_path):
with open(self.full_path, 'r') as insel_file:
self._content = insel_file.read()
else:
self._content = ''
return self._content
@staticmethod
def add_block(file, block_number, block_type, inputs='', parameters=''):
file += "S " + str(block_number) + " " + block_type + "\n"
for block_input in inputs:
file += block_input + "\n"
if len(parameters) > 0:
file += "P " + str(block_number) + "\n"
for block_parameter in parameters:
file += block_parameter + "\n"
return file
def add_content(self, new_content, mode):
# mode = 1: keep old content
if mode == 1:
self._content = self.content + '\n' + new_content
# mode = 2: over-write
elif mode == 2:
self._content = new_content
else:
raise Exception('Add content mode not supported')
return
def run(self):
finish = os.system('insel ' + str(self.full_path))
os.close(finish)
if not self._keep_files:
os.remove(self.full_path)
def results(self, path):
raise NotImplementedError