akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # encoding: utf-8 |
| 3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
| 4 | |
| 5 | import copy,re,os |
| 6 | from waflib import Logs,Utils |
| 7 | re_imp=re.compile('^(#)*?([^#=]*?)\ =\ (.*?)$',re.M) |
| 8 | class ConfigSet(object): |
| 9 | __slots__=('table','parent') |
| 10 | def __init__(self,filename=None): |
| 11 | self.table={} |
| 12 | if filename: |
| 13 | self.load(filename) |
| 14 | def __contains__(self,key): |
| 15 | if key in self.table:return True |
| 16 | try:return self.parent.__contains__(key) |
| 17 | except AttributeError:return False |
| 18 | def keys(self): |
| 19 | keys=set() |
| 20 | cur=self |
| 21 | while cur: |
| 22 | keys.update(cur.table.keys()) |
| 23 | cur=getattr(cur,'parent',None) |
| 24 | keys=list(keys) |
| 25 | keys.sort() |
| 26 | return keys |
| 27 | def __str__(self): |
| 28 | return"\n".join(["%r %r"%(x,self.__getitem__(x))for x in self.keys()]) |
| 29 | def __getitem__(self,key): |
| 30 | try: |
| 31 | while 1: |
| 32 | x=self.table.get(key,None) |
| 33 | if not x is None: |
| 34 | return x |
| 35 | self=self.parent |
| 36 | except AttributeError: |
| 37 | return[] |
| 38 | def __setitem__(self,key,value): |
| 39 | self.table[key]=value |
| 40 | def __delitem__(self,key): |
| 41 | self[key]=[] |
| 42 | def __getattr__(self,name): |
| 43 | if name in self.__slots__: |
| 44 | return object.__getattr__(self,name) |
| 45 | else: |
| 46 | return self[name] |
| 47 | def __setattr__(self,name,value): |
| 48 | if name in self.__slots__: |
| 49 | object.__setattr__(self,name,value) |
| 50 | else: |
| 51 | self[name]=value |
| 52 | def __delattr__(self,name): |
| 53 | if name in self.__slots__: |
| 54 | object.__delattr__(self,name) |
| 55 | else: |
| 56 | del self[name] |
| 57 | def derive(self): |
| 58 | newenv=ConfigSet() |
| 59 | newenv.parent=self |
| 60 | return newenv |
| 61 | def detach(self): |
| 62 | tbl=self.get_merged_dict() |
| 63 | try: |
| 64 | delattr(self,'parent') |
| 65 | except AttributeError: |
| 66 | pass |
| 67 | else: |
| 68 | keys=tbl.keys() |
| 69 | for x in keys: |
| 70 | tbl[x]=copy.deepcopy(tbl[x]) |
| 71 | self.table=tbl |
| 72 | def get_flat(self,key): |
| 73 | s=self[key] |
| 74 | if isinstance(s,str):return s |
| 75 | return' '.join(s) |
| 76 | def _get_list_value_for_modification(self,key): |
| 77 | try: |
| 78 | value=self.table[key] |
| 79 | except KeyError: |
| 80 | try:value=self.parent[key] |
| 81 | except AttributeError:value=[] |
| 82 | if isinstance(value,list): |
| 83 | value=value[:] |
| 84 | else: |
| 85 | value=[value] |
| 86 | else: |
| 87 | if not isinstance(value,list): |
| 88 | value=[value] |
| 89 | self.table[key]=value |
| 90 | return value |
| 91 | def append_value(self,var,val): |
| 92 | current_value=self._get_list_value_for_modification(var) |
| 93 | if isinstance(val,str): |
| 94 | val=[val] |
| 95 | current_value.extend(val) |
| 96 | def prepend_value(self,var,val): |
| 97 | if isinstance(val,str): |
| 98 | val=[val] |
| 99 | self.table[var]=val+self._get_list_value_for_modification(var) |
| 100 | def append_unique(self,var,val): |
| 101 | if isinstance(val,str): |
| 102 | val=[val] |
| 103 | current_value=self._get_list_value_for_modification(var) |
| 104 | for x in val: |
| 105 | if x not in current_value: |
| 106 | current_value.append(x) |
| 107 | def get_merged_dict(self): |
| 108 | table_list=[] |
| 109 | env=self |
| 110 | while 1: |
| 111 | table_list.insert(0,env.table) |
| 112 | try:env=env.parent |
| 113 | except AttributeError:break |
| 114 | merged_table={} |
| 115 | for table in table_list: |
| 116 | merged_table.update(table) |
| 117 | return merged_table |
| 118 | def store(self,filename): |
| 119 | try: |
| 120 | os.makedirs(os.path.split(filename)[0]) |
| 121 | except OSError: |
| 122 | pass |
| 123 | buf=[] |
| 124 | merged_table=self.get_merged_dict() |
| 125 | keys=list(merged_table.keys()) |
| 126 | keys.sort() |
| 127 | try: |
| 128 | fun=ascii |
| 129 | except NameError: |
| 130 | fun=repr |
| 131 | for k in keys: |
| 132 | if k!='undo_stack': |
| 133 | buf.append('%s = %s\n'%(k,fun(merged_table[k]))) |
| 134 | Utils.writef(filename,''.join(buf)) |
| 135 | def load(self,filename): |
| 136 | tbl=self.table |
| 137 | code=Utils.readf(filename,m='rU') |
| 138 | for m in re_imp.finditer(code): |
| 139 | g=m.group |
| 140 | tbl[g(2)]=eval(g(3)) |
| 141 | Logs.debug('env: %s'%str(self.table)) |
| 142 | def update(self,d): |
| 143 | for k,v in d.items(): |
| 144 | self[k]=v |
| 145 | def stash(self): |
| 146 | orig=self.table |
| 147 | tbl=self.table=self.table.copy() |
| 148 | for x in tbl.keys(): |
| 149 | tbl[x]=copy.deepcopy(tbl[x]) |
| 150 | self.undo_stack=self.undo_stack+[orig] |
| 151 | def revert(self): |
| 152 | self.table=self.undo_stack.pop(-1) |