示例#1
0
    def enforce_rules(self) -> Iterator[Fuss]:
        """Enforce rules for missing key/value pairs in the TOML file."""
        toml_doc = TomlDoc(path=self.file_path)
        comparison = Comparison(toml_doc, self.expected_config,
                                self.special_config)()
        if not comparison.has_changes:
            return

        document = parse(toml_doc.as_string) if self.autofix else None
        yield from chain(
            self.report(SharedViolations.DIFFERENT_VALUES, document,
                        comparison.diff),
            self.report(SharedViolations.MISSING_VALUES, document,
                        comparison.missing),
        )
        if self.autofix and self.dirty:
            self.file_path.write_text(dumps(document))
示例#2
0
    def _parse_setting(self, data: str):
        """We expect to have a section in pyproject looking like

        ```
        [tool.commitizen]
        name = "cz_conventional_commits"
        ```
        """
        doc = parse(data)
        try:
            self.settings.update(doc["tool"]["commitizen"])
        except exceptions.NonExistentKey:
            self.is_empty_config = True

        if "files" in self.settings:
            self.settings["version_files"] = self.settings["files"]
            TomlConfig._show_files_column_deprecated_warning
示例#3
0
    def __call__(self) -> bool:
        if self.args.config:
            path = Path(self.args.config)
        else:
            path = Path(self.config['project']) / 'pyproject.toml'
            if not path.exists():
                self.logger.error('cannot generate file without config')
                return False

        with path.open('r', encoding='utf8') as stream:
            config = tomlkit.parse(stream.read())
        config = dict(config['tool']['dephell'])
        project_path = Path(self.config['project'])
        text = make_contributing(config=config, project_path=project_path)
        (project_path / self.file_name).write_text(text, encoding='utf8')
        self.logger.info('generated', extra=dict(file=self.file_name))
        return True
示例#4
0
def test_declare_sub_table_with_intermediate_table():
    content = """
[students]
tommy = 87
mary = 66

[subjects]
maths = "maths"
english = "english"

[students.bob]
score = 91
"""

    doc = parse(content)
    assert {"tommy": 87, "mary": 66, "bob": {"score": 91}} == doc["students"]
    assert {"tommy": 87, "mary": 66, "bob": {"score": 91}} == doc.get("students")
示例#5
0
    def load(self, config_filepath: str):
        if not os.path.exists(config_filepath):
            raise OSError(
                "Could not load the default or provided settings file.")

        self.clear()

        with open(config_filepath, "r") as f:
            settings_data = parse(f.read())

        if "default" not in settings_data:
            raise Exception("Settings file missing required section 'default'")

        for table, items in settings_data.items():
            if table.startswith(self.env) or table == "default":
                for k, v in items.items():
                    self._set_value_from_config(k, v)
示例#6
0
def test_move_table():
    content = """a = 1
[x]
a = 1

[y]
b = 1
"""
    doc = parse(content)
    doc["a"] = doc.pop("x")
    doc["z"] = doc.pop("y")
    assert (doc.as_string() == """[a]
a = 1

[z]
b = 1
""")
示例#7
0
    def __init__(self, path=None):
        self.path = Path(path or os.environ.get("MOZCI_CONFIG_PATH")
                         or self.DEFAULT_CONFIG_PATH)

        self._config = copy.deepcopy(self.DEFAULTS["merge"])
        if self.path.is_file():
            with open(self.path, "r") as fh:
                content = fh.read()
                self.merge(parse(content)["mozci"])
        else:
            logger.warning(f"Configuration path {self.path} is not a file.")

        for k, v in self.DEFAULTS["replace"].items():
            self._config.setdefault(k, v)

        self.cache = CustomCacheManager(self._config["cache"])
        self.locked = True
示例#8
0
def test_booleans_comparison():
    boolean = Bool(True, Trivia())

    assert boolean

    boolean = Bool(False, Trivia())

    assert not boolean

    s = """[foo]
value = false
"""

    content = parse(s)

    assert {"foo": {"value": False}} == content
    assert {"value": False} == content["foo"]
