Browse Source

Simplified things

Joeri Exelmans 5 years ago
parent
commit
549c81a8a8
5 changed files with 26 additions and 25 deletions
  1. 1 1
      test/README.md
  2. 4 14
      test/lib/builder.py
  3. 14 3
      test/lib/os_tools.py
  4. 6 6
      test/render.py
  5. 1 1
      test/test.py

+ 1 - 1
test/README.md

@@ -10,7 +10,7 @@ This will create a 'build' directory with compiled statechart models. It is alwa
 
 ## render.py
 
-The Python program `render.py` renders SVG graphs for test files. Rendered SVG files are already checked in to this repository. If you wish to re-render them, you need the NPM (NodeJS) package [state-machine-cat](https://github.com/sverweij/state-machine-cat/). Install NodeJS and NPM, and then install the NPM package 'state-machine-cat':
+The Python program `render.py` renders the test file state machines as SVG images. Rendered SVG files are already checked in to this repository. If you wish to re-render them, you need the NPM (NodeJS) package [state-machine-cat](https://github.com/sverweij/state-machine-cat/). Install NodeJS and NPM, and then install the NPM package 'state-machine-cat':
 ```
 npm i -g state-machine-cat
 ```

+ 4 - 14
test/lib/builder.py

@@ -1,25 +1,14 @@
 import os
 import importlib
 from sccd.compiler.sccdc import generate, Platforms
-
-def dropext(file):
-  return os.path.splitext(file)[0]
-
-def make_dirs(file):
-  os.makedirs(os.path.dirname(file), exist_ok=True)
+from lib.os_tools import *
 
 class Builder:
   def __init__(self, build_dir: str):
     self.build_dir = build_dir
 
-  def target_file(self, src_file: str, ext='.py') -> str:
-    return os.path.join(self.build_dir, dropext(src_file)+ext)
-
-  def module_name(self, src_file: str) -> str:
-    return os.path.join(self.build_dir, dropext(src_file)).replace(os.path.sep, ".")
-
   def build(self, src_file: str):
-    target_file = self.target_file(src_file)
+    target_file = os.path.join(self.build_dir, dropext(src_file)+'.py')
 
     # Get src_file and target_file modification times
     src_file_mtime = os.path.getmtime(src_file)
@@ -36,4 +25,5 @@ class Builder:
 
   def build_and_load(self, src_file: str):
     self.build(src_file)
-    return importlib.import_module(self.module_name(src_file))
+    module_name = os.path.join(self.build_dir, dropext(src_file)).replace(os.path.sep, ".")
+    return importlib.import_module(module_name)

+ 14 - 3
test/lib/os_tools.py

@@ -1,8 +1,13 @@
 import os
 from typing import List, Callable, Set
 
-# For a given list of files and or directories, get all the 
-def get_files(paths: List[str], filter: Callable[[str], bool]) -> List[str]:
+filter_any = lambda x: True
+filter_xml = lambda x: x.endswith('.xml')
+
+# For a given list of files and or directories,
+# recursively find all the files that adhere to an optional filename filter,
+# merge the results while eliminating duplicates.
+def get_files(paths: List[str], filter: Callable[[str], bool] = filter_any) -> List[str]:
   already_have: Set[str] = set()
   src_files = []
 
@@ -26,4 +31,10 @@ def get_files(paths: List[str], filter: Callable[[str], bool]) -> List[str]:
 
   return src_files
 
-xml_filter = lambda x: x.endswith('.xml')
+# Drop file extension
+def dropext(file):
+  return os.path.splitext(file)[0]
+
+# Ensure path of directories exists
+def make_dirs(file):
+  os.makedirs(os.path.dirname(file), exist_ok=True)

+ 6 - 6
test/render.py

@@ -19,9 +19,8 @@ if __name__ == '__main__':
     args = parser.parse_args()
 
 
-    py_builder = Builder(args.build_dir)
-    svg_builder = Builder(args.output_dir)
-    srcs = get_files(args.path, filter=xml_filter)
+    builder = Builder(args.build_dir)
+    srcs = get_files(args.path, filter=filter_xml)
 
     if len(srcs):
       if not args.no_svg:
@@ -37,13 +36,14 @@ if __name__ == '__main__':
 
 
     def process(src):
-      module = py_builder.build_and_load(src)
+      module = builder.build_and_load(src)
       model = module.Model()
 
       # Produce an output file for each class in the src file
       for class_name, _class in model.classes.items():
-        smcat_target = svg_builder.target_file(src, '_'+class_name+'.smcat')
-        svg_target = svg_builder.target_file(src, '_'+class_name+'.svg')
+        target_path = lambda ext: os.path.join(args.output_dir, dropext(src)+'_'+class_name+ext)
+        smcat_target = target_path('.smcat')
+        svg_target = target_path('.svg')
         
         make_dirs(smcat_target)
 

+ 1 - 1
test/test.py

@@ -96,7 +96,7 @@ if __name__ == '__main__':
     parser.add_argument('--build-dir', metavar='BUILD_DIR', type=str, default='build', help="Directory for built tests. Defaults to 'build'")
     args = parser.parse_args()
 
-    src_files = get_files(args.path, filter=xml_filter)
+    src_files = get_files(args.path, filter=filter_xml)
 
     builder = Builder(args.build_dir)
     suite = unittest.TestSuite()