sysml2py 0.4.3__tar.gz → 0.5.2__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sysml2py
3
- Version: 0.4.3
3
+ Version: 0.5.2
4
4
  Summary:
5
5
  Author: Christopher Cox
6
6
  Author-email: chris.cox@westfall.io
@@ -0,0 +1,111 @@
1
+ [build-system]
2
+ #requires = ["setuptools>=61.0"]
3
+ #build-backend = "setuptools.build_meta"
4
+ requires = ["poetry-core"]
5
+ build-backend = "poetry.core.masonry.api"
6
+
7
+ [project]
8
+ name = "sysml2py"
9
+ version = "0.5.2"
10
+ authors = [
11
+ { name="Christopher Cox", email="chris.cox@westfall.io" },
12
+ ]
13
+ description = "SysML v2.0 Parser"
14
+ readme = "README.md"
15
+ requires-python = ">=3.7"
16
+ classifiers = [
17
+ "Development Status :: 3 - Alpha",
18
+ "Programming Language :: Python :: 3",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Operating System :: OS Independent",
21
+ ]
22
+
23
+ [tool.setuptools]
24
+ include-package-data = true
25
+
26
+ [tool.setuptools.packages.find]
27
+ where = ["src/sysml2py", "src"]
28
+
29
+ [tool.setuptools.package-data]
30
+ grammar = ["*.tx"]
31
+
32
+ [tool.pytest.ini_options]
33
+ pythonpath = [
34
+ "src"
35
+ ]
36
+
37
+ [tool.semantic_release]
38
+ version_variable = [
39
+ "src/sysml2py/__init__.py:version",
40
+ ]
41
+ version_toml=[
42
+ "pyproject.toml:tool.poetry.version",
43
+ "pyproject.toml:project.version",
44
+ ]
45
+ commit_parser = "emoji"
46
+ build_command = "pip install poetry && poetry build"
47
+ commit_message = ":bookmark:{version}\n\nAutomatically generated by python-semantic-release"
48
+
49
+ [tool.semantic_release.changelog]
50
+ exclude_commit_patterns = [":robot:",":twisted_rightwards_arrows:",
51
+ ":poop:", "^[^:].*"]
52
+
53
+ [tool.semantic_release.commit_parser_options]
54
+ major_tags = [":boom:"]
55
+ minor_tags = [
56
+ ":sparkles:",
57
+ ":children_crossing:",
58
+ ":lipstick:",
59
+ ":iphone:",
60
+ ":egg:",
61
+ ":chart_with_upwards_trend:",
62
+ ]
63
+ patch_tags = [
64
+ ":ambulance:",
65
+ ":lock:",
66
+ ":bug:",
67
+ ":zap:",
68
+ ":goal_net:",
69
+ ":alien:",
70
+ ":wheelchair:",
71
+ ":speech_balloon:",
72
+ ":mag:",
73
+ ":apple:",
74
+ ":penguin:",
75
+ ":checkered_flag:",
76
+ ":robot:",
77
+ ":green_apple:",
78
+ ":memo:",
79
+ ":heavy_plus_sign:",
80
+ ":construction:",
81
+ ":white_check_mark:",
82
+ ]
83
+
84
+ [tool.semantic_release.branches.main]
85
+ match = "(main|master)"
86
+ prerelease = false
87
+ build_command = "pip install poetry && poetry build"
88
+
89
+ [tool.semantic_release.branches."dev0.5.x"]
90
+ match = "dev0.5.x"
91
+ prerelease = true
92
+ prerelease_token = "rc"
93
+ build_command = "pip install poetry && poetry build"
94
+
95
+ [project.urls]
96
+ "Homepage" = "https://github.com/Westfall-io/sysml2py"
97
+ "Bug Tracker" = "https://github.com/Westfall-io/sysml2py/issues"
98
+
99
+ [tool.poetry]
100
+ name = "sysml2py"
101
+ version = "0.5.2"
102
+ description = ""
103
+ authors = ["Christopher Cox <chris.cox@westfall.io>"]
104
+ readme = "README.md"
105
+ packages = [{ include = "sysml2py", from = "src" }]
106
+
107
+ [tool.poetry.dependencies]
108
+ python = "^3.9"
109
+ textx = "^3.1.1"
110
+ astropy = "^5.3.1"
111
+ pyyaml = "^6.0.1"
@@ -6,14 +6,39 @@ Created on Mon May 29 23:26:16 2023
6
6
  @author: christophercox
