Skip to content

Commit

Permalink
fix: 🐛 add ENCRYPT_SUFFIX for inspect.getmodulename
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoQi99 committed Nov 5, 2024
1 parent 7e08edb commit a071ad1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pyencrypt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import traceback
import types
from importlib import abc
from importlib import abc, machinery
from importlib._bootstrap_external import _NamespacePath
from importlib.machinery import ModuleSpec
from importlib.util import spec_from_loader
Expand All @@ -21,6 +21,9 @@ def __dir__(self) -> Iterable[str]:
return []


ENCRYPT_SUFFIX = ".pye"


class EncryptFileLoader(abc.SourceLoader, Base):
POSSIBLE_PATH = [
Path(os.path.expanduser("~")) / ".licenses" / "license.lic",
Expand Down Expand Up @@ -79,12 +82,17 @@ def find_spec(
) -> ModuleSpec:
if path:
if isinstance(path, _NamespacePath):
file_path = Path(path._path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
file_path = (
Path(path._path[0])
/ f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
)
else:
file_path = Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}.pye'
file_path = (
Path(path[0]) / f'{fullname.rsplit(".", 1)[-1]}{ENCRYPT_SUFFIX}'
)
else:
for p in sys.path:
file_path = Path(p) / f"{fullname}.pye"
file_path = Path(p) / f"{fullname}{ENCRYPT_SUFFIX}"
if file_path.exists():
break
file_path = file_path.absolute().as_posix()
Expand All @@ -99,4 +107,5 @@ def invalidate_caches(cls):


# TODO: generate randomly AES Class
machinery.EXTENSION_SUFFIXES.append(ENCRYPT_SUFFIX)
sys.meta_path.insert(0, EncryptFileFinder)

0 comments on commit a071ad1

Please sign in to comment.