Beispiel #1
0
    def test_representation(self, plot, lattice_axes, grid_representation):

        kwargs = {"isos": [], "reduce_method": "average"}

        ndim = len(lattice_axes)
        if ndim == 3:
            kwargs["isos"] = [{"frac": 0.5}]

        new_grid, representation = grid_representation

        if new_grid.grid.min() == new_grid.grid.max() and ndim == 3:
            return

        plot.update_settings(axes=lattice_axes,
                             represent=representation,
                             nsc=[1, 1, 1],
                             **kwargs)
        new_plot = new_grid.plot(**ChainMap(
            plot.settings,
            dict(axes=lattice_axes, represent="real", grid_file=None)))

        assert np.allclose(
            self._get_plotted_values(plot),
            self._get_plotted_values(plot=new_plot)
        ), f"'{representation}' representation of the {ndim}D plot is not correct"
Beispiel #2
0
    def __init__(
        self,
        constructor_dict: ConstructorDict = DEFAULT_CLASS_MAP,
        default_encoding: Optional[str] = None,
        force_default_encoding: bool = False,
        global_environment: Optional[StrMap] = None,
    ) -> None:

        self.constructor_dict = constructor_dict
        self.default_encoding = default_encoding
        self.force_default_encoding = force_default_encoding
        self.global_environment = ChainMap(
            {} if global_environment is None else global_environment, )
        self.empty_environment: StrMap = ChainMap({})

        self._reset()
Beispiel #3
0
def scan_profile(args: ArgsType) -> None:
    """
    Report working connection parameters
    """
    if args["dryrun"]:
        raise SyntaxError("Can't reasonably show a dry run for a profile scan")

    url = args["url"]
    org = args["org"]
    fid = args["fid"]
    useragent = args["useragent"]
    gen_newfileuid = not args["nonewfileuid"]
    timeout = 2.0

    scan_results = _scan_profile(
        url=url,
        org=org,
        fid=fid,
        useragent=useragent,
        gen_newfileuid=gen_newfileuid,
        timeout=timeout,
    )

    v1, v2, signoninfo = scan_results
    if (not v2["versions"]) and (not v1["versions"]):
        msg = f"Scan found no working formats for {url}"
        print(msg)
    else:
        print(json.dumps(scan_results))

        if args["write"]:
            extra_args = _best_scan_format(scan_results)
            write_config(ChainMap(extra_args, dict(args)))
Beispiel #4
0
def merge_config(
    args: argparse.Namespace, config: configparser.ConfigParser
) -> ArgsType:
    """
    Merge CLI args > user config > OFX Home > defaults
    """
    logger.info("Merging args")
    # All ArgumentParser args that have a value set
    _args = extractns(args)
    logger.debug(f"CLI args: {_args}")

    if "server" in _args:
        user_cfg = read_config(config, _args["server"])
    else:
        user_cfg = {}
    logger.debug(f"Configs: {user_cfg}")
    merged: ArgsType = ChainMap(_args, user_cfg, DEFAULTS)
    #  logger.debug(f"CLI args merged with user configs and defaults: {extrargs(merged)}")

    # Try to perform an OFX Home lookup if:
    # - it's configured from the CLI
    # - it's configured in ofxget.cfg
    # - we don't have a URL
    if "ofxhome" in _args or "ofxhome" in user_cfg or (not merged["url"]):
        merge_from_ofxhome(merged)

    if not (
        merged.get("url", None)
        or merged.get("dryrun", False)
        or merged.get("request", None) == "list"
    ):
        err = "Missing URL"

        if "server" not in _args:
            logger.error(err)
            msg = (
                f"{err} - please provide a server nickname, "
                "or configure 'url' / 'ofxhome'\n"
            )
            print(msg)
            command = merged["request"]
            make_argparser().subparsers[command].print_help()  # type: ignore
            sys.exit()

        server = _args["server"]
        # Allow sloppy CLI args - passing URL as "server" positional arg
        if urllib_parse.urlparse(server).scheme:
            merged["url"] = server
            merged["server"] = None
        else:
            logger.error(err)
            msg = f"{err} - please configure 'url' or 'ofxhome' for server '{server}'"
            raise ValueError(msg)

    logger.info(f"Merged args: {extrargs(merged)}")
    return merged
