00001 __copyright__ = """\ 00002 (c). Copyright 1990-2008, Vyper Logix Corp., All Rights Reserved. 00003 00004 Published under Creative Commons License 00005 (http://creativecommons.org/licenses/by-nc/3.0/) 00006 restricted to non-commercial educational use only., 00007 00008 See also: http://www.VyperLogix.com and http://www.pypi.info for details. 00009 00010 THE AUTHOR VYPER LOGIX CORP DISCLAIMS ALL WARRANTIES WITH REGARD TO 00011 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 00012 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00013 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00014 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00015 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00016 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! 00017 00018 USE AT YOUR OWN RISK. 00019 """ 00020 00021 class ymlAttr(): 00022 def __str__(self): 00023 return '%s=%s' % (self.key, self.value) 00024 00025 class ymlObject(): 00026 def __init__(self,name): 00027 self.name = name 00028 self.attrs = [] 00029 00030 def attrNamed(self,name): 00031 for a in self.attrs: 00032 if (a.key == name): 00033 return a 00034 return None 00035 00036 def attrValueForName(self,name): 00037 _value = '' 00038 try: 00039 a = self.attrNamed(name) 00040 if (a != None): 00041 _value = a.value 00042 except Exception, details: 00043 print 'ERROR in determining the "%s" due to (%s)' % (name,str(details)) 00044 return _value 00045 00046 def attrsForName(self,name): 00047 attrs = [] 00048 for y in self.attrs: 00049 if (y.key == name): 00050 attrs.append(y) 00051 return attrs 00052 00053 def dumpAttrs(self): 00054 s = ''; 00055 i = 1 00056 n = len(self.attrs) 00057 for t in self.attrs: 00058 s += str(t) 00059 if (i < n): 00060 s += ',' 00061 i += 1 00062 return s 00063 00064 def __str__(self): 00065 return 'name=%s, (%s)' % (self.name, self.dumpAttrs()) 00066 00067 def add(self,key,value): 00068 a = ymlAttr() 00069 a.key = key.strip() 00070 a.value = value.strip() 00071 self.attrs.append(a) 00072 00073 def ymlAttrs(self): 00074 return self.attrs 00075 00076 class ymlReader(object): 00077 def __init__(self,fname): 00078 self.fname = fname 00079 self.objects = [] 00080 self.curObj = None 00081 00082 def objectsNamed(self,name): 00083 list = [] 00084 for o in self.objects: 00085 if (o.name == name): 00086 list.append(o) 00087 return list 00088 00089 def pruneToks(self,toks): 00090 n = len(toks) 00091 while (n > 0) and (len(toks[n-1]) == 0): 00092 toks.pop() 00093 n = len(toks) 00094 return toks 00095 00096 def parseLine(self,line): 00097 toks = line.split('#') 00098 return toks[0].strip() 00099 00100 def splitFirst(self,s,delim): 00101 toks = [] 00102 i = s.find(delim) 00103 if (i > -1): 00104 toks.append(s[0:i]) 00105 else: 00106 i = 0 00107 toks.append(s[i+1:len(s)]) 00108 return toks 00109 00110 def read(self): 00111 inObject = False 00112 isError = False 00113 try: 00114 fhand = open(self.fname, 'r') 00115 except Exception, details: 00116 print 'ERROR - (%s)' % str(details) 00117 isError = True 00118 if (isError == False): 00119 try: 00120 for line in fhand: 00121 buf = self.parseLine(line) 00122 toks = self.splitFirst(buf,':') 00123 toks = self.pruneToks(toks) 00124 if (inObject == False): 00125 inObject = (len(toks) == 1) 00126 if ( (inObject) and (self.curObj == None) ): 00127 self.curObj = ymlObject(toks[0]) 00128 else: 00129 if (len(toks) >= 2): 00130 self.curObj.add(toks[0],toks[1]) 00131 #print '[%s] buf=(%s)' % (str(len(buf)),buf) 00132 else: 00133 if (self.curObj != None): 00134 self.objects.append(self.curObj) 00135 self.curObj = None 00136 inObject = False 00137 finally: 00138 if (self.curObj != None): 00139 self.objects.append(self.curObj) 00140 self.curObj = None 00141 fhand.close() 00142 00143 if __name__ == "__main__": 00144 import sys 00145 print >>sys.stdout, __copyright__ 00146 print >>sys.stderr, __copyright__ 00147 00148 import cProfile 00149 import gc 00150 y = ymlReader('../database.yml') 00151 #gc.disable() 00152 #cProfile.run('for i in xrange(1000): y.read()') 00153 y.read() 00154 print str(y) 00155 print str(y.objects) 00156 print '\n' 00157 for y in y.objects: 00158 print str(y) 00159 print '==========' * 2 00160 00161
© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."
Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.
122,241 lines of code and growing...