Beispiel #1
0
def build_this_crazy_context_manager_():
    # NOTE We want to move this to the "curses adapter" sibling module soon
    # (this is like the `curses.wrapper` but ..)

    stack, self = _build_this_crazy_stack()

    from contextlib import contextmanager as cm

    @cm
    def cm():
        undo_stack = []
        try:
            while stack:
                item = stack.pop()
                item.do()

                # Don't add `undo` until after the `do` completes successfully
                undo_stack.append(item.undo)

            yield self

        finally:
            while undo_stack:
                undo_stack.pop()()

    return cm()
Beispiel #2
0
def populate():
    print "Getting the list of all packages."
    client = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')
    packages = client.list_packages()
    print "Ridiculous!  Found %i packages." % len(packages)

    print "Ingesting %i packages." % len(packages)
    with cm(shelve.open(fname, writeback=True)) as d:
        d['packages'] = d.get('packages', {})

        for package in packages:
            if package not in d['packages']:
                result = ingest_package(package)

                try:
                    requires = dependencies.get_dependencies(result['name'])
                    print result['name'], "depends on %r" % requires
                except Exception as e:
                    print str(e)
                    continue

                d['packages'][result['name']] = result
                d['packages'][result['name']]['dependencies'] = requires

                d.sync()
            else:
                print "Skipping          ", package
    print "Complete!"
Beispiel #3
0
    def open_traverse_identifiers_via_two_opener(twoer):
        from contextlib import contextmanager as cm

        @cm
        def cm():
            with twoer() as (sch, ents):
                yield (ent.identifier for ent in ents)

        return cm()
Beispiel #4
0
def main():
    print "Scraping pypi..."
    populate()
    print "Complete!"
    with cm(shelve.open(fname)) as d:
        ostensibly_in_py3 = filter(is_python3, d['packages'].values())
        ostensibly_not_in_py3 = filter(
            lambda p: not is_python3(p), d['packages'].values()
        )

    print "In py3:", len(ostensibly_in_py3)
    print "Not in py3:", len(ostensibly_not_in_py3)
def func(listener, port):
    from contextlib import contextmanager as cm

    from microservice_lib.tcp_ip_client import \
        open_dictionary_based_tcp_ip_client_via as func
    opened = func(listener, port)

    @cm  # idk
    def cm():
        with opened as impl:
            yield _Client(impl)

    return cm()
Beispiel #6
0
        def open_schema_and_entity_traversal(listener):
            # (There are never files to close. there are no resources to manage

            from contextlib import contextmanager as cm

            @cm
            def cm():
                yield None, each_entity()  # ..

            def each_entity():
                return (e for fr in to_FRs(listener) for e in fr.to_entities())

            return cm()
Beispiel #7
0
def _open_classified_row_ASTs_via_issues_collection(ic):
    from contextlib import contextmanager as cm

    @cm
    def cm():
        with ic.open_schema_and_issue_traversal() as (schema, ents):
            # Maybe the readme file doesn't begin its parse OK
            if ents is None:
                yield None
                return
            ks = schema.field_name_keys[1:]
            del schema
            yield _classified_row_ASTs_via_issues_collection(ks, ents)
    return cm()
Beispiel #8
0
    def do_open_etc(listener):
        from contextlib import contextmanager as cm

        @cm
        def cm():
            with coll.open_schema_and_RAW_entity_traversal(listener) as (sch, enx):  # noqa: E501
                if enx is None:
                    return
                body_ks = sch.field_name_keys[1:]
                if fixed_for_now != body_ks:
                    reason = f"expected {fixed_for_now!r} had {body_ks!r}"
                    raise RuntimeError(reason)
                yield sch, enx
        return cm()
def sync_agent_builder_(opener, ST_doc_scn_via_lines):
    def do_open_sync_session():
        from contextlib import contextmanager as cm

        @cm
        def cm():
            if (opened := opener()) is None:
                yield None
                return
            with opened as fh:

                def ST_doc_scn_via_listener(listener):
                    return ST_doc_scn_via_lines(fh, listener)

                yield sync_agent_(ST_doc_scn_via_listener, fh.name)

        return cm()