7
7
  """
8
8
 
9
- __all__ = ["load", "loads"]
9
+ __all__ = ["load", "loads", "load_grammar"]
10
10
  __author__ = "Christopher Cox"
11
+ __version__ = "0.1.0"
11
12
 
12
13
  from sysml2py.usage import Item, Attribute, Part, Port
13
14
  from sysml2py.definition import Model, Package
14
15
 
15
16
 
16
- def load_grammar(fp, formatting="json"):
17
+ def enforce_grammar(): # pragma: no cover
18
+ import re
19
+
20
+ comments_strip_rule = r"(?:(?:(?<!\\)(\/\/.*\n))|(?:\/\*(?:.|\n)*?\*\/))"
21
+ regex_rule = "\n[ ]*([\w]*)[ ]*:((?:(?:'[^']*')|(?:[^;']*))*;)"
22
+
23
+ g_list = []
24
+ rule_files = ["KerMLExpressions", "KerML", "SysML"]
25
+ for file in rule_files:
26
+ with open("grammar/" + file + ".tx", "r") as f:
27
+ g_list = (
28
+ re.findall(regex_rule, re.sub(comments_strip_rule, "", f.read()))
29
+ + g_list
30
+ )
31
+
32
+ # Make a new list and only add it if it's not already there
33
+ rules = []
34
+ [rules.append(x) for x in g_list if x[0] not in [x[0] for x in rules]]
35
+ grammar = "\n\n".join([":".join(x) for x in rules])
36
+ with open("grammar/SysML_compiled.tx", "w") as f:
37
+ f.write(grammar)
38
+ return grammar
39
+
40
+
41
+ def load_grammar(fp, debug=False, enforce=False):
17
42
  """SysML load from file pointer
18
43
 
19
44
  Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
@@ -73,36 +98,29 @@ def load_grammar(fp, formatting="json"):
73
98
  f"not {fp.__class__.__name__}"
74
99
  )
75
100
 
76
- import importlib.resources as pkg_resources
101
+ from sysml2py.formatting import reformat
77
102
 
78
- from textx import metamodel_from_file, TextXSyntaxError
103
+ if enforce: # pragma: no cover
104
+ # This can only be run in development mode.
105
+ from textx import metamodel_from_str, TextXSyntaxError
79
106
 
80
- import sysml2py
81
- from sysml2py.formatting import reformat
107
+ grammar = enforce_grammar()
108
+ meta = metamodel_from_str(grammar)
109
+ else:
110
+ import importlib.resources as pkg_resources
111
+ import sysml2py
112
+ from textx import metamodel_from_file, TextXSyntaxError
82
113
 
83
- if not isinstance(s, str):
84
- raise TypeError(f"the SysML object must be str, " f"not {s.__class__.__name__}")
114
+ grammar = str((pkg_resources.files(sysml2py) / "grammar/SysML_compiled.tx"))
115
+ meta = metamodel_from_file(grammar)
85
116
 
86
117
  try:
87
- grammar = str((pkg_resources.files(sysml2py) / "grammar/SysML.tx"))
88
- except:
89
- try:
90
- grammar = "./src/sysml2py/grammar/SysML.tx"
91
- except:
92
- grammar = "./grammar/SysML.tx"
93
- meta = metamodel_from_file(grammar)
94
- try:
95
- model = meta.model_from_str(s, debug=False)
118
+ model = meta.model_from_str(s, debug=debug)
96
119
  except TextXSyntaxError as e:
97
- print(e)
98
- import sys
120
+ print("TextX returned the following error: {}".format(e))
121
+ raise TextXSyntaxError("Invalid SysML")
99
122
 
100
- sys.exit()
101
-
102
- if formatting == "json":
103
- return reformat(model)
104
- else:
105
- return model
123
+ return reformat(model)
106
124
 
