Beispiel #1
0
    def execute(self, para, log):
        f = replaceValues(self.filename, para)
        v = replaceValues(self.value, para)
        s = replaceValues(self.subexpression, para)
        k = replaceValues(self.key, para)

        try:
            dictFile = ParsedParameterFile(f, backup=True)
            val = dictFile[k]
        except KeyError:
            self.error("Key: ", k, "not existing in File", f)
        except IOError:
            e = sys.exc_info()[
                1]  # Needed because python 2.5 does not support 'as e'
            self.error("Problem with file", k, ":", e)

        try:
            exec_("dictFile[k]" + s + "=v")
        except Exception:
            e = sys.exc_info()[
                1]  # Needed because python 2.5 does not support 'as e'
            error("Problem with subexpression:", sys.exc_info()[0], ":", e)

        dictFile.writeFile()

        return True, None
    def purgeFile(self):
        """Undo all the manipulations done by PyFOAM

        Goes through the file and removes all lines that were added"""
        if not self.name:
            error("File-handle",str(self.fh),"can not be purged")

        rmExp= re.compile("^"+self.removedString+"(.*)$")
        addExp=re.compile("^(.*)"+self.addedString+"$")

        l=LineReader()
        self.openFile()

        (fh,fn)=self.makeTemp()

        while l.read(self.fh):
            toPrint=l.line

            m=addExp.match(l.line)
            if m!=None:
                continue

            m=rmExp.match(l.line)
            if m!=None:
                toPrint=m.group(1)

            self.writeEncoded(fh,toPrint+"\n")

        self.closeFile()
        fh.close()
        os.rename(fn,self.name)
Beispiel #3
0
    def relativeError(self):
        """Return a table with the relative error"""
        dataRange=self.range()
        if self.__compare==None:
            error("Need comparison data for relative error")

        maxError=self._extractTable("max",self.__compare)
        rErr=self._makeEmptyTable()

        row,col=self._getLabels()
        for r in row:
            for c in col:
                rng=(lambda r:r[1]-r[0])(dataRange[(r,c)])
                mx=maxError[(r,c)]
                if rng>self.small:
                    try:
                        rErr[(r,c)]=mx/rng
                    except TypeError,e:
                        rErr[(r,c)]=self.failureValue
                        if self.failureValue==None:
                            raise e
                elif mx>self.small:
                    if self.noStrings:
                        rErr[(r,c)]=0.
                    else:
                        rErr[(r,c)]="constant (%g)" % mx
                else:
                    if self.noStrings:
                        rErr[(r,c)]=0.
                    else:
                        rErr[(r,c)]="constant =="
Beispiel #4
0
    def __getitem__(self, nr):
        """@param nr: Number of the variation
        @return:  dictionary with the variation"""
        if nr >= len(self):
            error("Index", nr, "of variation out of bounds: [0,",
                  len(self) - 1, "]")
        result = {}
        tmp = nr
        conditions = []
        for v in self.vList:
            if (tmp % len(v)) != 0:
                conditions.append(v.condition)

            k, val = v[tmp % len(v)]
            result[k] = val
            tmp /= len(v)

        assert tmp == 0

        use = True
        for c in conditions:
            cond = replaceValues(c, result)
            use = use and eval(cond)

        return use, result
Beispiel #5
0
    def __call__(self):
        texts={}
        for n,val in iteritems(self.templates):
            template=TemplateFile(content=val,
                                  expressionDelimiter="|-",
                                  encoding="ascii")
            try:
                texts[n]=str(template.getString(self.runner.getData()))
            except TemplateRenderError:
                e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                error("Template error",e,"while rendering",val)


        msg=Message()
        msg["To"]=self.sendTo
        msg["From"]=self.sentFrom
        msg["Subject"]=texts["subject"]
        for n,v in iteritems(self.mailFields):
            msg[n]=v
        msg.set_payload(texts["message"])

        print_("Connecting to SMTP-server",self.server)

        try:
            s=smtplib.SMTP(self.server)
        except:
            e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
            error("Could not connect to",self.server,":",e)

        print_("Sending mail")
        r=s.sendmail(self.sentFrom,self.sendTo.split(","),msg.as_string())
        print_("\n",self.name,"Sent mail to",self.sendTo," Response:",r)

# not yet tested with python3
Beispiel #6
0
    def __relativeErrorInternal(self,name):
        """Return a table with the relative error
        @param name: spcifies the name under which the error is found in the data"""
        dataRange=self.range()
        if self.__compare==None:
            error("Need comparison data for relative error")

        maxError=self._extractTable(name,self.__compare)
        rErr=self._makeEmptyTable()

        row,col=self._getLabels()
        for r in row:
            for c in col:
                rng=(lambda r:r[1]-r[0])(dataRange[(r,c)])
                mx=maxError[(r,c)]
                if rng>self.small:
                    try:
                        rErr[(r,c)]=mx/rng
                    except TypeError:
                        e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                        rErr[(r,c)]=self.failureValue
                        if self.failureValue==None:
                            raise e
                elif mx>self.small:
                    if self.noStrings:
                        rErr[(r,c)]=0.
                    else:
                        rErr[(r,c)]="constant (%g)" % mx
                else:
                    if self.noStrings:
                        rErr[(r,c)]=0.
                    else:
                        rErr[(r,c)]="constant =="

        return rErr
