Salome HOME
mise à jour catalogues Telemac branche v7p2
[tools/eficas.git] / Aster / eficasManager.py
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 #
4 # This module helps you to dynamically add a catalog in the list of
5 # Aster catalogs and then start Eficas with this catalog
6 #
7 # WARN: this requires that <EFICAS_ROOT> and <EFICAS_ROOT>/Aster are
8 # both in the PYTHONPATH
9 #
10 # WARN: This python module is a prototype. For industrial usage, it
11 # should be integrated in a package outside from the Aster specific
12 # package ==> define a generic prefs 
13 #
14 # (gboulant - 23/03/2012)
15
16 import sys
17 sys.path.append("..")
18
19 # ===================================================================
20 # This part is to manage the catalog that defines the data structure 
21 import prefs_ASTER
22 def addCatalog(catalogName, catalogPath):
23     """
24     Function to add a catalog caraterized by a name (for the -c option
25     of the command line) and a path (the location of the python module
26     that corresponds to the catalog).
27     """
28     prefs_ASTER.addCatalog(catalogName, catalogPath)
29
30 import sys
31 import prefs
32 from InterfaceQT4 import eficas_go
33 def start(catalogName=None):
34     """
35     This simply start Eficas as usual, and passing the catalog name as
36     an argument if not already present on the command line.
37     """
38     if catalogName is not None and not "-c" in sys.argv:
39         # The catalogName can be consider as the -c option
40         sys.argv.append("-c")
41         sys.argv.append(catalogName)
42     eficas_go.lance_eficas(code=prefs.code)
43
44 # ===================================================================
45 # This part is to manage the data saved from Eficas to comm file.
46 # The text contained in a comm file defines a JdC ("Jeu de Commandes").
47
48 def loadJdc(filename):
49     """
50     This function loads the text from the specified JdC file. A JdC
51     file is the persistence file of Eficas (*.comm).
52     """
53     fcomm=open(filename,'r')
54     jdc = ""
55     for line in fcomm.readlines():
56         jdc+="%s"%line
57
58     # Warning, we have to make sure that the jdc comes as a simple
59     # string without any extra spaces/newlines
60     return jdc.strip()
61
62 def getJdcParameters(jdc,macro):
63     """
64     This function converts the data from the specified macro of the
65     specified jdc text to a python dictionnary whose keys are the
66     names of the data of the macro.
67     """
68     context = {}
69     source = "def args_to_dict(**kwargs): return kwargs \n"
70     source+= "%s = _F = args_to_dict          \n"%macro
71     source+= "parameters="+jdc+"                        \n"
72     source+= "context['parameters'] = parameters         \n"
73     #print source
74     code = compile(source, 'file.py', 'exec')
75     eval(code)
76     parameters = context['parameters']
77     return parameters
78
79 #
80 # ===========================================================
81 # Unit tests
82 # ===========================================================
83 #
84 def TEST_start():
85     addCatalog(catalogName="demo", catalogPath="mycata.py")
86     #start()
87     start("demo")
88
89 def TEST_getJdcParameters_fromString():
90     jdc="S_EP_INTERNE(dir_name='/tmp',                      \n \
91            TYPE_SEP='TUBE_SOUS_EPAISSEUR',                \n \
92            MAIL_TUBE=_F(UNITE_LONGUEUR='MM',              \n \
93                         R_EXT=130.,                       \n \
94                         EP_NOMINALE=22.0,                 \n \
95                         NB_SEG_AMORTISSEMENT=11,          \n \
96                         NB_SEG_TRANSITION=4,              \n \
97                         NB_SEG_GENERATRICES=5,            \n \
98                         DIST_PTS_GEN_MIN=100.0,           \n \
99                         NB_SEG_GEN_MIN=3,                 \n \
100                         NB_SEG_ARC=5,                     \n \
101                         NB_SEG_EP=3,),);"
102
103     parameters=getJdcParameters(jdc,"S_EP_INTERNE")
104     print "parameters = ",parameters
105     print parameters['MAIL_TUBE']['R_EXT']
106     
107     
108 def TEST_getJdcParameters_fromFile():
109     jdc = loadJdc('data.comm')
110     parameters=getJdcParameters(jdc,"EPREUVE_ENCEINTE")
111     print parameters
112
113
114 if __name__ == "__main__":
115     TEST_start()
116     TEST_getJdcParameters_fromString()
117     TEST_getJdcParameters_fromFile()