Beispiel #5
0
    async def _update_backend_configs(self, backend_configs):
        added, removed, updated = compute_dict_delta(self.backend_replicas,
                                                     backend_configs)
        for backend_tag, config in ChainMap(added, updated).items():
            self.backend_replicas[backend_tag].set_max_concurrent_queries(
                config.max_concurrent_queries)

        for backend_tag in removed.keys():
            if backend_tag in self.backend_replicas:
                del self.backend_replicas[backend_tag]
Beispiel #6
0
    def test_symtable_ctx(self):
        node = _NodeWithSymbolTable(symbols=[_NodeWithSymbolName()])
        kwargs = dict(symtable=ChainMap({"a": True}))

        with eve.SymbolTableTrait.symtable_merger(None, node, kwargs):
            assert "symtable" in kwargs
            assert "symbol_name" in kwargs["symtable"]

        assert "symtable" in kwargs
        assert "symbol_name" not in kwargs["symtable"]
Beispiel #7
0
    def load_local(self, source: Union[str, Path, TextIO] = None):
        """Load schemas from a local file

        These files will be treated as local schemas, and will not be sent to the bus.
        This can be useful for validation during development and testing.
        """
        if isinstance(source, str):
            source = Path(source)

        def _load_schema(path, file_data):
            try:
                return json.loads(file_data)
            except JSONDecodeError as e:
                raise InvalidSchema(
                    "Could not parse schema file {}: {}".format(path, e.msg))

        if source is None:
            # No source, read from stdin
            schema = _load_schema("[stdin]", sys.stdin.read())
        elif hasattr(source, "is_dir") and source.is_dir():
            # Read each json file in directory
            schemas = []
            for file_path in source.glob("*.json"):
                schemas.append(
                    _load_schema(file_path,
                                 file_path.read_text(encoding="utf8")))
            schema = ChainMap(*schemas)
        elif hasattr(source, "read"):
            # Read file handle
            schema = _load_schema(source.name, source.read())
        elif hasattr(source, "read_text"):
            # Read pathlib Path
            schema = _load_schema(source.name, source.read_text())
        else:
            raise InvalidSchema(
                "Did not recognise provided source as either a "
                "directory path, file path, or file handle: {}".format(source))

        for api_name, api_schema in schema.items():
            self.local_schemas[api_name] = api_schema

        return schema
Beispiel #8
0
def run(exps: Iterable[Value],
        ctx: Context = None,
        show: bool = False) -> Value:
    res = VList([])
    if ctx is None:
        ctx = ChainMap({}, stdlib)
    for e in exps:
        res = e.run(ctx)
        if show:
            print(res)
    return res
Beispiel #9
0
    async def _update_replica_handles(self, replica_handles):
        added, removed, updated = compute_dict_delta(self.backend_replicas,
                                                     replica_handles)

        for backend_tag, replica_handles in ChainMap(added, updated).items():
            self._get_or_create_replica_set(
                backend_tag).update_worker_replicas(replica_handles)

        for backend_tag in removed.keys():
            if backend_tag in self.backend_replicas:
                del self.backend_replicas[backend_tag]
Beispiel #10
0
def build_request(
    body: MutableMapping[str, Any],
    parser: Parser,
    settings_class: Union[Type[HTTPRequestSettings], Type[SubscriptionRequestSettings]],
    schema: RequestSchema,
    dataset: Dataset,
    timer: Timer,
    referrer: str,
) -> Request:
    with sentry_sdk.start_span(description="build_request", op="validate") as span:
        try:
            request_parts = schema.validate(body)
            if settings_class == HTTPRequestSettings:
                settings = {
                    **request_parts.settings,
                    "consistent": _consistent_override(
                        request_parts.settings.get("consistent", False), referrer
                    ),
                }
                settings_obj: Union[
                    HTTPRequestSettings, SubscriptionRequestSettings
                ] = settings_class(**settings)
            elif settings_class == SubscriptionRequestSettings:
                settings_obj = settings_class(
                    consistent=_consistent_override(True, referrer)
                )

            query = parser(request_parts, settings_obj, dataset)

            request_id = uuid.uuid4().hex
            request = Request(
                request_id,
                # TODO: Replace this with the actual query raw body.
                # this can have an impact on subscriptions so we need
                # to be careful with the change.
                ChainMap(request_parts.query, *request_parts.extensions.values()),
                query,
                settings_obj,
                referrer,
            )
        except (InvalidJsonRequestException, InvalidQueryException) as exception:
            record_invalid_request(timer, referrer)
            raise exception
        except Exception as exception:
            record_error_building_request(timer, referrer)
            raise exception

        span.set_data("snuba_query", request.body)

        timer.mark("validate_schema")
        return request