Beispiel #7
0
    def execute(self,
                cmd,
                debug=False,
                workdir=None,
                echo=None,
                getReturnCode=False):
        """Execute the command cmd. If specified change the working directory

        Currently no error-handling is done
        :return: A list with all the output-lines of the execution"""
        if debug:
            print_(cmd)

        oldDir = None
        if workdir:
            oldDir = os.getcwd()
            os.chdir(workdir)

        if type(cmd) == list:
            fpath = cmd[0]
        else:
            fpath = cmd.split(" ")[0]

        # Check if the file is there. Then we assume that this is a script
        if os.path.exists(fpath):
            # Script seems to be unexecutable
            if not os.access(fpath, os.X_OK):
                error("The script file", fpath, "is not executable")

        if sys.version_info < (2, 6):
            raus, rein = popen4(cmd)
        else:
            p = Popen(cmd,
                      shell=True,
                      stdin=PIPE,
                      stdout=PIPE,
                      stderr=STDOUT,
                      close_fds=True,
                      universal_newlines=True)
            (rein, raus) = (p.stdin, p.stdout)
        if echo != None:
            tmp = []
            while p.poll() == None:
                l = raus.readline()
                print_(echo, l, end="")
                tmp.append(l)
        else:
            tmp = raus.readlines()
        # line=raus.readline()
        # while line!="":
        #     print line
        #     line=raus.readline()

        if oldDir:
            os.chdir(oldDir)

        if getReturnCode:
            return p.returncode, tmp
        else:
            return tmp
    def setCase(self, case):
        """Rewrite the state-file so that it uses another case than the one
        predefined in the state-file
        :param case: The path to the new case-file"""
        reader = self.getReader()
        caseDir = path.realpath(path.dirname(case))
        origPath = path.dirname(reader.getProperty("FileName"))
        print "Setting Foam-file", origPath, "to", caseDir
        typ = reader.data.getAttribute("type")
        if typ == "PV3FoamReader":
            reader.setProperty("FileName", case)
        elif typ == "OpenFOAMReader":
            oldFile = reader.getProperty("FileName")
            fName = path.basename(oldFile)
            newFile = path.join(path.dirname(case), fName)
            if not path.exists(newFile):
                open(newFile, "w")
            reader.setProperty("FileName", newFile)
        else:
            error("Reader type", typ,
                  "not implemented for state-file rewritting")

        for r in self.getProxy(".+Reader", regexp=True):
            if r.id() != reader.id():
                oldPath = r.getProperty("FileName")
                for p in [oldPath, path.realpath(oldPath)]:
                    if p.find(origPath) == 0:
                        newPath = path.join(caseDir, p[len(origPath) + 1:])
                        print "Rewriting path", oldPath, "to", newPath
                        r.setProperty("FileName", newPath)
Beispiel #9
0
    def __denormalize(self, s):
        """Denormalize the column name that was normalized by _normalize"""

        while s.find(RunDatabase.specialString) >= 0:
            pre, post = s.split(RunDatabase.specialString, maxsplit=1)
            spec = post[0:3]
            for k, v in iteritems(RunDatabase.specialChars):
                if spec == v:
                    s = pre + k + post[3:]
                    break
            else:
                error("No special character for encoding", spec, "found")

        result = ""
        underFound = False

        for c in s:
            if underFound:
                underFound = False
                result += c.upper()
            elif c == "_":
                underFound = True
            else:
                result += c

        if underFound:
            error("String", s, "was not correctly encoded")

        return result
def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        gnuplotTerminal=None,
                        showWindow=True,
                        registry=None):
    """Creates a plotting object
    :param timelines: The timelines object
    :type timelines: TimeLineCollection
    :param custom: specifies how the block should look like
    :param implementation: the implementation that should be used
    """
    if implementation==None:
        implementation=configuration().get("Plotting","preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation",implementation,
              "not in list of available implementations",list(lookupTable.keys()))

    options={
        "showWindow" : showWindow,
        "registry"   : registry
    }

    if implementation=="gnuplot":
        if not gnuplotTerminal:
            gnuplotTerminal=configuration().get("Plotting","gnuplot_terminal")
            if gnuplotTerminal not in validTerminals():
                error("Terminal",gnuplotTerminal,"not in list of valid terminals",
                      ", ".join(validTerminals()))
        options["terminal"]=gnuplotTerminal

    return lookupTable[implementation](timelines,
                                       custom,
                                       **options)
Beispiel #11
0
    def _getVertexes(self):
        """Get a dictionary with the 3 components of each vertex as keys
        and the 'raw' line as the value"""
        try:
            from collections import OrderedDict
            result=OrderedDict()
        except ImportError:
            error("This python-version doesn't have OrderedDict in library collections. Can't go on''")

        startPattern=re.compile("^\s*vertices")
        endPattern=re.compile("^\s*\);")
        vertexPattern=re.compile("^\s*\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\).*$")

        inVertex=False
        l=self.__startProcess()

        cnt=0
        while l.read(self.fh):
            if not inVertex:
                if startPattern.match(l.line):
                    inVertex=True
            elif endPattern.match(l.line):
                inVertex=False
            else:
                m=vertexPattern.match(l.line)
                if m!=None:
                    result[m.groups()]=(cnt,l.line)
                    cnt+=1

        return result
    def p_dictbody(self, p):
        '''dictbody : dictbody dictline
                    | dictline
                    | empty'''

        if len(p) == 3:
            p[0] = p[1]
            if self.duplicateCheck:
                if p[2][0] in p[0]:
                    if self.duplicateFail:
                        error("Key", p[2][0], "already defined")
                    else:
                        warning("Key", p[2][0], "already defined")
            if type(p[2][0]) == DictRedirection and p[2][1] == '':
                p[0].addRedirection(p[2][0])
            else:
                if type(p[2][1]) == DictRedirection:
                    p[0][p[2][0]] = p[2][1].getContent()
                else:
                    p[0][p[2][0]] = p[2][1]
                p[0].addDecoration(p[2][0], self.getDecoration())
        else:
            p[0] = self.dictStack[-1]

            if p[1]:
                if type(p[1][0]) == DictRedirection and p[1][1] == '':
                    p[0].addRedirection(p[1][0])
                else:
                    if type(p[1][1]) == DictRedirection:
                        p[0][p[1][0]] = p[1][1].getContent()
                    else:
                        p[0][p[1][0]] = p[1][1]
    def _buildKeyordArgumentList(self):
        """Go through the lists of options and build a dictionary of keyword
        arguments (in CamelCase)"""
        kwArgs={}
        for og in self.option_groups:
            for o in og.option_list:
                raw=o.get_opt_string().strip("-")
                pos=raw.find("-")
                if pos<0:
                    name=raw.lower()
                    raw=""
                else:
                    name=raw[:pos].lower()
                    raw=raw[pos:].strip("-")
                while len(raw)>0:
                    pos=raw.find("-")
                    if pos<0:
                        name+=raw.capitalize()
                        raw=""
                    else:
                        name+=raw[:pos].capitalize()
                        raw=raw[pos:].strip("-")
                if not name[0].isalpha() and name[0]!="_":
                    error("Option",o.get_opt_string(),"reduces to",name,
                          "with invalid first character")
                # Remove all characters that do not belong in a valid Python-name
                name="".join([c for c in name if c=="_" or c.isalnum])
                if name in kwArgs:
                    error("Keyword arguement",name,"appears at least twice")
                kwArgs[name]=o

        return kwArgs
