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
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)
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
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))
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
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."
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)
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')
def test_read_text_default_encoding(self): result = resources.read_text(self.data, 'utf-8.file') self.assertEqual(result, 'Hello, UTF-8 world!\n')
def get_list(): return resources.read_text('cslbot.static', 'allstar.txt').splitlines()
def get_asset_file(filename: str) -> str: return read_text(assets, filename)
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"
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,
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())
async def teapot(request): import importlib.resources as pkg_resources teapot = pkg_resources.read_text(api, "teapot.txt") return text(teapot, status=418)
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):
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))
def get_data(): return importlib_resources.read_text(__name__, 'mydata.txt')
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))
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)
def resource_text(filename: str) -> str: return read_text('pre_commit.resources', filename)
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: """
def contents(): return read_text("certifi", "cacert.pem", encoding="ascii")
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))
def get_data_from_file(filename: str) -> Dict: file_data = read_text("iscram.tests.system_graph_test_data", filename) return json.loads(file_data)
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)
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)
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
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))
def resource_text(filename): return read_text('pre_commit.resources', filename)
def readRequest(filename: str) -> InsightEngineRequest: text = resources.read_text(engine.TestInputs, filename) requestJson = json.loads(text) request = InsightEngineRequest(**requestJson) return request
def execute(self, package, path): resources.read_text(package, path)
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)
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')
def mock_item_q2_class(): return json.loads(pkg_resources.read_text( mock_data, 'mock_item_q2_class.json' ))
def load_resource_stylesheet(name, subs=dict()): template = importlib_resources.read_text('svg.charts', name) source = template % subs return cssutils.parseString(source)
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),
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
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}"
def get_sample_config(): return resources.read_text(emailer, 'sample-emailer.json')
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()
__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()
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)
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