예제 #1
0
    def to_toml(
        self,
        filename: Optional[Union[str, PathLike]] = None,
        encoding: str = "utf-8",
        errors: str = "strict",
    ) -> Optional[str]:
        """Convert to a toml string or save it to toml file

        Args:
            filename: The filename to save the toml to, if not given a toml
                string will be returned
            encoding: The encoding for saving to file
            errors: The errors handling for saveing to file
                See python's open function

        Returns:
            The toml string with filename is not given
        """
        try:
            import rtoml  # type: ignore[import]
        except ImportError as exc:  # pragma: no cover
            raise ImportError(
                "You need rtoml installed to export Diot as toml.") from exc
        toml_dump = self.to_dict()
        if not filename:
            return rtoml.dumps(toml_dump)
        with open(filename, "w", encoding=encoding, errors=errors) as ftml:
            rtoml.dump(toml_dump, ftml)
        return None
예제 #2
0
def project_with_setuptools_pep621_backend(tmp_path_factory):
    """
    Returns a temporary directory containing a PEP 621
    pyproject.toml configured to use setuptools as the
    build backend.
    """
    # Create the folder and pyproject.toml file
    folder: Path = tmp_path_factory.mktemp("myrepo")
    pyproject_toml = folder.joinpath("pyproject.toml")
    pyproject_toml.touch()

    build_system = {
        "build-system": {
            "requires": [
                "setuptools>=61",
                "wheel>=0.37.1",
            ],
            "build-backend": "setuptools.build_meta",
        }
    }

    with open(pyproject_toml, mode="w", encoding="utf-8") as f:
        rtoml.dump(build_system, f)

    return folder
예제 #3
0
def fix_manifest(file):
    # file is the absolute path to a manifest
    print(f"   Patching {file}...")
    try:
        manifest = rtoml.load(Path(file))
    except rtoml.TomlParsingError as ex:
        print(f"FAILED to patch {file}: " + str(ex))
        raise

    if "origin" not in manifest:
        print(f"   WARNING: manifest without origin? {file}")
        return

    origin = dict()
    url = fix_url(manifest.pop("origin"))

    # Move any '@commit' info to a separate key
    origin["url"] = url.split('@')[0]
    if '@' in url:
        origin["commit"] = url.split('@')[1]

    if "origin-hashes" in manifest:
        origin["hashes"] = manifest.pop("origin-hashes")
    if "archive-name" in manifest:
        origin["archive-name"] = manifest.pop("archive-name")

    manifest["origin"] = origin

    # Rewrite patched manifest
    with open(file, "wt") as file:
        rtoml.dump(utils.fix_manifest_order(manifest), file)
예제 #4
0
def create_variable_file(df, fn_variables):
    keys = df.keys()
    data = dict()
    for k in keys:
        data[k] = {"category": "general", "description": "", "unit": ""}
    with open(fn_variables, "w") as f:
        toml.dump(data, f)
예제 #5
0
 def write(self):
     if not os.path.exists(self.directory):
         os.makedirs(self.directory, exist_ok=True)
     lexicon_dict = {
         "words": [(word[0], word[1]) for word in self.words],
     }
     with open(self.toml_path, "w") as toml_file:
         toml.dump(lexicon_dict, toml_file)
예제 #6
0
 def write(self):
     if not os.path.exists(self.directory):
         os.makedirs(self.directory)
     report_dict = {
         "corpus_name": self.corpus_name,
         "lexicon_names": self.lexicon_names,
         "complete": self.complete,
     }
     with open(self.toml_path, "w") as toml_file:
         toml.dump(report_dict, toml_file)
예제 #7
0
 def write(self) -> None:
     """Serialize and write the report to disk"""
     if not os.path.exists(self.directory):
         os.makedirs(self.directory)
     report_dict = {
         "corpus_name": self.corpus_name,
         "lexicon_names": self.lexicon_names,
         "complete": self.complete,
     }
     with open(self.toml_path, "w", encoding="utf-8") as toml_file:
         toml.dump(report_dict, toml_file)
