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):
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 self.nr=CustomPlotInfo.nr 41 CustomPlotInfo.nr+=1 42 43 # Setting sensible default values 44 self.name="Custom%02d" % self.nr 45 self.theTitle="Custom %d" % self.nr 46 if name: 47 self.name+="_"+name 48 self.id=name 49 self.theTitle += " - "+name 50 else: 51 self.id=self.name 52 53 self.expr=None 54 self.titles=[] 55 self.accumulation="first" 56 self.start=None 57 self.end=None 58 self.persist=False 59 self.raisit=False 60 self.with_="lines" 61 self.type="regular"; 62 self.master=None 63 64 # Legacy format 65 if raw==None: 66 self.expr="" 67 elif type(raw)==str: 68 if raw[0]=='{': 69 data=eval(raw) 70 self.expr=data["expr"] 71 if "name" in data: 72 self.name+="_"+data["name"] 73 self.name=self.name.replace(" ","_").replace(path.sep,"Slash") 74 self.theTitle+=" - "+data["name"] 75 if "titles" in data: 76 self.titles=data["titles"] 77 for o in ["alternateAxis","logscale","with","ylabel","y2label"]: 78 if o=="with": 79 use="with_" 80 else: 81 use=o 82 if o in data: 83 self.set(use,data[o]) 84 if "accumulation" in data: 85 self.accumulation=data["accumulation"] 86 else: 87 self.expr=raw 88 # New format 89 else: 90 for k in raw: 91 data=raw[k] 92 if type(data)==str: 93 data=cleanString(data) 94 elif type(data)==list: 95 data=map(cleanString,data) 96 if k=="with": 97 k="with_" 98 self.set(k,data) 99 100 # Sanity check the data 101 if self.accumulation not in TimeLineCollection.possibleAccumulations: 102 error("Accumulation",self.accumulation,"not in the possible values",TimeLineCollection.possibleAccumulations) 103 104 if self.expr==None: 105 error("No expression set by data",raw)
106
107 - def set(self,key,value):
108 setattr(self,key,value)
109 110
111 - def __str__(self):
112 return makeString({self.id:self.getDict(wrapStrings=True)})
113
114 - def getDict(self,wrapStrings=False):
115 result={} 116 117 for d in dir(self): 118 if (type(getattr(self,d)) in [str,bool,int,list,dict,float]) and d.find("__")<0: 119 if d=="id" or d=="nr": 120 pass 121 else: 122 key=d.replace("_","") 123 val=getattr(self,d) 124 if wrapStrings: 125 if type(val)==str: 126 val=encloseString(val) 127 elif type(val)==list: 128 val=map(encloseString,val) 129 130 result[key]=val 131 return result
132 133
134 -def readCustomPlotInfo(rawData):
135 """Determines which of the three possible formats for custom-plotting is used 136 and returns a list of CustomPlotInfo-objects 137 @param rawData: a string that contains the raw data""" 138 info=[] 139 140 try: 141 data=FoamStringParser(rawData) 142 for k,d in data.data.iteritems(): 143 info.append(CustomPlotInfo(d,name=k)) 144 except PyFoamParserError: 145 for l in rawData.split('\n'): 146 if len(l)>0: 147 info.append(CustomPlotInfo(l)) 148 149 return info
150