Beispiel #11
0
def main():
    fis: Mapping[str, str] = ofxhome.list_institutions()
    for ofxhome_id in fis.keys():
        print("Scanning {}".format(ofxhome_id))

        lookup: Optional[ofxhome.OFXServer] = ofxhome.lookup(ofxhome_id)
        if lookup is None or lookup.url is None:
            continue

        url = lookup.url
        org = lookup.org
        fid = lookup.fid

        lookup_name = saxutils.unescape(lookup.name)
        srvr_nick = known_servers.get(ofxhome_id, lookup_name)

        ofxhome_id = lookup.id
        assert ofxhome_id
        names = LibraryConfig["NAMES"]
        if ofxhome_id not in names:
            names[ofxhome_id] = lookup_name

        if ofxhome.ofx_invalid(lookup) or ofxhome.ssl_invalid(lookup):
            blank_fmt = {"versions": [], "formats": {}}
            scan_results = (blank_fmt, blank_fmt, {})
        else:
            scan_results: ofxget.ScanResults = ofxget._scan_profile(
                url=url, org=org, fid=fid, timeout=10.0
            )

        v1, v2, _ = scan_results
        if (not v2["versions"]) and (not v1["versions"]):
            # If no OFX response, blank the server config
            LibraryConfig[srvr_nick] = {"ofxhome": ofxhome_id}
            continue

        format = ofxget._best_scan_format(scan_results)

        looked_up_data = {
            "ofxhome": ofxhome_id,
            "url": url,
            "org": org,
            "fid": fid,
            "brokerid": lookup.brokerid,
        }

        args = ChainMap({"server": srvr_nick}, looked_up_data, format)
        write_config(args)
Beispiel #12
0
    async def _update_traffic_policies(self, traffic_policies):
        added, removed, updated = compute_dict_delta(self.endpoint_policies,
                                                     traffic_policies)

        for endpoint, traffic_policy in ChainMap(added, updated).items():
            self.endpoint_policies[endpoint] = RandomEndpointPolicy(
                traffic_policy)
            if endpoint in self._pending_endpoints:
                future = self._pending_endpoints.pop(endpoint)
                future.set_result(_PendingEndpointFound.ADDED)

        for endpoint, traffic_policy in removed.items():
            del self.endpoint_policies[endpoint]
            if endpoint in self._pending_endpoints:
                future = self._pending_endpoints.pop(endpoint)
                future.set_result(_PendingEndpointFound.REMOVED)
Beispiel #13
0
def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-k',
        '--token',
        required=False,
        help="Ceye.io API token, geting on Ceye.io user's profile")
    parser.add_argument('-t',
                        '--type',
                        help='Ceye log type, dns or http, default http')
    parser.add_argument('-f', '--filter', type=str, help='filter args')
    args = parser.parse_args()

    cmd_args = {k: v for k, v in vars(args).items() if v}

    default = {'token': 'your token', 'type': 'http', 'filter': ''}
    return ChainMap(cmd_args, default)
Beispiel #14
0
def convert_env(
    r_env: parser.RObject,
    conversion_function: ConversionFunction,
) -> ChainMap[Union[str, bytes], Any]:
    """Convert environment objects."""
    if r_env.info.type is not parser.RObjectType.ENV:
        raise TypeError("Must receive a ENV object")

    frame = conversion_function(r_env.value.frame)
    enclosure = conversion_function(r_env.value.enclosure)
    hash_table = conversion_function(r_env.value.hash_table)

    dictionary = {}
    for d in hash_table:
        if d is not None:
            dictionary.update(d)

    return ChainMap(dictionary, enclosure)
