Skip to content

Commit 1c976ce

Browse files
committed
3.2.1 - adjust __init__.py for setup.py to work without deps
Due to some imports that weren't wrapped in try/except inside of `__init__.py`, the `setup.py` file was unable to function due to an ImportError raised when trying to import the VERSION variable. To ensure robustness when importing `privex.helpers`, all module imports inside of `privex/helpers/__init__.py` - apart from `privex.helpers.common`, are now wrapped inside of a try/except block, ensuring at least *partial* functionality if any module fails to load for whatever reason. Additionally, to prevent any risk of `setup.py` failing to function when no dependencies are installed, `VERSION` has been relocated to `privex.helpers.version.VERSION`, however, `__init__.py` imports `version.VERSION` and makes it available as `VERSION`, as to not break compatibility with any existing code / systems which expect the package version to be found inside of `__init__.py` The `setup.py` file now only imports from privex.helpers: `extras_require` from `privex.helpers.setuppy.common` (no extra dependencies), and `VERSION` from `privex.helpers.version` Both `privex.helpers.setuppy.commands` and `privex.helpers.settings` are still imported inside of `setup.py`, but kept within a safe try/except block, as they're not essential for setup.py to function
1 parent 0a60dad commit 1c976ce

5 files changed

Lines changed: 160 additions & 11 deletions

File tree

privex/helpers/__init__.py

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,91 @@
5353
import warnings
5454

5555
from privex.helpers.common import *
56-
from privex.helpers.collections import *
57-
from privex.helpers.decorators import *
58-
from privex.helpers.net import *
56+
57+
log = logging.getLogger(__name__)
58+
59+
60+
class _Dummy:
61+
def __init__(self):
62+
self.dummydata = {}
63+
64+
def __getattr__(self, item):
65+
try:
66+
return object.__getattribute__(self, item)
67+
except AttributeError:
68+
pass
69+
try:
70+
if item in object.__getattribute__(self, 'dummydata'):
71+
return object.__getattribute__(self, 'dummydata')[item]
72+
except AttributeError:
73+
pass
74+
75+
return lambda *args, **kwargs: None
76+
77+
def __setattr__(self, key, value):
78+
if key == 'dummydata':
79+
return object.__setattr__(self, key, value)
80+
m = object.__getattribute__(self, 'dummydata')
81+
m[key] = value
82+
83+
def __getitem__(self, item):
84+
try:
85+
return self.__getattr__(item)
86+
except AttributeError as ex:
87+
raise KeyError(str(ex))
88+
89+
def __setitem__(self, key, value):
90+
try:
91+
self.__setattr__(key, value)
92+
except AttributeError as ex:
93+
raise KeyError(str(ex))
94+
95+
try:
96+
from privex.helpers.collections import *
97+
except ImportError as e:
98+
log.warning(
99+
'privex.helpers __init__ failed to import "%s", not loading %s module. reason: %s %s',
100+
'privex.helpers.collections', 'collections', type(e), str(e)
101+
)
102+
103+
try:
104+
from privex.helpers.decorators import *
105+
except ImportError as e:
106+
log.warning(
107+
'privex.helpers __init__ failed to import "%s", not loading %s module. reason: %s %s',
108+
'privex.helpers.decorators', 'decorators', type(e), str(e)
109+
)
110+
try:
111+
from privex.helpers.net import *
112+
except ImportError as e:
113+
log.warning(
114+
'privex.helpers __init__ failed to import "%s", not loading %s module. reason: %s %s',
115+
'privex.helpers.net', 'net', type(e), str(e)
116+
)
117+
59118
from privex.helpers.exceptions import *
60-
from privex.helpers.plugin import *
61-
from privex.helpers.cache import CacheNotFound, CacheAdapter, CacheWrapper, MemoryCache, cached
62119

63-
from privex.helpers import plugin
120+
try:
121+
from privex.helpers.cache import CacheNotFound, CacheAdapter, CacheWrapper, MemoryCache, cached
122+
except ImportError as e:
123+
log.warning(
124+
'privex.helpers __init__ failed to import "%s", not loading %s module. reason: %s %s',
125+
'privex.helpers.cache', 'cache', type(e), str(e)
126+
)
127+
# noinspection PyTypeChecker
128+
CacheNotFound, CacheAdapter, CacheWrapper, MemoryCache, cached = _Dummy(), _Dummy(), _Dummy(), _Dummy(), _Dummy()
64129

65-
log = logging.getLogger(__name__)
66130

131+
try:
132+
from privex.helpers import plugin
133+
from privex.helpers.plugin import *
134+
except ImportError as e:
135+
log.warning(
136+
'privex.helpers __init__ failed to import "%s", not loading %s module. reason: %s %s',
137+
'privex.helpers.plugin', 'plugin', type(e), str(e)
138+
)
139+
# noinspection PyTypeChecker
140+
plugin = _Dummy()
67141

68142
try:
69143
from privex.helpers.cache.RedisCache import RedisCache
@@ -78,6 +152,11 @@
78152
except ImportError:
79153
log.debug('privex.helpers __init__ failed to import "MemcachedCache", not loading MemcachedCache')
80154

