diff --git a/docs/pages/enhancements.md b/docs/pages/enhancements.md index 388b743..19f3c98 100644 --- a/docs/pages/enhancements.md +++ b/docs/pages/enhancements.md @@ -6,7 +6,7 @@ Formerly a set of [helper functions](helpers.md), the following have been attach Code can be monkey-patched at runtime using multiple functions, depending on what should be patched and how. Note that some of the functions rely on the [`patchy`](https://github.com/adamchainz/patchy) module. -- `add_line`, `add_lines`, `insert_line`, `insert_line`: it allows to add line(s) at specific indices (starting from 0), before or after (using `after=True`). +- `add_line`, `add_lines`, `insert_line`, `insert_lines`: it allows to add line(s) at specific indices (starting from 0), before or after (using `after=True`). - `delete_line`, `delete_lines`, `remove_line`, `remove_lines`: it allows to delete line(s) by index (starting from 0). - `patch`: alias for `patchy.patch`, taking a function and a patch file's text as arguments. - `replace`: wrapper for `patchy.replace`, handling multiple replacements at a time, either replacing whole function (like in original `replace`) or only parts of the code. diff --git a/src/tinyscript/VERSION.txt b/src/tinyscript/VERSION.txt index ef657c1..18d580a 100644 --- a/src/tinyscript/VERSION.txt +++ b/src/tinyscript/VERSION.txt @@ -1 +1 @@ -1.29.3 +1.29.4 diff --git a/src/tinyscript/__conf__.py b/src/tinyscript/__conf__.py index 78d4959..3a2d162 100644 --- a/src/tinyscript/__conf__.py +++ b/src/tinyscript/__conf__.py @@ -2,7 +2,7 @@ """Deprecation warnings, for use when no backward-compatibility provided. """ -import builtins +import builtins as bi import warnings from importlib import import_module from inspect import currentframe @@ -32,39 +32,49 @@ def _wrapper(*a, **kw): _warn(old, new) -builtins.deprecate = deprecate -builtins.warn = warnings.warn +bi.deprecate = deprecate +bi.warn = warnings.warn -builtins.lazy_object = Proxy +bi.lazy_object = Proxy -def lazy_load_module(module, relative=None, alias=None, postload=None): +def lazy_load_module(module, relative=None, alias=None, preload=None, postload=None): """ Lazily load a module. """ glob = currentframe().f_back.f_globals def _load(): + if callable(preload): + preload() glob[alias or module] = m = import_module(*((module, ) if relative is None else ("." + module, relative))) m.__name__ = alias or module - if postload is not None: - postload(m) + if callable(postload): + try: + postload() + except TypeError: + postload(m) return m glob[alias or module] = m = Proxy(_load) return m -builtins.lazy_load_module = lazy_load_module +bi.lazy_load_module = lazy_load_module -def lazy_load_object(name, load_func, postload=None): +def lazy_load_object(name, load_func, preload=None, postload=None): """ Lazily load an object. """ glob = currentframe().f_back.f_globals def _load(): + if callable(preload): + preload() glob[name] = o = load_func() try: o._instance = o except (AttributeError, TypeError): pass - if postload is not None: - postload(o) + if callable(postload): + try: + postload() + except TypeError: + postload(o) return o glob[name] = o = Proxy(_load) return o -builtins.lazy_load_object = lazy_load_object +bi.lazy_load_object = lazy_load_object diff --git a/src/tinyscript/__main__.py b/src/tinyscript/__main__.py index a9c070e..0823577 100755 --- a/src/tinyscript/__main__.py +++ b/src/tinyscript/__main__.py @@ -155,7 +155,7 @@ def main(): __add_src(search) with commands.add_parser("update", help="update the list of publicly available scripts") as update: update.add_argument("-s", "--source", nargs="*", type=url, help="set a source URL for a list of scripts") - initialize(noargs_action="wizard") + initialize(add_version=True, noargs_action="wizard") if args.command =="add-source": s, sources = args.url, _get_sources_list() if not args.fetch or args.fetch and _fetch_source(s):