def schema(**kwargs): """ Create a schema. Mostly useful for creating single-use schemas on-the-fly. """ items = list(kwargs.items()) if len(items) != 1 or not isinstance(items[0][1], dict): raise RuntimeError('schema required 1 keyword argument of type dict') name, spec = items[0] schema_dict = {} for key, value in spec.items(): cls, description = value if isinstance(value, tuple) else (value, None) required = key.endswith('*') key = key.rstrip('*') kwargs = {'required': required, 'description': description} if isinstance(cls, SchemaMeta): schema_dict[key] = Nested(cls, required=required) elif isinstance(cls, list) and len(cls) == 1: cls = cls[0] schema_dict[key] = List(Nested(cls), **kwargs) if isinstance( cls, SchemaMeta) else List(cls, **kwargs) else: schema_dict[key] = cls.__call__(**kwargs) if callable(cls) else cls return type(name, (Schema, ), schema_dict)
class InstitutionsMixin: relatedID = Nested(RelatedIDSchema) aliases = List(SanitizedUnicode()) ico = SanitizedUnicode() url = Url() provider = Boolean(missing=False) formerNames = List(SanitizedUnicode())
class CatalogQueryJob(Job): type = 'catalog_query' description = 'Catalog Query' result: CatalogQueryJobResult = Nested(CatalogQueryJobResult, default={}) catalogs: TList[str] = List(String(), default=[]) ra_hours: float = Float() dec_degs: float = Float() radius_arcmins: float = Float() width_arcmins: float = Float() height_arcmins: float = Float() file_ids: TList[int] = List(Integer()) constraints: TDict[str, str] = Dict(keys=String, values=String) source_ids: TList[str] = List(String()) def run(self): self.result.data = run_catalog_query_job( self, catalogs=self.catalogs, ra_hours=getattr(self, 'ra_hours', None), dec_degs=getattr(self, 'dec_degs', None), radius_arcmins=getattr(self, 'radius_arcmins', None), width_arcmins=getattr(self, 'width_arcmins', None), height_arcmins=getattr(self, 'height_arcmins', None), file_ids=getattr(self, 'file_ids', None), constraints=getattr(self, 'constraints', None), source_ids=getattr(self, 'source_ids', None))
class SchemaTypeSchema(Schema): """ A wrapper type containing fields that can describe a Type, as well as its tag metadata. """ # When this type is a struct, each of its fields and the type of that field is included # Exclusive with is_a and options fields = Dict(dump_to="fields", load_from="fields", allow_none=False) # Set when this is type is an alias or simply an type expression (such as a generic). # Exclusive with having values for options or fields. is_a = SchemaTypeStructField(dump_to="is_a", load_from="is_a", allow_none=True) # The name and module information of this type's definition. named = SchemaReferenceField(dump_to="named", load_from="named", allow_none=False) # When this type is an enum includes the string values for each enum entry. Note that each # target language may have different rules for the enum constant names, but these entries are # canonical resident values. # Exclusive with is_a and fields. options = List(String(), dump_to="options", load_from="options", allow_none=False) # General metadata given to a type. Currently, atleast_one for Repeated types is supported. # Custom codegen can use these tags to implement semantic types on top of simple logic types. # In general, however, tags are considred optional and should not be required to # deserialize / serializeconsume correct logical values. tags = List(String(), dump_to="tags", load_from="tags", allow_none=False)
class JobResult(AfterglowSchema): """ Base class for job results Attributes:: errors: list of errors warnings: list of warning messages issued by :meth:`Job.run` The job plugin class usually subclasses :class:`JobResult` to define custom result fields in addition to the above: class MyJobResult(JobResult): value1 = fields.Integer() value2 = fields.Float() """ errors: TList[Dict[str, Union[str, int, float, bool]]] = List(Nested(AfterglowErrorSchema)) warnings: TList[str] = List(String()) def __init__(self, *args, **kwargs): """ Create job state structure :param args: see :class:`afterglow_core.schemas.AfterglowSchema` :param kwargs: --//-- """ super().__init__(*args, **kwargs) if not hasattr(self, 'errors'): self.errors = [] if not hasattr(self, 'warnings'): self.warnings = []
class PolygonSchema(BaseSchema): type = Str(required=True, validate=OneOf([POLYGON], error='Invalid polygon type')) coordinates = List( List(Tuple([lon, lat], required=True), required=True), required=True, )
class PixelOpsJobSchema(JobSchema): type = 'pixel_ops' result: PixelOpsJobResultSchema = Nested(PixelOpsJobResultSchema) file_ids: TList[int] = List(Integer(), default=[]) op: str = String(default=None) inplace: bool = Boolean(default=False) aux_file_ids: TList[int] = List(Integer(), default=[])
class LinkSchema(Schema, DatedSchema): id = String(dump_only=True) collection_id = Integer(dump_only=True, required=True) name = String(validate=Length(min=2, max=500), required=True) foreign_ids = List(String()) countries = List(Country()) schema = SchemaName() schemata = List(SchemaName())
class PolygonSchema(BaseSchema): type = String(required=True) coordinates = List(List( List(Float, validate=validate.Length(max=2, min=2)), validate=validate_polygon_coordinates_linestring, ), required=True)
class ContactInfoSchema(Schema): emails = List(String()) phone_numbers = List(String()) @post_load def to_model(self, data: dict) -> ContactInfo: return ContactInfo(emails=data.get('emails', []), phone_numbers=data.get('phone_numbers', []))
class PhotometryJobSchema(JobSchema): type = 'photometry' result: PhotometryJobResultSchema = Nested( PhotometryJobResultSchema, default={}) file_ids: TList[int] = List(Integer(), default=[]) sources: TList[SourceExtractionDataSchema] = List( Nested(SourceExtractionDataSchema), default=[]) settings: PhotSettingsSchema = Nested(PhotSettingsSchema, default={})
class OverlaySchema(Schema): id = String() my_peer = String() global_time = Integer() peers = List(String) overlay_name = String() max_peers = Integer() strategies = List(Nested(OverlayStrategySchema)) statistics = Nested(OverlayStatisticsSchema)
class MultiLineStringSchema(BaseSchema): type = Str(required=True, validate=OneOf([MULTI_LINE_STRING], error='Invalid multi line string string type')) coordinates = List( List(Tuple([lon, lat], required=True), required=True), required=True, )
class TransactionTrackRequest(SPSchema): """Validator for GET /transactions requests""" # Serialization fields. hashes = List(BytesField(), required=True, load_only=True) # Deserialization fields. missing = List(BytesField(), missing=[], dump_only=True) found = List(BytesField(), missing=[], dump_only=True)
class DataProviderSchema(Resource): """ Data provider plugin schema Fields: id: unique integer ID of the data provider; assigned automatically on initialization name: unique data provider name; can be used by the clients in requests like GET /data-providers/[id]/assets in place of the integer data provider ID auth_methods: list of data provider-specific authentication methods; if None, defaults to DEFAULT_DATA_PROVIDER_AUTH -> DATA_FILE_AUTH -> all auth methods available icon: optional data provider icon name display_name: data provider plugin visible in the Afterglow UI description: a longer description of the data provider columns: list of dictionary {name: string, field_name: string, sortable: boolean} sort_by: string - name of column to use for initial sort sort_asc: boolean - initial sort order should be ascending browseable: True if the data provider supports browsing (i.e. getting child assets of a collection asset at the given path); automatically set depending on whether the provider implements get_child_assets() searchable: True if the data provider supports searching (i.e. querying using the custom search keywords defined by `search_fields`); automatically set depending on whether the provider implements find_assets() search_fields: dictionary {field_name: {"label": label, "type": type, ...}, ...} containing names and descriptions of search fields used on the client side to create search forms readonly: True if the data provider assets cannot be modified (created, updated, or deleted); automatically set depending on whether the provider implements create_asset(), update_asset(), or delete_asset() quota: data provider storage quota, in bytes, if applicable usage: current usage of the data provider storage, in bytes, if applicable """ __polymorphic_on__ = 'name' __get_view__ = 'data_providers' id = Integer(default=None) name = String(default=None) auth_methods = List(String(), default=None) display_name = String(default=None) icon = String(default=None) description = String(default=None) columns = List(Dict(), default=[]) sort_by = String(default=None) sort_asc = Boolean(default=True) browseable = Boolean(default=False) searchable = Boolean(default=False) search_fields = Dict(default={}) readonly = Boolean(default=True) quota = Integer(default=None) usage = Integer(default=None)
class PolygonSchema(Schema): """GeoJSON Polygon schema. See https://tools.ietf.org/html/rfc7946#section-3.1.6 """ coordinates = List(List(List(Float)), required=True, validate=GeometryValidator(Polygon)) type = Constant('Polygon')
class MultiPointSchema(Schema): """GeoJSON MultiPoint schema. See https://tools.ietf.org/html/rfc7946#section-3.1.3 """ coordinates = List(List(Float), required=True, validate=GeometryValidator(MultiPoint)) type = Constant('MultiPoint')
class PaperSchema(mm.Schema): is_in_final_state = Boolean() contribution = Nested(ContributionSchema) event = Nested(PaperEventSchema) revisions = List(Nested(PaperRevisionSchema)) last_revision = Nested(PaperRevisionSchema) state = Nested(PaperRevisionStateSchema) rating_range = List(Integer(), attribute='cfp.rating_range') can_judge = Function(lambda paper, ctx: paper.can_judge(ctx.get('user'))) can_comment = Function(lambda paper, ctx: paper.can_comment(ctx.get('user'))) can_review = Function(lambda paper, ctx: paper.can_review(ctx.get('user')))
class ResellerCreateSchema(Schema): name = Str(required=True, error_messages=SCHEMA_MESSAGE_REQUIRED) last_name = Str(required=True, error_messages=SCHEMA_MESSAGE_REQUIRED) cpf = Str(required=True, validate=Length(max=11), error_messages=SCHEMA_MESSAGE_REQUIRED) email = Email(required=True, error_messages=SCHEMA_MESSAGE_REQUIRED) password = Str(required=True, error_messages=SCHEMA_MESSAGE_REQUIRED) active = Boolean(required=True, error_messages=SCHEMA_MESSAGE_REQUIRED) roles = List(Str(maxlenght=100), error_messages=SCHEMA_MESSAGE_REQUIRED) purchases = List(Nested(ResellerPurchaseCreateSchema))
class ExperimentResultDataSchema(BaseSchema): """Schema for ExperimentResultData.""" counts = Nested(ObjSchema, validate=PatternProperties( {Regexp('^0x([0-9A-Fa-f])+$'): Integer()})) snapshots = Nested(ObjSchema) memory = List(Raw(), validate=Length(min=1)) statevector = List(Complex(), validate=Length(min=1)) unitary = List(List(Complex(), validate=Length(min=1)), validate=Length(min=1))
class OverlaySchema(Schema): id = String() my_peer = String() global_time = Integer() peers = List(Nested(AddressWithPK)) overlay_name = String() max_peers = Integer() is_isolated = Boolean() my_estimated_wan = Nested(Address) my_estimated_lan = Nested(Address) strategies = List(Nested(OverlayStrategySchema)) statistics = Nested(OverlayStatisticsSchema)
class CVESchema(Schema): id = String(required=True) published = ParsedDateTime(allow_none=True) description = String(allow_none=True) ubuntu_description = String(allow_none=True) notes = List(Nested(Note)) priority = String(allow_none=True) status = String(allow_none=True) cvss3 = Float(allow_none=True) packages = List(Nested(CvePackage)) references = List(String()) bugs = List(String())
class AlignmentJobSchema(JobSchema): type = 'alignment' result: AlignmentJobResultSchema = Nested(AlignmentJobResultSchema, default={}) file_ids: TList[int] = List(Integer(), default=[]) settings: AlignmentSettingsSchema = Nested(AlignmentSettingsSchema, default={}) sources: TList[SourceExtractionDataSchema] = List( Nested(SourceExtractionDataSchema), default=[]) inplace: bool = Boolean(default=False) crop: bool = Boolean(default=False)
class CollectionCreateSchema(Schema): label = String(validate=Length(min=2, max=500), required=True) foreign_id = String(missing=None) casefile = Boolean(missing=None) summary = String(allow_none=True, missing=None) publisher = String(allow_none=True, missing=None) publisher_url = Url(allow_none=True, missing=None) data_url = Url(allow_none=True, missing=None) info_url = Url(allow_none=True, missing=None) countries = List(Country()) languages = List(Language()) category = Category(missing=None)
class MovieSchema(Schema): id = Str() title = Str() description = Str() imdb_rating = Str() writers = Nested(ShortSchema, many=True) actors = Nested(ShortSchema, many=True) genre = List(Str()) director = List(Str()) class Meta: ordered = True
class JobResultSchema(AfterglowSchema): """ Base class for job result schemas Fields:: errors: list of error messages warnings: list of warnings issued by :meth:`Job.run` """ errors: TList[Dict[str, Union[str, int, float, bool]]] = List(Nested(AfterglowErrorSchema), default=[]) warnings: TList[str] = List(String(), default=[])
class Message(Schema): message_id = Integer() _from = Nested(User, required=False, data_key='from', attribute='from') date = Timestamp(format='timestamp') chat = Nested(Chat) forward_from = Nested(User, required=False) forward_from_chat = Nested(Chat, required=False) forward_from_message_id = Integer(required=False) forward_signature = String(required=False) forward_sender_name = String(required=False) forward_date = Timestamp(required=False, format='timestamp') reply_to_message = Nested(lambda: Message(exclude=('reply_to_message', ))) edit_date = Timestamp(required=False, format='timestamp') media_group_id = String(required=False) author_signature = String(required=False) text = String(required=False) entities = List(Nested(MessageEntity, required=False)) caption_entities = List(Nested(MessageEntity, required=False)) audio = Nested(Audio, required=False) voice = Nested(Voice, required=False) document = Nested(Document, required=False) animation = Nested(Animation, required=False) game = Nested(Game, required=False) photo = List(Nested(PhotoSize, required=False)) sticker = Nested(Sticker, required=False) video = Nested(Video, required=False) video_note = Nested(VideoNote, required=False) caption = String(required=False) contact = Nested(Contact, required=False) location = Nested(Location, required=False) venue = Nested(Venue, required=False) poll = Nested(Poll, required=False) new_chat_members = List(Nested(User, required=False)) left_chat_member = Nested(User, required=False) new_chat_title = String(required=False) new_chat_photo = List(Nested(PhotoSize, required=False)) delete_chat_photo = Boolean(required=False) group_chat_created = Boolean(required=False) supergroup_chat_created = Boolean(required=False) channel_chat_created = Boolean(required=False) migrate_to_chat_id = Integer(required=False) migrate_from_chat_id = Integer(required=False) pinned_message = Nested(lambda: Message(exclude=('pinned_message', ))) invoice = Nested(Invoice, required=False) successful_payment = Nested(SuccessfulPayment, required=False) connected_website = String(required=False) passport_data = Nested(PassportData, required=False) reply_markup = Nested(InlineKeyboardMarkup, required=False)
class NoticeSchema(Schema): id = String(required=True, validate=Regexp(r"USN-\d{1,5}-\d{1,2}")) title = String(required=True) summary = String(required=True) instructions = String(required=True) references = List(String()) cves = List(String(validate=Regexp(r"(cve-|CVE-)\d{4}-\d{4,7}"))) published = ParsedDateTime(required=True) description = String(allow_none=True) release_packages = Dict( keys=ReleaseCodename(), values=List(Nested(NoticePackage), required=True), )
class PhotometryJob(Job): type = 'photometry' description = 'Photometer Sources' result: PhotometryJobResult = Nested(PhotometryJobResult, default={}) file_ids: TList[int] = List(Integer(), default=[]) sources: TList[SourceExtractionData] = List(Nested(SourceExtractionData), default=[]) settings: PhotSettings = Nested(PhotSettings, default={}) def run(self): self.result.data = run_photometry_job(self, self.settings, self.file_ids, self.sources)
class NoticeSchema(Schema): id = String(required=True, validate=Regexp(r"(USN|LSN)-\d{1,5}-\d{1,2}")) title = String(required=True) summary = String(required=True) instructions = String(required=True) references = List(String()) published = ParsedDateTime(required=True) details = String(allow_none=True, data_key="description") is_hidden = Boolean(required=False) release_packages = Dict( keys=ReleaseCodename(), values=List(Nested(NoticePackage), required=True), )