$search
00001 #!/usr/bin/env python 00002 # 00003 # hdfhelp.py 00004 # 00005 # This code makes using odb with Clearsilver as "easy as stealing candy 00006 # from a baby". - jeske 00007 # 00008 # How to use: 00009 # 00010 # rows = tbl.fetchAllRows() 00011 # rows.hdfExport("CGI.rows", hdf_dataset) 00012 # 00013 # row = tbl.fetchRow( ('primary_key', value) ) 00014 # row.hdfExport("CGI.row", hdf_dataset) 00015 # 00016 # How to setup: 00017 # 00018 # # define table 00019 # class AgentsTable(odb.Table): 00020 # def _defineRows(self): 00021 # self.d_addColumn("agent_id",kInteger,None,primarykey = 1,autoincrement = 1) 00022 # self.d_addColumn("login",kVarString,200,notnull=1) 00023 # self.d_addColumn("ticket_count",kIncInteger,None) 00024 # 00025 # # make sure you return a subclass of hdfhelp.HdfRow 00026 # 00027 # def defaultRowClass(self): 00028 # return hdfhelp.HdfRow 00029 # def defaultRowListClass(self): 00030 # return hdfhelp.HdfItemList 00031 # 00032 00033 import string, os 00034 import neo_cgi 00035 import neo_cs 00036 import neo_util 00037 import odb 00038 import time 00039 00040 import UserList 00041 00042 SECS_IN_MIN = 60 00043 SECS_IN_HOUR = (SECS_IN_MIN * 60) 00044 SECS_IN_DAY = (SECS_IN_HOUR * 24) 00045 SECS_IN_WEEK = (SECS_IN_DAY * 7) 00046 SECS_IN_MONTH = (SECS_IN_DAY * 30) 00047 00048 kYearPos = 0 00049 kMonthPos = 1 00050 kDayPos = 2 00051 kHourPos = 3 00052 kMinutePos = 4 00053 kSecondPos = 5 00054 kWeekdayPos = 6 00055 kJulianDayPos = 7 00056 kDSTPos = 8 00057 00058 00059 def renderDate(then_time,day=0,date=1): 00060 if then_time is None: 00061 then_time = 0 00062 then_time = int(then_time) 00063 if then_time == 0 or then_time == -1: 00064 return "" 00065 00066 then_tuple = time.localtime(then_time) 00067 00068 now_tuple = time.localtime(time.time()) 00069 00070 if day or (then_tuple[kHourPos]==0 and then_tuple[kMinutePos]==0 and then_tuple[kSecondPos]==0): 00071 # it's just a date 00072 if then_tuple[kYearPos] == now_tuple[kYearPos]: 00073 # no year 00074 return time.strftime("%m/%d",then_tuple) 00075 else: 00076 # add year 00077 return time.strftime("%m/%d/%Y",then_tuple) 00078 elif date: 00079 return time.strftime("%m/%d/%Y",then_tuple) 00080 else: 00081 # it's a full time/date 00082 00083 return time.strftime("%m/%d/%Y %I:%M%p",then_tuple) 00084 00085 class HdfRow(odb.Row): 00086 def hdfExport(self, prefix, hdf_dataset, *extra, **extranamed): 00087 skip_fields = extranamed.get("skip_fields", None) 00088 no_escape = extranamed.get("no_escape", None) 00089 translate_dict = extranamed.get("translate_dict", None) 00090 tz = extranamed.get("tz", "US/Pacific") 00091 00092 for col_name,value in self.items(): 00093 if skip_fields and (col_name in skip_fields): 00094 continue 00095 try: 00096 name,col_type,col_options = self._table.getColumnDef(col_name) 00097 except: 00098 col_type = odb.kVarString 00099 col_options = {} 00100 00101 if (value is not None): 00102 if col_options.get("no_export",0): continue 00103 if type(value) in [ type(0), type(0L) ]: 00104 hdf_dataset.setValue(prefix + "." + col_name,"%d" % value) 00105 elif type(value) == type(1.0): 00106 if int(value) == value: 00107 hdf_dataset.setValue(prefix + "." + col_name,"%d" % value) 00108 else: 00109 hdf_dataset.setValue(prefix + "." + col_name,"%0.2f" % value) 00110 else: 00111 if col_type == odb.kReal: 00112 log("why are we here with this value: %s" % value) 00113 if translate_dict: 00114 for k,v in translate_dict.items(): 00115 value = string.replace(value,k,v) 00116 if no_escape and col_name in no_escape: 00117 hdf_dataset.setValue(prefix + "." + col_name,str(value)) 00118 else: 00119 hdf_dataset.setValue(prefix + "." + col_name,neo_cgi.htmlEscape(str(value))) 00120 if col_options.get("int_date",0): 00121 hdf_dataset.setValue(prefix + "." + col_name + ".string",renderDate(value)) 00122 hdf_dataset.setValue(prefix + "." + col_name + ".day_string",renderDate(value,day=1)) 00123 hdf_dataset.setValue(prefix + "." + col_name + ".date_string",renderDate(value,date=1)) 00124 if value: 00125 try: 00126 neo_cgi.exportDate(hdf_dataset, "%s.%s" % (prefix, col_name), tz, value) 00127 except TypeError: 00128 pass 00129 00130 if col_options.has_key("enum_values"): 00131 enum = col_options["enum_values"] 00132 hdf_dataset.setValue(prefix + "." + col_name + ".enum", 00133 str(enum.get(value,''))) 00134 00135 class HdfItemList(UserList.UserList): 00136 def hdfExport(self,prefix,hdf_dataset,*extra,**extranamed): 00137 export_by = extranamed.get("export_by", None) 00138 n = 0 00139 for row in self: 00140 if export_by is not None: 00141 n = row[export_by] 00142 row.hdfExport("%s.%d" % (prefix,n),hdf_dataset,*extra,**extranamed) 00143 n = n + 1 00144 00145 def setList(hdf, prefix, lst): 00146 hdf.setValue(prefix+".0", str(len(lst))) 00147 for n in range(len(lst)): 00148 hdf.setValue(prefix+".%d" %(n+1), lst[n]); 00149 00150 def getList(hdf, name): 00151 lst = [] 00152 for n in range(hdf.getIntValue(name,0)): 00153 lst.append(hdf.getValue(name+".%d" %(n+1), "")) 00154 00155 return lst 00156 00157 def eval_cs(hdf,a_cs_string): 00158 cs = neo_cs.CS(hdf) 00159 try: 00160 cs.parseStr(a_cs_string) 00161 return cs.render() 00162 except: 00163 return "Error in CS tags: %s" % neo_cgi.htmlEscape(repr(a_cs_string)) 00164 00165 def childloop(hdf): 00166 children = [] 00167 if hdf: 00168 hdf = hdf.child() 00169 while hdf: 00170 children.append(hdf) 00171 hdf = hdf.next() 00172 return children 00173 00174 # ---------------------------- 00175 00176 class HDF_Database(odb.Database): 00177 def defaultRowClass(self): 00178 return HdfRow 00179 def defaultRowListClass(self): 00180 return HdfItemList 00181 00182 # ---------------------------- 00183 00184 00185 def loopHDF(hdf, name=None): 00186 results = [] 00187 if name: o = hdf.getObj(name) 00188 else: o = hdf 00189 if o: 00190 o = o.child() 00191 while o: 00192 results.append(o) 00193 o = o.next() 00194 return results 00195 00196 00197 def loopKVHDF(hdf, name=None): 00198 results = [] 00199 if name: o = hdf.getObj(name) 00200 else: o = hdf 00201 if o: 00202 o = o.child() 00203 while o: 00204 results.append((o.name(), o.value())) 00205 o = o.next() 00206 return results 00207 00208 00209 class hdf_iterator: 00210 def __init__(self, hdf): 00211 self.hdf = hdf 00212 self.node = None 00213 if self.hdf: 00214 self.node = self.hdf.child() 00215 00216 def __iter__(self): return self 00217 00218 def next(self): 00219 if not self.node: 00220 raise StopIteration 00221 00222 ret = self.node 00223 self.node = self.node.next() 00224 00225 return ret 00226 00227 class hdf_kv_iterator(hdf_iterator): 00228 def next(self): 00229 if not self.node: raise StopIteration 00230 00231 ret = (self.node.name(), self.node.value()) 00232 self.node = self.node.next() 00233 00234 return ret 00235 00236 class hdf_key_iterator(hdf_iterator): 00237 def next(self): 00238 if not self.node: raise StopIteration 00239 00240 ret = self.node.name() 00241 self.node = self.node.next() 00242 00243 return ret 00244 00245 class hdf_ko_iterator(hdf_iterator): 00246 def next(self): 00247 if not self.node: raise StopIteration 00248 00249 ret = (self.node.name(), self.node) 00250 self.node = self.node.next() 00251 00252 return ret 00253 00254 00255 def hdfExportDict(prefix, hdf, dict): 00256 for k,v in dict.items(): 00257 hdf.setValue(prefix + "." + str(k), str(v)) 00258 00259 00260 def hdfExportList(prefix, hdf, list): 00261 n = 0 00262 for item in list: 00263 n = n + 1 00264 hdf.setValue(prefix + "." + str(n), str(item)) 00265 00266 def exportDate(hdf, prefix, tz, ttime): 00267 lt = time.localtime(ttime) 00268 hour = lt.tm_hour 00269 if hour >= 12: 00270 am = 0; hour=hour-12 00271 else: am = 1 00272 hdf.setValue(prefix + ".hour", str(hour)) 00273 hdf.setValue(prefix + ".sec", str(am)) 00274 00275 hdf.setValue(prefix + ".24hour", str(lt.tm_hour)) 00276 hdf.setValue(prefix + ".min", "%02d" % lt.tm_min) 00277 hdf.setValue(prefix + ".sec", "%02d" % lt.tm_sec) 00278 00279 hdf.setValue(prefix + ".mday", str(lt.tm_mday)) 00280 hdf.setValue(prefix + ".mon", str(lt.tm_mon)) 00281 hdf.setValue(prefix + ".year", str(lt.tm_year)) 00282 hdf.setValue(prefix + ".2yr", str(lt.tm_year)[2:4]) 00283 hdf.setValue(prefix + ".wday", str(lt.tm_wday)) 00284 00285 hdf.setValue(prefix + ".tzoffset", "0") 00286 00287 00288 def exportGUID(prefix, hdf, guidStr): 00289 import guid 00290 try: 00291 t = guid.extract_time(guidStr) 00292 hdf.setValue(prefix + ".time_t", str(t)) 00293 exportDate(hdf, prefix + ".time", "US/Pacific", t) 00294 hdf.setValue(prefix + ".random", str(guid.extract_random(guidStr))) 00295 hdf.setValue(prefix + ".server_id", str(guid.extract_ip(guidStr))) 00296 except guid.InvalidGUID: 00297 pass 00298 00299 00300 def renderSize(prefix, hdf, size): 00301 s = "" 00302 units = "" 00303 if size > (1024*1024*1024): 00304 ret_size = "%.1f" % (size / (1024*1024*1024.)) 00305 units = "G" 00306 s = "%s%s" % (ret_size, units) 00307 elif size > (1024*1024): 00308 ret_size = "%.1f" % (size / (1024*1024.)) 00309 units = "M" 00310 s = "%s%s" % (ret_size, units) 00311 elif size > (1024): 00312 ret_size = "%.1f" % (size / (1024.)) 00313 units = "K" 00314 else: 00315 ret_size = str(size) 00316 if ret_size.endswith(".0"): ret_size = ret_size[:-2] 00317 s = "%s%s" % (ret_size, units) 00318 hdf.setValue(prefix + ".str", s) 00319 00320 # ---------------------------- 00321 00322 def test(): 00323 import neo_util 00324 hdf = neo_util.HDF() 00325 hdf.setValue("foo","1") 00326 print eval_cs(hdf,"this should say 1 ===> <?cs var:foo ?>") 00327 00328 00329 if __name__ == "__main__": 00330 test() 00331 00332 00333 00334 00335 00336