hdfhelp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # hdfhelp.py
4 #
5 # This code makes using odb with Clearsilver as "easy as stealing candy
6 # from a baby". - jeske
7 #
8 # How to use:
9 #
10 # rows = tbl.fetchAllRows()
11 # rows.hdfExport("CGI.rows", hdf_dataset)
12 #
13 # row = tbl.fetchRow( ('primary_key', value) )
14 # row.hdfExport("CGI.row", hdf_dataset)
15 #
16 # How to setup:
17 #
18 # # define table
19 # class AgentsTable(odb.Table):
20 # def _defineRows(self):
21 # self.d_addColumn("agent_id",kInteger,None,primarykey = 1,autoincrement = 1)
22 # self.d_addColumn("login",kVarString,200,notnull=1)
23 # self.d_addColumn("ticket_count",kIncInteger,None)
24 #
25 # # make sure you return a subclass of hdfhelp.HdfRow
26 #
27 # def defaultRowClass(self):
28 # return hdfhelp.HdfRow
29 # def defaultRowListClass(self):
30 # return hdfhelp.HdfItemList
31 #
32 
33 import string, os
34 import neo_cgi
35 import neo_cs
36 import neo_util
37 import odb
38 import time
39 
40 import UserList
41 
42 SECS_IN_MIN = 60
43 SECS_IN_HOUR = (SECS_IN_MIN * 60)
44 SECS_IN_DAY = (SECS_IN_HOUR * 24)
45 SECS_IN_WEEK = (SECS_IN_DAY * 7)
46 SECS_IN_MONTH = (SECS_IN_DAY * 30)
47 
48 kYearPos = 0
49 kMonthPos = 1
50 kDayPos = 2
51 kHourPos = 3
52 kMinutePos = 4
53 kSecondPos = 5
54 kWeekdayPos = 6
55 kJulianDayPos = 7
56 kDSTPos = 8
57 
58 
59 def renderDate(then_time,day=0,date=1):
60  if then_time is None:
61  then_time = 0
62  then_time = int(then_time)
63  if then_time == 0 or then_time == -1:
64  return ""
65 
66  then_tuple = time.localtime(then_time)
67 
68  now_tuple = time.localtime(time.time())
69 
70  if day or (then_tuple[kHourPos]==0 and then_tuple[kMinutePos]==0 and then_tuple[kSecondPos]==0):
71  # it's just a date
72  if then_tuple[kYearPos] == now_tuple[kYearPos]:
73  # no year
74  return time.strftime("%m/%d",then_tuple)
75  else:
76  # add year
77  return time.strftime("%m/%d/%Y",then_tuple)
78  elif date:
79  return time.strftime("%m/%d/%Y",then_tuple)
80  else:
81  # it's a full time/date
82 
83  return time.strftime("%m/%d/%Y %I:%M%p",then_tuple)
84 
85 class HdfRow(odb.Row):
86  def hdfExport(self, prefix, hdf_dataset, *extra, **extranamed):
87  skip_fields = extranamed.get("skip_fields", None)
88  no_escape = extranamed.get("no_escape", None)
89  translate_dict = extranamed.get("translate_dict", None)
90  tz = extranamed.get("tz", "US/Pacific")
91 
92  for col_name,value in self.items():
93  if skip_fields and (col_name in skip_fields):
94  continue
95  try:
96  name,col_type,col_options = self._table.getColumnDef(col_name)
97  except:
98  col_type = odb.kVarString
99  col_options = {}
100 
101  if (value is not None):
102  if col_options.get("no_export",0): continue
103  if type(value) in [ type(0), type(0L) ]:
104  hdf_dataset.setValue(prefix + "." + col_name,"%d" % value)
105  elif type(value) == type(1.0):
106  if int(value) == value:
107  hdf_dataset.setValue(prefix + "." + col_name,"%d" % value)
108  else:
109  hdf_dataset.setValue(prefix + "." + col_name,"%0.2f" % value)
110  else:
111  if col_type == odb.kReal:
112  log("why are we here with this value: %s" % value)
113  if translate_dict:
114  for k,v in translate_dict.items():
115  value = string.replace(value,k,v)
116  if no_escape and col_name in no_escape:
117  hdf_dataset.setValue(prefix + "." + col_name,str(value))
118  else:
119  hdf_dataset.setValue(prefix + "." + col_name,neo_cgi.htmlEscape(str(value)))
120  if col_options.get("int_date",0):
121  hdf_dataset.setValue(prefix + "." + col_name + ".string",renderDate(value))
122  hdf_dataset.setValue(prefix + "." + col_name + ".day_string",renderDate(value,day=1))
123  hdf_dataset.setValue(prefix + "." + col_name + ".date_string",renderDate(value,date=1))
124  if value:
125  try:
126  neo_cgi.exportDate(hdf_dataset, "%s.%s" % (prefix, col_name), tz, value)
127  except TypeError:
128  pass
129 
130  if col_options.has_key("enum_values"):
131  enum = col_options["enum_values"]
132  hdf_dataset.setValue(prefix + "." + col_name + ".enum",
133  str(enum.get(value,'')))
134 
135 class HdfItemList(UserList.UserList):
136  def hdfExport(self,prefix,hdf_dataset,*extra,**extranamed):
137  export_by = extranamed.get("export_by", None)
138  n = 0
139  for row in self:
140  if export_by is not None:
141  n = row[export_by]
142  row.hdfExport("%s.%d" % (prefix,n),hdf_dataset,*extra,**extranamed)
143  n = n + 1
144 
145 def setList(hdf, prefix, lst):
146  hdf.setValue(prefix+".0", str(len(lst)))
147  for n in range(len(lst)):
148  hdf.setValue(prefix+".%d" %(n+1), lst[n]);
149 
150 def getList(hdf, name):
151  lst = []
152  for n in range(hdf.getIntValue(name,0)):
153  lst.append(hdf.getValue(name+".%d" %(n+1), ""))
154 
155  return lst
156 
157 def eval_cs(hdf,a_cs_string):
158  cs = neo_cs.CS(hdf)
159  try:
160  cs.parseStr(a_cs_string)
161  return cs.render()
162  except:
163  return "Error in CS tags: %s" % neo_cgi.htmlEscape(repr(a_cs_string))
164 
165 def childloop(hdf):
166  children = []
167  if hdf:
168  hdf = hdf.child()
169  while hdf:
170  children.append(hdf)
171  hdf = hdf.next()
172  return children
173 
174 # ----------------------------
175 
177  def defaultRowClass(self):
178  return HdfRow
180  return HdfItemList
181 
182 # ----------------------------
183 
184 
185 def loopHDF(hdf, name=None):
186  results = []
187  if name: o = hdf.getObj(name)
188  else: o = hdf
189  if o:
190  o = o.child()
191  while o:
192  results.append(o)
193  o = o.next()
194  return results
195 
196 
197 def loopKVHDF(hdf, name=None):
198  results = []
199  if name: o = hdf.getObj(name)
200  else: o = hdf
201  if o:
202  o = o.child()
203  while o:
204  results.append((o.name(), o.value()))
205  o = o.next()
206  return results
207 
208 
210  def __init__(self, hdf):
211  self.hdf = hdf
212  self.node = None
213  if self.hdf:
214  self.node = self.hdf.child()
215 
216  def __iter__(self): return self
217 
218  def next(self):
219  if not self.node:
220  raise StopIteration
221 
222  ret = self.node
223  self.node = self.node.next()
224 
225  return ret
226 
228  def next(self):
229  if not self.node: raise StopIteration
230 
231  ret = (self.node.name(), self.node.value())
232  self.node = self.node.next()
233 
234  return ret
235 
237  def next(self):
238  if not self.node: raise StopIteration
239 
240  ret = self.node.name()
241  self.node = self.node.next()
242 
243  return ret
244 
246  def next(self):
247  if not self.node: raise StopIteration
248 
249  ret = (self.node.name(), self.node)
250  self.node = self.node.next()
251 
252  return ret
253 
254 
255 def hdfExportDict(prefix, hdf, dict):
256  for k,v in dict.items():
257  hdf.setValue(prefix + "." + str(k), str(v))
258 
259 
260 def hdfExportList(prefix, hdf, list):
261  n = 0
262  for item in list:
263  n = n + 1
264  hdf.setValue(prefix + "." + str(n), str(item))
265 
266 def exportDate(hdf, prefix, tz, ttime):
267  lt = time.localtime(ttime)
268  hour = lt.tm_hour
269  if hour >= 12:
270  am = 0; hour=hour-12
271  else: am = 1
272  hdf.setValue(prefix + ".hour", str(hour))
273  hdf.setValue(prefix + ".sec", str(am))
274 
275  hdf.setValue(prefix + ".24hour", str(lt.tm_hour))
276  hdf.setValue(prefix + ".min", "%02d" % lt.tm_min)
277  hdf.setValue(prefix + ".sec", "%02d" % lt.tm_sec)
278 
279  hdf.setValue(prefix + ".mday", str(lt.tm_mday))
280  hdf.setValue(prefix + ".mon", str(lt.tm_mon))
281  hdf.setValue(prefix + ".year", str(lt.tm_year))
282  hdf.setValue(prefix + ".2yr", str(lt.tm_year)[2:4])
283  hdf.setValue(prefix + ".wday", str(lt.tm_wday))
284 
285  hdf.setValue(prefix + ".tzoffset", "0")
286 
287 
288 def exportGUID(prefix, hdf, guidStr):
289  import guid
290  try:
291  t = guid.extract_time(guidStr)
292  hdf.setValue(prefix + ".time_t", str(t))
293  exportDate(hdf, prefix + ".time", "US/Pacific", t)
294  hdf.setValue(prefix + ".random", str(guid.extract_random(guidStr)))
295  hdf.setValue(prefix + ".server_id", str(guid.extract_ip(guidStr)))
296  except guid.InvalidGUID:
297  pass
298 
299 
300 def renderSize(prefix, hdf, size):
301  s = ""
302  units = ""
303  if size > (1024*1024*1024):
304  ret_size = "%.1f" % (size / (1024*1024*1024.))
305  units = "G"
306  s = "%s%s" % (ret_size, units)
307  elif size > (1024*1024):
308  ret_size = "%.1f" % (size / (1024*1024.))
309  units = "M"
310  s = "%s%s" % (ret_size, units)
311  elif size > (1024):
312  ret_size = "%.1f" % (size / (1024.))
313  units = "K"
314  else:
315  ret_size = str(size)
316  if ret_size.endswith(".0"): ret_size = ret_size[:-2]
317  s = "%s%s" % (ret_size, units)
318  hdf.setValue(prefix + ".str", s)
319 
320 # ----------------------------
321 
322 def test():
323  import neo_util
324  hdf = neo_util.HDF()
325  hdf.setValue("foo","1")
326  print eval_cs(hdf,"this should say 1 ===> <?cs var:foo ?>")
327 
328 
329 if __name__ == "__main__":
330  test()
331 
332 
333 
334 
335 
336 
def setList(hdf, prefix, lst)
Definition: hdfhelp.py:145
def exportDate(hdf, prefix, tz, ttime)
Definition: hdfhelp.py:266
def hdfExport(self, prefix, hdf_dataset, extra, extranamed)
Definition: hdfhelp.py:136
def hdfExport(self, prefix, hdf_dataset, extra, extranamed)
Definition: hdfhelp.py:86
def log(args)
Definition: log.py:107
def renderSize(prefix, hdf, size)
Definition: hdfhelp.py:300
def renderDate(then_time, day=0, date=1)
Definition: hdfhelp.py:59
def eval_cs(hdf, a_cs_string)
Definition: hdfhelp.py:157
def loopHDF(hdf, name=None)
Definition: hdfhelp.py:185
def childloop(hdf)
Definition: hdfhelp.py:165
def hdfExportList(prefix, hdf, list)
Definition: hdfhelp.py:260
def hdfExportDict(prefix, hdf, dict)
Definition: hdfhelp.py:255
def loopKVHDF(hdf, name=None)
Definition: hdfhelp.py:197
def exportGUID(prefix, hdf, guidStr)
Definition: hdfhelp.py:288
def getList(hdf, name)
Definition: hdfhelp.py:150


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13