class RegisterDevice(AuthBaseHandler): """移动设备登记 """ @validate_arguments_with( Schema( { Required("install_id"): str, Required("push_id"): All(str, Length(max=128)), Required("device_token"): str, Required("device_type"): str, Required("app_version"): str }, extra=REMOVE_EXTRA)) def post(self): form = self.validated_arguments if not device: device = Device.create( install_id=form['install_id'], push_id=form['push_id'], device_token=form['device_token'], device_type=form['device_type'], app_version=form['app_version'], ) else: Device.update(device_token=form['device_token'], app_version=form['app_version']).where( Device.id == device.id).execute() self.write({"device_id": device.id})
def _job_schema(): return Schema({ 'device_type': All(str, Length( min=1)), # not Required as some protocols encode it elsewhere Required('job_name'): All(str, Length(min=1, max=200)), 'priority': Any('high', 'medium', 'low'), 'protocols': _job_protocols_schema(), 'context': _context_schema(), 'metadata': dict, 'secrets': dict, 'tags': [str], Required('visibility'): visibility_schema(), Required('timeouts'): _job_timeout_schema(), Required('actions'): _job_actions_schema(), 'notify': _job_notify_schema() })
def from_dict(cls, adict): invalid_req = InvalidRequestObject() schema = Schema( { Required('full_name'): All(str, Length(min=1, max=128)), Required('reg_date'): Any( All( datetime.date, Range(min=datetime.date(1000, 1, 1), max=datetime.date.today())), Datetime('%Y-%m-%d')), }, extra=REMOVE_EXTRA) try: values = schema(adict) except MultipleInvalid as exc: for error in exc.errors: try: invalid_req.add_error(parameter='.'.join( [p for p in error.path]), message=error.error_message) except TypeError as e: invalid_req.add_error(parameter='.'.join( [p.schema for p in error.path]), message=error.error_message) if invalid_req.has_errors(): return invalid_req return ReaderAddRequestObject(init_values=values)
def test_encryption(self): self.task_schema = Schema({ 'task': { 'payload': { 'encryptedEnv': All(Length(2), [Match(r'^wcB')]) # Must have 2 elements, starting with wcB } } }, required=True, extra=True) test_kwargs = create_firefox_test_args({ 'updates_enabled': True, 'repo_path': 'foo/bar', 'branch': 'mozilla-beta', 'signing_class': 'dep-signing', 'release_channels': ['beta'], 'final_verify_channels': ['beta'], 'signing_pvt_key': PVT_KEY_FILE, 'accepted_mar_channel_id': 'firefox-mozilla-beta', 'signing_cert': 'dep', 'moz_disable_mar_cert_verification': True, 'en_US_config': { "platforms": { "macosx64": {"unsigned_task_id": "xyz", "signed_task_id": "xyz"}, "win32": {"unsigned_task_id": "xyz", "signed_task_id": "xyz"}, } }, }) graph = make_task_graph(**test_kwargs) do_common_assertions(graph) for p in ("win32", "macosx64"): for v in ("38.0build1", "37.0build2"): balrog = get_task_by_name(graph, "{}_en-US_{}_funsize_balrog_task".format(p, v)) verify(balrog, self.task_schema)
def schema_config(self): """Define the configuration options for FormulaGrader""" # Construct the default ItemGrader schema schema = super(FormulaGrader, self).schema_config # Append options forbidden_default = "Invalid Input: This particular answer is forbidden" return schema.extend({ Required('user_functions', default={}): schema_user_functions, Required('user_constants', default={}): validate_user_constants( Number, MathArray), # Blacklist/Whitelist have additional validation that can't happen here, because # their validation is correlated with each other Required('blacklist', default=[]): [str], Required('whitelist', default=[]): Any( All([None], Length(min=1, max=1)), [str] ), Required('forbidden_strings', default=[]): [str], Required('forbidden_message', default=forbidden_default): str, Required('required_functions', default=[]): [str], Required('tolerance', default='0.01%'): Any(PercentageString, NonNegative(Number)), Required('metric_suffixes', default=False): bool, Required('samples', default=5): Positive(int), Required('variables', default=[]): All([str], all_unique), Required('numbered_vars', default=[]): All([str], all_unique), Required('sample_from', default={}): dict, Required('failable_evals', default=0): NonNegative(int), Required('max_array_dim', default=0): NonNegative(int) })
def get(self): config = get_config() subvoat_utils = UserUtils(self.db) parser = reqparse.RequestParser() return_data = [] parser.add_argument('subvoat_name') args = parser.parse_args() schema = Schema({ Required('subvoat_name'): All(str, Length(min=config['min_length_subvoat_name'])) }) try: schema({'subvoat_name': args.get('subvoat_name')}) except MultipleInvalid as e: return {'error': '%s %s' % (e.msg, e.path)} posts = subvoat_utils.get_posts(args['subvoat_name']) for p in posts: return_data.append(p) return {'result': return_data}
def subvoat_name(self, subvoat_name): schema = Schema({ Required('subvoat_name'): All(str, Length(min=self.config['min_length_subvoat_name'])) }) return self.try_schema('subvoat_name', subvoat_name, schema)
class Handler(handler.TriggeredHandler): """Finds a hostnames ip address.""" handles_what = { 'message_matcher': matchers.match_or(matchers.match_slack("message"), matchers.match_telnet("message")), 'channel_matcher': matchers.match_channel(c.TARGETED), 'triggers': [ trigger.Trigger('dns lookup', takes_args=True), ], 'args': { 'order': ['hostname'], 'help': { 'hostname': 'hostname to lookup', }, 'schema': Schema({ Required("hostname"): All(su.string_types(), Length(min=1)), }), }, } def _run(self, hostname): replier = functools.partial(self.message.reply_text, threaded=True, prefixed=False) hostname_ip = socket.gethostbyname(hostname) replier("The ip address for `%s` is `%s`" % (hostname, hostname_ip))
def get_ecs_validator(): return Schema( { Required('Cluster'): Any(str, unicode), Required('TaskDefinition'): Any(str, unicode), Required('Count', default=1): All(ToInt, Range(min=1, max=10)), Required('Overrides', default=dict()): All(DictToString), Required('Instances', default=list()): All(list, Length(max=10)), Required('Tasks', default=list()): All(list), Required('Status', default=''): Any(str, unicode), Required('StartedBy', default='admin'): Any(str, unicode), Required('Timeout', default=3600): All(ToInt, Range(min=60, max=604800)), Required('Poll', default=10): All(ToInt, Range(min=10, max=3600)) }, extra=True)
def get_cfn_validator(): return Schema( { Required('Cluster'): Any(str, unicode), Required('TaskDefinition'): Any(str, unicode), Required('Count', default=1): All(ToInt, Range(min=0, max=10)), Required('RunOnUpdate', default=True): All(ToBool), Required('UpdateCriteria', default=[]): All([ Schema({ Required('Container'): Any(str, unicode), Required('EnvironmentKeys'): All(list) }) ]), Required('RunOnRollback', default=True): All(ToBool), Required('Timeout', default=290): All(ToInt, Range(min=0, max=3600)), Required('PollInterval', default=10): All(ToInt, Range(min=10, max=60)), Required('Overrides', default=dict()): All(DictToString), Required('Instances', default=list()): All(list, Length(max=10)), }, extra=True)
class SentimentAnalysis(Resource): parser = reqparse.RequestParser() parser.add_argument('text') schema = Schema({Required('text'): All(str, Length(min=1, max=1000))}) toolbox = SentimentAnalysisToolbox() decoder = SettingsDecoder() settings = toolbox.load_json('default', Settings()) settings.LEMMATIZER_TYPE = decoder.decode_type(settings.LEMMATIZER_TYPE) settings.CLASSIFIER_TYPE = decoder.decode_classifier( settings.CLASSIFIER_TYPE) classifier = settings.CLASSIFIER_TYPE() classifier.load('default') def post(self): args = self.parser.parse_args() try: self.schema(args) except MultipleInvalid as e: return str(e), 400, {'Access-Control-Allow-Origin': '*'} text = bagofwords.get_processed_bag_of_words(args['text'], None, self.settings) sa = self.classifier.classify(text) return { 'text': args['text'], 'sentiment': sa }, 200, { 'Access-Control-Allow-Origin': '*' }
def is_shape_specification(min_dim=1, max_dim=None): """ Validates shape specification for arrays. Valid inputs are standardized to tuples: >>> vec_or_mat = Schema(is_shape_specification(min_dim=1, max_dim=2)) >>> valid_examples = [3, (3,), [3], (4, 2), [4, 2] ] >>> [vec_or_mat(item) for item in valid_examples] [(3,), (3,), (3,), (4, 2), (4, 2)] Invalid inputs raise a useful error: >>> try: # doctest: +ELLIPSIS ... vec_or_mat(0) ... except Invalid as error: ... print(error) expected shape specification to be a positive integer,... """ msg = ('expected shape specification to be a positive integer, or a ' 'list/tuple of positive integers (min length {0}, max length {1})'. format(min_dim, max_dim)) return All( Any(All(Positive(int), lambda x: (x, )), (Positive(int), ), All([Positive(int)], Coerce(tuple)), msg=msg), Length(min=min_dim, max=max_dim), )
def post(self): """Authorizes user by email+pass, gets token, expire and user info in return input_json = { 'email': '*****@*****.**', 'password': '******' }""" # data validation scheme validate = Schema({ Required('email'): All(Email(msg='`email` did not pass validation')), Required('password'): All(Length(min=6, max=32, msg='`password` length is not enough')), }) data = json_decode(self.request.body) validate(data) access_token = User.authorize(data['email'], data['password']) if access_token: self.write_json(code=http_statuses['OK']['code'], msg=http_statuses['OK']['msg'], data=access_token) else: self.write_json( code=http_statuses['WRONG_CREDENTIALS']['code'], msg=http_statuses['WRONG_CREDENTIALS']['msg'], )
class Mandal(DbObject): schema = Schema( { Required('name'): All(str, Length(min=1)), 'location': All( Schema({ Inclusive('lat', 'latlong'): float, Inclusive('long', 'latlong'): float, })), 'date_established': datetime, Required('primary_contact'): str }, extra=PREVENT_EXTRA) def __init__(self): self._collection = 'mandal' self._fields = {} self._init_db_object() @classmethod def init_using_params(cls, data): instance = cls() instance.load(data) return instance @classmethod def init_empty_object(cls): return cls() def load(self, d): self._fields = Mandal.schema(d)
def Station(value: str) -> str: """ Performs a crude validation of a station ICAO """ try: return Length(min=4, max=4)(value.upper()) except (AttributeError, Invalid): raise Invalid(f"{value} is not a valid station ICAO")
class Handler(handler.TriggeredHandler): """Emit some message to some set of slack channels.""" required_clients = ('slack',) requires_slack_sender = True handles_what = { 'message_matcher': matchers.match_or( matchers.match_slack("message"), matchers.match_telnet("message") ), 'channel_matcher': matchers.match_channel(c.TARGETED), 'triggers': [ trigger.Trigger('emit message', takes_args=True), ], 'args': { 'order': ['channels', 'message'], 'schema': Schema({ Required("channels"): All(scu.string_types(), Length(min=1)), Required("message"): All(scu.string_types(), Length(min=1)), }), 'help': { 'channels': ('comma separated list of channels' ' to broadcast to'), 'message': 'what to broadcast', }, }, } def _run(self, channels, message): slack_sender = self.bot.slack_sender slack_server = self.bot.clients.slack_client.server ok_channels = [] seen = set() for maybe_c in channels.split(","): maybe_c = maybe_c.strip() if maybe_c and maybe_c not in seen: tmp_c = slack_server.channels.find(maybe_c) if tmp_c is None: raise RuntimeError("Could not find channel '%s'" % maybe_c) else: if tmp_c.id not in seen: seen.add(maybe_c) seen.add(tmp_c.id) ok_channels.append(tmp_c) for ch in ok_channels: slack_sender.rtm_send(message, channel=ch.id)
def schema_config(self): """Define the configuration options for IntervalGrader""" # Construct the default SingleListGrader schema schema = super(IntervalGrader, self).schema_config # Append options return schema.extend({ # Hardcode some SingleListGrader options Required('ordered', default=True): True, Required('length_error', default=True): True, Required('missing_error', default=True): True, # Subgrader default is set to NumericalGrader(tolerance=1e-13, allow_inf=True) # in initialization Required('subgrader', default=None): Any(FormulaGrader, None), Required('opening_brackets', default='[('): All(text_string, Length(min=1)), Required('closing_brackets', default='])'): All(text_string, Length(min=1)) })
def add_user(self, password, username): schema = Schema({ Required('username'): All(str, Length(min=self.config['min_length_username'])), Required('password'): All(str, Length(min=self.config['min_length_password']))}) try: schema({'username':username, 'password':password}) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] u_status, u_result = self.get_user(username) if not u_status: return [False, u_result] elif u_status and u_result: return [False, 'user already exists'] now = datetime.datetime.utcnow() password_hash = pwd_context.encrypt(password) api_token = str(uuid.uuid4()) token_expiration = now + relativedelta(months=self.config['months_to_token_expiration']) new_user = self.create_user_object(password_hash=password_hash, username=username, registration_date=now, api_token=api_token, verified=False) self.session.add(new_user) status = transaction.commit() # FIX: log this if not status: return [True, 'user created'] return [False, 'unable to create user']
class Validator: _car_schema = Schema({ Required('id'): All(str, Length(min=24, max=24)), }) _car_put_schema = Schema({ Optional('make'): All(str, Length(max=20)), Optional('model'): All(str, Length(max=20)), Optional('colour'): All(str, Length(max=15)), }) _car_post_schema = Schema({ Required('make'): All(str, Length(max=20)), Required('model'): All(str, Length(max=20)), Required('colour'): All(str, Length(max=15)), }) @classmethod def validate_get(cls, data): return cls._car_schema(data) @classmethod def validate_put(cls, data): return cls._car_put_schema(data) @classmethod def validate_post(cls, data): return cls._car_post_schema(data)
def _chart_delegate(service_fn): def handler(args): org_filter = args['org'] # TODO might not want case sensitivity month_window = args['month_window'] return service_fn(org_filter, month_window, current_app.config) return _handle_with_schema_validation(handler, { Optional('org', default=None): Any(None, All(str, Length(min=1))), Optional('month_window', default=12): All(Coerce(int), Range(min=1, max=24)) })
def _job_schema(): metadata_types = Any(str, int) return Schema({ 'device_type': All(str, Length( min=1)), # not Required as some protocols encode it elsewhere Required('job_name'): All(str, Length(min=1, max=200)), Optional('priority'): Any('high', 'medium', 'low', int), Optional('protocols'): _job_protocols_schema(), Optional('context'): _context_schema(), Optional('metadata'): All({metadata_types: metadata_types}), Optional('secrets'): dict, Optional('tags'): [str], Required('visibility'): visibility_schema(), Required('timeouts'): _job_timeout_schema(), Required('actions'): _job_actions_schema(), Optional('notify'): _job_notify_schema(), Optional('reboot_to_fastboot'): bool })
def _job_schema(): metadata_types = Any(str, int) return Schema({ "device_type": All(str, Length( min=1)), # not Required as some protocols encode it elsewhere Required("job_name"): All(str, Length(min=1, max=200)), Optional("priority"): Any("high", "medium", "low", int), Optional("protocols"): _job_protocols_schema(), Optional("context"): _context_schema(), Optional("metadata"): All({metadata_types: metadata_types}), Optional("secrets"): dict, Optional("tags"): [str], Required("visibility"): visibility_schema(), Required("timeouts"): _job_timeout_schema(), Required("actions"): _job_actions_schema(), Optional("notify"): _job_notify_schema(), Optional("reboot_to_fastboot"): bool, })
def _get_schema(): from voluptuous import Schema, Required, Range, Any positive_number = All(Any(int, float), Range(min=0, min_included=False)) number = Any(int, float) return Schema({ Required('out_dir'): All(str, Length(min=1)), Required('tasks'): [ Schema({ Required('name'): All(str, Length(min=1)), Required('mesh'): Schema({ Required('resolution'): positive_number, Required('diameter'): positive_number, Required('width'): positive_number, Required('weft_density'): positive_number, Required('length'): positive_number, Required('warp_density'): positive_number, }), Required('solver'): Schema({ Required('step'): positive_number, Required('n_steps'): All(int, Range(min=0, min_included=False, max=1000)), Required('pulse'): Schema({ Required('radius'): positive_number, Required('amplitude'): positive_number, Required('center'): Schema({ Required('x'): number, Required('y'): number, Required('z'): number, }) }), }) }) ] })
def vote_thread(self, thread_uuid, direction, user_id): schema = Schema({ Required('direction'): All(int, Range(min=-1, max=1)), Required('thread_uuid'): All(str, Length(min=36, max=36)) }) try: schema({'direction': direction, 'thread_uuid': thread_uuid}) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] thread = self.get_thread_by_uuid(thread_uuid) if not thread: return [False, 'no such thread'] # see if the user already voted, if so change the vote direction if its different sq = self.session.query(Thread).filter( Thread.uuid == thread_uuid).subquery() #sq = self.db.session.query(self.classes.thread).filter(self.classes.thread.uuid == thread_uuid).subquery() q = self.session.query( ThreadVote, sq).filter(ThreadVote.user_id == user_id).first() #q = self.db.session.query(self.classes.thread_vote, sq).filter(self.classes.thread_vote.user_id == user_id).first() # if the vote doesn't exist, create it and commit it if not q: new_vote = ThreadVote(user_id=user_id, direction=direction) thread.votes.append(new_vote) self.db.session.add(thread) if not transaction.commit(): return [True, 'vote added'] return [False, 'unable to commit vote'] # If the vote is the same if q.ThreadVote.direction == int(direction): return [True, 'vote unchanged'] # Otherwise update the vote direction else: q.ThreadVote.direction = int(direction) self.db.session.add(q.vote) if not transaction.commit(): return [True, 'vote changed'] return [False, 'unable to commit vote change']
def vote_comment(self, comment_uuid, direction, user_id): schema = Schema({ Required('direction'): All(int, Range(min=-1, max=1)), Required('comment_uuid'): All(str, Length(min=36, max=36)) }) try: schema({'direction': direction, 'comment_uuid': comment_uuid}) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] # FIX: calling comment 2x (sq = ) comment = self.get_comment_by_uuid(comment_uuid) if not comment: return [False, 'no such comment'] # get the comments sq = self.session.query(Comment).filter( Comment.uuid == comment_uuid).subquery() q = self.session.query( CommentVote, sq).filter(CommentVote.user_id == user_id).first() #sq = self.db.session.query(self.classes.comment).filter(self.classes.comment.uuid == comment_uuid).subquery() #q = self.db.session.query(self.classes.comment_vote, sq).filter(self.classes.comment_vote.user_id == user_id).first() if not q: new_vote = CommentVote(user_id=user_id, direction=direction) comment.votes.append(new_vote) self.db.session.add(comment) if not transaction.commit(): return [True, 'vote added'] return [False, 'unable to commit vote'] if q.CommentVote.direction == int(direction): return [True, 'vote unchanged'] else: q.CommentVote.direction = int(direction) self.db.session.add(q.vote) if not transaction.commit(): return [True, 'vote changed'] return [False, 'unable to commit vote change']
def __init__(self, provider=None, name=None, docHref=None, children=[None], categoryCode=None, lastUpdate=None, exposed=False, db=None): super().__init__(db=db) self.configuration = configuration self.provider=provider self.name=name self.docHref=docHref self.children=children self.categoryCode=categoryCode self.lastUpdate=lastUpdate self.exposed=exposed self.configuration=configuration self.schema = Schema({'name': All(str, Length(min=1)), 'provider': All(str, Length(min=1)), 'children': Any([None,typecheck(bson.objectid.ObjectId)]), Optional('docHref'): Any(None,str), Optional('lastUpdate'): Any(None,typecheck(datetime)), 'categoryCode': All(str, Length(min=1)), 'exposed': typecheck(bool) }, required=True ) self.validate = self.schema({'provider': self.provider, 'categoryCode': self.categoryCode, 'name': self.name, 'children': self.children, 'docHref': self.docHref, 'lastUpdate': self.lastUpdate, 'exposed': self.exposed })
def password(self, password): schema = Schema({ Required('password'): All( str, Length(min=self.config['min_length_password'], max=self.config['max_length_password'])) }) return self.try_schema('password', password, schema)
def username(self, username): schema = Schema({ Required('username'): All( str, Length(min=self.config['min_length_username'], max=self.config['max_length_username'])) }) return self.try_schema('username', username, schema)
def comment_body(self, comment_body): schema = Schema({ Required('comment_body'): All( str, Length(min=self.config['min_length_comment_body'], max=self.config['max_length_comment_body'])) }) return self.try_schema('comment_body', comment_body, schema)
def get_validator(): return Schema({ Required('Cluster'): All(basestring), Required('TaskDefinition'): All(basestring), Required('Count', default=1): All(ToInt, Range(min=0, max=10)), Required('RunOnUpdate', default=True): All(ToBool), Required('Instances', default=list()): All(list, Length(max=10)), Required('Overrides', default=dict()): All(DictToString), Required('Timeout', default=3600): All(ToInt, Range(min=60, max=3600)) }, extra=True)