def assault(self): stats = AssaultStats.from_cfg( self.name, self.info.get("assault", Configuration())) for equipment in self.equipments: stats = stats.update_from_cfg( equipment.info.get("assault", Configuration())) return stats
def range(self): """TODO""" stats = RangeStats.from_cfg(self.name, self.info.get("range", Configuration())) for equipment in self.equipments: stats = stats.update_from_cfg( equipment.info.get("range", Configuration())) return stats
def _all_tomls(): file_paths = pathlib.Path(__file__).parent.glob("*.toml") toml = Configuration() for path in file_paths: print(path) toml.update_from_file(path) return toml
def test_ini_handle_int_key_gracefully(cfg_with_int_key): """Test that INI format handles int keys gracefully""" roundtripped = Configuration.from_str( cfg_with_int_key.as_str(format="ini"), format="ini") # INI converts integer keys to strings assert roundtripped.answer.entry_keys == ["42"]
def test_yaml_handle_int_key_gracefully(cfg_with_int_key): """Test that YAML format handles int keys gracefully""" roundtripped = Configuration.from_str( cfg_with_int_key.as_str(format="yaml"), format="yaml") # YAML supports integer keys assert roundtripped.as_dict() == cfg_with_int_key.as_dict()
def test_json_handle_int_key_gracefully(cfg_with_int_key): """Test that JSON format handles int keys gracefully""" roundtripped = Configuration.from_str( cfg_with_int_key.as_str(format="json"), format="json") # JSON converts integer keys to strings assert roundtripped.answer.entry_keys == ["42"] assert roundtripped.long_answer.parts.entry_keys == ["1", "2"]
def generate_powerpoint(template_url, cfg, data, output_path): """Generate PowerPoint report from data based on cfg. Store to output_path""" template = files.get_url_or_asset(template_url, local_assets="geong_common.assets") template_cfg = Configuration.from_str(template.read_text(), format="toml") template_ppt = (template.parent / template_cfg.replace("template")).read_bytes() prs = pptx.Presentation(io.BytesIO(template_ppt)) add_slides(prs, template_cfg, cfg, data) prs.save(output_path)
def read(cfg_name: str, *directories: pathlib.Path) -> Configuration: """Read one configuration from file Args: cfg_name: Name of configuration, `.toml`-suffix is added directories: Prioritized list of directories Returns: A configuration object """ cfg = Configuration(cfg_name) for file_path in _config_paths(cfg_name, [pathlib.Path(d) for d in directories]): cfg.update_from_file(file_path) if not cfg: raise FileNotFoundError( f"Configuration {cfg_name!r} not found in {', '.join(directories)}" ) return cfg
def _orders(self, order_type): unit_orders = self.info.orders.get(order_type, Configuration()).as_dict() for items in (self.models + self.equipments).as_dict().values(): item = items[0] for speed, orders in (item.info.get("orders_gained", {}).get(order_type, {}).items()): for order in orders: if order not in unit_orders.get(speed, []): unit_orders.setdefault(speed, []).append(order) return unit_orders
def cfg_with_int_key(): """Configuration with a key that is an integer""" return Configuration.from_dict({ "answer": { 42: "Life, the Universe, and Everything" }, "long_answer": { "parts": { 1: "The Answer to the Ultimate Question of", 2: "Life, the Universe, and Everything", }, }, })
def read_config(cfg_name: str) -> Configuration: """Read one configuration from file Args: cfg_name: Name of configuration, used for documentation Returns: A configuration object """ if cfg_name not in _CFGS: cfg = _CFGS[cfg_name] = Configuration(cfg_name) for file_path in config_paths(cfg_name): cfg.update_from_file(file_path) return _CFGS[cfg_name]
def synthetic_config(): return Configuration.from_str( """ [models] target = "value" label_column = "type" [models.type_1] label = "type_1" factors = ["key"] [models.type_1.key] label = "Key" values = ["A", "B", "C"] [models.type_2] label = "type_2" factors = [] """, format="toml", ).models
def test_read_yaml_sources(sample_dir): """Test that source is set correctly when reading a YAML file""" cfg_path = sample_dir / "sample.yaml" cfg = Configuration.from_file(cfg_path) assert cfg.sources == {f"{cfg_path} (YAML reader)"}
def test_read_yaml(sample_dir): """Test that reading a YAML file succeeds""" Configuration.from_file(sample_dir / "sample.yaml")
def test_read_json_sources(sample_dir): """Test that source is set correctly when reading a JSON file""" cfg_path = sample_dir / "sample.json" cfg = Configuration.from_file(cfg_path) assert cfg.sources == {f"{cfg_path} (JSON reader)"}
def test_read_json(sample_dir): """Test that reading an JSON file succeeds""" Configuration.from_file(sample_dir / "sample.json")
def test_read_ini_sources(sample_dir): """Test that source is set correctly when reading an INI file""" cfg_path = sample_dir / "sample.ini" cfg = Configuration.from_file(cfg_path) assert cfg.sources == {f"{cfg_path} (INI reader)"}
def test_read_ini(sample_dir): """Test that reading an INI file succeeds""" Configuration.from_file(sample_dir / "sample.ini")
def get(module_name: str): """Get the configuration for the given module""" cfg, *sections = module_name.split(".") return _CFGS.get(cfg, Configuration()).get(sections, Configuration())
def test_json_roundtrip(cfg): """Test that a configuration can be recovered after being stored as JSON""" roundtripped = Configuration.from_str(cfg.as_str(format="json"), format="json") assert roundtripped.as_dict() == cfg.as_dict()
def sample_cfg(sample_dir): """Sample configuration""" return Configuration.from_file(sample_dir / "sample.toml")
def read_config(cfg_file: str) -> Configuration: """Read one configuration from file""" with resources.path(__package__, cfg_file) as cfg_path: return Configuration.from_file(cfg_path, name=cfg_path.stem)