Beispiel #14
0
    def erasePatches(self, patchNames):
        """Erase the patches in the list"""
        processed = []

        keep = True
        currentName = None

        for l in self._contents:
            nextState = keep
            parts = l.split()
            if len(parts) > 0:
                if parts[0] == "endsolid":
                    nextState = True
                    if currentName != parts[1]:
                        error("Patch name", parts[1], "Expected", currentName)
                    currentName = None
                elif parts[0] == "solid":
                    currentName = parts[1]
                    if currentName in patchNames:
                        keep = False
                        nextState = False
            if keep:
                processed.append(l)
            keep = nextState

        self._contents = processed
Beispiel #15
0
    def __init__(self,
                 name,
                 time,
                 create=False,
                 region=None,
                 processor=None,
                 yieldParsedFiles=False):
        """@param name: name of the case directory
        @param time: time in the directory
        @param create: Create the directory if it does not exist
        @param region: The mesh region for multi-region cases
        @param yieldParsedFiles: let the iterator return PasedParameterFile objects instead of SolutionFile"""

        self.name=name
        self.yieldParsedFiles=yieldParsedFiles
        if processor!=None:
            if type(processor)==int:
                processor="processor%d" % processor
            self.name=path.join(self.name,processor)
        self.name=path.join(self.name,time)
        if region!=None:
            self.name=path.join(self.name,region)
            
        if path.exists(self.name):
            if not path.isdir(self.name):
                error(self.name,"is not a directory")
        elif create:
            makedirs(self.name)
        else:
            error(self.name,"does not exist")
            
        self.values=[]

        self.lastReread=0L
        self.reread()
    def __scanForPostproc(self,dirName):
        for d in listdir(path.join(self.name,dirName)):
            full=path.join(self.name,dirName,d)
            if not path.isdir(full):
                continue
            try:
                # we don't want time directories
                float(d)
                continue
            except ValueError:
                pass
            c=self.__classifyDirectory(full)
            use=path.join(dirName,d)
            if c=="timeline":
                self.__postprocInfo["timelines"].append(use)
            elif c=="sample":
                self.__postprocInfo["samples"].append(use)
            elif c=="surface":
                self.__postprocInfo["surfaces"].append(use)
            elif c=="distribution":
                self.__postprocInfo["distributions"].append(use)
            elif c==None:
                pass
            else:
                error("Unknown classification",c,"for",full)

            # Pick up additional distributions certain swak-functionobjects generate
            if path.exists(path.join(full,"distributions")):
                c=self.__classifyDirectory(path.join(full,"distributions"))
                if c=="distribution":
                    self.__postprocInfo["distributions"].append(path.join(use,"distributions"))
Beispiel #17
0
def getVCS(vcs,
           path,
           init=False,
           tolerant=False):
    """Factory to create a proper VCS-interface
    :param vcs: name of the VCS-implementation
    :param path: path which is under version control
    :param init: whether the Version-control should be initialized here
    :param tolerant: If there is no interface for the VCS in question return None"""

    table = { "hg"   : "HgInterface" ,
              "git"  : "GitInterface",
              "svn"  : "SvnInterface",
              "svk"  : "SvkInterface" }

    if vcs not in table:
        if tolerant:
            return None
        else:
            error("Unknown VCS",vcs,". Known are",list(table.keys()))

    modName=table[vcs]

    if PY3:
        # fix the import.
        dot="."
    else:
        dot=""
    exec_("from "+dot+modName+" import "+modName)

    return eval(modName+"(path,init)")
Beispiel #18
0
    def __parseBase(self,e):
        """:param e: The 'base'-element"""

        self.template=path.expandvars(e.getAttribute("template"))
        if self.template=="":
            error("No template is given")
        if not path.exists(self.template):
            error("Template",self.template,"does not exist")
        self.id=path.basename(self.template)
        if e.hasAttribute('extension'):
            self.extension=e.getAttribute('extension')
            self.id+="."+self.extension
        else:
            self.extension=""
        self.solver=e.getAttribute("solver")
        if self.solver=="":
            error("No solver is given")
        prep=e.getElementsByTagName("preparation")
        if prep.length!=1:
            error("One 'preparation'-element needed. Found",prep.length)
        self.prep=PreparationChain(prep[0])
        post=e.getElementsByTagName("evaluation")
        if post.length!=1:
            error("One 'evaluation'-element needed. Found",post.length)
        self.post=EvaluationChain(post[0])
