Skip to content

Commit

Permalink
fix(loader): typeError: '_NamespacePath' object does not support inde…
Browse files Browse the repository at this point in the history
…xing

Close #5
  • Loading branch information
zhaoqi219 committed Nov 18, 2021
1 parent b6bb494 commit 9aaf013
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ dmypy.json
# Cython debug symbols
cython_debug/

encrypted/
.DS_Store
17 changes: 11 additions & 6 deletions pyencrypt/loader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import sys
import traceback
import types
from importlib import abc
from importlib._bootstrap_external import _NamespacePath
from importlib.machinery import ModuleSpec
import importlib.util
from importlib.util import spec_from_loader
from pathlib import Path
from typing import Iterable, Sequence, Union

from pyencrypt.decrypt import *
import traceback

_Path = Union[bytes, str]
sys.dont_write_bytecode = True
Expand All @@ -25,12 +27,12 @@ def __init__(self,path) -> None:


def get_filename(self, fullname: str) -> _Path:
return f"{fullname.rsplit('.',1)[-1]}.pye"
return self.path

def get_data(self, path: _Path) -> bytes:
try:
__n, __d = self.__private_key.split('O', 1)
return decrypt_file(Path(self.path), decrypt_key(self.__cipher_key, int(__n), int(__d)))
return decrypt_file(Path(path), decrypt_key(self.__cipher_key, int(__n), int(__d)))
except Exception:
traceback.print_exc()
return b''
Expand All @@ -39,13 +41,16 @@ def get_data(self, path: _Path) -> bytes:
class EncryptFileFinder(abc.MetaPathFinder,Base):
def find_spec(self, fullname: str, path: Sequence[_Path], target: types.ModuleType = None) -> ModuleSpec:
if path:
file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
if isinstance(path, _NamespacePath):
file_path = Path(path._path[0])/ f'{fullname.rsplit(".",1)[-1]}.pye'
else:
file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.pye'
else:
file_path = f'{fullname}.pye'
if not os.path.exists(file_path):
return None
loader = EncryptFileLoader(file_path)
return importlib.util.spec_from_loader(name=fullname, loader=loader, origin='origin-encrypt')
return spec_from_loader(name=fullname, loader=loader, origin='origin-encrypt')

# TODO: generate randomly AES Class
sys.meta_path.append(EncryptFileFinder())

0 comments on commit 9aaf013

Please sign in to comment.