コード例 #1
0
ファイル: acronym.py プロジェクト: tjcsl/cslbot
def get_list():
    # wordlist (COMMON.TXT) from http://www.gutenberg.org/ebooks/3201
    rawlist = sorted(resources.read_text('cslbot.static', 'wordlist').splitlines())
    words = {}
    for key, group in groupby(rawlist, key=lambda word: word[0].lower()):
        words[key] = list(group)
    return words
コード例 #2
0
ファイル: config.py プロジェクト: tjcsl/cslbot
def migrate_config(config_file: str, config_obj: configparser.ConfigParser, send: Callable[[str], None]) -> None:
    example_obj = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    example_obj.read_string(resources.read_text('cslbot.static', 'config.example'))
    modified = False

    # Check for new sections/options
    for section in example_obj.sections():
        if not config_obj.has_section(section):
            send("Adding config section %s" % section)
            config_obj.add_section(section)
            modified = True
        for option in example_obj.options(section):
            if not config_obj.has_option(section, option):
                send("Adding default value for config option %s.%s" % (section, option))
                config_obj[section][option] = example_obj[section][option]
                modified = True
    # Check for removed sections/options
    for section in config_obj.sections():
        if example_obj.has_section(section):
            for option in config_obj.options(section):
                if not example_obj.has_option(section, option):
                    send("Obsolete config option %s.%s, consider removing." % (section, option))
        else:
            send("Obsolete config section %s, consider removing." % section)
    if modified:
        send("Config file automatically migrated, please review.")
        with open(config_file, 'w') as f:
            config_obj.write(f)
コード例 #3
0
ファイル: utils.py プロジェクト: JNRowe/rdial
def read_config(
    user_config: Optional[str] = None,
    cli_options: Optional[Dict[str, Union[bool, str]]] = None
) -> configparser.ConfigParser:
    """Read configuration data.

    Args:
        user_config: User defined config file
        cli_options: Command line options

    Returns:
        Parsed configuration data

    """
    # Only base *must* exist
    conf = configparser.ConfigParser()
    # No, it *really* must
    conf.read_string(resources.read_text('rdial', 'config'), 'pkg config')
    conf['DEFAULT'] = {'xdg_data_location': xdg_basedir.user_data('rdial')}
    for f in xdg_basedir.get_configs('rdial'):
        conf.read(f)
    conf.read(os.path.abspath('.rdialrc'))
    if user_config:
        conf.read(user_config)

    if cli_options:
        conf.read_dict({
            'rdial': {k: v
                      for k, v in cli_options.items()
                      if v is not None}
        })

    return conf
コード例 #4
0
ファイル: textutils.py プロジェクト: tjcsl/cslbot
def gen_slogan(msg):
    # Originally from sloganizer.com
    if not slogan_cache:
        slogan_cache.extend(resources.read_text('cslbot.static', 'slogans').splitlines())
    # handle arguments that end in '\', which is valid in irc, but causes issues with re.
    msg = msg.replace('\\', '\\\\')
    return re.sub('%s', msg, choice(slogan_cache))
コード例 #5
0
ファイル: textutils.py プロジェクト: tjcsl/cslbot
def gen_shakespeare(msg):
    # Originally from http://www.shmoop.com/shakespeare-translator/
    table = json.loads(resources.read_text('cslbot.static', 'shakespeare-dictionary.json'))
    replist = reversed(sorted(table.keys(), key=len))
    pattern = re.compile(r'\b(' + '|'.join(replist) + r')\b', re.I)
    # Normalize text to hopefully match more words.
    result = pattern.sub(lambda x: table[x.group().lower()], msg)
    return result
コード例 #6
0
ファイル: textutils.py プロジェクト: tjcsl/cslbot
def gen_word():
    r = random()

    if r < 0.8:
        wordlist = resources.read_text('cslbot.static', 'wordlist').strip().split()
        return choice(wordlist)
    else:
        return "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