Beispiel #19
0
    def reread(self,force=False):
        """Scan the directory for files with valid names"""

        if not force and stat(self.name)[ST_CTIME]<=self.lastReread:
            return

        self.values=[]

        ex=["*~",".svn"]

        for f in listdir(self.name):
            matched=False
            for e in ex:
                if fnmatch(f,e):
                    matched=True

            if path.isdir(path.join(self.name,f)):
                continue

            if not matched:
                nm=f
                if len(nm)>3:
                    if nm[-3:]==".gz":
                        nm=nm[:-3]
                if nm not in self.values:
                    self.values.append(nm)
                else:
                    if not self.tolerant:
                        error(nm," already found, propably exists as zipped and unzipped")
                    else:
                        warning(nm," already found, propably exists as zipped and unzipped")

        self.values.sort()

        self.lastReread=stat(self.name)[ST_CTIME]
Beispiel #20
0
    def mergePatches(self, patchNames, targetPatchName):
        """Merge the patches in the list and put them into a new patch"""

        processed = []
        saved = []

        keep = True
        currentName = None

        for l in self._contents:
            nextState = keep
            parts = l.split()
            if len(parts) > 0:
                if parts[0] == "endsolid":
                    nextState = True
                    if currentName != parts[1]:
                        error("Patch name", parts[1], "Expected", currentName)
                    currentName = None
                elif parts[0] == "solid":
                    currentName = parts[1]
                    if currentName in patchNames:
                        keep = False
                        nextState = False
            if keep:
                processed.append(l)
            elif len(parts) > 0:
                if parts[0] not in ["solid", "endsolid"]:
                    saved.append(l)
            keep = nextState

        self._contents = processed

        self._contents.append("solid " + targetPatchName)
        self._contents += saved
        self._contents.append("endsolid " + targetPatchName)
Beispiel #21
0
    def determineIndex(self,fName,vName,data):
        """Determines the index of the data from the filename and a dataset
        :param fName: name of the file
        :param vName: Name of the quantity
        :param data: A list with the data
        :returns: A tuple of a boolean (whether the data is supposed to be
        a vector or a scalar) and an integer (the index of the data set -
        or the first component of the vector"""

        vals=self.extractValues(fName)

        if len(vals)+1==len(data):
            vector=False
        elif len(vals)*3+1==len(data):
            vector=True
        else:
            error("The data in file",fName,"is neither vector nor scalar:",data)

        index=vals.index(vName)
        if vector:
            index=index*3+1
        else:
            index=index+1

        return vector,index
    def mergePatches(self,patchNames,targetPatchName):
        """Merge the patches in the list and put them into a new patch"""

        processed=[]
        saved=[]

        keep=True
        currentName=None

        for l in self._contents:
            nextState=keep
            parts=l.split()
            if len(parts)>0:
                if parts[0]=="endsolid":
                    nextState=True
                    if currentName!=parts[1]:
                        error("Patch name",parts[1],"Expected",currentName)
                    currentName=None
                elif parts[0]=="solid":
                    currentName=parts[1]
                    if currentName in patchNames:
                        keep=False
                        nextState=False
            if keep:
                processed.append(l)
            elif len(parts)>0:
                if parts[0] not in ["solid","endsolid"]:
                    saved.append(l)
            keep=nextState

        self._contents=processed

        self._contents.append("solid "+targetPatchName)
        self._contents+=saved
        self._contents.append("endsolid "+targetPatchName)
    def setCase(self,case):
        """Rewrite the state-file so that it uses another case than the one
        predefined in the state-file
        :param case: The path to the new case-file"""
        reader=self.getReader()
        caseDir=path.realpath(path.dirname(case))
        origPath=path.dirname(reader.getProperty("FileName"))
        print "Setting Foam-file",origPath,"to",caseDir
        typ=reader.data.getAttribute("type")
        if typ=="PV3FoamReader":
            reader.setProperty("FileName",case)
        elif typ=="OpenFOAMReader":
            oldFile=reader.getProperty("FileName")
            fName=path.basename(oldFile)
            newFile=path.join(path.dirname(case),fName)
            if not path.exists(newFile):
                open(newFile,"w")
            reader.setProperty("FileName",newFile)
        else:
            error("Reader type",typ,"not implemented for state-file rewritting")

        for r in self.getProxy(".+Reader",regexp=True):
            if r.id()!=reader.id():
                oldPath=r.getProperty("FileName")
                for p in [oldPath,path.realpath(oldPath)]:
                    if p.find(origPath)==0:
                        newPath=path.join(caseDir,p[len(origPath)+1:])
                        print "Rewriting path",oldPath,"to",newPath
                        r.setProperty("FileName",newPath)
    def erasePatches(self,patchNames):
        """Erase the patches in the list"""
        processed=[]

        keep=True
        currentName=None

        for l in self._contents:
            nextState=keep
            parts=l.split()
            if len(parts)>0:
                if parts[0]=="endsolid":
                    nextState=True
                    if currentName!=parts[1]:
                        error("Patch name",parts[1],"Expected",currentName)
                    currentName=None
                elif parts[0]=="solid":
                    currentName=parts[1]
                    if currentName in patchNames:
                        keep=False
                        nextState=False
            if keep:
                processed.append(l)
            keep=nextState

        self._contents=processed
Beispiel #25
0
    def getReader(self):
        """Return the Proxy-Element with the reader"""
        tmp=self.getProxy("PV3FoamReader")+self.getProxy("OpenFOAMReader")
        if len(tmp)!=1:
            error("Wrong number of Readers in State-File. Need 1 but got",len(tmp))

        return tmp[0]
