Skip to content

Commit

Permalink
Addresses ipython#30 but still needs a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfast committed May 11, 2018
1 parent 0eb26a6 commit 4c3730c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
_site
docs/*.md
docs/*.html

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -102,4 +106,3 @@ venv.bak/

# mypy
.mypy_cache/

40 changes: 26 additions & 14 deletions src/importnb/utils/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
try:
try:
from .. import exporter
except:
import importnb.exporter
Expand All @@ -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:
Expand All @@ -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])

35 changes: 35 additions & 0 deletions src/notebooks/utils/setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down

0 comments on commit 4c3730c

Please sign in to comment.