107
125
 
108
126
  def load(fp):
@@ -36,23 +36,7 @@ class Model:
36
36
  from sysml2py import load_grammar
37
37
 
38
38
  # Try to load the grammar from the string
39
- try:
40
- definition = load_grammar(s)
41
- except TextXSyntaxError:
42
- import sys
43
-
44
- print("Invalid SysML input, please correct the error.")
45
- sys.exit()
46
-
47
- # Ensure this is valid
48
- if definition["name"] == "PackageBodyElement":
49
- # This is a root element
50
- definition = definition["ownedRelationship"]
51
- else:
52
- import sys
53
-
54
- print("SysML does not match a base model.")
55
- sys.exit()
39
+ definition = load_grammar(s)["ownedRelationship"]
56
40
 
57
41
  # Add each sub-element to children.
58
42
  member_grammar = []
@@ -233,6 +217,15 @@ class Package:
233
217
  child.children[0].children.children.children
234
218
  )
235
219
  )
220
+ elif (
221
+ child.children[0].children.children.children.__class__.__name__
222
+ == "PartUsage"
223
+ ):
224
+ self.children.append(
225
+ Part().load_from_grammar(
226
+ child.children[0].children.children.children
227
+ )
228
+ )
236
229
  else:
237
230
  print(child.children[0].children[0].__class__.__name__)
238
231
  raise NotImplementedError
@@ -254,3 +247,8 @@ class Package:
254
247
 
255
248
  # self.children.append()
256
249
  return self
250
+
251
+ def _get_grammar(self):
252
+ # Force updates to grammar if something has changed.
253
+ self._ensure_body()
254
+ return self.grammar
@@ -8,8 +8,6 @@ Created on Wed May 31 13:26:53 2023
8
8
 
9
9
  from sysml2py.grammar.classes import RootNamespace
10
10
 
11
- # from sysml2py import Part
12
-
13
11
 
14
12
  def remove_classes(model):
15
13
  """An example docstring for a class definition."""
@@ -39,39 +37,11 @@ def remove_classes(model):
39
37
  def reformat(model):
40
38
  """An example docstring for a class definition."""
41
39
  # Convert to dictionary format
42
- try:
43
- model_out = {"name": model.__class__.__name__}
44
- model_out.update(remove_classes(model.__dict__))
45
- except Exception as e:
46
- print(e)
47
- print("Error in printing")
40
+ model_out = {"name": model.__class__.__name__}
41
+ model_out.update(remove_classes(model.__dict__))
48
42
 
49
43
  return model_out
50
44
 
51
45
 
52
46
  def classtree(model):
53
47
  return RootNamespace(model)
54
-
55
-
56
- # def collapse(model):
57
- # """Take a classtree and collapse it into our special classes that
58
- # package all of the sub-grammar classes."""
59
- # output = []
60
- # if model.__class__.__name__ == "RootNamespace":
61
- # for child in model.children[0].children:
62
- # if child.__class__.__name__ == "UsageElement":
63
- # ue = child.children
64
- # if ue.__class__.__name__ == "OccurrenceUsageElement":
65
- # se = ue.children
66
- # if se.children.__class__.__name__ == "PartUsage":
67
- # output.append(Part().load_from_grammar(se.children))
68
- # else:
69
- # pass
70
- # else:
71
- # # OccurrenceUsageElement
72
- # pass
73
- # else:
74
- # pass
75
- # else:
76
- # print("no")
77
- # return output
@@ -709,7 +709,7 @@ FeatureConjugation :
709
709
 
710
710
  FeatureValue :
711
711
  ( '=' | isDefault ?= 'default' ( '=' | isInitial ?= ':=' )? )
712
- ownedRelatedElement += OwnedExpression
712
+ ownedRelatedElement = OwnedExpression
713
713
  ;
714
714
 
715
715
  FeatureValueExpression :
@@ -936,8 +936,7 @@ Function :
936
936
  ;
937
937
 
938
938
  ReturnFeatureMember :