155+
try:
156+
from privex.helpers.cache.SqliteCache import SqliteCache
157+
except ImportError:
158+
log.debug('privex.helpers __init__ failed to import "SqliteCache", not loading SqliteCache')
159+
81160
try:
82161
from privex.helpers.cache.asyncx import *
83162
except ImportError:
@@ -160,7 +239,10 @@ def _setup_logging(level=logging.WARNING):
160239
log = _setup_logging()
161240
name = 'helpers'
162241

163-
VERSION = '3.2.0'
242+
from privex.helpers import version as _version_mod
243+
244+
VERSION = _version_mod.VERSION
245+
164246

165247

166248

privex/helpers/version.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
This file should not import any modules that may potentially be unavailable, as this file is required
3+
for setup.py to run - which is often ran before ANY dependencies are installed.
4+
5+
Privex's Python Helpers - https://github.com/privex/python-helpers
6+
7+
X11 / MIT License
8+
9+
**Copyright**::
10+
11+
+===================================================+
12+
| © 2020 Privex Inc. |
13+
| https://www.privex.io |
14+
+===================================================+
15+
| |
16+
| Originally Developed by Privex Inc. |
17+
| |
18+
| Core Developer(s): |
19+
| |
20+
| (+) Chris (@someguy123) [Privex] |
21+
| (+) Kale (@kryogenic) [Privex] |
22+
| |
23+
+===================================================+
24+
25+
Copyright 2019 Privex Inc. ( https://www.privex.io )
26+
27+
Permission is hereby granted, free of charge, to any person obtaining a copy of
28+
this software and associated documentation files (the "Software"), to deal in
29+
the Software without restriction, including without limitation the rights to use,
30+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
31+
Software, and to permit persons to whom the Software is furnished to do so,
32+
subject to the following conditions:
33+
34+
The above copyright notice and this permission notice shall be included in all
35+
copies or substantial portions of the Software.
36+
37+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
38+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
40+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
41+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43+
44+
45+
"""
46+
name = 'helpers'
47+
VERSION = '3.2.1'

privex_stubs/privex/helpers/__init__.pyi

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@ from privex.helpers.net import *
55
from privex.helpers.exceptions import *
66
from privex.helpers.plugin import *
77
from privex.helpers.cache.asyncx import *
8+
from privex.helpers.cache.extras import *
89
from privex.helpers.asyncx import *
910
from privex.helpers.extras import *
1011
from privex.helpers.converters import *
1112
from privex.helpers.geoip import *
1213
from privex.helpers.thread import *
1314
from privex.helpers import plugin as plugin
14-
from privex.helpers import black_magic
1515
from privex.helpers.cache import CacheAdapter as CacheAdapter, CacheNotFound as CacheNotFound, CacheWrapper as CacheWrapper, MemoryCache as MemoryCache, cached as cached
1616
from privex.helpers.cache.MemcachedCache import MemcachedCache as MemcachedCache
1717
from privex.helpers.cache.RedisCache import RedisCache as RedisCache
18+
from privex.helpers.cache.SqliteCache import SqliteCache as SqliteCache
1819
from privex.helpers.crypto import EncryptHelper as EncryptHelper, Format as Format, KeyManager as KeyManager, auto_b64decode as auto_b64decode, is_base64 as is_base64
1920
from privex.helpers.setuppy.bump import bump_version as bump_version, get_current_ver as get_current_ver
2021
from privex.helpers.setuppy.commands import BumpCommand as BumpCommand, ExtrasCommand as ExtrasCommand
2122
from privex.helpers.setuppy.common import extras_require as extras_require, reqs as reqs
2223
from typing import Any
2324

2425
log: Any
26+
27+
class _Dummy:
28+
dummydata: Any = ...
29+
def __init__(self) -> None: ...
30+
def __getattr__(self, item: Any): ...
31+
def __setattr__(self, key: Any, value: Any): ...
32+
def __getitem__(self, item: Any): ...
33+
def __setitem__(self, key: Any, value: Any) -> None: ...
34+
35+
def _setup_logging(level: Any = ...): ...
36+
2537
name: str
26-
VERSION: str
38+
VERSION: Any
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: str
2+
VERSION: str

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
from setuptools import setup, find_packages
3939
from os.path import join, dirname, abspath
40-
from privex.helpers import VERSION, extras_require
40+
from privex.helpers.setuppy.common import extras_require
41+
from privex.helpers.version import VERSION
4142

4243
BASE_DIR = dirname(abspath(__file__))
4344

@@ -51,6 +52,11 @@
5152
try:
5253
# noinspection PyUnresolvedReferences
5354
import privex.helpers.setuppy.commands
55+
from privex.helpers import settings
56+
57+
# The file which contains "VERSION = '1.2.3'"
58+
settings.VERSION_FILE = join(BASE_DIR, 'privex', 'helpers', 'version.py')
59+
5460
extra_commands['extras'] = privex.helpers.setuppy.commands.ExtrasCommand
5561
extra_commands['bump'] = privex.helpers.setuppy.commands.BumpCommand
5662
except (ImportError, AttributeError) as e:

0 commit comments

Comments
 (0)