예제 #8
0
    def write(self, path: Path = defaults.CONFIG_FILE) -> None:
        """
        Overwrites the config file at `path` with the attributes from
        the calling instance.

        Args:
            path (Path, optional): Config file to overwrite.
                Defaults to defaults.CONFIG_FILE.
        """
        with open(path, mode="w", encoding="utf-8") as f:
            rtoml.dump({"pytoil": self.to_dict()}, f, pretty=True)
예제 #9
0
파일: split-crates.py 프로젝트: onox/alr
def write_externals(crate_dirname, name, general, external):
    print(f"      Writing externals for {name}...")
    crate = deepcopy(general)
    crate["name"] = name
    crate["external"] = external

    fix_order(crate)

    with open(os.path.join(crate_dirname, name + "-external.toml"),
              "wt") as file:
        rtoml.dump(crate, file)
예제 #10
0
def migrate(path):
    print(f"Migrating {path}...")

    # Visit all nested TOML files. We do not check for proper placement.
    for file in glob.iglob(os.path.join(path, '**/*.toml'), recursive=True):
        if "/index.toml" not in file and "-external.toml" not in file:
            fix_manifest(file)

    # Finalize by updating the index version
    with open(os.path.join(path, "index.toml"), "wt") as file:
        rtoml.dump(INTO_VERSION, file)
예제 #11
0
 def write(self):
     if not os.path.exists(self.directory):
         os.makedirs(self.directory, exist_ok=True)
     corpus_dict = {
         "type": RedditCorpus.corpus_type,
         "subreddits": self.subreddits,
         "start_time": self.start_time.isoformat(),
         "end_time": self.end_time.isoformat(),
         "compiled": self.compiled,
     }
     with open(self.toml_path, "w") as toml_file:
         toml.dump(corpus_dict, toml_file)
예제 #12
0
파일: split-crates.py 프로젝트: onox/alr
def migrate(path):
    print(f"Migrating {path}...")

    # All two-letter folders will contain crates...
    with os.scandir(path) as contents:
        for entry in contents:
            if len(entry.name) == 2 and entry.is_dir():
                split_contents(entry.path)
            elif entry.name != "index.toml":
                print(f"EXTRANEOUS entry looking for shelves: {entry.path}")

    # Finalize by updating the index version
    with open(os.path.join(path, "index.toml"), "wt") as file:
        rtoml.dump(INTO_VERSION, file)
예제 #13
0
파일: split-crates.py 프로젝트: onox/alr
def write_release(crate_dirname, name, general, version, release):
    print(f"      Writing release {name}={version}...")
    crate = deepcopy(general)
    crate["name"] = name
    crate["version"] = version

    # Ensure proper merging
    for key in release:
        if key in crate:
            if isinstance(crate[key], dict) and isinstance(release[key], dict):
                # Merge these dicts:
                crate[key].update(release[key])
            elif isinstance(crate[key], list) and isinstance(
                    release[key], list):
                # Merge arrays
                crate[key] += release[key]
            else:
                raise RuntimeError(
                    f"Key {key} is both in general and release sections in {crate_dirname} {version}"
                )
        else:
            crate[key] = release[key]

    # Fix relative origins which now are one level deeper than before
    if "origin" in crate and "../" in crate["origin"]:
        crate['origin'] = crate['origin'].replace("../", "../../", 1)

    # Fix dictionary ordering to ensure atoms (or arrays of atoms) are before tables/arrays of tables. Otherwise, rtoml
    # does not do it for us, and we end with an invalid TOML serialization order (top-level atoms must come before any
    # tables/arrays).

    fix_order(crate)

    # Fix `depends-on` to be an array. No matter that dependencies are grouped.
    if "depends-on" in crate:
        deps = crate.pop("depends-on")
        crate["depends-on"] = [deps]

    with open(os.path.join(crate_dirname, name + f"-{version}.toml"),
              "wt") as file:
        rtoml.dump(crate, file)