939
- MemberPrefix 'return'
940
- ownedRelatedElement += FeatureElement
939
+ MemberPrefix 'return' ownedRelatedElement += FeatureElement
941
940
  ;
942
941
 
943
942
 
@@ -33,7 +33,7 @@
33
33
  /* Operator Expressions */
34
34
 
35
35
  OwnedExpressionMember :
36
- ownedRelatedElement += OwnedExpression
36
+ ownedRelatedElement = OwnedExpression
37
37
  ;
38
38
 
39
39
  OwnedExpression :
@@ -47,7 +47,7 @@ OwnedExpressionReference :
47
47
  ;
48
48
 
49
49
  ConditionalExpression :
50
- operand += NullCoalescingExpression
50
+ operand = NullCoalescingExpression
51
51
  | operator = ConditionalOperator operand += NullCoalescingExpression
52
52
  '?' operand += OwnedExpressionReference 'else' operand += OwnedExpressionReference
53
53
  ;
@@ -125,10 +125,15 @@ XorOperator :
125
125
  'xor'
126
126
  ;
127
127
 
128
+ AndOperand :
129
+ (operator = AndOperator operand = EqualityExpression)
130
+ |
131
+ (operator = ConditionalAndOperator operand = EqualityExpressionReference)
132
+ ;
133
+
128
134
  AndExpression :
129
- equality=EqualityExpression (
130
- ( operator = AndOperator operand += EqualityExpression
131
- | operator = ConditionalAndOperator operand += EqualityExpressionReference ) )*
135
+ equality=EqualityExpression
136
+ (operation+=AndOperand)*
132
137
  ;
133
138
 
134
139
  AndOperator :
@@ -142,17 +147,20 @@ ConditionalAndOperator :
142
147
  // Equality Expressions
143
148
 
144
149
  EqualityExpressionReference :
145
- ownedRelationship += EqualityExpressionMember
150
+ ownedRelationship = EqualityExpressionMember
146
151
  ;
147
152
 
148
153
  EqualityExpressionMember :
149
- ownedRelatedElement += EqualityExpression
154
+ ownedRelatedElement = EqualityExpression
150
155
  ;
151
156
 
152
- EqualityExpression :
153
- classification=ClassificationExpression (
154
- operator = EqualityOperator operand += ClassificationExpression )*
157
+ EqualityOperand:
158
+ operator=EqualityOperator operand=ClassificationExpression
159
+ ;
155
160
 
161
+ EqualityExpression :
162
+ classification=ClassificationExpression
163
+ (operation+=EqualityOperand)*
156
164
  ;
157
165
 
158
166
  EqualityOperator :
@@ -227,37 +235,45 @@ EmptyFeature ://This doesn't work.
227
235
  ;
228
236
 
229
237
  // Relational Expressions
238
+ RelationalOperand:
239
+ operator=RelationalOperator operand=RangeExpression
240
+ ;
230
241
 
231
242
  RelationalExpression :
232
- range=RangeExpression (
233
- operator = RelationalOperator operand += RangeExpression )*
243
+ range=RangeExpression operation+=RelationalOperand*
234
244
  ;
235
245
 
236
246
  RelationalOperator :
237
- '<' | '>' | '<=' | '>='
247
+ '<=' | '>=' | '<' | '>'
238
248
  ;
239
249
 
240
250
  // Range Expressions
241
251
 
242
252
  RangeExpression :
243
- additive=AdditiveExpression (
244
- operator = '..' operand += AdditiveExpression )?
253
+ additive=AdditiveExpression ('..' operand = AdditiveExpression )?
245
254
  ;
246
255
 
247
256
  // Arithmetic Expressions
248
257
 
258
+ AdditiveOperand:
259
+ operator=AdditiveOperator operand=MultiplicativeExpression
260
+ ;
261
+
249
262
  AdditiveExpression :
250
- multiplicitive=MultiplicativeExpression (
251
- operator = AdditiveOperator operand += MultiplicativeExpression )*
263
+ multiplicitive=MultiplicativeExpression
264
+ (operation+=AdditiveOperand)*
252
265
  ;
253
266
 
254
267
  AdditiveOperator :
