00001 00002 import os, types 00003 00004 00005 def StrDate(): 00006 d = os.popen("date").read().split() 00007 return "_".join([d[1],d[2],"".join(d[3].split(":")),d[-1]]) 00008 00009 00010 def IsQuotedStr(i): 00011 if type(i) != str: 00012 return False 00013 if len(i)<2: 00014 return False 00015 if i[0] == "'" or i[0] == '"': 00016 return i[0] == i[-1] 00017 return False 00018 00019 00020 def QuoteStr(i,*args,**kwargs): 00021 if IsQuotedStr(i): 00022 return i 00023 else: 00024 return str(i).join(('"','"')) 00025 00026 def StripQuote(i): 00027 if IsQuotedStr(i): 00028 return i[1:-1] 00029 else: 00030 return i 00031 00032 00033 00034 def FormatListOrTuple(l, pad, depth=0,Delims=True,Quoted=True): 00035 00036 curtype=type(l) 00037 00038 if curtype == types.TupleType: 00039 opend,closed,isep = {True:("(\n", ")",",\n"), False:("\n","","\n")}[Delims] 00040 else: 00041 opend,closed,isep = {True:("[\n", "]",",\n"), False:("\n","","\n")}[Delims] 00042 00043 rv = opend 00044 depth+=1 00045 for i in l: 00046 rv += pad*depth + FormatNested(i,pad,depth,Delims=Delims,Quoted=Quoted)+isep 00047 00048 return rv + pad*(depth-1)+closed 00049 00050 00051 def FormatDic(d, pad,depth=0,Delims=True,Quoted=True): 00052 00053 opend,closed,isep = {True: ("{\n","}",",\n"), False:("\n","","\n")}[Delims] 00054 00055 rv = opend 00056 depth+=1 00057 for k in d.keys(): 00058 rv += pad*depth + QuoteStr(k) + " : " + FormatNested(d[k], pad, depth,Delims=Delims,Quoted=Quoted) + isep 00059 00060 return rv +pad*(depth-1)+closed 00061 00062 00063 00064 00065 def FormatNested(struct, pad=" ", depth=0, AsType=None,Delims=True,Quoted=True): 00066 00067 FormatLookup = { types.DictType : FormatDic, 00068 types.ListType : FormatListOrTuple, 00069 types.TupleType: FormatListOrTuple, 00070 types.StringType: QuoteStr 00071 } 00072 00073 struct_t = type(struct) 00074 00075 if AsType==None: 00076 if not FormatLookup.has_key(struct_t): 00077 if hasattr(struct,"keys"): 00078 AsType=types.DictType 00079 elif hasattr(struct, "index"): 00080 AsType=types.TupleType 00081 else: 00082 AsType=struct_t 00083 00084 00085 if FormatLookup.has_key(AsType): 00086 return FormatLookup[AsType](struct, pad,depth,Delims,Quoted=Quoted) 00087 elif Quoted: 00088 return QuoteStr(struct) 00089 else: 00090 return str(struct) 00091 00092