Beispiel #26
0
    def run(self):
        if self.pathInfo():
            return

        app = QtGui.QApplication(self.parser.getArgs())

        fName = None
        if len(self.parser.getArgs()) == 0:
            dialog = CaseBuilderBrowser()
            if len(dialog.descriptions) == 1:
                fName = dialog.descriptions[0][1]
                self.warning("Automatically choosing the only description",
                             fName)
        elif len(self.parser.getArgs()) == 1:
            fName = self.searchDescriptionFile(self.parser.getArgs()[0])

            if not path.exists(fName):
                error("The description file", fName, "does not exist")
        else:
            error("Too many arguments")

        if fName != None:
            dialog = CaseBuilderDialog(fName)

        dialog.show()
        sys.exit(app.exec_())
Beispiel #27
0
    def run(self):
        if self.pathInfo():
            return

        app = QtGui.QApplication(self.parser.getArgs())

        fName = None
        if len(self.parser.getArgs()) == 0:
            dialog = CaseBuilderBrowser()
            if len(dialog.descriptions) == 1:
                fName = dialog.descriptions[0][1]
                self.warning("Automatically choosing the only description", fName)
        elif len(self.parser.getArgs()) == 1:
            fName = self.searchDescriptionFile(self.parser.getArgs()[0])

            if not path.exists(fName):
                error("The description file", fName, "does not exist")
        else:
            error("Too many arguments")

        if fName != None:
            dialog = CaseBuilderDialog(fName)

        dialog.show()
        sys.exit(app.exec_())
Beispiel #28
0
 def addTimeListener(self,listener):
     """@param listener: An object that is notified when the time changes. Has to
     implement a timeChanged method"""
     if not 'timeChanged' in dir(listener):
         error("Error. Object has no timeChanged-method:"+str(listener))
     else:
         self.timeListeners.append(listener)
Beispiel #29
0
    def __getitem__(self,key):
        """Get the data for a value on a specific line
        @param key: A tuple with the surface-name and the value-name
        @returns: a path to the VTK-file"""

        if key in self.cache:
            return self.cache[key]

        surface,val=key
        if surface not in self.surfaces or val not in self.values:
            raise KeyError(key)

        fName=None

        for pth in glob(path.join(self.dir,"*.vtk")):
            f=path.basename(pth)
            if surface==extractSurface(f) and val==extractValue(f):
                fName=f
                break

        if fName==None:
            error("Can't find a file for the surface",line,"and the value",val,"in the directory",self.dir)

        self.cache[key]=(path.join(self.dir,fName),self.time,surface,val)

        return self.cache[key]
    def p_dictbody(self,p):
        '''dictbody : dictbody dictline
                    | dictline
                    | empty'''

        if len(p)==3:
            p[0]=p[1]
            if self.duplicateCheck:
                if p[2][0] in p[0]:
                    if self.duplicateFail:
                        error("Key",p[2][0],"already defined")
                    else:
                        warning("Key",p[2][0],"already defined")
            if type(p[2][0])==DictRedirection and p[2][1]=='':
                p[0].addRedirection(p[2][0])
            else:
                if type(p[2][1])==DictRedirection:
                    p[0][p[2][0]]=p[2][1].getContent()
                else:
                    p[0][p[2][0]]=p[2][1]
                p[0].addDecoration(p[2][0],self.getDecoration())
        else:
            p[0]=self.dictStack[-1]

            if p[1]:
                if type(p[1][0])==DictRedirection and p[1][1]=='':
                    p[0].addRedirection(p[1][0])
                else:
                    if type(p[1][1])==DictRedirection:
                        p[0][p[1][0]]=p[1][1].getContent()
                    else:
                        p[0][p[1][0]]=p[1][1]
Beispiel #31
0
 def __scanForPostproc(self,dirName):
     for d in listdir(path.join(self.name,dirName)):
         full=path.join(self.name,dirName,d)
         if not path.isdir(full):
             continue
         try:
             # we don't want time directories
             float(d)
             continue
         except ValueError:
             pass
         c=self.__classifyDirectory(full)
         use=path.join(dirName,d)
         if c=="timeline":
             self.__postprocInfo["timelines"].append(use)
         elif c=="sample":
             self.__postprocInfo["samples"].append(use)
         elif c=="surface":
             self.__postprocInfo["surfaces"].append(use)
         elif c=="distribution":
             self.__postprocInfo["distributions"].append(use)
         elif c==None:
             pass
         else:
             error("Unknown classification",c,"for",full)
Beispiel #32
0
    def purgeFile(self):
        """Undo all the manipulations done by PyFOAM

        Goes through the file and removes all lines that were added"""
        if not self.name:
            error("File-handle", str(self.fh), "can not be purged")

        rmExp = re.compile("^" + self.removedString + "(.*)$")
        addExp = re.compile("^(.*)" + self.addedString + "$")

        l = LineReader()
        self.openFile()

        (fh, fn) = self.makeTemp()

        while l.read(self.fh):
            toPrint = l.line

            m = addExp.match(l.line)
            if m != None:
                continue

            m = rmExp.match(l.line)
            if m != None:
                toPrint = m.group(1)

            self.writeEncoded(fh, toPrint + "\n")

        self.closeFile()
        fh.close()
        os.rename(fn, self.name)