255
268
  '+' | '-'
256
269
  ;
257
270
 
271
+ MultiplicativeOperand:
272
+ operator=MultiplicativeOperator operand=ExponentiationExpression
273
+ ;
274
+
258
275
  MultiplicativeExpression :
259
- exponential=ExponentiationExpression (
260
- operator = MultiplicativeOperator operand += ExponentiationExpression )*
276
+ exponential=ExponentiationExpression operation+=MultiplicativeOperand*
261
277
  ;
262
278
 
263
279
  MultiplicativeOperator :
@@ -295,16 +311,16 @@ ExtentExpression :
295
311
 
296
312
  PrimaryExpression :
297
313
  base=BaseExpression
298
- ( '.' ownedRelationship += FeatureChainMember )?
314
+ ( '.' ownedRelationship1 += FeatureChainMember )?
299
315
  (
300
316
  ( operator += '#' '(' operand += SequenceExpression ')'
301
317
  | operator += '[' operand += SequenceExpression ']'
302
- | operator += '->' ownedRelationship += ReferenceTyping
318
+ | operator += '->' ownedRelationship = ReferenceTyping
303
319
  ( operand += BodyExpression | operand += FunctionReferenceExpression | operand+= ArgumentList )
304
320
  | operator += '.' operand += BodyExpression
305
321
  | operator += '.?' operand += BodyExpression
306
322
  )
307
- ( '.' ownedRelationship += FeatureChainMember )?
323
+ ( '.' ownedRelationship2 += FeatureChainMember )?
308
324
  )*
309
325
  ;
310
326
 
@@ -321,8 +337,8 @@ FunctionReference :
321
337
  ;
322
338
 
323
339
  FeatureChainMember :
324
- memberElement = QualifiedName
325
- | ownedRelatedElement += OwnedFeatureChain
340
+ ownedRelatedElement = OwnedFeatureChain
341
+ | memberElement = QualifiedName
326
342
  ;
327
343
 
328
344
  /* Base Expressions */
@@ -330,9 +346,9 @@ FeatureChainMember :
330
346
  BaseExpression :
331
347
  ownedRelationship=NullExpression
332
348
  | ownedRelationship=LiteralExpression
333
- | ownedRelationship=FeatureReferenceExpression
334
- | ownedRelationship=MetadataAccessExpression
335
349
  | ownedRelationship=InvocationExpression
350
+ | ownedRelationship=FeatureReferenceExpression
351
+ | ownedRelationship=MetadataAccessExpression
336
352
  | ownedRelationship=BodyExpression
337
353
  | '(' ownedRelationship=SequenceExpression ')'
338
354
  ;
@@ -340,11 +356,11 @@ BaseExpression :
340
356
  // Expression Bodies
341
357
 
342
358
  BodyExpression :
343
- ownedRelationship += ExpressionBodyMember
359
+ ownedRelationship = ExpressionBodyMember
344
360
  ;
345
361
 
346
362
  ExpressionBodyMember :
347
- ownedRelatedElement += ExpressionBody
363
+ ownedRelatedElement = ExpressionBody
348
364
  ;
349
365
 
350
366
  // This default production is overridden in the KerML and SysML grammars.
@@ -367,8 +383,14 @@ BodyParameter :
367
383
 
368
384
  // Sequence Expressions
369
385
 
386
+ SequenceOperand:
387
+ (',' operand = OwnedExpression)
388
+ |
389
+ ','
390
+ ;
391
+
370
392
  SequenceExpression :
371
- ownedRelationship=OwnedExpression ( ',' | operator = ',' operand += SequenceExpression)?
393
+ ownedRelationship=OwnedExpression operation+=SequenceOperand*
372
394
  ;
373
395
 
374
396
  // Feature Reference Expressions
@@ -390,7 +412,7 @@ MetadataAccessExpression :
390
412
  // Invocation Expressions
391
413
 
392
414
  InvocationExpression :
393
- ownedRelationship += OwnedFeatureTyping ArgumentList
415
+ ownedRelationship=OwnedFeatureTyping arg_list=ArgumentList
394
416
  ;