示例#9
0
def test_remove_does_not_live_empty_groups(
    tester: "CommandTester",
    app: "PoetryTestApplication",
    repo: "TestRepository",
    command_tester_factory: "CommandTesterFactory",
    installed: "Repository",
):
    """
    Empty groups are automatically discarded after package removal.
    """
    installed.add_package(Package("foo", "2.0.0"))
    repo.add_package(Package("foo", "2.0.0"))
    repo.add_package(Package("baz", "1.0.0"))

    content = app.poetry.file.read()

    groups_content = tomlkit.parse(
        """\
[tool.poetry.group.bar.dependencies]
foo = "^2.0.0"
baz = "^1.0.0"

"""
    )
    content["tool"]["poetry"]["dependencies"]["foo"] = "^2.0.0"
    content["tool"]["poetry"].value._insert_after(
        "dependencies", "group", groups_content["tool"]["poetry"]["group"]
    )
    app.poetry.file.write(content)

    app.poetry.package.add_dependency(Factory.create_dependency("foo", "^2.0.0"))
    app.poetry.package.add_dependency(
        Factory.create_dependency("foo", "^2.0.0", groups=["bar"])
    )
    app.poetry.package.add_dependency(
        Factory.create_dependency("baz", "^1.0.0", groups=["bar"])
    )

    tester.execute("foo baz --group bar")

    content = app.poetry.file.read()["tool"]["poetry"]
    assert "foo" in content["dependencies"]
    assert "foo" not in content["group"]["bar"]["dependencies"]
    assert "baz" not in content["group"]["bar"]["dependencies"]
    assert "[tool.poetry.group.bar]" not in content.as_string()
    assert "[tool.poetry.group]" not in content.as_string()
示例#10
0
def test_locker_should_raise_an_error_if_lock_version_is_newer_and_not_allowed(
    locker: Locker, caplog: LogCaptureFixture
):
    content = """\
[metadata]
lock-version = "2.0"
python-versions = "~2.7 || ^3.4"
content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77"

[metadata.files]
"""
    caplog.set_level(logging.WARNING, logger="poetry.packages.locker")

    locker.lock.write(tomlkit.parse(content))

    with pytest.raises(RuntimeError, match="^The lock file is not compatible"):
        _ = locker.lock_data
示例#11
0
def load_toml_from_package(package, project_file_name='pyproject.toml'):
    if package is None:
        raise ValueError(
            'Expected package to load a <{file}>-file from, but got <{package}>'
            .format(file=project_file_name, package=str(package)))

    package_path = files(package)

    while package_path.parent != package_path:
        possible_project_file_path = package_path.joinpath(project_file_name)
        if possible_project_file_path.exists():
            return tomlkit.parse(possible_project_file_path.read_text())
        package_path = package_path.parent

    raise ValueError(
        'Could not find <{file}>-file in package <{package}>'.format(
            file=project_file_name, package=str(package)))
示例#12
0
def test_toml_document_with_dotted_keys(example):
    content = example("0.5.0")

    doc = parse(content)

    assert "physical" in doc
    assert "color" in doc["physical"]
    assert "shape" in doc["physical"]
    assert doc["physical"]["color"] == "orange"
    assert doc["physical"]["shape"] == "round"

    assert "site" in doc
    assert "google.com" in doc["site"]
    assert doc["site"]["google.com"]

    assert doc["a"]["b"]["c"] == 1
    assert doc["a"]["b"]["d"] == 2
示例#13
0
def test_document_with_new_sub_table_after_other_table():
    content = """[foo]
name = "Bar"

[bar]
name = "Baz"

[foo.baz]
name = "Test 1"
"""

    doc = parse(content)
    assert doc["foo"]["name"] == "Bar"
    assert doc["bar"]["name"] == "Baz"
    assert doc["foo"]["baz"]["name"] == "Test 1"

    assert doc.as_string() == content
示例#14
0
def test_toml_document_has_always_a_new_line_after_table_header():
    content = """[section.sub]"""

    doc = parse(content)
    assert doc.as_string() == """[section.sub]"""

    doc["section"]["sub"]["foo"] = "bar"
    assert (
        doc.as_string()
        == """[section.sub]
foo = "bar"
"""
    )

    del doc["section"]["sub"]["foo"]

    assert doc.as_string() == """[section.sub]"""
    def load(self, projectBaseDir: Path) -> PackageMetadata:
        pyprojectPath = projectBaseDir.joinpath('pyproject.toml')

        with pyprojectPath.open('r') as t:
            lock = tomlkit.parse(t.read())

            toolParams = lock['tool']['poetry']

            packageName = str(toolParams['name'])
            packageVersion = float(str(toolParams['version']))

            return PackageMetadata(
                packageName,
                packageVersion,
                datetime.now(),
                RandomStringGenerator().generate(10),
            )