Beispiel #33
0
    def _buildKeyordArgumentList(self):
        """Go through the lists of options and build a dictionary of keyword
        arguments (in CamelCase)"""
        kwArgs = {}
        for og in self.option_groups:
            for o in og.option_list:
                raw = o.get_opt_string().strip("-")
                pos = raw.find("-")
                if pos < 0:
                    name = raw.lower()
                    raw = ""
                else:
                    name = raw[:pos].lower()
                    raw = raw[pos:].strip("-")
                while len(raw) > 0:
                    pos = raw.find("-")
                    if pos < 0:
                        name += raw.capitalize()
                        raw = ""
                    else:
                        name += raw[:pos].capitalize()
                        raw = raw[pos:].strip("-")
                if not name[0].isalpha() and name[0] != "_":
                    error("Option", o.get_opt_string(), "reduces to", name,
                          "with invalid first character")
                # Remove all characters that do not belong in a valid Python-name
                name = "".join([c for c in name if c == "_" or c.isalnum])
                if name in kwArgs:
                    error("Keyword arguement", name, "appears at least twice")
                kwArgs[name] = o

        return kwArgs
    def getReader(self):
        """Return the Proxy-Element with the reader"""
        tmp=self.getProxy("PV3FoamReader")+self.getProxy("OpenFOAMReader")
        if len(tmp)!=1:
            error("Wrong number of Readers in State-File. Need 1 but got",len(tmp))

        return tmp[0]
Beispiel #35
0
    def patchInfo(self):
        """Get info about the patches. A list of dictionaries with the relevant information"""
        if self._patchInfo:
            return self._patchInfo

        self._patchInfo = []

        newPatch = True

        e = enumerate(self._contents)

        goOn = True
        while goOn:
            try:
                i, l = iterNext(e)
                if newPatch:
                    self.expectedToken(l, "solid", i)
                    info = {}
                    if len(l.split()) < 2:
                        info["name"] = self.noName
                    else:
                        info["name"] = l.split()[1]
                    info["start"] = i + 1
                    info["facets"] = 0
                    info["min"] = [1e100] * 3
                    info["max"] = [-1e100] * 3
                    newPatch = False
                elif l.strip().find("endsolid") == 0:
                    info["end"] = i + 1
                    self._patchInfo.append(info)
                    newPatch = True
                else:
                    self.expectedToken(l, "facet normal", i)
                    i, l = iterNext(e)
                    self.expectedToken(l, "outer loop", i)
                    for v in range(3):
                        i, l = iterNext(e)
                        self.expectedToken(l, "vertex", i)
                        info["min"] = [
                            min(m) for m in
                            zip(info["min"],
                                [float(v) for v in l.strip().split()[1:4]])
                        ]
                        info["max"] = [
                            max(m) for m in
                            zip(info["max"],
                                [float(v) for v in l.strip().split()[1:4]])
                        ]
                    i, l = iterNext(e)
                    self.expectedToken(l, "endloop", i)
                    i, l = iterNext(e)
                    self.expectedToken(l, "endfacet", i)
                    info["facets"] += 1
            except StopIteration:
                goOn = False

        if not newPatch:
            error("File", self.filename(), "seems to be incomplete")

        return self._patchInfo
Beispiel #36
0
    def __init__(self):
        CaseBuilderTUI.__init__(self)

        self.descriptions=CaseBuilderDescriptionList()
        if len(self.descriptions)==0:
            error("No description-files (.pfcb) found in path",config().get("CaseBuilder","descriptionpath"))
            
        self.items=[]

        mxlen=apply(max,map(lambda x:len(x[2]),self.descriptions)+[0])
        
        for i,d in enumerate(self.descriptions):
            txt=urwid.Text([('button',d[2]+" "*(mxlen-len(d[2]))),"  ",d[1],"\n",d[3]])
            if i%2==0:
                attr='bgl'
            else:
                attr='bg'
            self.items.append(urwid.AttrWrap(txt,attr,'bgbutton'))

        self.walk=urwid.SimpleListWalker(self.items)
        self.listbox = urwid.ListBox(self.walk)

        self.statusText=urwid.Text(" Press <ESC> to exit  <ENTER> to select description")

        footer = urwid.AttrWrap( self.statusText, 'header' )

        top = urwid.Frame(self.listbox, footer=footer)
        if len(self.descriptions)%2==0:
            self.top=urwid.AttrWrap(top,'bgl')
        else:
            self.top=urwid.AttrWrap(top,'bg')
            
        self.ui.run_wrapper(self.runTUI)
 def setAccumulator(self,name,accu):
     """Sets a special accumulator fopr a timeline
     :param name: Name of the timeline
     :param accu: Name of the accumulator"""
     if not (accu in TimeLineCollection.possibleAccumulations):
         error("Value",accu,"not in list of possible values:",TimeLineCollection.possibleAccumulations,"When setting for",name)
     self.accumulations[name]=accu
Beispiel #38
0
    def extractValues(self,fName):
        """Extracts the names of the contained Values from a filename"""

        if self.__defaultValueNames:
            self.__valueNames=self.__defaultValueNames[:]
            return self.__valueNames

        if self.__namesFromFirstLine:
            line=open(path.join(self.dir,fName)).readline().split()
            if line[0]!="#":
                error("First line of",path.join(self.dir,fName),
                      "does not start with a '#'")
            return line[2:]

        def preUnder(m):
            return "&"+m.group(1)+m.group(2)
        def postUnder(m):
            return m.group(1)+m.group(2)+"&"

        for p in self.prefixes:
            fName=re.sub("([_&.]|^)("+p+")_",postUnder,fName)
        for p in self.postfixes:
            fName=re.sub("_("+p+")([_&.]|$)",preUnder,fName)

        self.__valueNames=[]
        try:
            tmp=fName.split("_")[1:]
            tmp[-1]=tmp[-1].split(".")[0]

            for t in tmp:
                self.__valueNames.append(t.replace("&","_"))
        except IndexError:
            pass

        return self.__valueNames