コード例 #7
0
ファイル: config.py プロジェクト: tjcsl/cslbot
def do_setup(configfile: str) -> None:
    config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
    config.read_string(resources.read_text('cslbot.static', 'config.example'))
    do_config(config)
    configdir = dirname(configfile)
    try:
        if not exists(configdir):
            mkdir(configdir)
        with open(configfile, 'w') as cfgfile:
            config.write(cfgfile)
        groupsfile = join(configdir, 'groups.cfg')
        if not exists(groupsfile):
            with open(groupsfile, 'w') as f:
                f.write(resources.read_text('cslbot.static', 'groups.example'))
    except PermissionError:
        raise Exception("Please make sure that the user you are running CslBot as has permission to write to %s" % configdir)
    print('WARNING: you must set the db.engine option for the bot to work.')
    print("Configuration succeded, please review %s and restart the bot." % configfile)
コード例 #8
0
ファイル: test_read.py プロジェクト: Dongese/cpython
 def test_read_text_with_errors(self):
     # Raises UnicodeError without the 'errors' argument.
     self.assertRaises(
         UnicodeError, resources.read_text, self.data, 'utf-16.file')
     result = resources.read_text(self.data, 'utf-16.file', errors='ignore')
     self.assertEqual(
         result,
         'H\x00e\x00l\x00l\x00o\x00,\x00 '
         '\x00U\x00T\x00F\x00-\x001\x006\x00 '
         '\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00')
コード例 #9
0
ファイル: test_read.py プロジェクト: Dongese/cpython
 def test_read_text_default_encoding(self):
     result = resources.read_text(self.data, 'utf-8.file')
     self.assertEqual(result, 'Hello, UTF-8 world!\n')
コード例 #10
0
ファイル: shrek.py プロジェクト: tjcsl/cslbot
def get_list():
    return resources.read_text('cslbot.static', 'allstar.txt').splitlines()
コード例 #11
0
def get_asset_file(filename: str) -> str:
    return read_text(assets, filename)
コード例 #12
0
from importlib import resources as pkg_resources

# Authrocket uses RS256 public keys, so you can validate anywhere and safely
# store the key.
AUTHROCKET_PUB_KEY = pkg_resources.read_text('microsetta_private_api',
                                             "authrocket.pubkey")
CRONJOB_PUB_KEY = pkg_resources.read_text('microsetta_private_api',
                                          'cronjob.pubkey')
JWT_ISS_CLAIM_KEY = 'iss'
JWT_SUB_CLAIM_KEY = 'sub'
JWT_EMAIL_CLAIM_KEY = 'email'
ACCT_NOT_FOUND_MSG = "Account not found"
SRC_NOT_FOUND_MSG = "Source not found"
INVALID_TOKEN_MSG = "Invalid token"
コード例 #13
0
from . import span as tep_span
from . import table as tep_table
import text_extensions_for_pandas.resources

from text_extensions_for_pandas.jupyter.widget.stubs import (
    ipw, display, clear_output, HTML)

# TODO: This try/except block is for Python 3.6 support, and should be
# reduced to just importing importlib.resources when 3.6 support is dropped.
try:
    import importlib.resources as pkg_resources
except ImportError:
    import importlib_resources as pkg_resources

_WIDGET_SCRIPT: str = pkg_resources.read_text(
    text_extensions_for_pandas.resources, "dataframe_widget.js"
)
_WIDGET_STYLE: str = pkg_resources.read_text(
    text_extensions_for_pandas.resources, "dataframe_widget.css"
)
_WIDGET_TABLE_CONVERT_SCRIPT: str = pkg_resources.read_text(
    text_extensions_for_pandas.resources, "dataframe_widget_table_converter.js"
)


