Python toml.loads() Examples

The following are 30 code examples of toml.loads(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module toml , or try the search function .
Example #1
Source File: helpers.py    From shakedown with Apache License 2.0 6 votes vote down vote up
def read_config(args):
    """ Read configuration options from ~/.shakedown (if exists)

        :param args: a dict of arguments
        :type args: dict

        :return: a dict of arguments
        :rtype: dict
    """

    configfile = os.path.expanduser('~/.shakedown')

    if os.path.isfile(configfile):
        with open(configfile, 'r') as f:
            config = toml.loads(f.read())

        for key in config:
            param = key.replace('-', '_')

            if not param in args or args[param] in [False, None]:
                args[param] = config[key]

    return args 
Example #2
Source File: logs.py    From sawtooth-core with Apache License 2.0 6 votes vote down vote up
def _get_config():
    """Determines if there is a log config in the config directory
       and returns it. If it does not exist, return None.

    Returns:
        log_config (dict): The dictionary to pass to logging.config.dictConfig
    """
    conf_file = os.path.join(_get_config_dir(), 'log_config.toml')
    if os.path.exists(conf_file):
        with open(conf_file) as fd:
            raw_config = fd.read()
        log_config = toml.loads(raw_config)
        return log_config

    conf_file = os.path.join(_get_config_dir(), 'log_config.yaml')
    if os.path.exists(conf_file):
        with open(conf_file) as fd:
            raw_config = fd.read()
        log_config = yaml.safe_load(raw_config)
        return log_config

    return None 
Example #3
Source File: loader.py    From python-shortcuts with MIT License 6 votes vote down vote up
def loads(cls, string: str) -> 'Shortcut':
        from shortcuts import Shortcut  # noqa

        if isinstance(string, (bytearray, bytes)):
            string = string.decode('utf-8')

        shortcut_dict = toml.loads(string)
        shortcut = Shortcut(name=shortcut_dict.get('name', 'python-shortcuts'))

        if not isinstance(shortcut_dict.get('action'), list):
            raise ValueError('toml file must contain "action" array with actions')

        for params in shortcut_dict['action']:
            action_params = copy.deepcopy(params)
            del action_params['type']

            action_class = actions_registry.get_by_keyword(params['type'])
            action = action_class(data=action_params)
            shortcut.actions.append(action)

        return shortcut 
Example #4
Source File: loader.py    From python-shortcuts with MIT License 6 votes vote down vote up
def loads(cls, string: Union[str, bytes]) -> 'Shortcut':
        from shortcuts import Shortcut  # noqa

        if isinstance(string, str):
            string = string.encode('utf-8')

        shortcut_dict: Dict = plistlib.loads(string)
        shortcut = Shortcut(
            name=shortcut_dict.get('name', 'python-shortcuts'),
            client_release=shortcut_dict['WFWorkflowClientRelease'],
            client_version=shortcut_dict['WFWorkflowClientVersion'],
        )

        for action in shortcut_dict['WFWorkflowActions']:
            shortcut.actions.append(cls._action_from_dict(action))

        return shortcut 
Example #5
Source File: cli_config.py    From sawtooth-core with Apache License 2.0 6 votes vote down vote up
def _load_toml_cli_config(filename=None):
    if filename is None:
        filename = os.path.join(
            _get_config_dir(),
            'cli.toml')

    if not os.path.exists(filename):
        LOGGER.info(
            "Skipping CLI config loading from non-existent config file: %s",
            filename)

        return {}

    LOGGER.info("Loading CLI information from config: %s", filename)

    try:
        with open(filename) as fd:
            raw_config = fd.read()
    except IOError as e:
        raise CliConfigurationError(
            "Unable to load CLI configuration file: {}".format(str(e)))

    return toml.loads(raw_config) 
Example #6
Source File: repo.py    From TagBot with MIT License 6 votes vote down vote up
def is_registered(self) -> bool:
        """Check whether or not the repository belongs to a registered package."""
        try:
            root = self._registry_path
        except InvalidProject as e:
            logger.debug(e.message)
            return False
        if not root:
            return False
        contents = self._only(self._registry.get_contents(f"{root}/Package.toml"))
        package = toml.loads(contents.decoded_content.decode())
        gh = cast(str, urlparse(self._gh_url).hostname).replace(".", r"\.")
        if "@" in package["repo"]:
            pattern = rf"{gh}:(.*?)(?:\.git)?$"
        else:
            pattern = rf"{gh}/(.*?)(?:\.git)?$"
        m = re.search(pattern, package["repo"])
        if not m:
            return False
        # I'm not really sure why mypy doesn't like this line without the cast.
        return cast(bool, m[1].casefold() == self._repo.full_name.casefold()) 
Example #7
Source File: config.py    From indy-ssivc-tutorial with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        # Load entity config
        config_path = os.path.abspath(settings.BASE_DIR + '/config.toml')
        try:
            with open(config_path, 'r') as config_file:
                config_toml = config_file.read()
        except FileNotFoundError as e:
            logger.error('Could not find config.toml. Exiting.')
            raise

        self.config = toml.loads(config_toml)

        genesis_txn_path = "/app/.genesis"

        checkGenesisFile(genesis_txn_path)
        self.config["genesis_txn_path"] = genesis_txn_path
        # bhs: configuring/grabbing the wallet seed is now done through agent.py
        # at least in theory. so i'm commenting this one out to make sure
        # we are using the same env vars as much as possible :(
        #
        # Wallet seeds should be configurable through env or the config file
#        self.config["wallet_seed"] = os.getenv('WALLET_SEED',
#            self.config["wallet_seed"]) 
Example #8
Source File: updater.py    From pipenv with MIT License 6 votes vote down vote up
def update(cls, content, dependency, version, spec="==", hashes=()):
        data = toml.loads(content)
        if data:
            for package_type in ['packages', 'dev-packages']:
                if package_type in data:
                    if dependency.full_name in data[package_type]:
                        data[package_type][dependency.full_name] = "{spec}{version}".format(
                            spec=spec, version=version
                        )
        try:
            from pipenv.project import Project
        except ImportError:
            raise ImportError("Updating a Pipfile requires the pipenv extra to be installed. Install it with "
                              "pip install dparse[pipenv]")
        pipfile = tempfile.NamedTemporaryFile(delete=False)
        p = Project(chdir=False)
        p.write_toml(data=data, path=pipfile.name)
        data = open(pipfile.name).read()
        os.remove(pipfile.name)
        return data 
Example #9
Source File: updater.py    From pipenv with MIT License 6 votes vote down vote up
def update(cls, content, dependency, version, spec="==", hashes=()):
        data = json.loads(content)
        if data:
            for package_type in ['default', 'develop']:
                if package_type in data:
                    if dependency.full_name in data[package_type]:
                        data[package_type][dependency.full_name] = {
                            'hashes': [
                                "{method}:{hash}".format(
                                    hash=h['hash'],
                                    method=h['method']
                                ) for h in hashes
                            ],
                            'version': "{spec}{version}".format(
                                spec=spec, version=version
                            )
                        }
        return json.dumps(data, indent=4, separators=(',', ': ')) + "\n" 
Example #10
Source File: convert.py    From utensor_cgen with Apache License 2.0 6 votes vote down vote up
def convert_graph(model_file, output_nodes=None, config='utensor_cli.toml', target='utensor', model_name=None):
  from utensor_cgen.frontend import FrontendSelector

  if os.path.exists(config):
    logger.info('config file {} found, reading configurations'.format(config))
    with open(config) as fid:
      config = loads(fid.read())
  else:
    config = {}
  ugraph = FrontendSelector.parse(
    model_file, output_nodes,
    config=config,
    model_name=model_name
  )
  backend = BackendManager.get_backend(target)(config)
  backend.apply(ugraph)
  return ugraph 
Example #11
Source File: testing.py    From mriqc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mock_config():
    """Create a mock config for documentation and testing purposes."""
    from . import config

    filename = Path(pkgrf("mriqc", "data/config-example.toml"))
    settings = loads(filename.read_text())
    for sectionname, configs in settings.items():
        if sectionname != "environment":
            section = getattr(config, sectionname)
            section.load(configs, init=False)
    config.nipype.init()
    config.loggers.init()

    config.execution.work_dir = Path(mkdtemp())
    config.execution.bids_dir = Path(pkgrf("mriqc", "data/tests/ds000005")).absolute()
    config.execution.init()

    yield 
Example #12
Source File: updater.py    From dparse with MIT License 6 votes vote down vote up
def update(cls, content, dependency, version, spec="==", hashes=()):
        data = json.loads(content)
        if data:
            for package_type in ['default', 'develop']:
                if package_type in data:
                    if dependency.full_name in data[package_type]:
                        data[package_type][dependency.full_name] = {
                            'hashes': [
                                "{method}:{hash}".format(
                                    hash=h['hash'],
                                    method=h['method']
                                ) for h in hashes
                            ],
                            'version': "{spec}{version}".format(
                                spec=spec, version=version
                            )
                        }
        return json.dumps(data, indent=4, separators=(',', ': ')) + "\n" 
Example #13
Source File: updater.py    From dparse with MIT License 6 votes vote down vote up
def update(cls, content, dependency, version, spec="==", hashes=()):
        data = toml.loads(content)
        if data:
            for package_type in ['packages', 'dev-packages']:
                if package_type in data:
                    if dependency.full_name in data[package_type]:
                        data[package_type][dependency.full_name] = "{spec}{version}".format(
                            spec=spec, version=version
                        )
        try:
            from pipenv.project import Project
        except ImportError:
            raise ImportError("Updating a Pipfile requires the pipenv extra to be installed. Install it with "
                              "pip install dparse[pipenv]")
        pipfile = tempfile.NamedTemporaryFile(delete=False)
        p = Project(chdir=False)
        p.write_toml(data=data, path=pipfile.name)
        data = open(pipfile.name).read()
        os.remove(pipfile.name)
        return data 
Example #14
Source File: build.py    From flutter-app-template with MIT License 6 votes vote down vote up
def collect_env(args):
    PROJ_DIR = look_for_proj_dir(os.path.abspath(__file__), 'pubspec.yaml')
    RUST_PROJ_DIR = os.path.join(PROJ_DIR, 'rust')
    RUST_ASSETS_DIR = os.path.join(RUST_PROJ_DIR, 'assets')
    TOML_FILE = os.path.join(RUST_PROJ_DIR, 'Cargo.toml')
    META = toml.loads(open(TOML_FILE).read())
    NAME = META['package']['name']
    VERSION = META['package']['version']
    DESCRIPTION = META['package']['description']

    DEBUG = not args.release
    RELEASE = args.release

    TARGET_DIR = os.path.join(RUST_PROJ_DIR, 'target')
    WORKSPACE = get_workspace_dir(RUST_PROJ_DIR)
    WORKSPACE_TARGET_DIR = os.path.join(WORKSPACE, 'target') if WORKSPACE else None
    OUTPUT_DIR = os.path.join(TARGET_DIR, 'debug' if DEBUG else 'release')
    FLUTTER_CONFIG = META['package']['metadata']['flutter']
    IDENTIFIER =  FLUTTER_CONFIG['identifier'] if 'identifier' in FLUTTER_CONFIG else 'one.juju.flutter-app'
    FLUTTER_LIB_VER = get_flutter_version()
    FLUTTER_ASSETS = os.path.join(os.path.dirname(RUST_PROJ_DIR), 'build', 'flutter_assets')
    return locals() 
Example #15
Source File: repo.py    From TagBot with MIT License 5 votes vote down vote up
def _project(self, k: str) -> str:
        """Get a value from the Project.toml."""
        if self.__project is not None:
            return str(self.__project[k])
        for name in ["Project.toml", "JuliaProject.toml"]:
            try:
                contents = self._only(self._repo.get_contents(name))
                break
            except UnknownObjectException:
                pass
        else:
            raise InvalidProject("Project file was not found")
        self.__project = toml.loads(contents.decoded_content.decode())
        return str(self.__project[k]) 
Example #16
Source File: cli.py    From tracboat with GNU General Public License v3.0 5 votes vote down vote up
def _loads(content, fmt=None):
    if fmt == 'toml':
        return toml.loads(content)
    elif fmt == 'json':
        return json.loads(content, object_hook=json_util.object_hook)
    elif fmt == 'python':
        return ast.literal_eval(content)
    elif fmt == 'pickle':
        return pickle.loads(content)
    else:
        return content 
Example #17
Source File: utilities.py    From maildown with MIT License 5 votes vote down vote up
def get_config() -> MutableMapping[str, Any]:
    """
    Returns the existing configuration from the local environment
    """
    try:
        with open(os.path.join(os.path.expanduser("~"), "maildown.toml")) as f:
            return toml.loads(f.read())
    except FileNotFoundError:
        pass
    return {} 
Example #18
Source File: toml_conf.py    From pop with Apache License 2.0 5 votes vote down vote up
def render(hub, val):
    '''
    Take the string and render it in json
    '''
    return toml.loads(val) 
Example #19
Source File: config.py    From mriqc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load(filename):
    """Load settings from file."""
    from toml import loads

    filename = Path(filename)
    settings = loads(filename.read_text())
    for sectionname, configs in settings.items():
        if sectionname != "environment":
            section = getattr(sys.modules[__name__], sectionname)
            section.load(configs) 
Example #20
Source File: loader.py    From python-shortcuts with MIT License 5 votes vote down vote up
def loads(cls, string: str) -> 'Shortcut':
        raise NotImplementedError() 
Example #21
Source File: default_handlers.py    From python-frontmatter with MIT License 5 votes vote down vote up
def load(self, fm, **kwargs):
            return toml.loads(fm, **kwargs) 
Example #22
Source File: default_handlers.py    From python-frontmatter with MIT License 5 votes vote down vote up
def load(self, fm, **kwargs):
        return json.loads(fm, **kwargs) 
Example #23
Source File: default_handlers.py    From python-frontmatter with MIT License 5 votes vote down vote up
def detect(self, text):
        """
        Decide whether this handler can parse the given ``text``,
        and return True or False.

        Note that this is *not* called when passing a handler instance to 
        :py:func:`frontmatter.load <frontmatter.load>` or :py:func:`loads <frontmatter.loads>`.
        """
        if self.FM_BOUNDARY.match(text):
            return True
        return False 
Example #24
Source File: parser.py    From dparse with MIT License 5 votes vote down vote up
def parse(self):
        """
        Parse a Pipfile.lock (as seen in pipenv)
        :return:
        """
        try:
            data = json.loads(self.obj.content, object_pairs_hook=OrderedDict)
            if data:
                for package_type in ['default', 'develop']:
                    if package_type in data:
                        for name, meta in data[package_type].items():
                            # skip VCS dependencies
                            if 'version' not in meta:
                                continue
                            specs = meta['version']
                            hashes = meta['hashes']
                            self.obj.dependencies.append(
                                Dependency(
                                    name=name, specs=SpecifierSet(specs),
                                    dependency_type=filetypes.pipfile_lock,
                                    hashes=hashes,
                                    line=''.join([name, specs]),
                                    section=package_type
                                )
                            )
        except ValueError:
            pass 
Example #25
Source File: parser.py    From dparse with MIT License 5 votes vote down vote up
def parse(self):
        """
        Parse a Pipfile (as seen in pipenv)
        :return:
        """
        try:
            data = toml.loads(self.obj.content, _dict=OrderedDict)
            if data:
                for package_type in ['packages', 'dev-packages']:
                    if package_type in data:
                        for name, specs in data[package_type].items():
                            # skip on VCS dependencies
                            if not isinstance(specs, str):
                                continue
                            if specs == '*':
                                specs = ''
                            self.obj.dependencies.append(
                                Dependency(
                                    name=name, specs=SpecifierSet(specs),
                                    dependency_type=filetypes.pipfile,
                                    line=''.join([name, specs]),
                                    section=package_type
                                )
                            )
        except (toml.TomlDecodeError, IndexError) as e:
            pass 
Example #26
Source File: util.py    From archon with MIT License 5 votes vote down vote up
def parse_toml(filename):
    toml_string = toml_file(filename)
    parsed_toml = toml.loads(toml_string)
    return parsed_toml 
Example #27
Source File: thanks.py    From thanks with Apache License 2.0 5 votes vote down vote up
def pipfile(self, pipfile):
        project_data = toml.loads(pipfile)
        reqs = []
        reqs += list(project_data.get("packages", {}).keys())
        reqs += list(project_data.get("dev-packages", {}).keys())
        for req in reqs:
            self.package(req) 
Example #28
Source File: strategy.py    From archon with MIT License 5 votes vote down vote up
def agent_config():
    toml_string = toml_file("agent.toml")
    parsed_toml = toml.loads(toml_string)
    return parsed_toml 
Example #29
Source File: util.py    From archon with MIT License 5 votes vote down vote up
def parse_toml(filename):
    toml_string = toml_file(filename)
    parsed_toml = toml.loads(toml_string)
    return parsed_toml 
Example #30
Source File: stellar_toml.py    From py-stellar-base with Apache License 2.0 5 votes vote down vote up
def fetch_stellar_toml(
    domain: str,
    client: Union[BaseAsyncClient, BaseSyncClient] = None,
    use_http: bool = False,
) -> Union[Coroutine[Any, Any, Dict[str, Any]], Dict[str, Any]]:
    """Retrieve the stellar.toml file from a given domain.

    Retrieve the stellar.toml file for information about interacting with
    Stellar's federation protocol for a given Stellar Anchor (specified by a
    domain).

    :param domain: The domain the .toml file is hosted at.
    :param use_http: Specifies whether the request should go over plain HTTP vs HTTPS.
        Note it is recommend that you *always* use HTTPS.
    :param client: Http Client used to send the request.
    :return: The stellar.toml file as a an object via :func:`toml.loads`.
    :raises: :exc:`StellarTomlNotFoundError <stellar_sdk.sep.exceptions.StellarTomlNotFoundError>`:
        if the Stellar toml file could not not be found.
    """
    if not client:
        client = RequestsClient()

    toml_link = "/.well-known/stellar.toml"
    protocol = "https://"
    if use_http:
        protocol = "http://"
    url = protocol + domain + toml_link

    if isinstance(client, BaseAsyncClient):
        return __fetch_async(url, client)
    elif isinstance(client, BaseSyncClient):
        return __fetch_sync(url, client)
    else:
        raise TypeError(
            "This `client` class should be an instance "
            "of `stellar_sdk.client.base_async_client.BaseAsyncClient` "
            "or `stellar_sdk.client.base_sync_client.BaseSyncClient`."
        )