示例#16
0
def edit_tm_cfg(path, base_port, peers, *, custom_edit=None):
    doc = tomlkit.parse(open(path).read())
    # tendermint is start in process, not needed
    # doc['proxy_app'] = 'tcp://127.0.0.1:%d' % abci_port(base_port)
    doc["rpc"]["laddr"] = "tcp://0.0.0.0:%d" % ports.rpc_port(base_port)
    doc["rpc"]["pprof_laddr"] = "localhost:%d" % ports.pprof_port(base_port)
    doc["rpc"]["grpc_laddr"] = "tcp://0.0.0.0:%d" % ports.grpc_port_tx_only(
        base_port)
    doc["p2p"]["laddr"] = "tcp://0.0.0.0:%d" % ports.p2p_port(base_port)
    doc["p2p"]["persistent_peers"] = peers
    doc["p2p"]["addr_book_strict"] = False
    doc["p2p"]["allow_duplicate_ip"] = True
    doc["consensus"]["timeout_commit"] = "1s"
    doc["rpc"]["timeout_broadcast_tx_commit"] = "30s"
    if custom_edit is not None:
        custom_edit(doc)
    open(path, "w").write(tomlkit.dumps(doc))
示例#17
0
def test_valid_decode(test):
    toml_file = os.path.join(SPEC_TEST_DIR, "values", test + ".toml")
    yaml_file = os.path.join(SPEC_TEST_DIR, "values", test + ".yaml")
    with io.open(toml_file, encoding="utf-8") as f:
        toml_content = f.read()
        toml_val = parse(toml_content)

    if os.path.exists(yaml_file):
        with io.open(yaml_file, encoding="utf-8") as f:
            yaml_val = yaml.load(f.read(), Loader=loader)
    else:
        with io.open(os.path.join(SPEC_TEST_DIR, "values", test + ".json"),
                     encoding="utf-8") as f:
            yaml_val = untag(json.loads(f.read()))

    assert toml_val == yaml_val
    assert toml_val.as_string() == toml_content
示例#18
0
def test_locker_properly_loads_extras(locker):
    content = """\
# @generated

[[package]]
category = "main"
description = "httplib2 caching for requests"
name = "cachecontrol"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "0.12.5"

[package.dependencies]
msgpack = "*"
requests = "*"

[package.dependencies.lockfile]
optional = true
version = ">=0.9"

[package.extras]
filecache = ["lockfile (>=0.9)"]
redis = ["redis (>=2.10.5)"]

[metadata]
content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77"
lock-version = "1.1"
python-versions = "~2.7 || ^3.4"

[metadata.files]
cachecontrol = []
"""

    locker.lock.write(tomlkit.parse(content))

    packages = locker.locked_repository().packages

    assert 1 == len(packages)

    package = packages[0]
    assert 3 == len(package.requires)
    assert 2 == len(package.extras)

    lockfile_dep = package.extras["filecache"][0]
    assert lockfile_dep.name == "lockfile"
示例#19
0
def test_string_output_order_is_preserved_for_out_of_order_tables():
    content = """
[tool.poetry]
name = "foo"

[tool.poetry.dependencies]
python = "^3.6"
bar = "^1.0"


[build-system]
requires = ["poetry-core"]
backend = "poetry.core.masonry.api"


[tool.other]
a = "b"
"""

    doc = parse(content)
    constraint = tomlkit.inline_table()
    constraint["version"] = "^1.0"
    doc["tool"]["poetry"]["dependencies"]["bar"] = constraint

    assert "^1.0" == doc["tool"]["poetry"]["dependencies"]["bar"]["version"]

    expected = """
[tool.poetry]
name = "foo"

[tool.poetry.dependencies]
python = "^3.6"
bar = {version = "^1.0"}


[build-system]
requires = ["poetry-core"]
backend = "poetry.core.masonry.api"


[tool.other]
a = "b"
"""

    assert expected == doc.as_string()
