class StringSequence(SequenceSchema): _ = SchemaNode(String())
class ObjTypeSchema(MappingSchema): schema_type = ObjType ''' These are the default schema settings for GnomeId objects. Put them in your schema declaration if an override is desired. ''' # Attr will appear in save files save = True # Attr is updatable through .update update = True # Attr is read only. This supersedes update read_only = False # Attr will be ignored in == tests test_equal = True # Attr references filenames to be added to save files isdatafile = False # Attr is a link to another GnomeId and should be saved as a reference # in save files save_reference = True # (Colander) Set default = drop to skip this attribute if it is None # during serialization default = null # (Colander) If not present in cstruct, ignore. This attribute is NOT # required for correct deserialization. Set missing=required for all # attributes that ARE required for object init. missing = drop # These are the defaults automatically applied to children of this node # if not defined already _colander_defaults = {'save': save, 'update': update, 'read_only': read_only, 'test_equal': test_equal, 'isdatafile': isdatafile, 'missing': missing, 'default': default} # defines the obj_type which is stored by all gnome objects when persisting # to save files # It also optionally stores the 'id' if present id = SchemaNode(String(), save=True, read_only=True) obj_type = SchemaNode(String(), missing=required, read_only=True) name = SchemaNode(String()) def __init__(self, *args, **kwargs): super(ObjTypeSchema, self).__init__(*args, **kwargs) for c in self.children: for k, v in self._colander_defaults.items(): if not hasattr(c, k): setattr(c, k, v) elif (hasattr(c, k) and hasattr(c.__class__, k) and getattr(c, k) is getattr(c.__class__, k)): # things like missing, which by default are 'required' # on the class. If overridden, it will be on the instance # instead setattr(c, k, v) if c.read_only and c.update: c.update = False def serialize(self, appstruct=None, options={}): return self.typ.serialize(self, appstruct, options=options) def deserialize(self, cstruct=None, refs=None): if refs is None: refs = Refs() return self.typ.deserialize(self, cstruct, refs=refs) def _save(self, obj, zipfile_=None, refs=None): ''' Saves the object passed in to a zip file. Note that ths name of this function is '_save' to allow the attribute 'save' to be used to specify if this SchemaNode represents an attribute that should be saved. :param obj: Gnome object to be saved :param zipfile_: an open zipfile.Zipfile object, in append mode :param name: unless specified, uses name of obj :param refs: references dict :returns Processed json representation of the object. When this returns, zipfile_ should be a complete zip savefile of obj, and refs will be a dictionary of all GNOME objects keyed by id. ''' if obj is None: raise ValueError('{}: Cannot save a None' .format(self.__class__.__name__)) if obj._schema is not self.__class__: raise TypeError('A {} cannot save a {}' .format(self.__class__.__name__, obj.__class__.__name__)) if zipfile is None: raise ValueError('Must pass an open zipfile.Zipfile ' 'in append mode to zipfile_') if refs is None: refs = Refs() return self.typ.save(self, obj, zipfile_, refs) def load(self, obj_json, saveloc=None, refs=None): if obj_json is None: raise ValueError('{}: Cannot load a None' .format(self.__class__.__name__)) cls = class_from_objtype(obj_json['obj_type']) if cls._schema is not self.__class__: raise TypeError('A {} cannot save a {}' .format(self.__class__.__name__, cls.__name__)) if zipfile is None: raise ValueError('Must pass an open zipfile.Zipfile ' 'in append mode to saveloc') return self.typ.load(self, obj_json, saveloc, refs) def get_nodes_by_attr(self, attr): ''' Returns a list of child node names that have the specified attr set on them. This replaces the State and Field mechanisms from the old serialization paradigm. Now such attributes are on the schema directly. If attr is 'all' it just returns a list of all child node names ''' if attr == 'all': return [n.name for n in self.children] else: # sequences need to be taken into account. If present they will # considered to always have 'save' and 'update' as true, # read as false, return [n.name for n in self.children if hasattr(n, attr) and getattr(n, attr)] @staticmethod def register_refs(node, subappstruct, refs): if (node.schema_type in (Sequence, OrderedCollectionType) and isinstance(node.children[0], ObjTypeSchema)): [subitem._schema.register_refs(subitem._schema(), subitem, refs) for subitem in subappstruct] if not isinstance(node, ObjTypeSchema) or subappstruct is None: return if subappstruct.id not in refs: refs[subappstruct.id] = subappstruct names = node.get_nodes_by_attr('all') for n in names: subappstruct._schema.register_refs(subappstruct._schema().get(n), getattr(subappstruct, n), refs) @staticmethod def process_subnode(subnode, appstruct, subappstruct, subname, cstruct, subcstruct, refs): if subnode.schema_type is ObjType: if subcstruct is None: return None o = refs.get(subcstruct.get('id', None), None) if o is not None: # existing object, so update using subdict (dict_[name]) # and set dict_[name] to object o.update_from_dict(subcstruct, refs=refs) return o else: # object doesn't exist, so attempt to instantiate through # deserialize, or use existing object if found in refs o = subnode.deserialize(subcstruct, refs=refs) refs[o.id] = o return o elif (subnode.schema_type is Sequence and isinstance(subnode.children[0], ObjTypeSchema)): if subappstruct is not None: del subappstruct[:] else: subappstruct = [] for subitem in subcstruct: subappstruct.append(ObjTypeSchema .process_subnode(subnode.children[0], appstruct, subappstruct, subname, cstruct, subitem, refs)) return subappstruct elif (subnode.schema_type is OrderedCollectionType and isinstance(subnode.children[0], ObjTypeSchema)): if subappstruct is not None: subappstruct.clear() else: raise ValueError('why is this None????') for subitem in subcstruct: subappstruct.add(ObjTypeSchema .process_subnode(subnode.children[0], appstruct, subappstruct, subname, cstruct, subitem, refs)) return subappstruct else: return subnode.deserialize(subcstruct)
class SeriesSchema(MappingSchema): name = SchemaNode(String()) dates = DatesSchema()
class Models(SequenceSchema): model = SchemaNode(String(encoding='utf-8', allow_empty=False), validator=Length(min=2, max=100))
class SimilaritySchema(MappingSchema): base = SchemaNode(String(encoding='utf-8', allow_empty=False), validator=Length(min=100, max=10000)) proba = SchemaNode(String(encoding='utf-8', allow_empty=False), validator=Length(min=100, max=10000))
class FilterPreferencesSchema(MappingSchema): activities = SchemaNode(Sequence(), SchemaNode(String(), validator=OneOf(activities)), missing=required) areas = SchemaNode(Sequence(), SchemaAssociationDoc(), missing=required) followed_only = SchemaNode(Boolean(), missing=required)
class OilSchema(ObjTypeSchema): '''schema for Oil object''' name = SchemaNode( String(), missing=drop, save=True, update=True ) api = SchemaNode( Float(), missing=drop, save=True, update=True ) adios_oil_id = SchemaNode( Int(), missing=drop, save=True, update=True ) pour_point = SchemaNode( Float(), missing=drop, save=True, update=True ) flash_point = SchemaNode( Float(), missing=drop, save=True, update=True ) solubility = SchemaNode( Float(), missing=drop, save=True, update=True ) bullwinkle_fraction = SchemaNode( Float(), missing=drop, save=True, update=True ) bullwinkle_time = SchemaNode( Float(), missing=drop, save=True, update=True ) emulsion_water_fraction_max = SchemaNode( Float(), missing=drop, save=True, update=True ) densities = SchemaNode( Float(), missing=drop, save=True, update=True ) density_ref_temps = SchemaNode( Float(), missing=drop, save=True, update=True ) density_weathering = SchemaNode( Float(), missing=drop, save=True, update=True ) kvis = SchemaNode( Float(), missing=drop, save=True, update=True ) kvis_ref_temps = SchemaNode( Float(), missing=drop, save=True, update=True ) kvis_weathering = SchemaNode( Float(), missing=drop, save=True, update=True ) mass_fraction = SchemaNode( Float(), missing=drop, save=True, update=True ) boiling_point = SchemaNode( Float(), missing=drop, save=True, update=True ) molecular_weight = SchemaNode( Float(), missing=drop, save=True, update=True ) component_density = SchemaNode( Float(), missing=drop, save=True, update=True ) sara_type = SchemaNode( Int(), missing=drop, save=True, update=True ) num_pcs = SchemaNode( Int(), missing=drop, save=True, update=True ) num_components = SchemaNode( Int(), missing=drop, save=True, update=True )
class LoginForm(MappingSchema): user = SchemaNode(String()) password = SchemaNode(String())
class SignupForm(MappingSchema): name = SchemaNode(String(), validator=Length(max=30)) email = SchemaNode(String(), validator=Email())
class PasswordForm(MappingSchema): current = SchemaNode(String()) new = SchemaNode(String(), validator=Length(min=6)) verify = SchemaNode(String())
class SubscribeForm(MappingSchema): url = SchemaNode(String(), validator=Length(min=1))
class FeedSettingsForm(MappingSchema): days_back = SchemaNode(Integer(), validator=Range(1, 7)) date_offset = SchemaNode(Integer(), validator=Range(0, 2)) link_format = SchemaNode(String(), validator=Length(min=6))
class ProfileForm(MappingSchema): email = SchemaNode(String(), validator=Email()) site_news = SchemaNode(Boolean()) password = SchemaNode(String())
class NewsletterSchema(MappingSchema): email = SchemaNode(String(), validator=Email())
class ModelField(MappingSchema): name = SchemaNode(String()) description = SchemaNode(String())
class SearchForm(MappingSchema): search = SchemaNode(String(), validator=Length(min=2))
class ModelDefinition(MappingSchema): title = SchemaNode(String(), location="body") fields = ModelFields(validator=Length(min=1), location="body")
class PasswordResetForm(MappingSchema): email = SchemaNode(String(), validator=Email()) password = SchemaNode(String(), validator=Length(min=6)) verify = SchemaNode(String())
class ModifyNameSchema(MappingSchema): mac_address = SchemaNode(String()) modified_name = SchemaNode(String(), validator=Length(min=1))
class RequestPasswordResetForm(MappingSchema): email = SchemaNode(String(), validator=Email())
class SimpleTextSchema(MappingSchema): text = SchemaNode(String(encoding='utf-8', allow_empty=False), validator=Length(min=2, max=100000))
class Labels(SequenceSchema): label = SchemaNode(String(), missing='')
class SimpleModelSchema(SimpleTextSchema): model = SchemaNode(String(encoding='utf-8', allow_empty=False), # TODO: write validator )
class Formats(SequenceSchema): format = SchemaNode(String(), missing='')
class ClusterizeSchema(SimpleTextSchema): topics = SchemaNode(String(encoding='utf-8', allow_empty=False), validator=Length(min=10, max=10000))
class GraphSchema(ObjTypeSchema): title = SchemaNode(String(), missing=drop, save=True, update=True) points = Points(save=True, update=True) labels = Labels(save=True, update=True) formats = Formats(save=True, update=True)
class CollectionItemMap(MappingSchema): ''' This stores the obj_type and obj_index ''' obj_type = SchemaNode(String()) id = SchemaNode(String(), missing=drop)
class SchemaFromQuerystring(MappingSchema): yeah = SchemaNode(String(), location="querystring", type='str')
class MySchema(MappingSchema): name = SchemaNode(String()) title = SchemaNode(String()) cool = SchemaNode(Boolean(), default=True, missing=True) series = SeriesSchema()
class WifiSchema(MappingSchema): key = SchemaNode(String()) frequency = SchemaNode(Integer(), missing=0) channel = SchemaNode(Integer(), missing=0) signal = SchemaNode(Integer(), missing=0) signalToNoiseRatio = SchemaNode(Integer(), missing=0)