simapy 4.6.1.dev1__py3-none-any.whl → 4.6.1.dev2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
simapy/sre.py CHANGED
@@ -1,43 +1,18 @@
1
1
  """Run a command using SIMA runtime engine (sre executable)"""
2
- import asyncio
2
+ import subprocess
3
3
  import os
4
4
  from pathlib import Path
5
5
  from typing import Sequence,Callable
6
6
  from simapy.sima_writer import SIMAWriter
7
7
 
8
8
 
9
- async def run_command(args, error_file, out_file, console_handler):
10
- """"Run a command asynchronously and print output to standard out.
11
- Standard err is piped to stderr.txt in the working directory"""
12
- process = await asyncio.create_subprocess_exec(
13
- *args, stdout=asyncio.subprocess.PIPE, stderr=error_file
14
- )
15
-
16
- while True:
17
- # Use an awaitable to read data from stdout
18
- # Read the next line of data from stdout asynchronously
19
- data = await process.stdout.readline()
20
- if not data:
21
- break # Break the loop when there's no more data
22
- sline = data.decode('utf-8')
23
- if console_handler:
24
- console_handler(sline)
25
- else:
26
- # Print the line to standard out
27
- print(sline, end="\n")
28
- print(sline, end="\n", file=out_file)
29
-
30
- # Wait for the subprocess to finish
31
- await process.wait()
32
- if process.returncode is not None:
33
- return process.returncode
34
-
35
9
  class SIMA:
36
10
  """"Run a command using SIMA runtime engine (sre executable)"""
37
11
 
38
12
  def __init__(self, exe=None, fail_on_error=True):
39
13
  self.fail_on_error = fail_on_error
40
- # A status handler is a function that can handle standard out from the process
14
+ # A Console handler is a function that can handle standard out from the process
15
+ # This can i.e. be used to parse @STATUS messages from the SIMA process wich can be used for progress indication
41
16
  self.console_handler: Callable = None
42
17
  if exe:
43
18
  self.exe = exe
@@ -48,26 +23,42 @@ class SIMA:
48
23
 
49
24
 
50
25
  def run(self, working_dir, command_args: Sequence[str]):
