def append(self, name, value, reverse_name = None, validate = True): if multi_is_instance(value, AbstractNode): if reverse_name is None: reverse_name = is_of(name) self.add_link(name, value, reverse_name) else: raise TypeError("Trying to append to property '%s' of %s: '%s', but it is not of a supported type (literal or object node): %s" % (name, self, value, type(value).__name__)) # update the cache of valid classes if validate: self.update_valid_classes()
def append(self, name, value, reverse_name=None, validate=True): if multi_is_instance(value, AbstractNode): if reverse_name is None: reverse_name = is_of(name) self.add_link(name, value, reverse_name) else: raise TypeError( "Trying to append to property '%s' of %s: '%s', but it is not of a supported type (literal or object node): %s" % (name, self, value, type(value).__name__)) # update the cache of valid classes if validate: self.update_valid_classes()
def invalid_properties(self, cls): invalid = [] for prop in cls.valid: if prop not in self: invalid.append("property '%s' is missing" % prop) continue # FIXME: link type checking doesn't work if isinstance(getattr(self, prop), collections.Iterator): continue if not multi_is_instance(getattr(self, prop), cls.schema[prop]): invalid.append("property '%s' is of type '%s', but should be of type '%s'" % (prop, type(getattr(self, prop)).__name__, cls.schema[prop].__name__)) return '\n'.join(invalid)
def to_string(self, cls=None, default=None, recurseLimit=2, fancyIndent=False): # TODO: smarter stringize that guesses the class, should it always be there? cls = self.virtual_class() if cls is None: log.error('FIXME: remove me') # most likely called from a node, but anyway we can't infer anything on the links so just display # them as anonymous ObjectNodes cls = self.__class__ props = [(prop, [cls.__name__] * len(tolist(value))) if multi_is_instance(value, ObjectNode) else (prop, unicode(value)) for prop, value in self.items()] else: props = [] for prop, value in self.items(): # only print explicitly defined properties #if prop not in cls.schema: if prop in cls.schema._implicit: continue elif isinstance(value, collections.Iterator): if recurseLimit: # TODO: if prop is ONE_TO_ONE, do not do a join props.append((prop, '[' + ', '.join([ v.to_string(cls=cls.schema.get(prop) or default, recurseLimit=recurseLimit - 1, fancyIndent=fancyIndent) for v in value ]) + ']')) else: props.append((prop, '[...]')) else: props.append((prop, unicode(value))) # could be toJson() instead of simply fancyIndent if fancyIndent: result = '%s {\n' % cls.__name__ for k, v in props: indented = '\n '.join(v.split('\n')) result += ' %s: %s\n' % (k, indented) return result + '}' else: return '%s(%s)' % (cls.__name__, ', '.join( ['%s=%s' % (k, v) for k, v in props]))
def invalid_properties(self, cls): invalid = [] for prop in cls.valid: if prop not in self: invalid.append("property '%s' is missing" % prop) continue # FIXME: link type checking doesn't work if isinstance(getattr(self, prop), collections.Iterator): continue if not multi_is_instance(getattr(self, prop), cls.schema[prop]): invalid.append( "property '%s' is of type '%s', but should be of type '%s'" % (prop, type(getattr( self, prop)).__name__, cls.schema[prop].__name__)) return '\n'.join(invalid)
def to_string(self, cls = None, default = None, recurseLimit = 2, fancyIndent = False): # TODO: smarter stringize that guesses the class, should it always be there? cls = self.virtual_class() if cls is None: log.error('FIXME: remove me') # most likely called from a node, but anyway we can't infer anything on the links so just display # them as anonymous ObjectNodes cls = self.__class__ props = [ (prop, [ cls.__name__ ] * len(tolist(value))) if multi_is_instance(value, ObjectNode) else (prop, unicode(value)) for prop, value in self.items() ] else: props = [] for prop, value in self.items(): # only print explicitly defined properties #if prop not in cls.schema: if prop in cls.schema._implicit: continue elif isinstance(value, collections.Iterator): if recurseLimit: # TODO: if prop is ONE_TO_ONE, do not do a join props.append((prop, '['+', '.join([v.to_string(cls=cls.schema.get(prop) or default, recurseLimit=recurseLimit-1, fancyIndent=fancyIndent) for v in value ])+']')) else: props.append((prop, '[...]')) else: props.append((prop, unicode(value))) # could be toJson() instead of simply fancyIndent if fancyIndent: result = '%s {\n' % cls.__name__ for k, v in props: indented = '\n '.join(v.split('\n')) result += ' %s: %s\n' % (k, indented) return result + '}' else: return '%s(%s)' % (cls.__name__, ', '.join([ '%s=%s' % (k, v) for k, v in props ]))