Package PyFoam :: Package Basics :: Module CustomPlotInfo
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.CustomPlotInfo

  1  #  ICE Revision: $Id$ 
  2  """Information about custom plots""" 
  3   
  4  from PyFoam.Basics.TimeLineCollection import TimeLineCollection 
  5  from PyFoam.Basics.FoamFileGenerator import makeString 
  6  from PyFoam.RunDictionary.ParsedParameterFile import FoamStringParser,PyFoamParserError 
  7   
  8  from PyFoam.Error import error 
  9  from PyFoam.ThirdParty.six import iteritems 
 10   
 11  from os import path 
 12   
13 -def cleanString(data):
14 if type(data)==str: 15 if len(data)>0: 16 if data[0]=='"' and data[-1]=='"': 17 data=data[1:-1] 18 elif data in ["true","on","yes"]: 19 data=True 20 elif data in ["false","off","no"]: 21 data=False 22 return data
23
24 -def encloseString(data):
25 if type(data)!=str: 26 return data 27 if data.find(' ')<0: 28 return data 29 else: 30 return '"'+data+'"'
31
32 -class CustomPlotInfo(object):
33 """Information about a custom plot""" 34 35 nr=1 36
37 - def __init__(self,raw=None,name=None,enabled=True):
38 """@param raw: The raw data. Either a string for the two legacy-formats or a 39 dictionary for the new format 40 @param name: Name of the expression (only to be used for the new format) 41 @param enabled: Should this plot be actually used?""" 42 self.nr=CustomPlotInfo.nr 43 CustomPlotInfo.nr+=1 44 45 # Setting sensible default values 46 self.name="Custom%02d" % self.nr 47 self.theTitle="Custom %d" % self.nr 48 if name: 49 self.name+="_"+name 50 self.id=name 51 self.theTitle += " - "+name 52 else: 53 self.id=self.name 54 55 self.expr=None 56 self.titles=[] 57 self.accumulation="first" 58 self.start=None 59 self.end=None 60 self.persist=False 61 self.raisit=False 62 self.with_="lines" 63 self.type="regular"; 64 self.master=None 65 self.progress=None 66 self.enabled=enabled 67 self.xlabel="Time [s]" 68 self.ylabel=None 69 70 # Legacy format 71 if raw==None: 72 self.expr="" 73 elif type(raw)==str: 74 if raw[0]=='{': 75 data=eval(raw) 76 self.expr=data["expr"] 77 if "name" in data: 78 self.name+="_"+data["name"] 79 self.name=self.name.replace(" ","_").replace(path.sep,"Slash") 80 self.theTitle+=" - "+data["name"] 81 if "titles" in data: 82 self.titles=data["titles"] 83 for o in ["alternateAxis","logscale","with","ylabel","y2label"]: 84 if o=="with": 85 use="with_" 86 else: 87 use=o 88 if o in data: 89 self.set(use,data[o]) 90 if "accumulation" in data: 91 self.accumulation=data["accumulation"] 92 else: 93 self.expr=raw 94 # New format 95 else: 96 for k in raw: 97 data=raw[k] 98 if type(data)==str: 99 data=cleanString(data) 100 elif type(data)==list: 101 data=list(map(cleanString,data)) 102 if k=="with": 103 k="with_" 104 self.set(k,data) 105 106 # Sanity check the data 107 if self.accumulation not in TimeLineCollection.possibleAccumulations: 108 error("Accumulation",self.accumulation,"not in the possible values",TimeLineCollection.possibleAccumulations) 109 110 if self.expr==None: 111 error("No expression set by data",raw)
112
113 - def set(self,key,value):
114 setattr(self,key,value)
115 116
117 - def __str__(self):
118 return makeString({self.id:self.getDict(wrapStrings=True)})
119
120 - def getDict(self,wrapStrings=False):
121 result={} 122 123 for d in dir(self): 124 if (type(getattr(self,d)) in [str,bool,int,list,dict,float]) and d.find("__")<0: 125 if d=="id" or d=="nr": 126 pass 127 else: 128 key=d.replace("_","") 129 val=getattr(self,d) 130 if wrapStrings: 131 if type(val)==str: 132 val=encloseString(val) 133 elif type(val)==list: 134 val=list(map(encloseString,val)) 135 136 result[key]=val 137 return result
138 139
140 -def readCustomPlotInfo(rawData,useName=None):
141 """Determines which of the three possible formats for custom-plotting is used 142 and returns a list of CustomPlotInfo-objects 143 @param rawData: a string that contains the raw data""" 144 info=[] 145 146 try: 147 data=FoamStringParser(rawData, 148 duplicateCheck=True, 149 duplicateFail=True) 150 for k,d in iteritems(data.data): 151 info.append(CustomPlotInfo(d,name=k)) 152 except PyFoamParserError: 153 for i,l in enumerate(rawData.split('\n')): 154 if len(l)>0: 155 name=useName 156 if i>0 and name!=None: 157 name+=("_%d" % i) 158 info.append(CustomPlotInfo(l,name=name)) 159 160 return info
161
162 -def resetCustomCounter():
163 """Reset the counter. Use with care""" 164 CustomPlotInfo.nr=1
165 166 # Should work with Python3 and Python2 167