2020-05-18 13:56:54 -04:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
class Insel:
|
|
|
|
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:
|
2021-08-31 12:17:41 -04:00
|
|
|
self._full_path = (Path(self._path) / 'tests/tmp' / self._name).resolve()
|
|
|
|
print(self._full_path)
|
2020-05-18 13:56:54 -04:00
|
|
|
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
|
|
|
|
|
2020-05-19 17:00:15 -04:00
|
|
|
@staticmethod
|
|
|
|
def add_block(file, block_number, block_type, inputs='', parameters=''):
|
2021-08-31 12:17:41 -04:00
|
|
|
file += "S " + str(block_number) + " " + block_type + "\n"
|
2020-05-19 17:00:15 -04:00
|
|
|
for block_input in inputs:
|
2021-08-31 12:17:41 -04:00
|
|
|
file += block_input + "\n"
|
2020-05-19 17:00:15 -04:00
|
|
|
if len(parameters) > 0:
|
2021-08-31 12:17:41 -04:00
|
|
|
file += "P " + str(block_number) + "\n"
|
2020-05-19 17:00:15 -04:00
|
|
|
for block_parameter in parameters:
|
2021-08-31 12:17:41 -04:00
|
|
|
file += block_parameter + "\n"
|
2020-05-19 17:00:15 -04:00
|
|
|
return file
|
2020-05-18 13:56:54 -04:00
|
|
|
|
|
|
|
def add_content(self, new_content, mode):
|
|
|
|
# mode = 1: keep old content
|
|
|
|
if mode == 1:
|
2021-08-31 12:17:41 -04:00
|
|
|
self._content = self.content + '\n' + new_content
|
2020-05-18 13:56:54 -04:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def results(self):
|
|
|
|
raise Exception('Not implemented')
|