Beispiel #15
0
    def gen_clinit(self, var_decls: Iterable[VarDecl],
                   symtab: MutableMapping[str, Symbol]) -> Iterator[str]:
        methodname = '<clinit>'
        methodtype = MType([], VOID_TYPE)

        frame = Frame(methodname, methodtype.rettype)
        frame.func_type = methodtype
        frame.enterScope(True)

        method_env = MethodEnv(frame, ChainMap(symtab))
        var_gens = [
            self.visitVarDecl(var_decl, method_env) for var_decl in var_decls
        ]
        # Generate static field definitions for global variables
        yield from map(next, var_gens)  # type: ignore

        yield from emitter.emit_method(methodname, methodtype)
        # Generate variable initialization code
        yield from chain.from_iterable(var_gens)
        yield from emitter.emit_return(frame)
        yield from emitter.emit_end_method(frame)
Beispiel #16
0
def _merge_acctinfo(args: ArgsType, markup: BytesIO) -> None:
    # *ACCTINFO classes don't have rich comparison methods;
    # can't sort by class
    sortKey = attrgetter("__class__.__name__")
    acctinfos: List[AcctInfo] = sorted(extract_acctinfos(markup), key=sortKey)

    def parse_acctinfos(clsName, acctinfos):
        dispatcher = {
            "BANKACCTINFO": parse_bankacctinfos,
            "CCACCTINFO": parse_ccacctinfos,
            "INVACCTINFO": parse_invacctinfos,
        }
        parser = dispatcher.get(clsName, lambda x: {})
        return parser(acctinfos)

    parsed_args: List[ParsedAcctinfo] = [
        parse_acctinfos(clsnm, infos)
        for clsnm, infos in itertools.groupby(acctinfos, key=sortKey)
    ]

    # Insert extracted ACCTINFO after CLI commands, but before config files
    args.maps.insert(1, ChainMap(*parsed_args))
Beispiel #17
0
    def _superdict(cls) -> Mapping[str, Any]:
        """
        Consolidate ``cls.__dict__`` with that of all superclasses.

        Ordering is significant for OFX messages, which maps to significant ordering
        of class attributes of ``ofxtools.models.base.Aggregate`` subclasses.

        That ordering is implemented by combining `PEP 520`_ (which was implemented
        as of Python 3.6), `collections.ChainMap`_, and Python's `inheritance chain`_
        (i.e. the MRO).

        PEP 520 guarantees that each class's ``__dict__`` preserves the order in which
        its attributes and methods were defined.  ``ChainMap`` searches its list of
        mappings from left to right.  By feeding a class's MRO into ``ChainMap``, we
        get an ordering where attributes defined on subclasses override those defined
        on the parent, preserving the order of the class definition in each case.

        .. _PEP 520: https://www.python.org/dev/peps/pep-0520/
        .. _collections.ChainMap: https://docs.python.org/3/library/collections.html#collections.ChainMap
        .. _inheritance order: https://www.python.org/download/releases/2.3/mro/
        """
        return ChainMap(*[base.__dict__ for base in cls.mro()])
Beispiel #18
0
    def visitFuncDecl(self, ast: FuncDecl,
                      global_symtab: MutableMapping[str, Symbol]) -> BodyGen:
        name = ast.name.name
        func_sym = global_symtab[name]
        func_type = cast(MType, func_sym.type)

        frame = Frame(name, func_type.rettype)
        frame.func_type = func_type
        frame.enterScope(True)

        param_syms = {
            param.variable.name: Symbol(param_type, frame.getNewIndex())
            for param, param_type in zip(ast.param, func_type.partype)
        }
        symtab = ChainMap(param_syms, global_symtab)
        if name == 'main' and not ast.param:
            # Method `main` always has a parameter
            frame.getNewIndex()

        method_env = MethodEnv(frame, symtab)

        body_code = []
        body_gen = self.visit_stmt_list(*ast.body, method_env, new_scope=False)
        for ret in body_gen:
            if isinstance(ret, SkipToFunc):
                yield ret
            else:
                body_code.append(ret)

        yield from emitter.emit_method(name, func_type)
        # Generate .var directives for parameters
        for param in ast.param:
            name = param.variable.name
            param_sym = symtab[name]
            yield from emitter.emit_var(name, param_sym, frame)
        yield from iter(body_code)
        yield from emitter.emit_return(frame)
        yield from emitter.emit_end_method(frame)