Beispiel #39
0
def createPlotTimelines(timelines,
                        custom,
                        implementation=None,
                        gnuplotTerminal=None,
                        showWindow=True,
                        quiet=False,
                        registry=None):
    """Creates a plotting object
    :param timelines: The timelines object
    :type timelines: TimeLineCollection
    :param custom: specifies how the block should look like
    :param implementation: the implementation that should be used
    """
    if implementation == None:
        implementation = configuration().get("Plotting",
                                             "preferredImplementation")

    if implementation not in lookupTable:
        error("Requested plotting implementation", implementation,
              "not in list of available implementations",
              list(lookupTable.keys()))

    options = {"showWindow": showWindow, "quiet": quiet, "registry": registry}

    if implementation == "gnuplot":
        if not gnuplotTerminal:
            gnuplotTerminal = configuration().get("Plotting",
                                                  "gnuplot_terminal")
            if gnuplotTerminal not in validTerminals():
                error("Terminal", gnuplotTerminal,
                      "not in list of valid terminals",
                      ", ".join(validTerminals()))
        options["terminal"] = gnuplotTerminal

    return lookupTable[implementation](timelines, custom, **options)
Beispiel #40
0
 def addTimeListener(self, listener):
     """:param listener: An object that is notified when the time changes. Has to
     implement a timeChanged method"""
     if not 'timeChanged' in dir(listener):
         error("Error. Object has no timeChanged-method:" + str(listener))
     else:
         self.timeListeners.append(listener)
    def buildMPIrun(self,argv,expandApplication=True):
        """Builds a list with a working mpirun command (for that MPI-Implementation)
        @param argv: the original arguments that are to be wrapped
        @param expandApplication: Expand the
        @return: list with the correct mpirun-command"""

        nr=str(self.cpuNr())
        mpirun=[config().get("MPI","run_"+foamMPI(),default="mpirun")]
        mpiRunPath=self.which(" ".join(mpirun))
        if not mpiRunPath:
            error("Could not find a path for",mpirun,
                  "Check configuration variable","run_"+foamMPI(),
                  "in section 'MPI'")
        mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_pre",default="[]"))

        if(foamMPI()=="LAM"):
            mpirun+=["-np",nr]
        elif foamMPI().find("OPENMPI")>=0:
            nr=[]
            if "MPI_ARCH_PATH" in environ and config().getboolean("MPI","OpenMPI_add_prefix"):
                nr+=["--prefix",environ["MPI_ARCH_PATH"]]
            if self.procNr!=None:
                nr+=["--n",str(self.procNr)]
            machine=[]
            if self.mFile!=None:
                machine=["--machinefile",self.mFile]
                if config().getdebug("ParallelExecution"):
                    debug("Start of",self.mFile)
                    debug("\n"+open(self.mFile).read())
                    debug("End of",self.mFile)
            mpirun+=machine+nr
        else:
            error(" Unknown or missing MPI-Implementation for mpirun: "+foamMPI())

        mpirun+=eval(config().get("MPI","options_"+foamMPI()+"_post",default="[]"))

        progname=argv[0]
        if expandApplication:
            # old implementation: stat,progname=commands.getstatusoutput('which '+progname)
            progname=self.which(progname)
            if progname:
                progname=argv[0]
                warning("which can not find a match for",progname,". Hoping for the best")

        if oldAppConvention():
            mpirun+=[progname]+argv[1:3]+["-parallel"]+argv[3:]
        else:
            mpirun+=[progname]+argv[1:]+["-parallel"]

        if config().getdebug("ParallelExecution"):
            debug("MPI:",foamMPI())
            debug("Arguments:",mpirun)
            system("which mpirun")
            system("which rsh")
            debug("Environment",environ)
            for a in mpirun:
                if a in environ:
                    debug("Transfering variable",a,"with value",environ[a])

        return mpirun
Beispiel #42
0
    def __call__(self):
        if self.useSSL:
            meth = httplib.HTTPSConnection
        else:
            meth = httplib.HTTPConnection

        conn = meth(self.host)

        parameters = {}
        for n, val in iteritems(self.parameters):
            if n in self.templates:
                template = TemplateFile(content=val,
                                        expressionDelimiter="|-",
                                        encoding="ascii")
                try:
                    parameters[n] = str(
                        template.getString(self.runner.getData()))
                except TemplateRenderError:
                    e = sys.exc_info()[
                        1]  # Needed because python 2.5 does not support 'as e'
                    error("Template error", e, "while rendering", val)
            else:
                parameters[n] = val
        encoded = urllib.urlencode(parameters)
        try:
            conn.request(self.method, self.url, encoded, self.headers)
        except socket.error:
            e = sys.exc_info()[
                1]  # Needed because python 2.5 does not support 'as e'
            error("Could not connect to", self.host, ":", e)

        result = conn.getresponse()
        print_("\n", self.name, "Result of request:", result.status,
               result.reason, result.read())
Beispiel #43
0
    def _getVertexes(self):
        """Get a dictionary with the 3 components of each vertex as keys
        and the 'raw' line as the value"""
        try:
            from collections import OrderedDict
            result = OrderedDict()
        except ImportError:
            error(
                "This python-version doesn't have OrderedDict in library collections. Can't go on''"
            )

        startPattern = re.compile("^\s*vertices")
        endPattern = re.compile("^\s*\);")
        vertexPattern = re.compile("^\s*\(\s*(\S+)\s+(\S+)\s+(\S+)\s*\).*$")

        inVertex = False
        l = self.__startProcess()

        cnt = 0
        while l.read(self.fh):
            if not inVertex:
                if startPattern.match(l.line):
                    inVertex = True
            elif endPattern.match(l.line):
                inVertex = False
            else:
                m = vertexPattern.match(l.line)
                if m != None:
                    result[m.groups()] = (cnt, l.line)
                    cnt += 1

        return result
