def __init__(self, family = None, name = None): # bool variable set to True if the user wants to ignore warning for key specified in # the xml that are not present in the dictionaryOfVariables. Default False self._ignoreMissing = False ## # bool variable set True by default. If True all warnings are enabled. self.warnings = True ## # self.family = self.normfamily = self.keyfamily = family self.name = self.normname = self.keyname = name from iscesys.Parsers.Parser import Parser from iscesys.DictUtils.DictUtils import DictUtils ##### #become hard to keep track of the name #### if self.normfamily: self.normfamily = Parser().normalize_comp_name(self.normfamily) if self.normname: self.normname = Parser().normalize_comp_name(self.normname) if self.keyfamily: self.keyfamily = DU.renormalizeKey(self.family) if self.keyname: self.keyname = DU.renormalizeKey(self.name) self.ext = '.xml' self.logger = None self.catalog = {} self.descriptionOfVariables = {} self.descriptionOfFacilities = {} self._dictionaryOfFacilities = {} self._cmdLineDict = None self.typeOfVariables = {} self.unitsOfVariables = {} self.dictionaryOfOutputVariables = {} self.dictionaryOfVariables = {} self.mandatoryVariables = [] self.optionalVariables = [] import sys if "--help" in sys.argv or "-h" in sys.argv: #assume that the factory for which we want to get the help # is after the keyword --help or -h from iscehelp import Helper help = Helper() if ("--steps" or "-s") in sys.argv: help.askHelp(self, steps=True) else: help.askHelp(self, steps=False) '''
def main(self): #The main greeting self.greeting(self.name) #some information on the internals from iscesys.DictUtils.DictUtils import DictUtils normname = DictUtils.renormalizeKey(NAME.public_name) print() print("NAME.public_name = {0}".format(NAME.public_name)) print("normname = {0}".format(normname)) print("self.name = {0}".format(self.name)) if self.descriptionOfVariables[normname]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normname]['doc'])) if normname in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normname]['units'])) normlang = DictUtils.renormalizeKey(LANGUAGE.public_name) print("LANGUAGE.public_name = {0}".format(LANGUAGE.public_name)) print("normlang = {0}".format(normlang)) print("self.language = {0}".format(self.language)) if self.descriptionOfVariables[normlang]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normlang]['doc'])) if normlang in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normlang]['units'])) print() print("For more fun, try this command line:") print("./greeter.py greeter.xml") print("./greeter.py greeterS.xml") print("./greeter.py greeterEC.xml") print("Try the different styles that are commented out in greeter.xml") print("Try entering data on the command line mixing with xml:") print( "./greeter.py greeter.xml greeter.'language to use in greeting'=spanish" ) print("or try this,") cl = "./greeter.py " cl += "Greeter.name\ to\ use\ \ \ IN\ greeting=Juan " cl += "gREETER.LANGUAGE\ TO\ USE\ IN\ GREETING=cowboy " cl += "greeter.name\ to\ use\ in\ greeting.units='m/s' " cl += "greeter.'name to use in greeting'.doc='My new doc'" print("{0}".format(cl)) print("etc.") return
def main(self): print("Hello, {0}!".format(self.gname)) #Print some extra information to see how it all works print() print("Some additional information") from iscesys.DictUtils.DictUtils import DictUtils normname = DictUtils.renormalizeKey(NAME.public_name) print("Parameter NAME public name = {0}".format(NAME.public_name)) print("Parameter NAME internal normalized name = {0}".format(normname)) if self.descriptionOfVariables[normname]['doc']: print("Parameter NAME doc = {0}".format(self.descriptionOfVariables[normname]['doc'])) if normname in self.unitsOfVariables.keys(): print("Parameter NAME units = {0}".format(self.unitsOfVariables[normname]['units'])) print("Application attribute: self.gname = {0}".format(self.gname)) print() print() print("For more fun, try these command lines:") print("./greeter.py greeter.xml") print("Try the different styles that are commented out in greeter.xml") print("Try entering data on the command line:") print("./greeter.py greeter.'name to use in greeting'=Jane") print("or try this,") cl = "./greeter.py " cl += "Greeter.name\ to\ use\ \ \ IN\ greeting=Juan " cl += "greeter.'name to use in greeting'.units='m/s' " cl += "greeter.'name to use in greeting'.doc='My new doc string'" print("{0}".format(cl)) print("etc.") return
def main(self): print("Hello, {0}!".format(self.name)) print() print("Some information") from iscesys.DictUtils.DictUtils import DictUtils normname = DictUtils.renormalizeKey(NAME.public_name) print("NAME.public_name = {0}".format(NAME.public_name)) print("normname = {0}".format(normname)) print("self.name = {0}".format(self.name)) if self.descriptionOfVariables[normname]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normname]['doc'])) if normname in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normname]['units'])) print() print("For more fun, try this command line:") print("./greeter.py greeter.xml") print("Try the different styles that are commented out in greeter.xml") print("Try entering data on the command line:") print("./greeter.py greeter.'name to use in greeting'=Jane") print("or try this,") cl = "./greeter.py " cl += "Greeter.name\ to\ use\ \ \ IN\ greeting=Juan " cl += "greeter.'name to use in greeting'.units='m/s' " cl += "greeter.'name to use in greeting'.doc='My new doc string'" print("{0}".format(cl)) print("etc.") return
def _processCommandLine(self,cmdline=None): from iscesys.Parsers.Parser import Parser if cmdline: if(isinstance(cmdline,str)): #just in case a string is passed, turn it into a list cmdline = [cmdline] self.cmdline = cmdline else: self.cmdline = self._getCommandLine() #process the command line and return a dictionary of dictionaries with # components per each node. # propDict contains the property for each component. # factDict contains the info for the component factory. # miscDict might contain doc and units. opts are the command lines # preceeded by -- PA = Parser() propDict, factDict, miscDict, self._argopts = PA.commandLineParser( self.cmdline ) CmdLinePropDict().update(propDict) CmdLineFactDict().update(factDict) CmdLineMiscDict().update(miscDict) #extract doc from miscDict docDict = DU.extractDict(miscDict, 'doc') CmdLineDocDict().update(docDict) #extract units from miscDict unitsDict = DU.extractDict(miscDict, 'units') CmdLineUnitsDict().update(unitsDict) # self.catalog stores the properties for all configurable components # as a dictionary of dictionaries which wil be used to recursively # initialize the components if propDict: # propDict contains a only the Application dictionary at the top # level self.catalog = propDict[list(propDict.keys())[0]] self._cmdLineDict = (factDict, docDict, unitsDict) return None
def renderHdr(self): from datetime import datetime from isceobj.XmlUtil import xmlUtils as xml from isce import release_version, release_svn_revision, release_date, svn_revision # odProp = {} # odMisc = {} # odFact = {} odProp = xml.OrderedDict() odFact = xml.OrderedDict() odMisc = xml.OrderedDict() # hack since the length is normally not set but obtained from the file # size, before rendering make sure that coord1.size is set to length self.coord2.coordSize = self.length self.renderToDictionary(self, odProp, odFact, odMisc) # remove key,value parir with empty value (except if value is zero) DU.cleanDictionary(odProp) DU.cleanDictionary(odFact) DU.cleanDictionary(odMisc) odProp['ISCE_VERSION'] = "Release: %s, svn-%s, %s. Current: svn-%s." % \ (release_version, release_svn_revision, release_date, svn_revision) outfile = self.getFilename() + '.xml' firstTag = 'imageFile' XD = XmlDumper() XD.dump(outfile, odProp, odFact, odMisc, firstTag) return None
def askHelp(self, instance, steps=False): #since it can be called externally, make sure that we remove the #arguments that are not understood by the isce Parser try: sys.argv = [sys.argv[0]] instance._parameters() instance.initProperties({}) instance._init() instance._facilities() instance._dictionaryOfFacilities = DU.renormalizeKeys(instance._dictionaryOfFacilities) self.helper(instance, steps) except Exception as e: print("No help available.")
def main(self): #the main greeting message self.greeting(self.name) #some information on the inner workings print() print("Some information") from iscesys.DictUtils.DictUtils import DictUtils normname = DictUtils.renormalizeKey(NAME.public_name) print("NAME.public_name = {0}".format(NAME.public_name)) print("normname = {0}".format(normname)) print("self.name = {0}".format(self.name)) if self.descriptionOfVariables[normname]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normname]['doc'])) if normname in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normname]['units'])) print() print("For more fun, try this command line:") print("./greeter.py greeter.xml") print("./greeter.py greeterS.xml") print("./greeter.py greeterEC.xml") print("Try the different styles that are commented out in greeter.xml") print("Try entering data on the command line:") print("./greeter.py greeter.'name to use in greeting'=Jane") print("or try this,") cl = "./greeter.py " cl += "Greeter.name\ to\ use\ \ \ IN\ greeting=Juan " cl += "greeter.'Greeting Message'.factorymodule=greetings " cl += "greeter.'Greeting message'.factoryname=english_cowboy " cl += "greeter.name\ to\ use\ in\ greeting.units='m/s' " cl += "greeter.'name to use in greeting'.doc='My new doc'" print("{0}".format(cl)) print("etc.") return
def renderToDictionary(self,obj,propDict,factDict,miscDict): #remove meaningless values from the dictionaries for k,v in obj.dictionaryOfVariables.items(): val = getattr(obj, v[0].replace('self.','')) if v[1] == 'component':#variable type propDict[k] = {} miscDict[k] = {} #check if the key are equivalent and possible replace the one in the dict with k if DU.keyIsIn(k,obj._dictionaryOfFacilities,True): factDict[k] = obj._dictionaryOfFacilities[k] else: factDict[k] = {} self.renderToDictionary(val,propDict[k],factDict[k],miscDict[k]) else: propDict.update({k:val}) if k in obj.unitsOfVariables: miscDict[k] = {'units':obj.unitsOfVariables[k]} if k in obj.descriptionOfVariables: try: miscDict[k].update({'doc':obj.descriptionOfVariables[k]}) except KeyError: miscDict[k] = {'doc':obj.descriptionOfVariables[k]}
def commandLineParser(self, args): from iscesys.DictUtils.DictUtils import DictUtils as DU """commandLineParser Parses a command line, which may include files and command line options and returns dictionaries containing propDict, factDict, miscDict, and listOfOptions where propDict contains the input values for the properties of an ISCE application as well as those for the components declared as facilities in the application factDict contains input values for the factories used in constructing instances of the components declared as facilities in the application. miscDict contains the above two types of information that are entered in-line on the command line. These will override those given in the files during component initialization if there are conflicts. listOfOptions contains the '--' style options such as '--help'. """ propDict = {} factDict = {} miscDict = {} listOfOptions = [] for arg in args: if arg.startswith('--'): listOfOptions.append(arg) continue isFile = False for filetype in self._filetypes: if arg.endswith('.' + filetype): ## imports from iscesys.DictUtils.DictUtils import DictUtils as DU from iscesys.Parsers.FileParserFactory import createFileParser FP = createFileParser(filetype) tmpProp, tmpFact, tmpMisc = FP.parse(arg) if tmpProp: DU.updateDictionary(propDict, tmpProp, replace=True) if tmpFact: DU.updateDictionary(factDict, tmpFact, replace=True) if tmpMisc: DU.updateDictionary(miscDict, tmpMisc, replace=True) isFile = True break if isFile: continue #if it gets here the argument is not a file #assume a form like, #component1.component2 .... .componentN.attribute=value . #no space otherwise the split above will not work properly #probably it is better if we specify from the top component so it #is easier to handle the case in which the files come after #(otherwise the key of the first node is not defined) tmpProp, tmpFact, tmpMisc = self.dotStringToDicts(arg) if tmpProp: DU.updateDictionary(propDict, tmpProp, replace=True) if tmpFact: DU.updateDictionary(factDict, tmpFact, replace=True) if tmpMisc: DU.updateDictionary(miscDict, tmpMisc, replace=True) return (DU.renormalizeKeys(propDict), DU.renormalizeKeys(factDict), DU.renormalizeKeys(miscDict), listOfOptions)
def _selectFromDicts(self,dblist): ''' Select all the relevant information for this instance from the different dictionaries and merge them. Returns a tuple with (propDict,factDict,miscDict,unitsDict,docDict) with proctDict already with the top key removed ''' #Parse the dblist into the separate configuration dictionaries from iscesys.Parsers.Parser import Parser PA = Parser() propDict, factDict, miscDict, argopts = PA.commandLineParser( dblist ) #extract doc from miscDict docDict = DU.extractDict(miscDict, 'doc') #extract units from miscDict unitsDict = DU.extractDict(miscDict, 'units') from iscesys.Component.Application import CmdLinePropDict from iscesys.Component.Application import CmdLineFactDict from iscesys.Component.Application import CmdLineMiscDict from iscesys.Component.Application import CmdLineDocDict from iscesys.Component.Application import CmdLineUnitsDict cmdLinePropDict = DU.renormalizeKeys(CmdLinePropDict()) cmdLineFactDict = DU.renormalizeKeys(CmdLineFactDict()) cmdLineMiscDict = DU.renormalizeKeys(CmdLineMiscDict()) cmdLineUnitsDict = DU.renormalizeKeys(CmdLineUnitsDict()) cmdLineDocDict = DU.renormalizeKeys(CmdLineDocDict()) propName = {} factName = {} miscName = {} unitsName = {} docName = {} # NOTE: all dicts have the key used for search removed #NOTE CmdLine... have highest priority #extract everything that belongs to self.name from the command line. #this has the top priority if(self.keyname): propName,factName,miscName,unitsName,docName = \ self._extractFromDicts([cmdLinePropDict,cmdLineFactDict,cmdLineMiscDict, cmdLineUnitsDict,cmdLineDocDict],self.keyname) #extract everything that belongs to self.family from the command line. #this has the second highest priority propFamily = {} factFamily = {} miscFamily = {} unitsFamily = {} docFamily = {} if(self.keyfamily): propFamily,factFamily,miscFamily,unitsFamily,docFamily =\ self._extractFromDicts([cmdLinePropDict,cmdLineFactDict,cmdLineMiscDict, cmdLineUnitsDict,cmdLineDocDict],self.keyfamily) propDictF = {} factDictF = {} miscDictF = {} unitsDictF = {} docDictF = {} #extract everything that belongs to self.family from the dblist that include local and db directory files #this has the second highest priority if(self.keyfamily in propDict): propDictF,factDictF,miscDictF,unitsDictF,docDictF =\ self._extractFromDicts( [propDict,factDict,miscDict,unitsDict,docDict],self.keyfamily ) propDictN = {} factDictN = {} miscDictN = {} unitsDictN = {} docDictN = {} if(self.keyname in propDict): propDictN,factDictN,miscDictN,unitsDictN,docDictN =\ self._extractFromDicts( [propDict,factDict,miscDict,unitsDict,docDict],self.keyname ) self._updateFromDicts([propDictF,factDictF,miscDictF,unitsDictF,docDictF], [propDictN,factDictN,miscDictN,unitsDictN,docDictN], [True,True,True,True,False]) self._updateFromDicts([propDictF,factDictF,miscDictF,unitsDictF,docDictF], [propFamily,factFamily,miscFamily,unitsFamily,docFamily], [True,True,True,True,False]) self._updateFromDicts([propDictF,factDictF,miscDictF,unitsDictF,docDictF], [propName,factName,miscName,unitsName,docName], [True,True,True,True,False]) return propDictF,factDictF,miscDictF,unitsDictF,docDictF
def init(self,propDict=None,factDict=None,docDict=None,unitsDict=None): if propDict is None: propDict = {} else: propDict = DU.renormalizeKeys(propDict) if factDict is None: factDict = {} else: factDict = DU.renormalizeKeys(factDict) if docDict is None: docDict = {} else: docDict = DU.renormalizeKeys(docDict) if unitsDict is None: unitsDict = {} else: unitsDict = DU.renormalizeKeys(unitsDict) self.catalog = DU.renormalizeKeys(self.catalog) self._dictionaryOfFacilities = DU.renormalizeKeys( self._dictionaryOfFacilities ) self.descriptionOfVariables = DU.renormalizeKeys( self.descriptionOfVariables ) self.unitsOfVariables = DU.renormalizeKeys(self.unitsOfVariables) #update the various dictionaries with what was read from command line if not propDict == {}: # the top level has only one key that is the appicaltion name DU.updateDictionary(self.catalog,propDict,replace=True) if not factDict == {}: # the top level has only one key that is the appicaltion name #note: the _dictionaryOfFacilities has also a doc str. add this as a spare keyword so the # content will be appended instead of replaced DU.updateDictionary(self._dictionaryOfFacilities,factDict,replace=True,spare='doc') if not docDict == {}: #The top level has only one key that is the appicaltion name. #the update does a append if there is already an entry with a particular key DU.updateDictionary(self.descriptionOfVariables,docDict) if not unitsDict == {}: #The top level has only one key, the application name. In this case replace and hopefully they put the same units!!! DU.updateDictionary(self.unitsOfVariables,unitsDict,replace = True) #init recursively self.initRecursive(self.catalog,self._dictionaryOfFacilities)
def _configureThis(self): #temp hack when the instance does not support the configurability #from local files if(self.name or self.family or self.normname or self.normfamily): #Determine the possible configuration file names. #x refers to the files in the install directory where #the component is installed. #r refers to a directory defined through the #environment variable ISCEDB. #l refers to the local directory. #family refers to the name given to a component in its definition. #name refers to an instance name of the component. xfamilydb = rfamilydb = lfamilydb = '' xnamedb = rnamedb = lnamedb = '' import inspect, os xpath = os.path.split(inspect.getfile(self.__class__))[0] lpath = os.curdir #rpath, rafmilydb, and rnamedb are only used if environment #variable ISCEDB is defined rpath = '' try: rpath = os.environ['ISCEDB'] except: pass #the family name remote and local db filenames if self.family: familydb = self.family+self.ext xfamilydb = os.path.join(xpath, familydb) lfamilydb = os.path.join(lpath, familydb) if rpath: rfamilydb = os.path.join(rpath, familydb) #the instance name remote and local db filenames if self.name: namedb = self.name+self.ext xnamedb = os.path.join(xpath, namedb) lnamedb = os.path.join(lpath, namedb) if rpath: rnamedb = os.path.join(rpath, namedb) #Build the configuration data base list #ordered in increasing order of priorities. dblist = [] #Lowest priority: from the install directory #family-name db if os.path.exists(xfamilydb): dblist.append(xfamilydb) #instance-name db if os.path.exists(xnamedb): dblist.append(xnamedb) #Second priority: remote ISCEDB directory #family-name db if os.path.exists(rfamilydb): dblist.append(rfamilydb) #instance-name db if os.path.exists(rnamedb): dblist.append(rnamedb) #Third priority: current directory #family-name db if os.path.exists(lfamilydb): dblist.append(lfamilydb) #instance-name db if os.path.exists(lnamedb): dblist.append(lnamedb) self._parameters() propDict,factDict,miscDict, unitsDict,docDict = self._selectFromDicts(dblist) self._updateFromDicts([self.catalog],[propDict],[True]) #just to be sure that the facilities, even if default ones, #are defined so we can check against the dictionaryOfFacilities #to make sure that a property is indeed a property and #not a facilities (used in initProperties to validate #the property) self._facilities() self.initProperties(self.catalog) self._init() self._facilities() self._dictionaryOfFacilities = DU.renormalizeKeys(self._dictionaryOfFacilities) self._updateFromDicts([self._dictionaryOfFacilities],[factDict],[True]) self.init(self.catalog,self._dictionaryOfFacilities,unitsDict,docDict) # Run the user's _configure to transfer user-configured facilities to # the instance variables self._configure() ''' #Create the configurable's dictionaryOfVariables and initialize #its parameters with code defaults: the lowest priority. self._parameters() # should use self.name, but not quite ready for that yet this will not work recursively. it has to look down the full dictionary to see if the key appears. The assumption will be that here we only update "global" attributes, so it's fine if the same normname is used more than once since all those instances are meant to be initialiazed with the same values. it also need to do the same thing for the other dictionaries. #Configure the parameters from the propDict dictionary #that was created from the dblist if propDict.keys(): if DU.renormalizeKey(self.normfamily) in propDict.keys(): self.catalog = propDict[DU.renormalizeKey(self.normfamily)] if DU.renormalizeKey(self.normname) in propDict.keys(): DU.updateDictionary(self.catalog, propDict[DU.renormalizeKey(self.normname)], replace=True) else: self.catalog = {} # based on the local inputs and application inputs select the highest priority # properties and factories. Note self.catalog has already the first key stripped self.catalog,factDict,miscDict, unitsDict,docDict = self._selectFromDicts(dblist) self.initProperties(self.catalog) self._init() #Initialize the configurable's facilities #with code defaults: the lowest priority self._facilities() #Configure the facilities with the factDict dictionary #that was created from the dblist if factDict.keys() or docDict.keys() or unitsDict.keys(): self._cmdLineDict = (factDict, docDict, unitsDict) else: self._cmdLineDict = ({}, {}, {}) # Note that is something was in the global dictionary it would have been picked # here and no need to initialize in the initRecursive, so set self._initAlready # catalog and factDict enough to know that there was something to initialize if self.catalog or factDict: self._initAlready = True self._processFacilities(self._cmdLineDict) ''' return
def _updateFromDicts(self,toUpgrade,upgrade,replace=None): if not replace: replace = [False]*len(toUpgrade) for dictT,dictU,rep in zip(toUpgrade,upgrade,replace): DU.updateDictionary(dictT,dictU, replace=rep)
def _extractFromDicts(self,listIn,name): listOut = [] for dictIn in listIn: listOut.append(DU.getDictWithKey(dictIn,name,False)) return tuple(listOut)
def main(self): #The main greeting self.greeting(self.gname) #some information on the internals from iscesys.DictUtils.DictUtils import DictUtils normname = DictUtils.renormalizeKey(NAME.public_name) print() print("In this version of greeter.py, we use an input parameter to ") print("select a greeter 'facility' to perform the greeting. The") print("greeting facility is created in greetings/greetings.py using") print("its language method, which takes a string argument specifying") print("the desired language as an argument. The factories to create") print( "the greeter for each pre-selected language is contained in that") print( "file. The components that fill the role of the greeter facility") print("are the components (such as EnglishStandard) in the greetings") print("directory") print() print("Some further information") print("Parameter NAME: public_name = {0}".format(NAME.public_name)) print( "Parameter NAME: internal normalized name = {0}".format(normname)) if self.descriptionOfVariables[normname]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normname]['doc'])) if normname in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normname]['units'])) print("Greeter attribute self.name = {0}".format(self.name)) normlang = DictUtils.renormalizeKey(LANGUAGE.public_name) print("Parameter LANGUAGE: public_name = {0}".format( LANGUAGE.public_name)) print("normlang = {0}".format(normlang)) if self.descriptionOfVariables[normlang]['doc']: print("doc = {0}".format( self.descriptionOfVariables[normlang]['doc'])) if normlang in self.unitsOfVariables.keys(): print("units = {0}".format( self.unitsOfVariables[normlang]['units'])) print("Greeter attribute self.language = {0}".format(self.language)) print() print("For more fun, try this command line:") print("./greeter.py greeter.xml") print("./greeter.py greeterS.xml") print("./greeter.py greeterEC.xml") print("Try the different styles that are commented out in greeter.xml") print("Try entering data on the command line mixing with xml:") print( "./greeter.py greeter.xml greeter.'language to use in greeting'=spanish" ) print("or try this,") cl = "./greeter.py " cl += "Greeter.name\ to\ use\ \ \ IN\ greeting=Juan " cl += "gREETER.LANGUAGE\ TO\ USE\ IN\ GREETING=cowboy " print("{0}".format(cl)) print("etc.") return