Beispiel #19
0
def merge_msdf(
    *msdfs: MappingSetDataFrame,
    reconcile: bool = True,
) -> MappingSetDataFrame:
    """Merge multiple MappingSetDataFrames into one.

    :param msdfs: A Tuple of MappingSetDataFrames to be merged
    :param reconcile: If reconcile=True, then dedupe(remove redundant lower confidence mappings)
        and reconcile (if msdf contains a higher confidence _negative_ mapping,
        then remove lower confidence positive one. If confidence is the same,
        prefer HumanCurated. If both HumanCurated, prefer negative mapping).
        Defaults to True.
    :returns: Merged MappingSetDataFrame.
    """
    merged_msdf = MappingSetDataFrame()

    # Inject metadata of msdf into df
    msdf_with_meta = [inject_metadata_into_df(msdf) for msdf in msdfs]

    # merge df [# 'outer' join in pandas == FULL JOIN in SQL]
    df_merged = reduce(
        lambda left, right: left.merge(right, how="outer", on=list(left.columns)),
        [msdf.df for msdf in msdf_with_meta if msdf.df is not None],
    )

    # merge the non DataFrame elements
    prefix_map_list = [msdf.prefix_map for msdf in msdf_with_meta]
    # prefix_map_merged = {k: v for d in prefix_map_list for k, v in d.items()}
    merged_msdf.prefix_map = dict(ChainMap(*prefix_map_list))
    merged_msdf.df = df_merged
    if reconcile:
        merged_msdf.df = filter_redundant_rows(merged_msdf.df)
        if PREDICATE_MODIFIER in merged_msdf.df.columns:
            merged_msdf.df = deal_with_negation(merged_msdf.df)  # deals with negation

    # TODO: Add default values for license and mapping_set_id.
    return merged_msdf
Beispiel #20
0
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
from typing import Union, ChainMap, Optional

from fhirtordf.rdfsupport.namespaces import AnonNS, namespaces as fhirtordf_namespaces, FHIR, SCT
from fhirtordf.rdfsupport.numericnamespace import NumericNamespace
from rdflib import Namespace, URIRef

HGNC = Namespace("http://www.genenames.org/")
RXNORM = NumericNamespace("http://www.nlm.nih.gov/research/umls/rxnorm/")

# Note that this is a different namespace than the one that is included in the type arc
SNOMED = Namespace("http://snomed.info/sct/")

namespaces = ChainMap(fhirtordf_namespaces, {
    "hgnc": str(HGNC),
    "rxnorm": str(RXNORM)
})


def fhir_namespace_for(uri: Union[URIRef, Namespace, str]) -> Optional[str]:
    """
    Reverse namespace lookup.  Note that returned namespace may not be unique

    :param uri: namespace URI
    :return: namespace (lower case form)
    """
    uri = str(uri)
    if not uri.endswith(('#', '/')):
        uri += '/'
    if uri not in namespaces.values():
        if uri.startswith(str(FHIR)):
Beispiel #21
0
    Counter,
    DefaultDict,
    Deque,
    Dict,
    FrozenSet,
    List,
    Optional,
    OrderedDict,
    Set,
    Tuple,
    Type,
    Union,
)

# These should be marked deprecated for Python >= 3.9
v1: List[int] = [1, 2, 3]
v2: Dict[int, str] = {}
v3: Set[int] = set()
v4: Tuple[int] = (3, )
v5: FrozenSet[int] = frozenset()
v6: Type[int] = int
v7 = Deque()
v8 = DefaultDict()
v9 = OrderedDict()
v10 = Counter()
v11 = ChainMap()