395
417
 
396
418
  OwnedFeatureTyping :
@@ -404,8 +426,7 @@ OwnedFeatureChain :
404
426
 
405
427
  // For use in KerML and SysML grammars
406
428
  FeatureChain :
407
- ownedRelationship += OwnedFeatureChaining '.' ownedRelationship += OwnedFeatureChaining
408
- ( '.' ownedRelationship += OwnedFeatureChaining )*
429
+ ownedRelationship += OwnedFeatureChaining['.']
409
430
  ;
410
431
 
411
432
  OwnedFeatureChaining :
@@ -413,33 +434,31 @@ OwnedFeatureChaining :
413
434
  ;
414
435
 
415
436
  ArgumentList :
416
- '(' ( PositionalArgumentList | NamedArgumentList )? ')'
437
+ '(' ( pos_list=PositionalArgumentList | named_list=NamedArgumentList )? ')'
417
438
  ;
418
439
 
419
440
  PositionalArgumentList :
420
- ownedRelationship += ArgumentMember
421
- ( ',' ownedRelationship += ArgumentMember )*
441
+ ownedRelationship += ArgumentMember[',']
422
442
  ;
423
443
 
424
444
  ArgumentMember :
425
- ownedRelatedElement += Argument
445
+ ownedRelatedElement = Argument
426
446
  ;
427
447
 
428
448
  Argument :
429
- ownedRelationship += ArgumentValue
449
+ ownedRelationship = ArgumentValue
430
450
  ;
431
451
 
432
452
  NamedArgumentList :
433
- ownedRelationship += NamedArgumentMember
434
- ( ',' ownedRelationship += NamedArgumentMember )*
453
+ ownedRelationship += NamedArgumentMember[',']
435
454
  ;
436
455
 
437
456
  NamedArgumentMember :
438
- ownedRelatedElement += NamedArgument
457
+ ownedRelatedElement = NamedArgument
439
458
  ;
440
459
 
441
460
  NamedArgument :
442
- ownedRelationship += ParameterRedefinition '=' ownedRelationship += ArgumentValue
461
+ redefinition = ParameterRedefinition '=' value = ArgumentValue
443
462
  ;
444
463
 
445
464
  ParameterRedefinition :
@@ -447,7 +466,7 @@ ParameterRedefinition :
447
466
  ;
448
467
 
449
468
  ArgumentValue :
450
- ownedRelatedElement += OwnedExpression
469
+ ownedRelatedElement = OwnedExpression
451
470
  ;
452
471
 
453
472
  // Null Expressions
@@ -461,8 +480,8 @@ NullExpression :
461
480
  LiteralExpression :
462
481
  LiteralBoolean
463
482
  | LiteralString
464
- | LiteralInteger
465
483
  | LiteralReal
484
+ | LiteralInteger
466
485
  | LiteralInfinity
467
486
  ;
468
487
 
@@ -497,7 +516,9 @@ LiteralInfinity :
497
516
  /* NAMES */
498
517
 
499
518
  ReservedKeyword:
500
- 'about' | 'to' | 'connect' | 'connection'
519
+ 'about' | /to[\s]/ | 'connect' | 'connection' | 'from' | /of[\s]/ | 'then' |
520
+ 'entry' | /accept[\s]/ | /do[\s]/ | /exit[\s]/ | /action[\s]/ | /at[\s]/ |
521
+ /assign[\s]/
501
522
  ;
502
523
 
503
524
  Name:
@@ -509,13 +530,14 @@ Qualification :
509
530
  ;
510
531
 
511
532
  QualifiedName:
512
- name1=Name ( '::' names+=Name )*
533
+ //name1=Name ( '::' names+=Name )*
534
+ names+=Name['::']
513
535
  ;
514
536
 
515
537
  /* TERMINALS */
516
538
 
517
539
  DECIMAL_VALUE :
518
- /[0-9]*/;
540
+ /[0-9]+/;
519
541
 
520
542
  EXP_VALUE:
521
543
  DECIMAL_VALUE ('e' | 'E') ('+' | '-')? DECIMAL_VALUE;