__init__.py
1 # SPDX-License-Identifier: MIT 2 # 3 # Copyright (c) 2021 The Anvil Extras project team members listed at 4 # https://github.com/anvilistas/anvil-extras/graphs/contributors 5 # 6 # This software is published at https://github.com/anvilistas/anvil-extras 7 8 import sys 9 from functools import lru_cache 10 11 __version__ = "3.1.0" 12 13 14 def __dir__(): 15 return [ 16 "auto_refreshing", 17 "ProxyItem", 18 "correct_canvas_resolution", 19 "import_module", 20 "timed", 21 "wait_for_writeback", 22 ] 23 24 25 def import_module(name, package=None): 26 """Import a module. 27 28 The 'package' argument is required when performing a relative import. It 29 specifies the package to use as the anchor point from which to resolve the 30 relative import to an absolute import. 31 """ 32 level = 0 33 if name.startswith("."): 34 if not package: 35 msg = ( 36 "the 'package' argument is required to perform a relative " 37 "import for {!r}" 38 ) 39 raise TypeError(msg.format(name)) 40 for character in name: 41 if character != ".": 42 break 43 level += 1 44 if package not in sys.modules: 45 # make sure the package exists 46 __import__(package, {"__package__": None}) 47 48 name = name[level:] 49 mod = __import__(name, {"__package__": package}, level=level) 50 attrs = name.split(".")[1:] 51 for attr in attrs: 52 mod = getattr(mod, attr) 53 return mod 54 55 56 _imports = { 57 "auto_refreshing": "._auto_refreshing", 58 "BindingRefreshDict": "._auto_refreshing", 59 "ProxyItem": "._auto_refreshing", 60 "correct_canvas_resolution": "._canvas_helpers", 61 "timed": "._timed", 62 "wait_for_writeback": "._writeback_waiter", 63 } 64 65 66 @lru_cache(maxsize=None) 67 def __getattr__(name): 68 try: 69 rel_import = _imports[name] 70 except KeyError: 71 raise AttributeError(name) 72 73 module = import_module(rel_import, __package__) 74 return getattr(module, name)