51
- """Run the sima executable and print output to standard out.
26
+ """Run the sima executable and print output to standard out.
52
27
  Standard err is piped to stderr.txt in the working directory"""
53
28
 
54
29
  wdir = Path(str(working_dir))
55
30
  arguments = [self.exe]
56
- arguments.extend(["-configuration", "/var/opt/sima/config"])
57
31
  arguments.append("-consoleLog")
58
32
  arguments.append("--progress")
59
33
  arguments.extend(["-data", working_dir])
60
34
  wdir.mkdir(parents=True, exist_ok=True)
61
35
  self.__add_command_args(working_dir, arguments, command_args)
62
- err_file = wdir / "stderr.txt"
63
36
  out_file = wdir / "stdout.txt"
64
- with open(err_file, "w", encoding="utf8") as e_f, open(
65
- out_file, "w", encoding="utf8"
66
- ) as o_f:
67
- exit_code = asyncio.run(run_command(arguments, e_f, o_f, self.console_handler))
37
+ with open(out_file, "w", encoding="utf8") as o_f:
38
+ proc = subprocess.Popen(arguments,
39
+ stdin=subprocess.PIPE,
40
+ stdout=subprocess.PIPE,
41
+ stderr=subprocess.STDOUT)
42
+ self.__read_lines(proc, o_f)
43
+ # Wait for the process to finish
44
+ exit_code = proc.wait()
68
45
  if exit_code != 0 and self.fail_on_error:
69
46
  raise RuntimeError(f"SIMA exited with error code {exit_code}")
70
47
 
48
+ def __read_lines(self,proc,out_file):
49
+ console_handler = self.console_handler
50
+ while True:
51
+ line = proc.stdout.readline()
52
+ if not line:
53
+ break
54
+ sline = line.decode(encoding="utf8").rstrip()
55
+ if console_handler:
56
+ console_handler(sline)
57
+ else:
58
+ # Print the line to standard out
59
+ print(sline, end="\n")
60
+ print(sline, end="\n", file=out_file)
61
+
71
62
  def __add_command_args(self, working_dir: Path, args: Sequence, command_args: Sequence[str]):
72
63
  """Add command arguments to the list of arguments"""
73
64
  if len(command_args) > 0:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: simapy
3
- Version: 4.6.1.dev1
3
+ Version: 4.6.1.dev2
4
4
  Summary: Python utilities for SIMA
5
5
  Home-page: https://github.com/SINTEF/simapy
6
6
  Author: SINTEF Ocean
@@ -1,7 +1,7 @@
1
1
  simapy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  simapy/sima_reader.py,sha256=86P_CMaz1i6o8pIzXtNBHcJj0zS6Bw8_8BVO-Wvzs5c,1364
3
3
  simapy/sima_writer.py,sha256=MPwvooPLfGpOISxJUVlOWbtf4wVOEvgVCVsvQfLzxxY,1209
4
- simapy/sre.py,sha256=OpRNl66_dUnbXpE4UPSy4s3Q4IV8DtOng2VAQDY73l4,3601
4
+ simapy/sre.py,sha256=Lm14wKFg1afnEhSqcry0RsD7oLGinU8P8d8jBc4pHhQ,3273
5
5
  simapy/metocean/__init__.py,sha256=AeEF1FYpBDe46JuZ0EYJOofOuf2dwsaAdQZ0DsseB_g,39
6
6
  simapy/metocean/metocean.py,sha256=TmfSYezAzxWLt9n_ChLKIdaIz6GxSTUu6-5wnipQz6Y,2912
7
7
  simapy/metocean/package_info.py,sha256=6FEJ4gZkXieP2WWTA-oZO23BjQOi4YnXx3nLQY0ydYE,208
@@ -2400,8 +2400,8 @@ simapy/sima/workflow/blueprints/workflowsetitem.py,sha256=EyyxbrnPDcL-f1cgjWcH3o
2400
2400
  simapy/sima/workflow/blueprints/workflowsetnode.py,sha256=UIZYi61xYtOEBzSZ5wQclUjzdAarZ8ymJ67iGARdXH4,4018
2401
2401
  simapy/sima/workflow/blueprints/workflowtask.py,sha256=c1rpN0KRKg7zIhonXKEaAGL0c6k8-ecIcP3RK3EUD8g,1514
2402
2402
  simapy/sima/workflow/blueprints/xytablenode.py,sha256=J-ADKlq47efkQMbay2eS0WJGtB9K0B75caOUTbj7Ogs,1667
2403
- simapy-4.6.1.dev1.dist-info/LICENSE,sha256=F5QFv7maV1Hoof3UbIwTNKqDFoo2JykLoI_w6F4HqxI,1088
2404
- simapy-4.6.1.dev1.dist-info/METADATA,sha256=Sn8FREYcao6qN2meKBHoeHmkELFaQxbbgr-GQuGjJcY,1350
2405
- simapy-4.6.1.dev1.dist-info/WHEEL,sha256=00yskusixUoUt5ob_CiUp6LsnN5lqzTJpoqOFg_FVIc,92
2406
- simapy-4.6.1.dev1.dist-info/top_level.txt,sha256=AVWKlvxcgSVR0H1omucqMujOVuQbcFNLjlgp3skT_1Q,7
2407
- simapy-4.6.1.dev1.dist-info/RECORD,,
2403
+ simapy-4.6.1.dev2.dist-info/LICENSE,sha256=F5QFv7maV1Hoof3UbIwTNKqDFoo2JykLoI_w6F4HqxI,1088
2404
+ simapy-4.6.1.dev2.dist-info/METADATA,sha256=PRwk_Co4_-Yc8DUENfeqgRqkSnUL-8Jlmn0ARJnjS_c,1350
2405
+ simapy-4.6.1.dev2.dist-info/WHEEL,sha256=00yskusixUoUt5ob_CiUp6LsnN5lqzTJpoqOFg_FVIc,92
2406
+ simapy-4.6.1.dev2.dist-info/top_level.txt,sha256=AVWKlvxcgSVR0H1omucqMujOVuQbcFNLjlgp3skT_1Q,7
2407
+ simapy-4.6.1.dev2.dist-info/RECORD,,