示例#20
0
def read_toml(tomlpath: AnyStr) -> XgmContainer:
    """read an XgmContainer from a toml file and its content files

    :param tomlpath: path to the toml file
    :return: XgmContainer instance read from tomlpath
    """
    tomldir = os.path.dirname(tomlpath)
    with open(tomlpath, "rt", encoding="utf-8") as tomlfile:
        tomldoc = tomlkit.parse(tomlfile.read())

    if not ("ImageItem" in tomldoc or "ModelItem" in tomldoc):
        return XgmContainer([], [])

    imageitems = []
    for tomlimage in tomldoc["ImageItem"]:
        # 1. read ImageItem info from TOML
        name16 = tomlimage["name16"]
        filepath = tomlimage.get("file-path")
        if filepath is None:
            filepath = name16
        # 2. read file data
        with open(os.path.join(tomldir, filepath), "rb") as itemfile:
            filedata = itemfile.read()
        # 3. Convert to XgmImageItem
        imageitems.append(XgmImageItem(name16, filedata))

    modelitems = []
    for tomlmodel in tomldoc["ModelItem"]:
        # 1. read ImageItem info from TOML
        name16 = tomlmodel["name16"]
        filepath = tomlmodel.get("file-path")
        if filepath is None:
            filepath = name16
        animseppath = tomlmodel.get("animsep-path")
        if animseppath is None:
            animseppath = replaceext(filepath, ANIMSEP_EXT)
        # 2. read file data
        with open(os.path.join(tomldir, filepath), "rb") as itemfile:
            filedata = itemfile.read()
        with open(os.path.join(tomldir, animseppath), "rb") as animsepfile:
            animsepdata = animsepfile.read()
        # 3. Convert to XgmModelItem
        modelitems.append(XgmModelItem(name16, filedata, animsepdata))

    return XgmContainer(imageitems, modelitems)
示例#21
0
def test_adding_an_element_to_existing_table_with_ws_remove_ws():
    content = """[foo]

[foo.bar]

"""

    doc = parse(content)
    doc["foo"]["int"] = 34

    expected = """[foo]
int = 34

[foo.bar]

"""

    assert expected == doc.as_string()
示例#22
0
def get_project_meta() -> Optional[Dict]:
    pyproject = Path(__file__).parent / "../../pyproject.toml"

    if not pyproject.is_file():
        return None

    with pyproject.open() as pp_file:
        file_contents = pp_file.read()

    project_meta = None

    try:
        project_meta = tomlkit.parse(file_contents)
    except Exception as e:
        logger.error("Error parsing project metadata: {}".format(e))
        return None

    return project_meta
示例#23
0
def test_fill_descriptor(runner, disable_log):
    source = data_filename_resolver("cli/data/simple_cubes.mcnp")
    with runner.isolated_filesystem() as prefix:
        output = Path(prefix) / "simple_cubes.universes/fill-descriptor.toml"
        result = runner.invoke(
            mckit, args=["decompose", source], catch_exceptions=False
        )
        assert result.exit_code == 0, "Should success"
        assert output.exists()
        with output.open() as fid:
            fill_descriptor = fid.read()
            assert fill_descriptor.find("simple_cubes.mcnp")
            fill_descriptor = tk.parse(fill_descriptor)
            assert "created" in fill_descriptor
            assert "2" in fill_descriptor
            assert "universe" in fill_descriptor["2"]
            assert 1 == fill_descriptor["2"]["universe"]
            assert "u1.i" == fill_descriptor["2"]["file"]
示例#24
0
def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:
    """
    Return the version information from the [tool.poetry] section of the
    pyproject.toml file. The version may be in non standardized form.
    """
    if not pyproject_toml_path:
        path = Path(__file__)
        pyproject_toml_path = path.parent.parent / 'pyproject.toml'

    if not pyproject_toml_path.exists():
        raise RuntimeError('pyproject.toml file not found.')

    pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())
    if ('tool' in pyproject_toml and 'poetry' in pyproject_toml['tool']
            and 'version' in pyproject_toml['tool']['poetry']):
        return pyproject_toml['tool']['poetry']['version']

    raise RuntimeError('Version information not found in pyproject.toml file.')
