diff --git a/generator/pygen/example.py b/generator/pygen/example.py index 6851b5e371..09bf3c1e1d 100644 --- a/generator/pygen/example.py +++ b/generator/pygen/example.py @@ -98,10 +98,11 @@ def example__working_with_features(path): print("__repr__:", ft) for ft in it: - geometry = ft.geometry() - if ft.geom_type() == mwm.GeomType.area and len(geometry) < 10: - print("area geometry", geometry) - break + geometry = ft.geometry() + if ft.geom_type() == mwm.GeomType.area and len(geometry) < 10: + print("area geometry", geometry) + break + def example__working_with_mwm(path): map = mwm.Mwm(path) @@ -122,12 +123,10 @@ def main(path): if __name__ == "__main__": - main( - os.path.join( - os.path.dirname(os.path.realpath(__file__)), - "..", - "..", - "data", - "minsk-pass.mwm", - ) + resource_path = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "..", "..", "data" ) + + classif.init_classificator(resource_path) + + main(os.path.join(resource_path, "minsk-pass.mwm",)) diff --git a/generator/pygen/pygen.cpp b/generator/pygen/pygen.cpp index 0c8715e876..b472022b4d 100644 --- a/generator/pygen/pygen.cpp +++ b/generator/pygen/pygen.cpp @@ -10,6 +10,7 @@ #include "indexer/features_vector.hpp" #include "platform/mwm_version.hpp" +#include "platform/platform.hpp" #include "coding/string_utf8_multilang.hpp" @@ -18,10 +19,12 @@ #include "geometry/triangle2d.hpp" #include "base/assert.hpp" +#include "base/file_name_utils.hpp" #include "base/logging.hpp" #include "pyhelpers/module_version.hpp" +#include #include #include @@ -239,6 +242,22 @@ private: MwmIter Mwm::MakeMwmIter() { return MwmIter(m_self.lock()); } +std::string ReadAll(std::string const & filename) +{ + std::ifstream file; + file.exceptions(std::ios::failbit | std::ios::badbit); + file.open(filename); + return std::string(std::istreambuf_iterator(file), std::istreambuf_iterator()); +} + +bool InitClassificator(std::string const & resourcePath) +{ + classificator::LoadTypes( + ReadAll(base::JoinPath(resourcePath, "classificator.txt")), + ReadAll(base::JoinPath(resourcePath, "types.txt")) + ); +} + struct GeometryNamespace { }; @@ -256,7 +275,6 @@ BOOST_PYTHON_MODULE(pygen) { bp::scope().attr("__version__") = PYBINDINGS_VERSION; - classificator::Load(); { bp::scope geometryNamespace = bp::class_("geometry"); @@ -408,6 +426,8 @@ BOOST_PYTHON_MODULE(pygen) { bp::scope classifNamespace = bp::class_("classif"); + bp::def("init_classificator", InitClassificator); + bp::def( "readable_type", +[](uint32_t index) { return classif().GetReadableObjectName(classif().GetTypeForIndex(index)); diff --git a/tools/python/mwm/__init__.py b/tools/python/mwm/__init__.py index 0aa6e12691..95effadff3 100644 --- a/tools/python/mwm/__init__.py +++ b/tools/python/mwm/__init__.py @@ -1,16 +1,12 @@ import os -if "MWM_RESOURCES_DIR" not in os.environ: - os.environ["MWM_RESOURCES_DIR"] = os.path.join( - os.path.dirname(os.path.realpath(__file__)), "..", "..", "..", "data", - ) +resource_path = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "..", "..", "..", "data", +) -try: - from mwm.mwm_pygen import MwmPygen as Mwm - from mwm.mwm_pygen import FeaturePygen as Feature -except ImportError: - from mwm.mwm_python import MwmPython as Mwm - from mwm.mwm_python import FeaturePython as Feature +from mwm.types import init as _init + +_init(resource_path) from mwm.mwm_interface import GeomType from mwm.mwm_interface import MapType @@ -23,3 +19,14 @@ from mwm.mwm_python import get_region_info from mwm.types import readable_type from mwm.types import type_index from mwm.utils import EnumAsStrEncoder + +try: + from mwm.mwm_pygen import MwmPygen as Mwm + from mwm.mwm_pygen import FeaturePygen as Feature + + from mwm.mwm_pygen import init as _init + + _init(resource_path) +except ImportError: + from mwm.mwm_python import MwmPython as Mwm + from mwm.mwm_python import FeaturePython as Feature diff --git a/tools/python/mwm/mwm_pygen.py b/tools/python/mwm/mwm_pygen.py index 57d5246be0..0f00e5ab32 100644 --- a/tools/python/mwm/mwm_pygen.py +++ b/tools/python/mwm/mwm_pygen.py @@ -3,12 +3,17 @@ from typing import Iterable from typing import List from typing import Union +from pygen import classif from pygen import geometry from pygen import mwm from mwm import mwm_interface as mi +def init(resource_path): + classif.init_classificator(resource_path) + + class MwmPygen(mi.Mwm): def __init__(self, filename: str, parse: bool = True): super().__init__(filename) diff --git a/tools/python/mwm/types.py b/tools/python/mwm/types.py index 6f62c41ee7..3b23b0e99f 100644 --- a/tools/python/mwm/types.py +++ b/tools/python/mwm/types.py @@ -1,26 +1,24 @@ import os -from typing import Dict -from typing import Tuple + +NAME_TO_INDEX_TYPE_MAPPING = {} +INDEX_TO_NAME_TYPE_MAPPING = {} -def read_types_mappings() -> Tuple[Dict[int, str], Dict[str, int]]: - resources_path = os.environ.get("MWM_RESOURCES_DIR") - name_to_index = {} - index_to_name = {} - with open(os.path.join(resources_path, "types.txt")) as f: +def init(resource_path): + global NAME_TO_INDEX_TYPE_MAPPING + global INDEX_TO_NAME_TYPE_MAPPING + + NAME_TO_INDEX_TYPE_MAPPING = {} + INDEX_TO_NAME_TYPE_MAPPING = {} + with open(os.path.join(resource_path, "types.txt")) as f: for i, line in enumerate(f): s = line.strip() name = s.replace("|", "-") if s.startswith("*"): name = name[1:] - name_to_index[name] = i + NAME_TO_INDEX_TYPE_MAPPING[name] = i - index_to_name[i] = name - - return index_to_name, name_to_index - - -INDEX_TO_NAME_TYPE_MAPPING, NAME_TO_INDEX_TYPE_MAPPING = read_types_mappings() + INDEX_TO_NAME_TYPE_MAPPING[i] = name def readable_type(index: int) -> str: