class Engine(BaseEngine): given_definition = GivenDefinition(setup=GivenProperty(Str()), state=GivenProperty(Str())) def __init__(self, paths, rewrite=False): self.path = paths self._rewrite = rewrite def set_up(self): virtualenv = (hitchbuildpy.VirtualenvBuild( self.path.gen / "venv", hitchbuildpy.PyenvBuild(self.path.share / "python3.7.0", "3.7.0"), ).with_requirementstxt("requirements.txt")) virtualenv.ensure_built() self.example_py_code = (ExamplePythonCode( virtualenv.bin.python, self.path.gen).with_setup_code( self.given.get("setup", "")).with_terminal_size( 160, 100).with_strings(state=self.given["state"]).with_modules( "game.py", "state.py")) @no_stacktrace_for(HitchRunPyException) def run(self, code, will_output=None): result = self.example_py_code.with_code(code).run() if will_output is not None: try: Templex(will_output).assert_match(result.output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": result.output}) else: raise
def _get_base_part_mapping(cls) -> dict: """Base mapping for segment/phase schemas.""" polar_coeff_schema = CommaSeparated(Float()) | Str() polar_schema = Map({ "CL": polar_coeff_schema, "CD": polar_coeff_schema }) | Str() return { # TODO: this mapping covers all possible segments, but some options are relevant # only for some segments. A better check could be done in second-pass validation. Optional("target", default=None): cls._get_target_schema(), Optional("engine_setting", default=None): cls._get_value_schema(Str(), False), Optional(POLAR_TAG, default=None): polar_schema, Optional("thrust_rate", default=None): cls._get_value_schema(has_unit=False), Optional("climb_thrust_rate", default=None): cls._get_value_schema(has_unit=False), Optional("time_step", default=None): cls._get_value_schema(), Optional("maximum_flight_level", default=None): cls._get_value_schema(has_unit=False), Optional("mass_ratio", default=None): cls._get_value_schema(has_unit=False), Optional("reserve_mass_ratio", default=None): cls._get_value_schema(has_unit=False), Optional("use_max_lift_drag_ratio", default=None): cls._get_value_schema(Bool(), False), }
def FileMapping(): """Validator that matches a file mapping: a string or a mapping with source and target. """ return Str() | Map({ 'source': Str(), 'target': Str(), })
def NumberCapture(): """Validator that matches a predefined integer or float capture.""" return Map({ 'type': Choice('integer', 'float'), 'name': Str(), 'prefix': Str(), Optional('mode'): Choice('first', 'last', 'all'), })
def _get_schema(cls): return Map({ PHASE_DEFINITIONS_TAG: MapPattern(Str(), Map(cls._get_phase_mapping())), ROUTE_DEFINITIONS_TAG: MapPattern(Str(), Map(cls._get_route_mapping())), MISSION_DEFINITION_TAG: Map(cls._get_mission_mapping()), })
def _get_value_schema(cls, value_type: ScalarValidator = Float(), has_unit=True) -> Validator: """Schema for parameter value.""" map_dict = {"value": Float() | Str()} if has_unit: map_dict[Optional("unit", default=None)] = Str() return value_type | Str() | Map(map_dict)
def parse_yaml(yaml_text): selectors = load( yaml_text, MapPattern(Str(), Map({"appears when": Str(), "elements": ELEMENTS_SCHEMA})), ) for page, page_properties in selectors.items(): revalidate_subelements(page_properties["elements"]) return selectors
def test_operations_yaml_schema(self): try: load( Path("../operations.yaml").bytes().decode('utf8'), MapPattern(Str(), Map({ "command": Str(), "tags": Str() }))) except YAMLError as error: self.fail("Something wrong with your schema")
def _get_schema(cls): """Schema of the whole mission file.""" return Map({ PHASE_DEFINITIONS_TAG: MapPattern(Str(), cls._get_phase_schema()), ROUTE_DEFINITIONS_TAG: MapPattern(Str(), cls._get_route_schema()), MISSION_DEFINITION_TAG: MapPattern(Str(), cls._get_mission_schema()), })
def _get_mission_mapping(cls) -> dict: return { "name": Str(), STEPS_TAG: Seq( Map({ Optional(ROUTE_TAG, default=None): Str(), Optional(PHASE_TAG, default=None): Str(), })), }
def load(path: Path, schema_pointer): """Load and validate .yaml file.""" schema = copy.deepcopy(schema_pointer) with path.open() as f: yaml = f.read() data = yaml_load(yaml, Any()) is_template = path.name == "template.yaml" # Replace real Country and Timezone values with fakes if is_template: schema["woo/woocommerce_default_country"] = Enum(["LL"]) schema["wp/timezone_string"] = Enum(["Region/Country"]) schema["wp/DEFAULT_WPLANG"] = Enum(["ll_LL"]) schema["woo/woocommerce_currency"] = Enum(["LLL"]) if "woo/woocommerce_tax_classes" in data: # Inspect that tax classes and taxes match # create enum for taxes from defined tax_classes tax_classes = [ str(tax).lower().replace(" ", "-") for tax in data["woo/woocommerce_tax_classes"] ] # +1 is for standard schema which is never defined in tax class for x in range(len(tax_classes) + 1): # start counting with 1 schema[f"wootax/{x+1}"] = Map({ "country": Enum(["LL"]) if is_template else Enum(COUNTRIES), "state": Str(), "rate": Decimal(), "name": Str(), "priority": Int(), "compound": Int(), "shipping": Int(), "order": Int(), "class": Enum([""]) if x == 0 else Enum(tax_classes), "locations": Map({}), }) try: return yaml_load(yaml, Map(schema), path) except YAMLError: raise return as_document(schema)
def _get_target_schema(cls) -> Map: target_schema_map = {} for key in [f.name for f in fields(FlightPoint)]: target_schema_map[Optional( key, default=None)] = (Float() | Str() | Map({ "value": Float() | Str(), Optional("unit", default=None): Str() })) return Map(target_schema_map)
def _get_route_schema(cls) -> Map: """Schema of the route section.""" return Map({ Optional("range", default=None): cls._get_value_schema(), Optional(CLIMB_PARTS_TAG, default=None): Seq(Map({PHASE_TAG: Str()})), CRUISE_PART_TAG: cls._get_segment_schema(), Optional(DESCENT_PARTS_TAG, default=None): Seq(Map({PHASE_TAG: Str()})), })
def __init__(self, config_file): # yaml schema schema = Map({"api": Map({"url": Str(), "query": Str()}), "dir": Map({"anime": Str(), "exclude": Seq(Str())})}) # open yaml file and load self.config_file = config_file with open(config_file, "r", encoding="utf8") as f: self.data = load(f.read(), schema) # adding atribute to config for key1 in self.data.data: for key2 in self.data.data[key1]: setattr(self, key1+"_"+key2, self.data.data[key1][key2])
def _get_info(cls): # https://hitchdev.com/strictyaml schema = Map({ Optional("knobs"): Map({ Optional("log_to_console"): Bool(), Optional("log_level_debug"): Bool(), }) | EmptyDict(), "globals": Map({ "topic_prefix": Str(), Optional( "reconnect_interval", default=const.MQTT_DEFAULT_RECONNECT_INTERVAL, ): Float(), Optional("poll_interval", default=const.MYQ_DEFAULT_POLL_INTERVAL): Float(), Optional( "periodic_mqtt_report", default=const.DEFAULT_PERIODIC_MQTT_REPORT, ): Float(), Optional("user_agent"): Str(), }), "mqtt": Map({ "host": Str(), Optional("client_id", default=const.MQTT_DEFAULT_CLIENT_ID): Str(), Optional("username"): Str(), Optional("password"): Str(), }), "myq": Map({ "email": Email(), "password": Str(), }), Optional("alias"): MapPattern(Str(), Str()) | EmptyDict(), }) if not cls._info: config_filename = cls._get_config_filename() logger.info("loading yaml config file %s", config_filename) with open(config_filename, "r") as ymlfile: raw_cfg = load(ymlfile.read(), schema).data cls._parse_raw_cfg(raw_cfg) return cls._info
def get_type_schema_yaml_validator() -> Map: seq_validator = Seq( Map({ "field": Enum([str(el) for el in Fields]), "condition": Str(), "value": Str() | Seq(Str()), })) return Map({ Optional(str(RequirementTypes.INPUT_REQUIREMENTS)): seq_validator, Optional(str(RequirementTypes.OUTPUT_REQUIREMENTS)): seq_validator, })
def schema(cls): basic_schema = { # `name` is not part of the sub schema, name is the title of the block. # `default` must be provided in the sub schemas "label": Str(), "type": Str(), Optional("variable"): Str(), Optional("helptext"): Str(), Optional("visibility"): Int(), Optional("required"): Bool(), Optional("readonly"): Bool(), } return Map(basic_schema)
def __init__(self, sqlite_filename, templates): self._db = Database(sqlite_filename) self._templates = load( Path(templates).bytes().decode('utf8'), Map({ "documents": MapPattern(Str(), Str()), "steps": MapPattern(Str(), Str()), }), ).data self._stories = [ Story(recording, self._db, self._templates) for recording in self._db.Recording.select() ]
def get_robot_part_schema(): """ Getter for robot schema :return: schema that is used to verify the robot yaml """ return Map({ 'name': Str(), 'type': Str(), 'brick': Int(), 'x_offset': Float(), 'y_offset': Float(), Optional('port'): Regex('ev3-ports:(in[1-4]|out[A-D])'), Optional('side'): Regex('left|right|rear'), Optional('direction'): Regex('bottom|front'), })
def get_world_schema(): """ Getter for world schema :return: schema that is used to verify the world yaml """ return Map({ 'robots': Seq( Map({ 'name': Str(), 'center_x': Int(), 'center_y': Int(), 'orientation': Int(), Optional('type'): Str(), Optional('parts'): Seq(ConfigChecker.get_robot_part_schema()) })), 'board_height': Int(), 'board_width': Int(), 'board_color': CommaSeparated(Int()), 'obstacles': Seq( Map({ 'name': Str(), 'type': Str(), Optional('outer_spacing'): Int(), Optional('depth'): Int(), Optional('color'): CommaSeparated(Int()), Optional('border_width'): Int(), Optional('inner_radius'): Int(), Optional('x'): Int(), Optional('y'): Int(), Optional('width'): Int(), Optional('height'): Int(), Optional('angle'): Int(), Optional('movable'): Bool(), Optional('hole'): Bool(), Optional('radius'): Int(), })), })
def __init__(self, filename): """Load config from YAML file.""" filename = path.abspath(filename) if filename is None: self._config = [] else: try: with open(filename, 'r') as handle: self._yaml = handle.read() self._config = load( self._yaml, Seq( Map({ Optional("name"): "name", "request": Map({ Optional("path"): Str(), Optional("method"): Enum([ "get", "post", "put", "delete", "GET", "POST", "PUT", "DELETE", ]), Optional("headers"): MapPattern(Str(), Str()), Optional("data"): Str(), }), "response": Map({ "content": Str() | Map({"file": Str()}), Optional("code"): Int(), Optional("headers"): MapPattern(Str(), Str()), }), }))) except Exception as e: sys.stderr.write( "Error reading YAML config file: {0}\n".format(str(e))) sys.exit(1) # Read and store all references to external content files for pair in self._config: content = pair.get('response', {}).get('content') if type(content) != str and "file" in content: with open( path.join(path.dirname(filename), content['file']), 'r') as content_file_handle: pair['response']['content'] = \ content_file_handle.read()
class Engine(BaseEngine): given_definition = GivenDefinition(website=GivenProperty(Str())) def __init__(self, paths): self.path = paths def set_up(self): print("set up") print(self.given["website"]) def form_filled(self, **textboxes): for name, contents in sorted(textboxes.items()): print("{}: {}".format(name, contents)) def clicked(self, name): print(name) def email_was_sent(self): print("email was sent") def on_failure(self, result): print("failure") def on_success(self): print("success") def tear_down(self): print("tear down run")
def __init__(self, config_file): # yaml schema schema = Map({ "api": Map({ "url": Str(), "query": Str() }), "dir": Map({ "anime": Str(), "exclude": Seq(Str()) }) }) # open yaml file and load self.config_file = config_file with open(config_file, "r", encoding="utf8") as f: self.data = load(f.read(), schema)
def preprocess_parameters_for_cube_creator(elem_args): """ This function does two things: 1) convert class_ids from name: class_ids@text, values: [0, 1, 2, 3] to name: class_ids, values: {"@text": [0, 1, 2, 3]} 2) type conversion for "values" field. Parameters ---------- elem_args: strictyaml.YAML object (contains dict inside) Returns ------- new_elem_args: dict """ for param_portion in elem_args["parameters"]: name = str(param_portion["name"]) if name.startswith("class_ids"): validator = Float() | Seq(Float()) else: validator = Seq(ARTM_TYPES[name]) param_schema = Map({ "name": Str(), "values": validator }) param_portion.revalidate(param_schema)
def Regex(): """Validator that matches a regex: a mapping with pattern and optional mode. """ return Map({ 'pattern': Str(), Optional('mode'): Choice('first', 'last', 'all'), })
def Capture(): """Validator that matches any of the valid capture specs.""" return First( "capture", Str(), Regex(), NumberCapture(), )
def _get_mission_schema(cls) -> Map: """Schema of the mission section.""" return Map({ PARTS_TAG: Seq( Map({ Optional(ROUTE_TAG, default=None): Str(), Optional(PHASE_TAG, default=None): Str(), Optional(RESERVE_TAG, default=None): Map({ "ref": Str(), "multiplier": Float() | Str() }), })), })
def build_schema_for_cubes(): """ Returns ------- dict each element is str -> strictyaml.Map where key is name of cube, value is a schema used for validation and type-coercion """ schemas = {} for class_of_object in SUPPORTED_CUBES: res = build_schema_from_signature(class_of_object) # "selection" isn't used in __init__, but we will need it later res["selection"] = Seq(Str()) # shortcut for strategy intialization if is_key_in_schema("strategy", res): signature_validation = {} for strategy_class in SUPPORTED_STRATEGIES: local_signature_validation = build_schema_from_signature( strategy_class) signature_validation.update(local_signature_validation) res[Optional("strategy_params")] = Map(signature_validation) # we will deal with "values" later, but we can check at least some simple things already if class_of_object.__name__ == "CubeCreator": element = Map({"name": Str(), "values": Seq(Any())}) res["parameters"] = Seq(element) if class_of_object.__name__ == "RegularizersModifierCube": element = Map({ Optional("name"): Str(), Optional("regularizer"): Any(), Optional("tau_grid"): Seq(Float()) }) res["regularizer_parameters"] = element | Seq(element) res = Map(res) specific_schema = Map({class_of_object.__name__: res}) schemas[class_of_object.__name__] = specific_schema return schemas
def parse_args(): # Get config file as required arguemnt and load f = argument.Arguments() f.always("config", help="Machine Config file name") arguments, errors = f.parse() if arguments.get("config") is not None: machine_config = strictyaml.load( Path("machine_config/%s.yaml" % arguments.get("config")).bytes().decode('utf8')).data if machine_config.get("currency"): schema = Map({"denominations": Seq(Str())}) notes_config = strictyaml.load( Path("machine_config/notes_config/%s.yaml" % machine_config.get("currency")).bytes().decode('utf8'), schema).data else: print("Currency must be specified") exit(0) else: print("Config file must be specified") exit(0) valid_true_values = [ 'true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly' ] config = Config() config.NAME = machine_config.get("name") config.BASE_CURRENCY = machine_config.get("currency") config.DEBUG = machine_config.get("debug").lower() in valid_true_values config.CAMERA_METHOD = machine_config.get("camera_method") config.ZBAR_VIDEO_DEVICE = machine_config.get("camera_device") config.RELAY_METHOD = machine_config.get("relay_method") config.MOCK_VALIDATOR = machine_config.get( "mock_validator").lower() in valid_true_values config.ZMQ_URL_MOCK_VALIDATOR = machine_config.get( "zmq_url_mock_validator") config.NOTE_VALIDATOR_NV11 = machine_config.get( "validator_nv11").lower() in valid_true_values config.VALIDATOR_PORT = machine_config.get("validator_port") config.ZMQ_URL_PRICEFEED = machine_config.get("zmq_url_pricefeed") config.NOTES_VALUES = notes_config.get("denominations") config.ZMQ_URL_RPC = machine_config.get("zmq_url_rpc") config.ZMQ_URL_STATUS = machine_config.get("zmq_url_status") config.IS_FULLSCREEN = machine_config.get( "is_fullscreen").lower() in valid_true_values config.DEFAULT_SLIPPAGE = machine_config.get("default_slippage") config.BUY_LIMIT = int(machine_config.get("buy_limit")) if not os.uname()[4].startswith("arm"): config.RELAY_METHOD = RelayMethod.NONE return config
def read_corpus_config(filename='corpus.yml'): schema = Map({ 'dataset_path': Str(), 'batches_prefix': Str(), 'word': Str(), 'name': Str(), Optional("num_topics_interval"): Int(), Optional("nums_topics"): CommaSeparated(Int()), 'min_num_topics': Int(), 'max_num_topics': Int(), 'num_fit_iterations': Int(), 'num_restarts': Int(), }) with open(filename, 'r') as f: string = f.read() data = strictyaml.load(string, schema=schema).data return data