From 4c3730c5cf899eaf75d9c7d504b7acce5cdac09c Mon Sep 17 00:00:00 2001 From: tonyfast Date: Thu, 10 May 2018 20:47:56 -0400 Subject: [PATCH] Addresses #30 but still needs a test. --- .gitignore | 5 ++++- src/importnb/utils/setup.py | 40 +++++++++++++++++++++------------ src/notebooks/utils/setup.ipynb | 35 +++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index ff77194..275e0c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +_site +docs/*.md +docs/*.html + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -102,4 +106,3 @@ venv.bak/ # mypy .mypy_cache/ - diff --git a/src/importnb/utils/setup.py b/src/importnb/utils/setup.py index fd859c2..2120346 100644 --- a/src/importnb/utils/setup.py +++ b/src/importnb/utils/setup.py @@ -1,4 +1,4 @@ -try: +try: from .. import exporter except: import importnb.exporter @@ -8,20 +8,35 @@ from pathlib import Path import importlib - class build_ipynb(build_py): - def get_module_outfile(self, build_dir, package, module): module_mapper = {module[1]: module[2] for module in self.find_all_modules()} outfile_path = [build_dir] + list(package) + [module_mapper[module]] return os.path.join(*outfile_path) + def find_package_modules(self, package, package_dir): + from glob import glob + import os + self.check_package(package, package_dir) + module_files = glob(os.path.join(package_dir, "*.py")) + modules = [] + setup_script = os.path.abspath(self.distribution.script_name) + + for f in module_files + glob(os.path.join(package_dir, "*.ipynb")): + abs_f = os.path.abspath(f) + if abs_f != setup_script: + module = os.path.splitext(os.path.basename(f))[0] + modules.append((package, module, f)) + else: + self.debug_print("excluding %s" % setup_script) + return modules + def find_modules(self): packages, modules = {}, [] for module in self.py_modules: - path = module.split(".") - package = ".".join(path[0:-1]) + path = module.split('.') + package = '.'.join(path[0:-1]) module_base = path[-1] try: @@ -41,21 +56,18 @@ def find_modules(self): if Path(module_file).exists(): modules.append((package, module_base, str(module_file))) else: - module_file = str(Path(module_file).with_suffix(".py")) + module_file = str(Path(module_file).with_suffix('.py')) if self.check_module(module, module_file): - modules.append((package, module_base, str(module_file))) - - return modules + modules.append((package, module_base, str(module_file))) + return modules -if __name__ == "__main__": +if __name__ == '__main__': from pathlib import Path - try: from ..compiler_python import ScriptExporter except: from importnb.compiler_python import ScriptExporter - Path("../../importnb/utils/setup.py").write_text( - ScriptExporter().from_filename("setup.ipynb")[0] - ) + Path('../../importnb/utils/setup.py').write_text(ScriptExporter().from_filename('setup.ipynb')[0]) + diff --git a/src/notebooks/utils/setup.ipynb b/src/notebooks/utils/setup.ipynb index 830922a..f9da7c6 100644 --- a/src/notebooks/utils/setup.ipynb +++ b/src/notebooks/utils/setup.ipynb @@ -19,6 +19,17 @@ "It is important to distribute notebooks in packages during the initial stages of code development. This notebook creates a setuptools command class that allows for both python and notebook imports. This was specifically created to allow notebooks as py_module imports, but could serve a greater purpose." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " class BuildWithNotebooks(setuptools.command.build_py.build_py):\n", + " def __new__(cls, distribution):\n", + " from importnb.utils.setup import build_ipynb\n", + " return build_ipynb(distribution)\n", + " setup_args.update(cmdclass=dict(build_py=BuildWithNotebooks))\n" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -31,10 +42,34 @@ "import importlib\n", "\n", "class build_ipynb(build_py):\n", + " \"\"\"Lazy import build_ipynb in your setup.\n", + " \n", + " class BuildWithNotebooks(setuptools.command.build_py.build_py):\n", + " def __new__(cls, distribution):\n", + " from importnb.utils.setup import build_ipynb\n", + " return build_ipynb(distribution)\n", + " setup_args.update(cmdclass=dict(build_py=BuildWithNotebooks))\n", + " \"\"\"\n", " def get_module_outfile(self, build_dir, package, module):\n", " module_mapper = {module[1]: module[2] for module in self.find_all_modules()}\n", " outfile_path = [build_dir] + list(package) + [module_mapper[module]]\n", " return os.path.join(*outfile_path)\n", + " \n", + " def find_package_modules(self, package, package_dir):\n", + " from glob import glob\n", + " self.check_package(package, package_dir)\n", + " module_files = glob(os.path.join(package_dir, \"*.py\"))\n", + " modules = []\n", + " setup_script = os.path.abspath(self.distribution.script_name)\n", + "\n", + " for f in module_files + glob(os.path.join(package_dir, \"*.ipynb\")):\n", + " abs_f = os.path.abspath(f)\n", + " if abs_f != setup_script:\n", + " module = os.path.splitext(os.path.basename(f))[0]\n", + " modules.append((package, module, f))\n", + " else:\n", + " self.debug_print(\"excluding %s\" % setup_script)\n", + " return modules\n", "\n", " def find_modules(self):\n", " packages, modules = {}, []\n",