70 lines
1.6 KiB
Python
Raw Normal View History

2012-02-12 11:50:57 +00:00
"""
"""
class Scope(list):
2012-02-12 13:00:08 +00:00
def __init__(self, init=False):
2012-02-12 11:50:57 +00:00
super().__init__()
2012-02-12 12:11:15 +00:00
self._mixins = {}
2012-02-12 13:00:08 +00:00
if init: self.push()
2012-02-12 11:50:57 +00:00
def push(self):
2012-02-12 13:00:08 +00:00
"""
"""
2012-02-12 11:50:57 +00:00
self.append({
'__variables__' : {},
'__blocks__': [],
'__current__': None
})
2012-02-12 12:00:56 +00:00
@property
2012-02-12 11:50:57 +00:00
def current(self):
return self[-1]['__current__']
2012-02-12 12:00:56 +00:00
@current.setter
def current(self, value):
self[-1]['__current__'] = value
2012-02-12 12:11:15 +00:00
def add_block(self, block):
"""
"""
self[-1]['__blocks__'].append(block)
def add_mixin(self, mixin):
"""
"""
self._mixins[mixin.name()] = mixin
2012-02-12 13:00:08 +00:00
def add_variable(self, variable):
"""
"""
2012-02-25 17:08:08 +00:00
self[-1]['__variables__'][variable.name] = variable
2012-02-12 13:00:08 +00:00
def variables(self, name):
"""
"""
i = len(self)
while i >= 0:
i -= 1
if name in self[i]['__variables__']:
return self[i]['__variables__'][name]
return False
2012-02-12 12:00:56 +00:00
2012-02-12 12:11:15 +00:00
def mixins(self, name):
2012-02-12 11:50:57 +00:00
"""
"""
2012-02-12 12:11:15 +00:00
return (self._mixins[name]
if name in self._mixins
else False)
2012-02-12 12:00:56 +00:00
def in_mixin(self):
"""
"""
return any([s for s in self
2012-02-12 12:11:15 +00:00
if s['__current__'] == '__mixin__'])
def update(self, scope):
"""
"""
self._mixins.update(scope._mixins)
2012-02-12 13:00:08 +00:00
self[0]['__variables__'].update(scope[0]['__variables__'])
self[0]['__blocks__'].extend([b for b in scope[0]['__blocks__']])
2012-02-12 12:11:15 +00:00