class DataFrameWidget:
    def __init__(
        self,
        dataframe: pd.DataFrame,
        metadata_column: pd.Series = None,
        interactive_columns: list = None,
コード例 #14
0
from importlib.resources import read_text

import pytest

import tests.data
from strace_parser.parser import get_parser


@pytest.mark.parametrize("line",
                         read_text(tests.data, "samples.txt").splitlines())
def test_parser_parses(line):
    get_parser().parse(line + "\n")


@pytest.mark.parametrize("line",
                         read_text(tests.data,
                                   "samples.txt").splitlines()[:20])
def test_parser_tree(line):
    print(get_parser().parse(line + "\n").pretty())
コード例 #15
0
async def teapot(request):
    import importlib.resources as pkg_resources

    teapot = pkg_resources.read_text(api, "teapot.txt")
    return text(teapot, status=418)
コード例 #16
0
from logzero import logger

from .utils import (config_directory, get_merged_config, merge_dict)
from .websocket import register_handler

handles = partial(register_handler, 'config')

logger.info('Reading configuration from %s', config_directory)
config_file = config_directory / 'config.json'
config = get_merged_config(config_file, 'default_config.json')

# create user macro template if not exists
macro_file = config_directory / 'macro.js'
if not macro_file.exists():
    template = resources.read_text('akimous.resources', 'macro.js')
    with open(macro_file, 'w') as f:
        f.write(template)
config['macro']['userMacroFile'] = macro_file.resolve().parts


@handles('_connected')
async def connected(msg, send, context):
    last_opened_folder = config['lastOpenedFolder']
    if not last_opened_folder or not Path(last_opened_folder).is_dir():
        config['lastOpenedFolder'] = None
    await send('Connected', {'config': config, 'pathSeparator': os.sep})


@handles('SetConfig')
async def set_config(msg, send, context):
コード例 #17
0
def _load_codes():
    #creates the tree
    root = ET.fromstring(pkg_resources.read_text(data, 'icd_10_v2019.xml'))
    for child in root:
        chapter_list.append(_CodeTree(child))
コード例 #18
0
def get_data():
    return importlib_resources.read_text(__name__, 'mydata.txt')
コード例 #19
0
URL_SAFE_CHARACTERS = frozenset(string.ascii_letters + string.digits +
                                "$-_.+!*'(),")

# This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
# The file contains additional information about the date that it was last updated.
try:
    from importlib.resources import read_text  # type: ignore
except ImportError:
    # If we don't have importlib.resources (Python 3.7+) or the importlib_resources
    # backport available, fall back to __file__ and hope we're on a filesystem.
    f = os.path.join(os.path.dirname(__file__), "vendor",
                     "tlds-alpha-by-domain.txt")
    with open(f) as tld_file:
        _tlds = tld_file.read().splitlines()
else:  # pragma: no cover  # new in Python 3.7
    _tlds = read_text("hypothesis.vendor",
                      "tlds-alpha-by-domain.txt").splitlines()
assert _tlds[0].startswith("#")
TOP_LEVEL_DOMAINS = ["COM"] + sorted(_tlds[1:], key=len)


class DomainNameStrategy(SearchStrategy):
    @staticmethod
    def clean_inputs(minimum, maximum, value, variable_name):
        if value is None:
            value = maximum
        elif not isinstance(value, int):
            raise InvalidArgument("Expected integer but %s is a %s" %
                                  (variable_name, type(value).__name__))
        elif not minimum <= value <= maximum:
            raise InvalidArgument("Invalid value %r < %s=%r < %r" %
                                  (minimum, variable_name, value, maximum))
コード例 #20
0
import os
import json
try:
    # python 3.7+
    import importlib.resources as config_loader
except ImportError:
    # python <= 3.6
    import importlib_resources as config_loader

# get the configuration
config_name = os.environ.get('PROGRAM_ARGS', 'wm-sasl-example.json')
if not config_loader.is_resource('pystreams.resources.env', config_name):
    if not config_name.endswith('.json'):
        config_name += '.json'
config = json.loads(
    config_loader.read_text('pystreams.resources.env', config_name))


def replace_envs(conf, composite_key=''):
    for key, val in conf.items():
        new_composite_key = f'{composite_key}.{key}' if composite_key else key
        if isinstance(val, dict):
            replace_envs(val, new_composite_key)
        else:
            if val == '_env_':
                conf[key] = os.environ[new_composite_key.replace('.',
                                                                 '_').upper()]


try:
    replace_envs(config)
コード例 #21
0
def resource_text(filename: str) -> str:
    return read_text('pre_commit.resources', filename)
コード例 #22
0
from typing import List
import json

try:
    import importlib.resources as pkg_resources
except ImportError:
    # Try backported to PY<37 `importlib_resources`.
    import importlib_resources as pkg_resources

from smsdk.tool_register import SmsdkEntities, smsdkentities
from smsdk.utils import module_utility
from smsdk import config
from smsdk.ma_session import MaSession

ENDPOINTS = json.loads(pkg_resources.read_text(config, "api_endpoints.json"))


@smsdkentities.register("factory")
class Factory(SmsdkEntities, MaSession):
    # Decorator to register a function as utility
    # Only the registered utilites would be accessible
    # to outside world via client.get_data()
    mod_util = module_utility()

    def __init__(self, session, base_url) -> None:
        self.session = session
        self.base_url = base_url

    @mod_util
    def get_utilities(self, *args, **kwargs) -> List:
        """
コード例 #23
0
ファイル: core.py プロジェクト: Nivedita01/docker_assessment
def contents():
    return read_text("certifi", "cacert.pem", encoding="ascii")
コード例 #24
0
ファイル: utils.py プロジェクト: tandemdude/gtfo-randomiser
def _load_rundown(rundown_id: int) -> rundowns.RundownLoadoutInfo:
    data = resources.read_text(rundown_data, f"rundown_{rundown_id}.json")
    return rundowns.RundownLoadoutInfo.from_json(json.loads(data))
コード例 #25
0
ファイル: conftest.py プロジェクト: tkieras/iscram
def get_data_from_file(filename: str) -> Dict:
    file_data = read_text("iscram.tests.system_graph_test_data", filename)
    return json.loads(file_data)
コード例 #26
0
ファイル: test_json.py プロジェクト: pestun/strace-parser
            for k, v in obj.items():
                _assert_fully_serialized(k)
                _assert_fully_serialized(v)
        elif isinstance(obj, list):
            for v in obj:
                _assert_fully_serialized(v)
        else:
            assert isinstance(
                obj, (str, float, bool)
            ), f"Unexpected type {obj} in {original}"

    original = obj
    _assert_fully_serialized(obj)


@pytest.mark.parametrize("line", read_text(data, "samples.txt").splitlines())
def test_json_fully_transforms(line):
    tree = get_parser().parse(line + "\n")
    result = to_json(tree)
    assert_fully_serialized(result)


def test_json_transformer():
    text = (
        "1577836800.000000 connect("
        r'0<\x01\x23\x45>, {sa_family=AF_UNIX, sun_path="\x01\x23\x45"}, 123)'
        " = -123 ENOENT (No such file or directory) <0.000001>\n"
    )
    parser = get_parser()
    tree = parser.parse(text)
    result = to_json(tree)
コード例 #27
0
ファイル: conftest.py プロジェクト: tkieras/iscram
def get_sg_from_file(filename: str) -> SystemGraph:
    file_data = read_text("iscram.tests.system_graph_test_data", filename)
    sg_dict = json.loads(file_data)
    return SystemGraph(**sg_dict)
コード例 #28
0
ファイル: prompt.py プロジェクト: tiosgz/udiskie
import sys

try:
    from importlib.resources import read_text
except ImportError:  # for Python<3.7
    from importlib_resources import read_text

from .async_ import exec_subprocess, run_bg, Future
from .locale import _
from .config import DeviceFilter

Gtk = None

__all__ = ['password', 'browser']

dialog_definition = read_text(__package__, 'password_dialog.ui')


class Dialog(Future):
    def __init__(self, window):
        self._enter_count = 0
        self.window = window
        self.window.connect("response", self._result_handler)

    def _result_handler(self, window, response):
        self.set_result(response)

    def __enter__(self):
        self._enter_count += 1
        self._awaken()
        return self
コード例 #29
0
ファイル: file_component.py プロジェクト: ambikads/zygoat
 def create(self):
     log.info(f"Creating {self.path}")
     os.makedirs(self.base_path, exist_ok=True)
     with open(self.path, "w") as f:
         f.write(il_resources.read_text(self.resource_pkg, self.filename))
コード例 #30
0
ファイル: util.py プロジェクト: pre-commit/pre-commit
def resource_text(filename):
    return read_text('pre_commit.resources', filename)
コード例 #31
0
def readRequest(filename: str) -> InsightEngineRequest:
    text = resources.read_text(engine.TestInputs, filename)
    requestJson = json.loads(text)
    request = InsightEngineRequest(**requestJson)
    return request
コード例 #32
0
ファイル: test_read.py プロジェクト: Dongese/cpython
 def execute(self, package, path):
     resources.read_text(package, path)
コード例 #33
0
ファイル: read_ttsx.py プロジェクト: patarapolw/ttslib
from importlib.resources import read_text
from ruamel import yaml
import re

for k, v in yaml.safe_load(read_text('ttslib.data', 'espeak.yaml')).items():
    lang = v['name']
    print(lang)

for k, v in yaml.safe_load(read_text('ttslib.data', 'windows.yaml')).items():
    name, lang = re.search(r'(\S+) \S+ - (.+$)', v['name']).groups()
    print(name, lang)
コード例 #34
0
ファイル: test_read.py プロジェクト: Dongese/cpython
 def test_read_text_given_encoding(self):
     result = resources.read_text(
         self.data, 'utf-16.file', encoding='utf-16')
     self.assertEqual(result, 'Hello, UTF-16 world!\n')
コード例 #35
0
def mock_item_q2_class():
    return json.loads(pkg_resources.read_text(
        mock_data,
        'mock_item_q2_class.json'
    ))
コード例 #36
0
ファイル: graph.py プロジェクト: jaraco/svg.charts
 def load_resource_stylesheet(name, subs=dict()):
     template = importlib_resources.read_text('svg.charts', name)
     source = template % subs
     return cssutils.parseString(source)
コード例 #37
0
import base64
import json
import importlib.resources as pkg_resources
from json.decoder import JSONDecodeError
import aiohttp
from aiohttp_requests import requests
from operator import itemgetter
import logging

import sizebot.data
from sizebot.conf import conf
from sizebot.lib.decimal import Decimal

logger = logging.getLogger("sizebot")

model_heights = json.loads(pkg_resources.read_text(sizebot.data,
                                                   "models.json"))


def get_model_scale(model, view, height_in_meters):
    normal_height = Decimal(model_heights[model][view])
    return height_in_meters / normal_height


def get_entity_json(name, model, view, height, x):
    scale = get_model_scale(model, view, height)
    return {
        "name": model,
        "customName": name,
        "scale": float(scale),
        "view": view,
        "x": str(x),
コード例 #38
0
def _create_provider_info_schema_validator():
    """Creates JSON schema validator from the provider_info.schema.json"""
    schema = json.loads(importlib_resources.read_text('airflow', 'provider_info.schema.json'))
    cls = jsonschema.validators.validator_for(schema)
    validator = cls(schema)
    return validator
コード例 #39
0
ファイル: metadata.py プロジェクト: merveenoyan/datasets
def load_json_resource(resource: str) -> Tuple[Any, str]:
    content = pkg_resources.read_text(resources, resource)
    return json.loads(content), f"{BASE_REF_URL}/resources/{resource}"
コード例 #40
0
ファイル: args.py プロジェクト: WhiteHalmos/emailer
def get_sample_config():
  return resources.read_text(emailer, 'sample-emailer.json')
コード例 #41
0
class IGMPCollector(Collector):
    default = yaml.load(read_text(junos, "igmp.yaml"), Loader=yaml.SafeLoader)
    name = "igmp"
    base_name = "{0}_{1}".format(base, name)

    def __init__(self,
                 device: junosdevice.JuniperNetworkDevice,
                 config_path: str = None) -> IGMPCollector:
        config = self.default
        if config_path is not None:
            with open(config_path, "r") as file:
                config = yaml.load(file, Loader=yaml.SafeLoader)
        super(IGMPCollector, self).__init__(self.base_name, device, config)
        self._init_prometheus_metrics(
            metric_configuration=JunosMetricConfiguration)

    @cached_property
    def allowed_networks(self):
        return [
            ipaddress.ip_network(prefix)
            for prefix in self.config["IGMP_NETWORKS"].get("allow", {}).keys()
        ]

    @cached_property
    def ignored_networks(self):
        return [
            ipaddress.ip_network(net)
            for net in self.config["IGMP_NETWORKS"].get("ignore", {}).keys()
        ]

    def get_igmp_broadcasts(self):
        igmp = self.device.get_igmp()
        counter = {}

        for broadcaster in self.config["IGMP_NETWORKS"]["allow"].values():
            counter[broadcaster] = 0

        for network in self.allowed_networks:
            for mgm_addresses in igmp.values():
                for address in mgm_addresses["mgm_addresses"]:
                    current_addr = ipaddress.ip_address(address)
                    if current_addr in network:
                        for ignore in self.ignored_networks:
                            if current_addr not in ignore:
                                counter[self.config["IGMP_NETWORKS"]["allow"][
                                    str(network)]] += 1
        return [{
            "broadcaster": broadcaster,
            "broadcasts": broadcasts
        } for broadcaster, broadcasts in counter.items()]

    def collect(self):
        igmp_list = self.get_igmp_broadcasts()
        for prometheus in self.prometheus_metrics.values():
            for interface in igmp_list:
                prometheus.metric.add_metric(
                    labels=self.get_labels(interface),
                    value=prometheus.function(
                        interface.get(prometheus.json_key)),
                )
            yield prometheus.metric
            prometheus.flush()
コード例 #42
0
ファイル: __init__.py プロジェクト: HucksApp/BIT_SHARK
__all__ = [
    'contents',
    'is_resource',
    'open_binary',
    'open_text',
    'path',
    'read_binary',
    'read_text',
]

# Use the Python 3.7 stdlib implementation if available.
if sys.version_info >= (3, 7):
    from importlib.resources import (Package, Resource, contents, is_resource,
                                     open_binary, open_text, path, read_binary,
                                     read_text)
    from importlib.abc import ResourceReader
    __all__.extend(['Package', 'Resource', 'ResourceReader'])
elif sys.version_info >= (3, ):
    from importlib_resources._py3 import (Package, Resource, contents,
                                          is_resource, open_binary, open_text,
                                          path, read_binary, read_text)
    from importlib_resources.abc import ResourceReader
    __all__.extend(['Package', 'Resource', 'ResourceReader'])
else:
    from importlib_resources._py2 import (contents, is_resource, open_binary,
                                          open_text, path, read_binary,
                                          read_text)

__version__ = read_text('importlib_resources', 'version.txt').strip()
コード例 #43
0
 def __init__(self, types_file: str):
     """
     load JSON dictionary from inside the package with possible types
     """
     types_json = pkg_resources.read_text(data, types_file)
     self.label_types = json.loads(types_json)
コード例 #44
0
 def load_colors(self):
     data = loads(resources.read_text("pyedit", "syntax_colors.json"))
     self.colors.update(**data)
def loads_yaml_config(m: Any, config_name: str) -> str:
    """Load yaml config `config_name' from module `m` as string."""
    m = import_module(m) if isinstance(m, str) else m
    config_str = pkg_resources.read_text(m, config_name)
    return config_str