Exemplo n.º 1
0
def correct_table_dates (table):
    """
        this function takes a table as returned by pakskt.get_table_data
    and updates the date field to a standard datetime object if it exists.
    
    returns true on success 
    """
    try:
        for row in table:
             row['TimeOfRec'] = \
             datetime.fromtimestamp(pkb.nsec_to_time(row['TimeOfRec']))
    except:
        return table, False
    return table, True
Exemplo n.º 2
0
 def write(self, table, logger_name, w_dir = './', delim = ','):
     """ 
         writes given table to a file 
     
     arguments:
         table:          (stirng) the table
         logger_name:    (string) the logger name
         w_dir:          (string) the directory to write to
         delim:          (char) the delimiting char
         
     returns:
         the name of the file written
     """
     if not os.path.isdir(w_dir):
         os.makedirs(w_dir)
     
     filename = logger_name + '_' + table + ".dat"
     try:
         f = open(filename, 'r')
         first = f.readline()     
         f.seek(-2, 2)            
         while f.read(1) != "\n": 
             f.seek(-2, 1)        
         last = f.readline()
         oldrecs = int(last.split(',')[1].replace('"',''))
         f.close()
         f_exists = True
     except IOError:
         oldrecs = 0
         f_exists = False
         
     data = self.fetchdata(table, oldrecs)
     
     
     if len(data) == 0:
         return "no data to write"
     info = self.progstat()
     
     # here is the case for status and public
     if table == "Status" or table == "Public":
         line1 = '"TOA5"' + delim + '"' + logger_name + '"' + delim + \
              '"' + info["OSVer"].split('.')[0]  + '"' + delim + \
              '"' + str(info["SerialNbr"]) + '"' + delim + \
              '"' + info["OSVer"] + '"' + delim + \
              '"' + info["ProgName"] + '"' + delim + \
              '"' + str(info['ProgSig']) + '"' + delim + \
              '"' + table + '"\n'
              
              
         f = open(w_dir+filename, "w")
         if not f_exists:
             f.write(line1)
         
         f.write(data)
         f.close()
         return filename 
     
     
     units, pro = self.get_unit_info(table)
     line1 = '"TOA5"' + delim + '"' + logger_name + '"' + delim + \
              '"' + info["OSVer"].split('.')[0]  + '"' + delim + \
              '"' + str(info["SerialNbr"]) + '"' + delim + \
              '"' + info["OSVer"] + '"' + delim + \
              '"' + info["ProgName"] + '"' + delim + \
              '"' + str(info['ProgSig']) + '"' + delim + \
              '"' + table + '"\n'
     line2 = '"TIMESTAMP"' + delim + '"RN"' 
     line3 = '"TS"' + delim + '"RN"'
     line4 = '""' + delim + '""'
     for items in data[0]['Fields'].keys():
         line2 += delim + '"' + items + '"'
         line3 += delim + '"' + str(units[items]) + '"'
         line4 += delim + '"' + pro[items] + '"'
     line2 += '\n'    
     line3 += '\n'
     line4 += '\n'
     records = ""
     for recs in data:
         the_date = pakbus.nsec_to_time(recs['TimeOfRec'])
         the_date = dt.datetime.fromtimestamp(the_date) 
         records += '"' + str(the_date) + '"'
         records += delim + '"' + str(recs['RecNbr']) + '"'
         
         for items in data[0]['Fields'].keys():
             records += delim + '"' + str(recs['Fields'][items][0]) + '"'
         records += '\n'
     
     
     
     f = open(w_dir+filename, "a")
     if not f_exists:
         f.write(line1)
         f.write(line2)
         f.write(line3)
         f.write(line4)
     
     f.write(records)
     f.close()
     return filename