# These should be marked deprecated for Python >= 3.10
v20: Union[int, str]
v21: Optional[int]
    "country": "US"
}
print("Default command line args :")
print(defaults)

parser = argparse.ArgumentParser(
    description="Test reading command line arguments.")
parser.add_argument("-c", "--color", help="Specifies the preferred color.")
parser.add_argument("-u", "--user", help="Specifies the current user.")
parser.add_argument("-o",
                    "--os",
                    help="Specifies the preferred Operating System.")
parser.add_argument("-l", "--language", help="Specifies the user language.")
parser.add_argument("-n", "--country", help="Specifies the current country.")
namespace = parser.parse_args()

actual_command_line_args = {k: v for k, v in vars(namespace).items() if v}
# vars(obj) :   returns the __dict__ attribute for an object (if present).
print("actual_command_line_args:", actual_command_line_args)

# Dont't link them together like this, it will copy all the data:
# result = defaults.copy()
# result.update(os.environ)
# result.update(actual_command_line_args)

# use this instead, doesn't copy but itterates :
result = ChainMap(actual_command_line_args, defaults)
print("Resulting command line args :")
for k, v in result.items():
    print(k, "-->", v)
Beispiel #23
0
try:
    import readline
except ImportError:
    pass

from typing import cast, ChainMap, List, Sequence

import malcore as core
import maltypes as t
from malerrors import MalError, MalNoInputError
from reader import read_str
from printer import pr_str

Env = ChainMap[str, t.MalType]

repl_env: Env = ChainMap(core.ns)


def READ(in_: str) -> t.MalType:
    return read_str(in_)


def eval_ast(ast: t.MalType, env: Env) -> t.MalType:
    if isinstance(ast, t.MalSymbol):
        try:
            return env[ast.name]
        except KeyError:
            raise MalError(f'{ast.name} not found')
    if isinstance(ast, (t.MalList, t.MalVector)):
        return type(ast)([EVAL(i, env) for i in ast.items])
    if isinstance(ast, t.MalHashMap):
Beispiel #24
0
    @functools.wraps(op)
    def wrapper(*args: t.MalType) -> t.MalType:
        if not all(isinstance(i, t.MalInt) for i in args):
            raise MalError('Only ints are supported as arguments')
        acc, *rest = map(o.attrgetter('value'),
                         args)  # type: Tuple[int, List[int]]
        for i in rest:
            acc = op(acc, i)
        return t.MalInt(acc)

    return t.MalFunction(cast(t.MalNativeFunction, wrapper))


repl_env: Env = ChainMap({
    '+': _int_op(o.add),
    '-': _int_op(o.sub),
    '*': _int_op(o.mul),
    '/': _int_op(o.floordiv),
})


def READ(in_: str) -> t.MalType:
    return read_str(in_)


def eval_ast(ast: t.MalType, env: Env) -> t.MalType:
    if isinstance(ast, t.MalSymbol):
        try:
            return env[ast.name]
        except KeyError:
            raise MalError(f'{ast.name} not found')
    if isinstance(ast, (t.MalList, t.MalVector)):
Beispiel #25
0
 def data(self) -> ChainMap[Any, Any]:  # type: ignore
     return ChainMap(self._context_data, self._parent_context or {})
Beispiel #26
0
 def _superdict(cls) -> Mapping[str, Any]:
     """
     Consolidate cls.__dict__ with that of all superclasses.
     """
     return ChainMap(*[base.__dict__ for base in cls.mro()])
Beispiel #27
0
    def __post_init__(self) -> None:
        from simplegist import Simplegist

        gh_gist = Simplegist(username='******', api_token='API_TOKEN')
        self._gist = gh_gist[self.gist_id]

    def _getitem(self, key: str) -> str:
        return self._gist[key]


_base_config = {
    'handlers': ['console'],
    'level': logging.ERROR,
    'propagate': False
}
_info_config = ChainMap({'level': logging.INFO}, _base_config)
_debug_config = ChainMap({'level': logging.DEBUG}, _base_config)


def configure_logging():
    logging_config = {
        'version': 1,
        'formatters': {
            'f': {
                'format':
                '%(asctime)s %(levelname)-8s %(name)-20s -- %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',