示例#25
0
def get_default_module_file() -> Path:
    try:
        import tomlkit
    except ImportError:
        print("Could not get default module because 'tomlkit' is not installed")
        print("$ pip install tomlkit")
        sys.exit(1)
    pyproject = Path() / "pyproject.toml"
    if not pyproject.exists():
        print(f"Could not find pyproject.toml, looked at {pyproject}")
        sys.exit(1)
    data = tomlkit.parse(pyproject.read_text())
    try:
        module_name = data["tool"]["flit"]["metadata"]["module"]
    except Exception:
        print(f"Could not extract tool.flit.metadata.module from {pyproject}")
        sys.exit(1)
    return file_path(module_name)
示例#26
0
def test_strings_behave_like_strs():
    i = item("foo")

    assert i == "foo"
    assert i.as_string() == '"foo"'

    i += " bar"
    assert i == "foo bar"
    assert i.as_string() == '"foo bar"'

    i += " é"
    assert i == "foo bar é"
    assert i.as_string() == '"foo bar é"'

    doc = parse('str = "foo" # Comment')
    doc["str"] += " bar"

    assert doc.as_string() == 'str = "foo bar" # Comment'
示例#27
0
def test_dates_behave_like_dates():
    i = item(date(2018, 7, 22))

    assert i == date(2018, 7, 22)
    assert i.as_string() == "2018-07-22"

    i += timedelta(days=1)
    assert i == datetime(2018, 7, 23)
    assert i.as_string() == "2018-07-23"

    i -= timedelta(days=2)
    assert i == date(2018, 7, 21)
    assert i.as_string() == "2018-07-21"

    doc = parse("dt = 2018-07-22 # Comment")
    doc["dt"] += timedelta(days=1)

    assert doc.as_string() == "dt = 2018-07-23 # Comment"
示例#28
0
def test_datetimes_behave_like_datetimes():
    i = item(datetime(2018, 7, 22, 12, 34, 56))

    assert i == datetime(2018, 7, 22, 12, 34, 56)
    assert i.as_string() == "2018-07-22T12:34:56"

    i += timedelta(days=1)
    assert i == datetime(2018, 7, 23, 12, 34, 56)
    assert i.as_string() == "2018-07-23T12:34:56"

    i -= timedelta(days=2)
    assert i == datetime(2018, 7, 21, 12, 34, 56)
    assert i.as_string() == "2018-07-21T12:34:56"

    doc = parse("dt = 2018-07-22T12:34:56-05:00")
    doc["dt"] += timedelta(days=1)

    assert doc.as_string() == "dt = 2018-07-23T12:34:56-05:00"
示例#29
0
def test_floats_behave_like_floats():
    i = item(34.12)

    assert i == 34.12
    assert i.as_string() == "34.12"

    i += 1
    assert i == 35.12
    assert i.as_string() == "35.12"

    i -= 2
    assert i == 33.12
    assert i.as_string() == "33.12"

    doc = parse("float = +34.12")
    doc["float"] += 1

    assert doc.as_string() == "float = +35.12"
示例#30
0
def test_integers_behave_like_ints():
    i = item(34)

    assert i == 34
    assert i.as_string() == "34"

    i += 1
    assert i == 35
    assert i.as_string() == "35"

    i -= 2
    assert i == 33
    assert i.as_string() == "33"

    doc = parse("int = +34")
    doc["int"] += 1

    assert doc.as_string() == "int = +35"
示例#31
0
文件: conf.py 项目: amolenaar/gaphor
# sys.path.insert(0, os.path.abspath('.'))
from pathlib import Path

from tomlkit import parse

# -- Project information -----------------------------------------------------

project = "Gaphor"
copyright = "2019, Arjan J. Molenaar"
author = "Arjan J. Molenaar"

# The short X.Y version
version = ""
project_dir = Path(__file__).resolve().parent.parent
f = project_dir.joinpath("pyproject.toml")
release = parse(f.read_text())["tool"]["poetry"]["version"]

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.doctest",
    "sphinx.ext.intersphinx",
    "sphinx.ext.coverage",