def interpret(self, context): listType = self.source.check(context) if not isinstance(listType, IterableType): raise InternalError("Illegal source type: " + listType.typeName) itemType = listType.itemType items = self.source.interpret(context) if items is None: raise NullReferenceError() if not isinstance(items, IFilterable): raise InternalError("Illegal fetch source: " + str(items)) arrow = self.predicate.toArrowExpression() xfilter = arrow.getFilter(context, itemType) return items.filter(xfilter)
def interpret(self, context): resContext = context if isinstance( context, ResourceContext) else context.newResourceContext() o = self.resource.interpret(resContext) if o is None: raise NullReferenceError() if not isinstance(o, IResource): raise InternalError("Illegal write source: " + o) if not o.isWritable(): raise InvalidResourceError("Not writable") text = self.content.interpret(resContext) try: if context is resContext: o.writeLine(text) elif self.thenWith is not None: def callback(text): local = context.newChildContext() local.registerValue( Variable(self.thenWith.name, TextType.instance)) local.setValue(self.thenWith.name, TextValue(text)) self.thenWith.statements.interpret(local) o.writeFully(text, callback) else: o.writeFully(text) return None finally: if context is not resContext: o.close()
def getValue(self, context): from prompto.runtime.Context import BuiltInContext while context is not None: if isinstance(context, BuiltInContext): return context.value context = context.getParentContext() raise InternalError("Could not locate context for built-in value!")
def loadSingleton(self, typ): if self is self.globals: value = self.values.get(typ.typeName, None) if value is None: from prompto.declaration.ConcreteCategoryDeclaration import ConcreteCategoryDeclaration decl = self.declarations.get(typ.typeName, None) if not isinstance(decl, ConcreteCategoryDeclaration): raise InternalError("No such singleton:" + typ.typeName) value = ConcreteInstance(self, decl) value.mutable = True # a singleton is protected by "with x do", so always mutable in that context self.values[typ.typeName] = value if isinstance(value, ConcreteInstance): return value else: raise InternalError("Not a concrete instance:" + str(value)) else: return self.globals.loadSingleton(typ)
def getItem(self, context, item): if isinstance(item, TextValue): value = self.value.get(item.value, NullValue.instance) if isinstance(value, IValue): return value else: raise InternalError("Item not a value!") else: raise SyntaxError("No such item:" + str(item))
def getIterable(self, context, src): if isinstance(src, IIterable): return src.getIterator(context) elif isinstance(src, list): return src elif isinstance(src, Iterator): return src else: raise InternalError("Should never get there!")
def filter(self, context, itemName, filter): result = TupleValue() for o in self.getIterator(context): context.setValue(itemName, o) test = filter.interpret(context) from prompto.value.BooleanValue import BooleanValue if not isinstance(test, BooleanValue): raise InternalError("Illegal test result: " + test) if test.getValue(): result.append(o) return result
def interpret(self, context): if not context.isWithResourceContext(): raise SyntaxError("Not a resource context!") o = self.resource.interpret(context) if o is None: raise NullReferenceError() if not isinstance(o, IResource): raise InternalError("Illegal read source: " + o) if not o.isReadable(): raise InvalidResourceError("Not readable") return o.readLine()
def getItem(self, context, index): if isinstance(index, IntegerValue): try: value = self.computeItem(index.IntegerValue()) if isinstance(value, IValue): return value else: raise InternalError("Item not a value!") except IndexError: raise IndexOutOfRangeError() else: raise SyntaxError("No such item:" + str(index))
def interpret(self, context): context = context.newResourceContext() o = self.resource.interpret(context) if o is None: raise NullReferenceError() if not isinstance(o, IResource): raise InternalError("Illegal read source: " + o) if not o.isReadable(): raise InvalidResourceError("Not readable") try: return BlobValue(o.readBinary()) finally: o.close()
def interpret(self, context): itype = self.source.check(context) if not isinstance(itype, (ListType, TupleType, SetType)): raise SyntaxError("Unsupported type: " + itype) o = self.source.interpret(context) if o is None: raise NullReferenceError() if not isinstance(o, (ListValue, TupleValue, SetValue)): raise InternalError("Unexpected type:" + type(o).__name__) items = o.getIterator(context) getter = itype.itemType.getSortKeyReader(context, self.key) list = sorted(items, key=getter, reverse=self.desc) return ListValue(itype.itemType, items=list)
def loadSingleton(self, typ): if self is self.globals: value = self.values.get(typ.typeName, None) if value is None: from prompto.declaration.ConcreteCategoryDeclaration import ConcreteCategoryDeclaration decl = self.declarations.get(typ.typeName, None) if not isinstance(decl, ConcreteCategoryDeclaration): raise InternalError("No such singleton:" + typ.typeName) value = ConcreteInstance(self, decl) value.mutable = True # a singleton is protected by "with x do", so always mutable in that context method = decl.getInitializeMethod(self) if method is not None: instance = self.newInstanceContext(value, False) child = instance.newChildContext() method.interpret(child) self.values[typ.typeName] = value if isinstance(value, ConcreteInstance): return value else: raise InternalError("Not a concrete instance:" + str(value)) else: return self.globals.loadSingleton(typ)
def interpret(self, context): resContext = context if isinstance( context, ResourceContext) else context.newResourceContext() o = self.resource.interpret(resContext) if o is None: raise NullReferenceError() if not isinstance(o, IResource): raise InternalError("Illegal write source: " + o) if not o.isWritable(): raise InvalidResourceError("Not writable") text = self.content.interpret(resContext) try: if context is resContext: o.writeLine(text) else: o.writeFully(text) return None finally: if context is not resContext: o.close()
def doConvertPythonValueToPromptoValue(self, context, value, klass, returnType): if value is None: return NullValue.instance else: res = self.convertIValue(value) if res is not None: return res else: res = self.convertNative(context, value, klass, returnType) if res is not None: return res else: res = self.convertDocument(context, value, klass, returnType) if res is not None: return res else: res = self.convertList(context, value, klass, returnType) if res is not None: return res else: res = self.convertSet(context, value, klass, returnType) if res is not None: return res else: res = self.convertDict(context, value, klass, returnType) if res is not None: return res else: res = self.convertIterator(context, value, klass, returnType) if res is not None: return res else: res = self.convertCategory(context, value, klass, returnType) if res is not None: return res elif returnType is AnyType.instance: from prompto.value.NativeInstance import NativeInstance return NativeInstance(AnyNativeCategoryDeclaration.instance, value) else: raise InternalError("Unable to convert:" + type(value).__name__)
def getIterator(self, context, src): if getattr(src, "getIterator", None) is None: raise InternalError("Should never get there!") else: return src.getIterator(context)
def interpret(self, context): from prompto.error.InternalError import InternalError raise InternalError("Should never get there!")