-class GenericCaseViewer(object):
- """
- Gestion des commandes de creation d'une vue de cas
- """
- def __init__(self, __name="", __objname="case", __content=None):
- "Initialisation et enregistrement de l'entete"
- self._name = str(__name)
- self._objname = str(__objname)
- self._lineSerie = []
- self._switchoff = False
- self._numobservers = 2
- self._content = __content
- self._missing = """raise ValueError("This case requires beforehand to import or define the variable named <%s>. When corrected, remove this command, correct and uncomment the following one.")\n# """
- def _append(self):
- "Transformation de commande individuelle en enregistrement"
- raise NotImplementedError()
- def _extract(self):
- "Transformation d'enregistrement en commande individuelle"
- raise NotImplementedError()
- def _finalize(self):
- "Enregistrement du final"
- pass
- def _addLine(self, line=""):
- "Ajoute un enregistrement individuel"
- self._lineSerie.append(line)
- def dump(self, __filename=None):
- "Restitution normalisée des commandes"
- self._finalize()
- __text = "\n".join(self._lineSerie)
- __text +="\n"
- if __filename is not None:
- __file = os.path.abspath(__filename)
- __fid = open(__file,"w")
- __fid.write(__text)
- __fid.close()
- return __text
- def load(self, __filename=None):
- "Chargement normalisé des commandes"
- if os.path.exists(__filename):
- self._content = open(__filename, 'r').read()
- __commands = self._extract(self._content)
- return __commands
-
- # --> Inutile d'accrocher l'interpretation au cas
- # def _interpret(self):
- # "Interprétation d'une commande"
- # raise NotImplementedError()
- # def execCase(self, __filename=None):
- # "Exécution normalisée des commandes"
- # if os.path.exists(__filename):
- # self._content = open(__filename, 'r').read()
- # __retcode = self._interpret(self._content)
- # return __retcode
-
-class _TUIViewer(GenericCaseViewer):
- """
- Etablissement des commandes d'un cas TUI
- """
- def __init__(self, __name="", __objname="case", __content=None):
- "Initialisation et enregistrement de l'entete"
- GenericCaseViewer.__init__(self, __name, __objname, __content)
- self._addLine("# -*- coding: utf-8 -*-")
- self._addLine("#\n# Python script for ADAO TUI\n#")
- self._addLine("from numpy import array, matrix")
- self._addLine("import adaoBuilder")
- self._addLine("%s = adaoBuilder.New('%s')"%(self._objname, self._name))
- if self._content is not None:
- for command in self._content:
- self._append(*command)
- def _append(self, __command=None, __keys=None, __local=None, __pre=None, __switchoff=False):
- "Transformation d'une commande individuelle en un enregistrement"
- if __command is not None and __keys is not None and __local is not None:
- __text = ""
- if __pre is not None:
- __text += "%s = "%__pre
- __text += "%s.%s( "%(self._objname,str(__command))
- if "self" in __keys: __keys.remove("self")
- if __command not in ("set","get") and "Concept" in __keys: __keys.remove("Concept")
- for k in __keys:
- __v = __local[k]
- if __v is None: continue
- if k == "Checked" and not __v: continue
- if k == "Stored" and not __v: continue
- if k == "AvoidRC" and __v: continue
- if k == "noDetails": continue
- if isinstance(__v,Persistence.Persistence): __v = __v.values()
- if callable(__v): __text = self._missing%__v.__name__+__text
- if isinstance(__v,dict):
- for val in __v.values():
- if callable(val): __text = self._missing%val.__name__+__text
- numpy.set_printoptions(precision=15,threshold=1000000,linewidth=1000*15)
- __text += "%s=%s, "%(k,repr(__v))
- numpy.set_printoptions(precision=8,threshold=1000,linewidth=75)
- __text.rstrip(", ")
- __text += ")"
- self._addLine(__text)
- def _extract(self, __content=""):
- "Transformation un enregistrement en une commande individuelle"
- __is_case = False
- __commands = []
- __content = __content.replace("\r\n","\n")
- for line in __content.split("\n"):
- if "adaoBuilder.New" in line and "=" in line:
- self._objname = line.split("=")[0].strip()
- __is_case = True
- if not __is_case:
- continue
- else:
- if self._objname+".set" in line:
- __commands.append( line.replace(self._objname+".","",1) )
- return __commands
-
- # def _interpret(self, __content=""):
- # "Interprétation d'une commande"
- # __content = __content.replace("\r\n","\n")
- # exec(__content)
- # return 0
-
-class _DCTViewer(GenericCaseViewer):
- """
- Etablissement des commandes d'un cas DCT
- """
- def __init__(self, __name="", __objname="case", __content=None):
- "Initialisation et enregistrement de l'entete"
- GenericCaseViewer.__init__(self, __name, __objname, __content)
- self._observerIndex = 0
- self._addLine("# -*- coding: utf-8 -*-")
- self._addLine("#\n# Python script for ADAO DCT\n#")
- self._addLine("from numpy import array, matrix")
- self._addLine("#")
- self._addLine("%s = {}"%__objname)
- if self._content is not None:
- for command in self._content:
- self._append(*command)
- def _append(self, __command=None, __keys=None, __local=None, __pre=None, __switchoff=False):
- "Transformation d'une commande individuelle en un enregistrement"
- if __command is not None and __keys is not None and __local is not None:
- __text = ""
- if "execute" in __command: return
- if "self" in __keys: __keys.remove("self")
- if __command in ("set","get") and "Concept" in __keys:
- __key = __local["Concept"]
- __keys.remove("Concept")
- else:
- __key = __command.replace("set","").replace("get","")
- if "Observer" in __key and 'Variable' in __keys:
- self._observerIndex += 1
- __key += "_%i"%self._observerIndex
- __text += "%s['%s'] = {"%(self._objname,str(__key))
- for k in __keys:
- __v = __local[k]
- if __v is None: continue
- if k == "Checked" and not __v: continue
- if k == "Stored" and not __v: continue
- if k == "AvoidRC" and __v: continue
- if k == "noDetails": continue
- if isinstance(__v,Persistence.Persistence): __v = __v.values()
- if callable(__v): __text = self._missing%__v.__name__+__text
- if isinstance(__v,dict):
- for val in __v.values():
- if callable(val): __text = self._missing%val.__name__+__text
- numpy.set_printoptions(precision=15,threshold=1000000,linewidth=1000*15)
- __text += "'%s':%s, "%(k,repr(__v))
- numpy.set_printoptions(precision=8,threshold=1000,linewidth=75)
- __text.rstrip(", ").rstrip()
- __text += "}"
- if __text[-2:] == "{}": return # Supprime les *Debug et les variables
- self._addLine(__text)
- def _extract(self, __content=""):
- "Transformation un enregistrement en une commande individuelle"
- __is_case = False
- __commands = []
- __content = __content.replace("\r\n","\n")
- exec(__content)
- self._objdata = None
- __getlocals = locals()
- for k in __getlocals:
- try:
- if 'AlgorithmParameters' in __getlocals[k] and type(__getlocals[k]) is dict:
- self._objname = k
- self._objdata = __getlocals[k]
- except:
- continue
- if self._objdata is None:
- raise ValueError("Impossible to load given content as a ADAO DCT one (no 'AlgorithmParameters' key found).")
- for k in self._objdata:
- if 'Observer_' in k:
- __command = k.split('_',1)[0]
- else:
- __command = k
- __arguments = ["%s = %s"%(k,repr(v)) for k,v in self._objdata[k].items()]
- __commands.append( "set( Concept='%s', %s )"%(__command, ", ".join(__arguments)))
- __commands.sort() # Pour commencer par 'AlgorithmParameters'
- return __commands
-
-class _SCDViewer(GenericCaseViewer):