1
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
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
24 if type(data)!=str:
25 return data
26 if data.find(' ')<0:
27 return data
28 else:
29 return '"'+data+'"'
30
32 """Information about a custom plot"""
33
34 nr=1
35
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
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
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
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
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
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
150