예제 #14
0
def fake_flit_project(tmp_path_factory):
    """
    Returns a temporary directory containing a
    valid flit pyproject.toml file.
    """

    # Create the folder and pyproject.toml file
    folder: Path = tmp_path_factory.mktemp("myrepo")
    pyproject_toml = folder.joinpath("pyproject.toml")
    pyproject_toml.touch()

    # Create some fake poetry content
    build_system = {
        "build-system": {
            "requires": ["flit_core >=2,<4"],
            "build-backend": "flit_core.buildapi",
        },
    }

    with open(pyproject_toml, mode="w", encoding="utf-8") as f:
        rtoml.dump(build_system, f)

    return folder
예제 #15
0
def project_with_no_build_backend(tmp_path_factory):
    """
    Returns a temporary directory containing a
    pyproject.toml.
    This time we do write a 'build-system' but no
    'build-backend'.
    """

    # Create the folder and pyproject.toml file
    folder: Path = tmp_path_factory.mktemp("myrepo")
    pyproject_toml = folder.joinpath("pyproject.toml")
    pyproject_toml.touch()

    # Create some fake build-system
    build_system = {
        "build-system": {
            "requires": ["poetry-core>=1.0.0"],
        },
    }

    with open(pyproject_toml, mode="w", encoding="utf-8") as f:
        rtoml.dump(build_system, f)

    return folder
예제 #16
0
def write_options(optfile, **opts):
    with optfile.open("w") as f:
        rtoml.dump(opts, f)
예제 #17
0
from pathlib import Path

import rtoml
from diot import Diot

in_metafile = Path({{in.metafile | quote}})        # pyright: ignore
in_gmtfile = Path({{in.gmtfile | quote}})          # pyright: ignore
in_config = {{in.config | repr}}                   # pyright: ignore
out_metafile = Path({{out.metafile | quote}})      # pyright: ignore
out_gmtfile = Path({{out.gmtfile | quote}})        # pyright: ignore
out_configfile = Path({{out.configfile | quote}})  # pyright: ignore

# make symbolic link of in.metafile to out.metafile
out_metafile.symlink_to(in_metafile)

# make symbolic link of in.gmtfile to out.gmtfile
out_gmtfile.symlink_to(in_gmtfile)

# Check the config and save as toml file
if isinstance(in_config, str):
    if "\n" not in in_config and Path(in_config).is_file():
        Path(in_config).symlink_to(out_configfile)
    else:  # A toml string
        with out_configfile.open("w") as f:
            f.write(in_config)
else:  # A python dictionary
    with out_configfile.open("w") as f:
        rtoml.dump(in_config, f)
예제 #18
0
def test_dump_path(tmp_path):
    p = tmp_path / 'test.toml'
    assert rtoml.dump({'foo': 'bar'}, p) == 12
    assert p.read_text() == 'foo = "bar"\n'
예제 #19
0
def test_dump_file(tmp_path):
    p = tmp_path / 'test.toml'
    with p.open('w') as f:
        assert rtoml.dump({'foo': 'bar'}, f) == 12
    assert p.read_text() == 'foo = "bar"\n'
예제 #20
0
 def persist_settings(settings: Dict[str, Any]) -> None:
     with open(constants.settings_path, "w",
               encoding="utf-8") as settings_file:
         toml.dump(settings, settings_file)
예제 #21
0
import json
import rtoml

configstr = {{in.config | repr}}
outfile = {{out.outfile | quote}}
infmt = {{envs.infmt | quote}}
outfmt = {{envs.outfmt | quote}}

data = rtoml.loads(configstr) if infmt == "toml" else json.loads(configstr)

with open(outfile, "w") as fout:
    if outfmt == "toml":
        rtoml.dump(data, fout)
    else:
        json.dump(data, fout)