Beispiel #10
0
class directory_via_path:
    """Imagine that it's itself stateless, just a wrapper around the

    filesystem whose state determines its state, and also it exposes functions
    to mutate the filesystem and (in effect) move us through our states.

    (In practice we are seeing if we can avoid redudant filesystem hits while
    avoiding the pitfalls of caching:..)
    """
    def __init__(self, path, injected_functions=None, filesystem=None):
        self.path = path
        self.injected_functions = injected_functions
        self._injected_filesystem = filesystem
        self._d = None

    def create_directory(self, listener):
        return self.EXECUTE_COMMAND('create_directory', listener)

    # == Hook-in to component API

    def EXECUTE_COMMAND(self, cmd, listener, stylesheet=None):
        from pho.config_component_ import execute_command_ as func
        with self._open_cache_session():
            return func(self, cmd, listener, stylesheet)

    def to_additional_commands_(self):
        for k in self._state.transition_names:
            yield k, lambda kw: self._execute_action(**kw)

    def _execute_action(self, command_name, rest, stylesheet, listener):
        tr = self._state[command_name]
        args = () if rest is None else (rest, )  # idk
        func = getattr(self, tr.action_function_name)
        return func(listener, *args)

    # == Mutates filesystem

    def _create_directory(self, listener):
        self._clear_cache_early()
        pretend = ''.join(('mkdir ', self.path))
        listener('info', 'expression', 'executing_system_command', lambda:
                 (pretend, ))  # noqa: E501
        self._filesystem.mkdir(self.path)  # result is none
        return 0

    # == Read-only and derivations

    def _list_directory(self, listener):
        tup = self._cached('listdir_tuple')
        if len(tup):
            listener('output', 'expression', lambda: tup)
        else:
            msg = f'(empty directory: {self.path})'
            listener('info', 'expression', 'dir_is_empty', lambda: (msg, ))
        return 0

    @_result_is_output_lines
    def execute_show_(self, ss, listener):
        with self._open_cache_session():
            for line in self._do_execute_show(ss, listener):
                yield line

    def _do_execute_show(self, ss, listener):
        yield '(intermediate directory):'
        yield f'  path: {self.path!r}'
        yield f'  status: {self._status}'

    @property
    def _state(self):
        return self._FFSA[self._status]

    @property
    def status(self):
        with self._open_cache_session():
            return self._status

    @property
    def _status(self):
        return self._cached('status_symbol')

    # == BEGIN cache yikes

    def _cached(self, k):
        cache = self._d
        assert cache is not None
        if len(cache):
            return cache[k]  # VERY experimental

        # Experimental: do this whole cached "performance" all in once place
        status_symbol = self._hit_filesystem_for_initial_status_symbol()
        if 'exists_and_is_directory' == status_symbol:
            func = self._filesystem.listdir
            entries = tuple(func(self.path))
            cache['listdir_tuple'] = entries
            status_symbol = 'non_empty_directory' if len(
                entries) else 'empty_directory'  # noqa: E501
        cache['status_symbol'] = status_symbol

        return cache[k]

    def _clear_cache_early(self):
        self._d.clear()

    def _hit_filesystem_for_initial_status_symbol(self):
        os_stat_mode = self._filesystem.os_stat_mode
        try:
            mode = os_stat_mode(self.path)
        except FileNotFoundError:
            return 'noent'

        from stat import S_ISDIR as is_directory
        if is_directory(mode):
            return 'exists_and_is_directory'

        return 'exists_and_is_not_directory'

    def _open_cache_session(self):
        # produce the context manger within which you can use the cache

        from contextlib import nullcontext, contextmanager as cm

        # If you are already in the middle of using your cache, do nothing
        if self._d is not None:
            return nullcontext()

        # Since you haven't started your cache yet, create it then destroy
        @cm
        def cm():
            try:
                yield
            finally:
                if (dct := self._d) is None:  # idk
                    return
                dct.clear()  # OCD
                self._d = None

        self._d = {}
        return cm()