2012-02-12 11:50:57 +00:00
|
|
|
"""
|
|
|
|
"""
|
2012-02-26 15:50:07 +00:00
|
|
|
from . import utility
|
|
|
|
|
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-03-18 10:08:36 +00:00
|
|
|
self.in_mixin = False
|
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__' : {},
|
2012-02-26 13:29:22 +00:00
|
|
|
'__blocks__': [],
|
|
|
|
'__names__': [],
|
2012-02-12 11:50:57 +00:00
|
|
|
'__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
|
|
|
|
2012-02-25 18:28:05 +00:00
|
|
|
@property
|
|
|
|
def scopename(self):
|
2012-02-26 17:04:50 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
return [r['__current__']
|
|
|
|
for r in self
|
|
|
|
if r['__current__']]
|
|
|
|
|
2012-02-25 18:28:05 +00:00
|
|
|
|
2012-02-12 12:11:15 +00:00
|
|
|
def add_block(self, block):
|
|
|
|
"""
|
|
|
|
"""
|
2012-02-26 13:29:22 +00:00
|
|
|
self[-1]['__blocks__'].append(block)
|
2012-03-01 16:23:22 +00:00
|
|
|
self[-1]['__names__'].append(block.raw())
|
2012-02-12 12:11:15 +00:00
|
|
|
|
|
|
|
def add_mixin(self, mixin):
|
|
|
|
"""
|
|
|
|
"""
|
2012-03-18 13:21:47 +00:00
|
|
|
self._mixins[mixin.name.raw()] = 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-03-18 13:39:57 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
m = self._smixins(name)
|
|
|
|
if m: return m
|
|
|
|
return self._smixins(name.replace('?>?', ' '))
|
|
|
|
|
|
|
|
def _smixins(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-25 18:28:05 +00:00
|
|
|
|
|
|
|
def blocks(self, name):
|
2012-03-18 13:30:33 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
b = self._blocks(name)
|
|
|
|
if b: return b
|
|
|
|
return self._blocks(name.replace('?>?', ' '))
|
|
|
|
|
|
|
|
def _blocks(self, name):
|
2012-02-25 18:28:05 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
i = len(self)
|
|
|
|
while i >= 0:
|
|
|
|
i -= 1
|
2012-02-26 13:29:22 +00:00
|
|
|
if name in self[i]['__names__']:
|
|
|
|
for b in self[i]['__blocks__']:
|
2012-03-01 16:23:22 +00:00
|
|
|
if b.raw() == name:
|
2012-02-26 13:29:22 +00:00
|
|
|
return b
|
2012-02-28 20:49:26 +00:00
|
|
|
else:
|
|
|
|
for b in self[i]['__blocks__']:
|
2012-03-01 16:23:22 +00:00
|
|
|
if name.startswith(b.raw()):
|
2012-02-28 20:49:26 +00:00
|
|
|
b = utility.blocksearch(b, name)
|
|
|
|
if b: return b
|
2012-02-25 18:28:05 +00:00
|
|
|
return False
|
2012-02-12 12:11:15 +00:00
|
|
|
|
2012-03-03 19:58:56 +00:00
|
|
|
def update(self, scope, at=0):
|
2012-02-12 12:11:15 +00:00
|
|
|
"""
|
|
|
|
"""
|
2012-03-03 19:58:56 +00:00
|
|
|
if hasattr(scope, '_mixins') and not at:
|
|
|
|
self._mixins.update(scope._mixins)
|
|
|
|
self[at]['__variables__'].update(scope[at]['__variables__'])
|
|
|
|
self[at]['__blocks__'].extend(scope[at]['__blocks__'])
|
|
|
|
self[at]['__names__'].extend(scope[at]['__names__'])
|
2012-02-26 15:50:07 +00:00
|
|
|
|
|
|
|
def swap(self, name):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if name.startswith('@@'):
|
|
|
|
var = self.variables(name[1:])
|
|
|
|
if var is False: raise SyntaxError('Unknown variable %s' % name)
|
|
|
|
name = '@' + utility.destring(var.value[0])
|
|
|
|
var = self.variables(name)
|
|
|
|
if var is False: raise SyntaxError('Unknown variable %s' % name)
|
|
|
|
return var.value
|
2012-02-12 12:11:15 +00:00
|
|
|
|