def requirement_item_schema(idclass_tab): """ Return the data specification schema for a requirement line-item. """ item_id = idclass_tab['item'] requirement_line_item = Schema([ SUMMARY_TEXT, Optional({ 'notes': PARAGRAPH_TEXT, Extra: Reject }), Required({ Required('type'): Any('guidance', 'mandate', 'information'), Extra: Reject }), Required({ Required('state'): Any('draft'), Extra: Reject }), Optional({ Required('ref'): [item_id], Extra: Reject }), { Extra: Reject } ]) return requirement_line_item
def function_scope_schema(idclass_tab): """ Return the validation schema for data within python function scope. """ return Schema( Any( Required(function_metadata_schema()), Optional( da.check.schema.common.requirement_set_schema(idclass_tab))))
def function_metadata_schema(): """ Return the data validation schema for python function metadata. """ key_value_pair = Schema({da.check.schema.common.LOWERCASE_NAME: str}) return Schema({ 'type': Any('constructor', 'context_manager', 'coroutine', 'function', 'generator'), 'args': key_value_pair, Optional('yields'): Any(key_value_pair, str), Optional('returns'): Any(key_value_pair, str), Optional('preconditions'): [str], Optional('side_effects'): [str], })
def get(): """ Return the data validation schema for the identifier class register. """ common = da.check.schema.common return Schema({ 'title': common.TITLE_TEXT, 'introduction': common.PARAGRAPH_TEXT, 'register': { common.LOWERCASE_NAME: { 'desc': common.TITLE_TEXT, Optional('pfix'): Match(r'[a-z]{1,3}'), Optional('dgts'): Range(1, 12), Optional('expr'): str, 'note': common.PARAGRAPH_TEXT } }, Extra: Reject })
def metadata(): """ Return the engineering document metadata schema. """ return Schema({ Required('_metadata'): { Required('document_type'): str, Required('document_type_acronym'): str, Optional('system_of_interest_name'): str, Optional('document_id'): str, Optional('compilation_date'): str, Optional('timebox_id'): str, Optional('configuration_id'): str, Optional('lifecycle_stage'): str, Optional('protective_marking'): str, Optional('contact_person'): str, Optional('contact_email'): str, Extra: Reject } })
data_schema = Any( # generated from operator # Schema({ # 'type': 'generated', # 'operator': str, # 'bits': All(int, Range(min=1)), # Optional('out_width'): All(int, Range(min=1)), # Optional('add_noise'): Range(min=0.0), # Optional('targets'): target_subset_schema, # }), # read from file Schema({ 'type': 'file', 'filename': str, Optional('dir'): IsDir, Optional('add_noise'): Range(min=0.0), Optional('targets'): target_subset_schema, }), # pre-split, read from file Schema({ 'type': 'split', 'training_filename': str, 'test_filename': str, Optional('dir'): IsDir, Optional('add_noise'): Range(min=0.0), Optional('targets'): target_subset_schema, })) sampling_schema = Any( # randomly generated
def _check_frame_pair(frame_pair): if (not isinstance(frame_pair, list) or len(frame_pair) != 2 or not all(isinstance(x, int) for x in frame_pair)): raise Invalid('Invalid initial frame pair format') return FramePair(*frame_pair) DATASET_CONFIG_SCHEMA = Schema({ 'tests': { Any(str): { 'camera': str, 'ground_truth': str, 'rgb': str, Optional('initial_frames'): Any(_check_frame_pair, Default(None)) } } }) TestInfo = namedtuple('TestInfo', ( 'camera', 'ground_truth', 'rgb', 'initial_frames' )) def read_config(config_path): root = path.dirname(path.abspath(config_path))
# -*- coding: utf-8 -*- from good import Schema, All, Required, Optional, Length, Match, Email login_schema = Schema({ Required('login'): unicode, Required('password'): unicode }) signup_schema = Schema({ Required('username'): All(unicode, Match(r'^[\w.]+$'), Length(max=32)), Required('email'): Email(), Required('password'): All(unicode, Length(min=3, max=32)) }) edit_schema = Schema({ Optional('firstname'): All(unicode, Length(max=32)), Optional('lastname'): All(unicode, Length(max=32)), Optional('password'): Schema({ Required('old'): All(unicode, Length(min=3, max=32)), Required('new'): All(unicode, Length(min=3, max=32)) }) })
def get_student_schema(schoolclass): def start_before_end(): def validator(d): if 'start_date' not in d or 'end_date' not in d: assert False, "La date d'arrivée dans la classe ou la date de départ ne sont pas renseignées" if not isinstance(d['start_date'], date) or not isinstance( d['end_date'], date): assert False assert d['start_date'] <= d[ 'end_date'], "La date d'arrivée dans la classe doit être antérieur à la date de départ" return d return validator schema = Schema( { 'firstname': Msg(Length(min=1), "Le prénom doit être renseigné."), 'lastname': Msg(Length(min=1), "Le nom doit être renseigné."), Optional('birth_date'): Any( '', Msg(Date('%d/%m/%Y'), "La date de naissance doit être renseignée.")), 'gender': Msg(Any('f', 'm'), "Vous devez sélectionner une valeur pour le sexe"), 'grade': All( Coerce(int), Msg( In([grade.id for grade in schoolclass.grades]), "Le niveau choisi n'est pas disponible pour cette classe.") ), 'start_date': All( Msg(Date('%d/%m/%Y'), "La date d'arrivée dans la classe doit être renseignée."), Msg( Range(schoolclass.schoolyear.start_date, schoolclass.schoolyear.end_date), "La date d'arrivée dans la classe doit être comprise entre le %s et le %s" % (schoolclass.schoolyear.start_date.strftime("%d/%m/%Y"), schoolclass.schoolyear.end_date.strftime("%d/%m/%Y")))), 'end_date': All( Msg(Date('%d/%m/%Y'), "La date de départ de la classe doit être renseignée."), Msg( Range(schoolclass.schoolyear.start_date, schoolclass.schoolyear.end_date), "La date de départ de la classe doit être comprise entre le %s et le %s" % (schoolclass.schoolyear.start_date.strftime("%d/%m/%Y"), schoolclass.schoolyear.end_date.strftime("%d/%m/%Y")))), 'ulis': Any(Msg(Boolean(), "Le champ ULIS doit être renseigné"), Default(False)), Entire: start_before_end() }, default_keys=Required) return schema
def get(idclass_tab): """ Return the build configuration schema. """ common = da.check.schema.common environment_id = idclass_tab['environment'] build_scope = Schema({ Optional('defined_baselines'): [common.GIT_COMMIT_ISH], Optional('environment'): [environment_id], Optional('restriction'): common.BUILD_RESTRICTION, Extra: Reject }) build_options = Schema({ Optional('clean_tmp_dir'): bool, Optional('auto_commit'): bool, Optional('loglevel_overall'): common.LOG_LEVEL, Optional('loglevel_file'): common.LOG_LEVEL, Optional('loglevel_console'): common.LOG_LEVEL, Optional('enable_build_profiling'): bool, Optional('enable_build_debugger'): bool, Optional('enable_cms_registration'): bool, Optional('enable_cms_delete_old_builds'): bool, Optional('cms_expiration_days'): int, Optional('check_changed_files_only'): bool, Optional('errors_abort_immediately'): bool, Optional('dep_build_exclusion'): Maybe(str), Optional('dep_build_limitation'): Maybe(str), Optional('optimisation_module'): Maybe(str), Extra: Reject }) build_steps = Schema({ Optional('enable_dep_fetch_src'): bool, Optional('enable_dep_build'): bool, Optional('enable_main_build'): bool, Optional('enable_test_python_unittest'): bool, Optional('enable_static_data_validation'): bool, Optional('enable_static_indexing'): bool, Optional('enable_static_test_python_complexity'): bool, Optional('enable_static_test_python_codestyle'): bool, Optional('enable_static_test_python_docstyle'): bool, Optional('enable_static_test_python_pylint'): bool, Optional('enable_static_test_python_typecheck'): bool, Optional('enable_compile_gcc'): bool, Optional('enable_compile_clang'): bool, Optional('enable_generate_design_docs'): bool, Optional('enable_report_generation'): bool, Optional('enable_bulk_data_checks'): bool, Extra: Reject }) return Schema({ Required('title'): common.TITLE_TEXT, Optional('scope'): build_scope, Optional('options'): build_options, Optional('steps'): build_steps, Extra: Reject })
# python imports from good import Schema, All, Any, Required, Optional, Length, Range, Match, Default # project imports from project.modules.recaptcha_validator import ReCaptcha create_schema = Schema({ Required('name'): All(unicode, Length(min=1, max=32)), Required('starts_at'): int, Required('ends_at'): int, Required('recaptcha'): ReCaptcha() }) edit_schema = Schema({ Optional('name'): All(unicode, Length(min=1, max=32)), Optional('starts_at'): int, Optional('ends_at'): int }) team_join_schema = Schema({Required('team_id'): unicode}) problem_create_schema = Schema({ Required('title'): All(unicode, Length(min=1, max=32)), Required('time_limit'): All(Any(float, int), Range(min=0.1, max=10.0)), Required('space_limit'): All(int, Range(min=16, max=256)) })
if (not isinstance(frame_pair, list) or len(frame_pair) != 2 or not all(isinstance(x, int) for x in frame_pair)): raise Invalid('Invalid initial frame pair format') return FramePair(*frame_pair) DATASET_CONFIG_SCHEMA = Schema({ 'tests': { Any(str): { 'camera': str, 'ground_truth': str, 'rgb': str, Optional('initial_frames'): Any(_check_frame_pair, Default(None)), Optional('translation_error_allowed'): Any(Range(0.0, 1.0), Default(TRANSLATION_ERROR_ALLOWED)), Optional('rotation_deg_error_allowed'): Any( Range(0.0, 360.0), Default(ROTATION_DEG_ERROR_ALLOWED), ) } } }) TestInfo = namedtuple( 'TestInfo', ('camera', 'ground_truth', 'rgb', 'initial_frames', 'translation_error_allowed', 'rotation_deg_error_allowed'))
# -*- coding: utf-8 -*- __author__ = 'AminHP' from good import Schema, All, Any, Required, Optional, Length, Match, Default create_schema = Schema({ Required('name'): All(unicode, Length(min=1, max=32)), Required('members'): Any(All([Schema(unicode)], Length(max=2)), Default([])) }) edit_schema = Schema({ Optional('name'): All(unicode, Length(min=1, max=32)), Optional('members'): All([Schema(unicode)], Length(max=2)) })
def test_validators(self): # 0. Select configuration file k_ = Konf(self._get_asset('5.yml')) # 1. Declare validators # You can cache validators inside a Konf object as if it's a standard python dict k_['v1'] = { 'key': STRING, 'secret': STRING, } k_['v2'] = { 'key': STRING, 'secret': STRING, 'public_name': STRING } # 2. Get variables from config # For avoid copy-paste and massive chunks of code, just declare a new variable # and pass data from config to it sn_ = k_('SN', { 'vk': k_['v1'], # You can get validator you want, for example v1... 'google': k_['v1'], 'twitter': k_['v1'], 'ok': k_['v2'] # ...or v2 }) # 3. Fill everything to a python variables which are required for 3rd-party library SOCIAL_AUTH_VK_OAUTH2_KEY = sn_['vk']['key'] SOCIAL_AUTH_VK_OAUTH2_SECRET = sn_['vk']['secret'] SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = sn_['google']['key'] SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = sn_['google']['secret'] SOCIAL_AUTH_TWITTER_KEY = sn_['twitter']['key'] SOCIAL_AUTH_TWITTER_SECRET = sn_['twitter']['secret'] SOCIAL_AUTH_ODNOKLASSNIKI_KEY = sn_['ok']['key'] SOCIAL_AUTH_ODNOKLASSNIKI_SECRET = sn_['ok']['secret'] SOCIAL_AUTH_ODNOKLASSNIKI_OAUTH2_PUBLIC_NAME = sn_['ok']['public_name'] # 4. Check that config doesn't contain some garbage # (this might mean you forgot to get these variables, or this config is wrong, some draft for example) k_.check_redundant() # 5. If server is running without errors, and you will meet issue with this 3rd-party library later, # you can be sure that problem isn't in your configuration file. # Otherwise, you'll just catch a error on a start server stage. from good import Optional k2_ = Konf(self._get_asset('6.yml')) k2_['sn_data'] = { 'key': STRING, 'secret': STRING, Optional('public_name'): STRING } sn2_ = lambda: k2_('SN', { name: k2_['sn_data'] for name in ['vk', 'google', 'twitter'] }) self.assertRaises(Konf.ValidationError, sn2_) def assign(): k_['v1'] = {'foo': 'bar'} self.assertRaises(Konf.ValidatorManagementError, assign) self.assertRaises(Konf.ValidatorManagementError, lambda: k_['v3'])
from jinja2 import Template, Environment import requests from json import dumps from good import Schema, Optional from glue import p template_schema = Schema({ # TODO: not just a string, but valid Jinja template! 'url' : str, 'type' : str, Optional('data') : str }) env = Environment() env.filters['json'] = dumps example = { 'url' : 'http://httpbin.org/post', 'type' : 'post', 'data' : '{{match|json}}' } def dictmap(fun, d): return {key:fun(d[key]) for key in d} #TODO: validation class Trigger: def __init__(self, template): self._template = dictmap(env.from_string, template) def _render(self, **context): return dictmap(lambda t: t.render(**context), self._template)
# python imports from good import Schema, All, Required, Optional, Length, Match, Email # project imports from project.modules.recaptcha_validator import ReCaptcha signup_schema = Schema({ Required('username'): All(unicode, Match(r'^[a-zA-Z0-9_]*$'), Length(min=1, max=32)), Required('email'): Email(), Required('password'): All(unicode, Length(min=3, max=32)), Required('recaptcha'): ReCaptcha() }) login_schema = Schema({ Required('login'): unicode, Required('password'): unicode }) edit_schema = Schema({ Optional('email'): Email(), Optional('password'): Schema({ Required('old_password'): All(unicode, Length(min=3, max=32)), Required('new_password'): All(unicode, Length(min=3, max=32)) }), Optional('firstname'): All(unicode, Length(min=1, max=32)), Optional('lastname'): All(unicode, Length(min=1, max=32)) })