def convertElement(self, parent: DocumentValue, element: minidom.Element): tagName = element.tagName # cater for repeated elements if parent.hasMember(tagName): self.convertListElement(parent, tagName, element) else: parent.setMember(None, tagName, self.convertElementValue(element))
def toDocumentValue(self, context): from prompto.value.DocumentValue import DocumentValue doc = DocumentValue() for key in self.values.keys(): value = self.values[key].toDocumentValue(context) doc.setMember(context, key, value) return doc
def readJSONValue(self, context, node, parts): from prompto.value.DocumentValue import DocumentValue instance = DocumentValue() for key, node in node.items(): value = self.readJSONField(context, node, parts) instance.setMember(context, key, value) return instance
def interpret(self, context): if len(self.entries) > 0: self.check(context) doc = DocumentValue() for e in self.entries: key = e.getKey().interpret(context) val = e.getValue().interpret(context) doc.setMember(context, key.value, val) return doc else: return self.value
def convertListElement(self, parent: DocumentValue, tagName: str, element: minidom.Element): list = None current = parent.getMemberValue(None, tagName, False) if isinstance(current, ListValue): list = current else: list = ListValue(AnyType.instance) list.add(current) parent.setMember(None, tagName, list) list.add(self.convertElementValue(element))
def convertDocument(self, context, value, klass, returnType): if isinstance(returnType, DocumentType) and isinstance(value, dict): doc = DocumentValue() for key, val in value.iteritems(): klass = type(val) itype = self.pythonToPromptoMap.get(klass.__name__, DocumentType.instance) if itype is not None: ival = itype.convertPythonValueToPromptoValue(context, val, None) else: ival = self.doConvertPythonValueToPromptoValue(context, val, klass, itype) doc.setMember(context, str(key), ival) return doc else: return None
def getGlobalMethodSortKeyReader(self, context, name): from prompto.statement.MethodCall import MethodCall from prompto.value.DocumentValue import DocumentValue exp = ValueExpression(self, DocumentValue()) arg = Argument(None, exp) args = ArgumentList(items=[arg]) from prompto.expression.MethodSelector import MethodSelector call = MethodCall(MethodSelector(name), args) def keyGetter(o): argument = call.arguments[0] argument.setExpression(ValueExpression(self, o)) return call.interpret(context) return keyGetter
def convertElementValue(self, element): hasAttributes = self.keepAttributes and element.hasAttributes() hasChildren = self.elementHasChildren(element) if hasAttributes or hasChildren: result = DocumentValue() if self.keepAttributes: for attr in element.attributes: result.setMember(None, "@" + attr.name, TextValue(attr.value)) if hasChildren: for node in element.childNodes: if isinstance(node, minidom.Element): self.convertElement(result, node) else: result.setMember(None, "$value", element.firstChild.nodeValue) return result else: return TextValue(element.firstChild.nodeValue)
def convertDocument(obj): values = {} for k, v in obj.items(): values[k] = convert(v) return DocumentValue(values=values)
def interpret(self, context): if self.source is None: return DocumentValue() else: value = self.source.interpret(context) return self.documentFromValue(context, value)
def convertDocument(self, doc): result = DocumentValue() self.convertElement(result, doc.documentElement) return result
def toDocument(node): values = {} for k, v in node.items(): values[k] = toValue(v) return DocumentValue(values=values)
def __init__(self, entries=None): if entries is None: entries = DocEntryList() super().__init__("{}", DocumentValue()) self.entries = entries