Forbid most operations for Context instances

This commit is contained in:
ZyX 2014-09-19 22:36:16 +04:00
parent 891a9cbaa5
commit b333f9ec11
1 changed files with 24 additions and 14 deletions

View File

@ -38,26 +38,36 @@ def list_themes(data, context):
class Context(tuple):
def __new__(cls, base, other=None):
if other is not None:
return tuple.__new__(cls, tuple.__add__(base, other))
else:
return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),))
for func in dir(tuple):
if func in ('__getitem__', '__init__', '__getattribute__', '__len__', '__iter__'):
continue
exec((
'def {0}(self, *args, **kwargs):\n'
' raise TypeError("{0} is not allowed for Context")'
).format(func))
del func
def __add__(self, arg):
assert(len(arg) == 1)
assert(type(arg) is tuple)
assert(len(arg[0]) == 2)
assert(type(arg[0]) is tuple)
havemarks(arg[0][0], arg[0][1])
return Context.__new__(Context, self, arg)
__slots__ = ()
def __new__(cls, base, context_key=None, context_value=None):
if context_key is not None:
assert(context_value is not None)
assert(type(base) is Context)
havemarks(context_key, context_value)
return tuple.__new__(cls, tuple.__add__(base, ((context_key, context_value),)))
else:
havemarks(base)
return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),))
@property
def key(self):
return key_sep.join((c[0] for c in self))
def enter_key(self, value, key):
return self + ((value.keydict[key], value[key]),)
return self.enter(value.keydict[key], value[key])
def enter_item(self, name, item):
return self + ((MarkedUnicode(name, item.mark), item),)
return self.enter(MarkedUnicode(name, item.mark), item)
def enter(self, context_key, context_value):
return Context.__new__(Context, self, context_key, context_value)