source: CIVL/mods/dev.civl.com/scripts/state_space_analyzer.py

main
Last change on this file was c2b37db, checked in by Alex Wilton <awilton@…>, 8 months ago

Merged focus branch into trunk.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5988 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.5 KB
Line 
1import sys
2import os
3
4import inspect
5import dataclasses
6
7import state_space
8from state_space import *
9from state_space_parser import parseStateSpace as parse
10
11def _getShortDoc(doc, prepend=""):
12 lines = doc.split('\n')
13 newLines = []
14 for i, line in enumerate(lines):
15 if line.strip() == '':
16 break
17
18 newLines.append(prepend+line)
19
20 return '\n'.join(newLines)
21
22def _validObj(name, obj):
23 return name[0] != '_' and (inspect.isfunction(obj) or inspect.isclass(obj))
24
25def _brieflyDescribe(name, obj):
26 print(" * "+name, end="")
27 if inspect.isfunction(obj):
28 print(inspect.signature(obj))
29 else:
30 print()
31
32 if (doc := inspect.getdoc(obj)):
33 print(_getShortDoc(doc, " "))
34
35def _describe(name, obj):
36 if name != __name__:
37 print(name)
38 print('='*len(name))
39 if (doc := inspect.getdoc(obj)):
40 print(doc)
41 print()
42
43 if dataclasses.is_dataclass(obj):
44 print("Fields")
45 print("------")
46 for field in dataclasses.fields(obj):
47 print(f"{field.name}: {field.type.__name__}")
48 print()
49
50 functions = []
51 classes = []
52
53 for member in inspect.getmembers(obj):
54 if member[0][0] == '_':
55 continue
56
57 if inspect.isfunction(member[1]):
58 functions.append(member)
59 elif inspect.isclass(member[1]):
60 classes.append(member)
61
62 print("Functions")
63 print("---------")
64 for f in functions:
65 _brieflyDescribe(f[0], f[1])
66 print()
67
68 print("Classes")
69 print("-------")
70 for c in classes:
71 _brieflyDescribe(c[0], c[1])
72 print()
73
74def _getObjFromSpecifier(specifier):
75 currentObj = (__name__, sys.modules[__name__])
76 currentSubSpecifier = ""
77
78 for objName in specifier.split('.'):
79 if objName == "":
80 continue
81
82 foundNextObj = False
83 for member in inspect.getmembers(currentObj[1]):
84 if not _validObj(member[0], member[1]):
85 continue
86
87 if objName == member[0]:
88 currentObj = member
89 foundNextObj = True
90 break
91
92 if not foundNextObj:
93 notFoundString = "Failed to find \""+objName+"\""
94 if currentSubSpecifier != "":
95 notFoundString += " under object \""+currentSubSpecifier[1:]+"\""
96 print(notFoundString)
97 return None
98
99 currentSubSpecifier += "."+objName
100
101 return currentObj
102
103def help(ident=""):
104 """
105 Prints out description of current module, or of the object specified by 'ident' if it is nonempty.
106
107 If 'ident' is empty, then prints out all available functions and classes of this interactive session.
108 If 'ident' is nonempty, then it prints a description of the function or class specified by 'ident', along with all available functions and classes available within this object.
109 'ident' can access subobjects using '.' as a separator.
110 """
111 if (obj := _getObjFromSpecifier(ident)):
112 _describe(obj[0], obj[1])
113
114##############################################
115
116print("CIVL interactive state space analyzer")
117print("=====================================")
118print("Execute function `help()` to get list of methods and structures available to you.\n")
119
120if len(sys.argv) > 1:
121 if not os.path.isfile(sys.argv[1]):
122 print("File does not exist: "+sys.argv[1])
123 exit()
124
125 print("Parsing file: "+sys.argv[1])
126 space = parse(sys.argv[1])
127 print("Parse complete. Resulting state space contained in variable 'space'.")
128
Note: See TracBrowser for help on using the repository browser.