Coverage for src/importnb/utils/setup.py: 0%

1# coding: utf-8

2"""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.

3"""

4

5""" class BuildWithNotebooks(setuptools.command.build_py.build_py):

6 def __new__(cls, distribution):

7 from importnb.utils.setup import build_ipynb

8 return build_ipynb(distribution)

9 setup_args.update(cmdclass=dict(build_py=BuildWithNotebooks))

10

11"""

12

13 import importlib

14 import os

15 import sys

16 from pathlib import Path

17

18 from setuptools.command.build_py import build_py

19

20

21 class build_ipynb(build_py):

22 """Should really use manifest.in

23

24 Lazy import build_ipynb in your setup.

25

26 class BuildWithNotebooks(setuptools.command.build_py.build_py):

27 def __new__(cls, distribution):

28 from importnb.utils.setup import build_ipynb

29 return build_ipynb(distribution)

30 setup_args.update(cmdclass=dict(build_py=BuildWithNotebooks))

31 """

32

33 def get_module_outfile(self, build_dir, package, module):

34 module_mapper = {module[1]: module[2] for module in self.find_all_modules()}

35 outfile_path = [build_dir] + list(package) + [module_mapper[module]]

36 return os.path.join(*outfile_path)

37

38 def find_package_modules(self, package, package_dir):

39 from glob import glob

40

41 self.check_package(package, package_dir)

42 module_files = glob(os.path.join(package_dir, "*.py"))

43 modules = []

44 setup_script = os.path.abspath(self.distribution.script_name)

45

46 for f in module_files + glob(os.path.join(package_dir, "*.ipynb")):

47 abs_f = os.path.abspath(f)

48 if abs_f != setup_script:

49 module = os.path.splitext(os.path.basename(f))[0]

50 modules.append((package, module, f))

51 else:

52 self.debug_print("excluding %s" % setup_script)

53 return modules

54

55 def find_modules(self):

56 packages, modules = {}, []

57

58 for module in self.py_modules:

59 path = module.split(".")

60 package = ".".join(path[0:-1])

61 module_base = path[-1]

62

63 try:

64 (package_dir, checked) = packages[package]

65 except KeyError:

66 package_dir = self.get_package_dir(package)

67 checked = 0

68

69 if not checked:

70 init_py = self.check_package(package, package_dir)

71 packages[package] = (package_dir, 1)

72 if init_py:

73 modules.append((package, "__init__", init_py))

74

75 module_file = os.path.join(package_dir, module_base + ".ipynb")

76

77 if Path(module_file).exists():

78 modules.append((package, module_base, str(module_file)))

79 else:

80 module_file = str(Path(module_file).with_suffix(".py"))

81 if self.check_module(module, module_file):

82 modules.append((package, module_base, str(module_file)))

83

84 return modules