def addPeriod(fname,start_time,end_time,entry="All",frontend="All",security_class="All",comment="",create_if_empty=True): exists=os.path.isfile(fname) if (not exists) and (not create_if_empty): raise IOError, "[Errno 2] No such file or directory: '%s'"%fname comment=comment.replace("\n", " "); comment=comment.replace("\r", " "); fd=open(fname,'a+') try: fcntl.flock(fd,fcntl.LOCK_EX) if not exists: # new file, create header fd.write("#%-29s %-30s %-20s %-30s %-20s # %s\n"%("Start","End","Entry","Frontend","Sec_Class","Comment")) if end_time is not None: fd.write("%-30s %-20s %-20s %-30s %-20s # %-20s\n"%(timeConversion.getISO8601_Local(start_time),timeConversion.getISO8601_Local(end_time),entry,frontend,security_class,comment)) else: fd.write("%-30s %-30s %-20s %-30s %-20s # %s\n"%(timeConversion.getISO8601_Local(start_time),"None",entry,frontend,security_class,comment)) finally: fd.close() return 0;
def get_xml_updated(when,indent_tab=xmlFormat.DEFAULT_TAB,leading_tab=""): xml_updated={"UTC":{"unixtime":timeConversion.getSeconds(when), "ISO8601":timeConversion.getISO8601_UTC(when), "RFC2822":timeConversion.getRFC2822_UTC(when)}, "Local":{"ISO8601":timeConversion.getISO8601_Local(when), "RFC2822":timeConversion.getRFC2822_Local(when), "human":timeConversion.getHuman(when)}} return xmlFormat.dict2string(xml_updated, dict_name="updated",el_name="timezone", subtypes_params={"class":{}}, indent_tab=indent_tab,leading_tab=leading_tab)
def time2xml(the_time, outer_tag, indent_tab=DEFAULT_TAB, leading_tab=""): xml_data = {"UTC":{"unixtime":timeConversion.getSeconds(the_time), "ISO8601":timeConversion.getISO8601_UTC(the_time), "RFC2822":timeConversion.getRFC2822_UTC(the_time)}, "Local":{"ISO8601":timeConversion.getISO8601_Local(the_time), "RFC2822":timeConversion.getRFC2822_Local(the_time), "human":timeConversion.getHuman(the_time)}} return dict2string(xml_data, dict_name=outer_tag, el_name="timezone", subtypes_params={"class":{}}, indent_tab=indent_tab, leading_tab=leading_tab)
def endDowntime(fname,end_time=None,entry="All",frontend="All",security_class="All",comment=""): comment=comment.replace("\r", " "); comment=comment.replace("\n", " "); if end_time is None: end_time=long(time.time()) try: fd=open(fname,'r+') except IOError: return 0 # no file -> nothing to end try: fcntl.flock(fd,fcntl.LOCK_EX) # read the old info inlines=fd.readlines() outlines=[] lnr=0 closed_nr=0 for long_line in inlines: lnr+=1 line=long_line.strip() if len(line)==0: outlines.append(long_line) continue # pass on empty lines if line[0:1]=='#': outlines.append(long_line) continue # pass on comments arr=line.split() if len(arr)<2: outlines.append(long_line) continue # pass on malformed lines #make sure this is for the right entry if ((entry!="All")and(len(arr)>2)and(entry!=arr[2])): outlines.append(long_line) continue if ((entry=="All")and(len(arr)>2)and("factory"==arr[2])): outlines.append(long_line) continue if ((frontend!="All")and(len(arr)>3)and(frontend!=arr[3])): outlines.append(long_line) continue #make sure that this time tuple applies to this security_class if ((security_class!="All")and(len(arr)>4)and(security_class!=arr[4])): outlines.append(long_line) continue cur_start_time=0 if arr[0]!='None': cur_start_time=timeConversion.extractISO8601_Local(arr[0]) if arr[1]!='None': cur_end_time=timeConversion.extractISO8601_Local(arr[1]) if arr[1]=='None' or ((cur_start_time<long(time.time())) and (cur_end_time>end_time)): # open period -> close outlines.append("%-30s %-30s"%(arr[0],timeConversion.getISO8601_Local(end_time))) if (len(arr)>2): sep=" "; t=2 for param in arr[2:]: if t<5: outlines.append("%s%-20s" % (sep,param)); else: outlines.append("%s%s" % (sep,param)); t=t+1 if (comment!=""): outlines.append("; %s" % (comment)); outlines.append("\n"); closed_nr+=1 else: # closed just pass on outlines.append(long_line) #Keep parsing file, since there may be multiple downtimes #pass # end for # go back to start to rewrite fd.seek(0) fd.writelines(outlines) fd.truncate() finally: fd.close() return closed_nr
def format_time(self, timestamp): return timeConversion.getISO8601_Local(timestamp)
outlines.append(long_line) continue if ((entry=="All")and(len(arr)>2)and("factory"==arr[2])): outlines.append(long_line) continue if ((frontend!="All")and(len(arr)>3)and(frontend!=arr[3])): outlines.append(long_line) continue #make sure that this time tuple applies to this security_class if ((security_class!="All")and(len(arr)>4)and(security_class!=arr[4])): outlines.append(long_line) continue if arr[1]=='None': # open period -> close outlines.append("%-30s %-30s"%(arr[0],timeConversion.getISO8601_Local(end_time))) if (len(arr)>2): sep=" "; t=2 for param in arr[2:]: if t<5: outlines.append("%s%-20s" % (sep,param)); else: outlines.append("%s%s" % (sep,param)); t=t+1 if (comment!=""): outlines.append("; %s" % (comment)); outlines.append("\n"); closed_nr+=1 else: # closed just pass on