Beispiel #44
0
    def __getitem__(self, key):
        """Get the data for a value on a specific line
        @param key: A tuple with the surface-name and the value-name
        @returns: a path to the VTK-file"""

        if key in self.cache:
            return self.cache[key]

        surface, val = key
        if surface not in self.surfaces or val not in self.values:
            raise KeyError(key)

        fName = None

        for pth in glob(path.join(self.dir, "*.vtk")):
            f = path.basename(pth)
            if surface == extractSurface(f) and val == extractValue(f):
                fName = f
                break

        if fName == None:
            error("Can't find a file for the surface", line, "and the value",
                  val, "in the directory", self.dir)

        self.cache[key] = (path.join(self.dir, fName), self.time, surface, val)

        return self.cache[key]
Beispiel #45
0
    def __init__(self):
        CaseBuilderQt.__init__(self)

        self.descriptions = CaseBuilderDescriptionList()
        if len(self.descriptions) == 0:
            error("No description-files (.pfcb) found in path", config().get("CaseBuilder", "descriptionpath"))

        mainLayout = QtGui.QVBoxLayout()
        self.setLayout(mainLayout)

        self.descriptsList = QtGui.QListWidget()
        self.descriptsList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        mainLayout.addWidget(self.descriptsList)

        self.itemlist = []
        for d in self.descriptions:
            item = QtGui.QListWidgetItem(d[2])
            item.setToolTip(d[3])
            self.descriptsList.addItem(item)
            self.itemlist.append((item, d))

        buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel)
        mainLayout.addWidget(buttons)
        selectButton = QtGui.QPushButton("Select")
        selectButton.setToolTip("Select the case description that we want to work with")
        buttons.addButton(selectButton, QtGui.QDialogButtonBox.AcceptRole)
        try:
            buttons.accepted.connect(self.selectPressed)
            buttons.rejected.connect(self.reject)
        except AttributeError:
            warning("Old QT-version where QDialogButtonBox doesn't have the accepted/rejected-attributes")
            self.connect(buttons, QtCore.SIGNAL("accepted()"), self.selectPressed)
            self.connect(buttons, QtCore.SIGNAL("rejected()"), self.reject)
Beispiel #46
0
    def __init__(self,
                 timelines,
                 custom,
                 showWindow=True,
                 registry=None):
        """@param timelines: The timelines object
        @type timelines: TimeLineCollection
        @param custom: A CustomplotInfo-object. Values in this object usually override the
        other options
        """

        self.hasSubplotHost=True
        try:
            global plt,matplotlib,firstTimeImport,SubplotHost
            import matplotlib
            if not showWindow and firstTimeImport:
#                matplotlib.use("MacOSX")
                matplotlib.use("agg")
                firstTimeImport=False
            import matplotlib.pyplot as plt
            try:
                from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
            except ImportError:
                self.hasSubplotHost=False
                warning("Matplotlib-Version does not support SubplotHost")
        except ImportError:
            error("Matplotlib not installed.")
    
        GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry)

        self.figNr=MatplotlibTimelines.figureNr
        MatplotlibTimelines.figureNr+=1

        self.figure=None
        self.title=""
        
        self.ylabel=""
        self.ylabel2=""
        try:
            if self.spec.ylabel:
                self.setYLabel(self.spec.ylabel)
        except AttributeError:
            pass
        try:
            if self.spec.y2label:
                self.setYLabel2(self.spec.y2label)
        except AttributeError:
            pass
        
        self.axis1=None
        self.axis2=None

        self.setTitle(self.spec.theTitle)

        self.with_=self.spec.with_
        if not self.with_ in ['lines','points','dots','steps','linespoints']:
            warning("'with'-style",self.with_,"not implemented, using 'lines'")
            self.with_='lines'
        self.redo()
Beispiel #47
0
def getNonEmpty(e,name,default=None):
    result=e.getAttribute(name)
    if result=="":
        if default==None:
            error("Missing attribute",name,"in element",e.tagName)
        else:
            return default
    return result
def simpleBikeTutorial():
    prefix=foamTutorials()
    if not oldTutorialStructure():
        prefix=path.join(prefix,"incompressible")
    else:
        error("The simpleFoam-motorBike-case does not exist before 1.6")

    return path.join(prefix,"simpleFoam","motorBike")
    def __init__(self,runner,name):
        RunHook.__init__(self,runner,name)

        self.create=self.conf().getboolean("createDatabase")
        self.database=path.expanduser(self.conf().get("database"))

        if not self.create and not path.exists(self.database):
            error("The database",self.database,"does not exists")
Beispiel #50
0
    def taskParameters(self,id):
        """Parameters for a specific task
        @param id: the id of the task
        @return: a dictionary with parameters for this task"""

        error("taskParameter not implemented. Not a parameterized job")

        return {}
Beispiel #51
0
    def __init__(self,fName):
        """@param fName: the XML-file that describes how to build the case"""
        
        dom=parse(fName)
        self.doc=dom.documentElement

        if self.doc.tagName!='casebuilder':
            error("Wrong root-element",self.doc.tagName,"Expected: 'casebuilder'")
Beispiel #52
0
    def __init__(self, runner, name):
        RunHook.__init__(self, runner, name)

        self.create = self.conf().getboolean("createDatabase")
        self.database = path.expanduser(self.conf().get("database"))

        if not self.create and not path.exists(self.database):
            error("The database", self.database, "does not exists")
Beispiel #53
0
def simpleBikeTutorial():
    prefix=foamTutorials()
    if not oldTutorialStructure():
        prefix=path.join(prefix,"incompressible")
    else:
        error("The simpleFoam-motorBike-case does not exist before 1.6")

    return path.join(prefix,"simpleFoam","motorBike")