Skip to content

Commit

Permalink
first code...
Browse files Browse the repository at this point in the history
  • Loading branch information
nl78 committed Nov 4, 2024
1 parent 725ae10 commit d3f3b06
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions fmutool/fmu_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .fmu_operations import FMU, OperationAbstract, FMUException
from .version import __version__ as tool_version
from .ssp import SSP

logger = logging.getLogger("fmutool")

Expand Down Expand Up @@ -630,6 +631,8 @@ def read(self, description_filename: Union[str, Path]) -> FMUContainer:

if description_filename.suffix == ".csv":
return self.read_csv(description_filename)
elif description_filename.suffix == ".ssp":
return self.read_ssp(description_filename)
else:
logger.critical(f"Unable to read from '{description_filename}': format unsupported.")

Expand Down Expand Up @@ -707,6 +710,13 @@ def check_headers(reader):
if not headers == ["rule", "from_fmu", "from_port", "to_fmu", "to_port"]:
raise FMUContainerError("Header (1st line of the file) is not well formatted.")

def read_ssp(self, ssp_filename: Path) -> FMUContainer:
logger.info(f"Building FMU Container from '{ssp_filename}'")
logger.warning("This feature is ALPHA stage.")
ssp = SSP(self.fmu_directory, ssp_filename)
container = FMUContainer(ssp_filename.stem, self.fmu_directory)
return container


class FMUContainerSpecWriter:
def __init__(self, container: FMUContainer):
Expand Down
52 changes: 52 additions & 0 deletions fmutool/ssp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
import xml.parsers.expat
import zipfile
from pathlib import Path

logger = logging.getLogger("fmutool")


class SSP:
def __init__(self, fmu_directory: Path, ssp_filename: Path):
self.ssp_filename = ssp_filename
self.analyse_ssp(fmu_directory)

def analyse_ssp(self, fmu_directory: Path):
with zipfile.ZipFile(self.ssp_filename) as zin:
for file in zin.filelist:
target_filename = Path(file.filename).name
if file.filename.endswith(".fmu"): # Extract all FMUs into the fmu_directory
zin.getinfo(file.filename).filename = target_filename
zin.extract(file, path=fmu_directory)
#elif file.filename == "SystemStructure.ssd":
elif file.filename.endswith(".ssd"):
zin.getinfo(file.filename).filename = target_filename
zin.extract(file, path=fmu_directory)
with zin.open(file) as file_handle:
self.analyse_ssd(file_handle, target_filename)

def analyse_ssd(self, file, filename):
logger.info(f"Find {filename}")
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = self.start_element
parser.EndElementHandler = self.end_element
parser.CharacterDataHandler = self.char_data

parser.ParseFile(file)


def start_element(self, name, attrs):
#logger.info(f"{name} {attrs}")
pass

def end_element(self, name):
pass

def char_data(self, data):
pass

class SSD:
class Component:
pass
class Unit:
pass
Binary file added tests/containers/bouncing_ball/bouncing.ssp
Binary file not shown.

0 comments on commit d3f3b06

Please sign in to comment.