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