Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Check various settings.
5"""
7import sys
8import os
9import site
12def getsitepackages():
13 """
14 Overwrites function :epkg:`getsitepackages`
15 which does not work for a virtual environment.
17 @return site-package somewhere
18 """
19 try:
20 return site.getsitepackages()
21 except AttributeError:
22 import sphinx
23 return [os.path.normpath(os.path.join(os.path.dirname(sphinx.__file__), ".."))]
26def locate_image_documentation(image_name):
27 """
28 Tries to local an image in the module for help generation in a folder ``_doc``.
30 @param image_name path
31 @return local file
33 When a notebook is taken out from the sources, the image using NbImage
34 cannot be displayed because the function cannot guess from which project
35 it was taken. The function was entering an infinite loop.
36 The function can deal with subfolder and not only the folder which contains the notebook.
37 """
38 folder, filename = os.path.split(image_name)
39 while len(folder) > 0 and (not os.path.exists(folder) or "_doc" not in os.listdir(folder)):
40 fold = os.path.split(folder)[0]
41 if fold == folder:
42 break
43 folder = fold
44 doc = os.path.join(folder, "_doc")
45 if not os.path.exists(doc):
46 raise FileNotFoundError(
47 "unable to find a folder called _doc, the function cannot locate an image\n{0}".format(image_name))
48 for root, _, files in os.walk(doc):
49 for name in files:
50 t = os.path.join(root, name)
51 fn = os.path.split(t)[-1]
52 if filename == fn:
53 return t
54 raise FileNotFoundError(image_name)
57def NbImage(name, repository=None, force_github=False, width=None):
58 """
59 Retrieves a name or a url of the image if it is not found in the local folder
60 or a subfolder.
62 @param name image name (name.png)
63 @param force_github force the system to retrieve the image from GitHub
64 @param repository repository, see below
65 @param width to modify the width
66 @return an `Image object <http://ipython.org/ipython-doc/2/api/generated/IPython.core.display.html
67 #IPython.core.display.Image>`_
69 We assume the image is retrieved from a notebook.
70 This function will display an image even though the notebook is not run
71 from the sources. IPython must be installed.
73 if *repository* is None, then the function will use the variable ``module.__github__`` to
74 guess the location of the image.
75 The function is able to retrieve an image in a subfolder.
76 Displays a better message if ``__github__`` was not found.
77 """
78 from IPython.core.display import Image
79 local = os.path.abspath(name)
80 if not force_github and os.path.exists(local):
81 return Image(local, width=width)
83 local_split = local.replace("\\", "/").split("/")
84 if "notebooks" not in local_split:
85 local = locate_image_documentation(local)
86 return Image(local, width=width)
88 # otherwise --> github
89 paths = local.replace("\\", "/").split("/")
90 try:
91 pos = paths.index("notebooks") - 1
92 except IndexError as e:
93 # we are looking for the right path
94 mes = "The image is not retrieved from a notebook from a folder `_docs/notebooks`" + \
95 " or you changed the current folder:\n{0}"
96 raise IndexError(mes.format(local)) from e
97 except ValueError as ee:
98 # we are looking for the right path
99 mes = "the image is not retrieve from a notebook from a folder ``_docs/notebooks`` " + \
100 "or you changed the current folder:\n{0}"
101 raise IndexError(mes.format(local)) from ee
103 if repository is None:
104 module = paths[pos - 1]
105 if module not in sys.modules:
106 if "ensae_teaching_cs" in local:
107 # For some specific modules, we add the location.
108 repository = "https://github.com/sdpython/ensae_teaching_cs/"
109 else:
110 raise ImportError(
111 "The module {0} was not imported, cannot guess the location of the repository".format(module))
112 else:
113 modobj = sys.modules[module]
114 if not hasattr(modobj, "__github__"):
115 raise AttributeError(
116 "The module has no attribute '__github__'. The repository cannot be guessed.")
117 repository = modobj.__github__
118 repository = repository.rstrip("/")
120 loc = "/".join(["master", "_doc", "notebooks"] + paths[pos + 2:])
121 url = repository + "/" + loc
122 url = url.replace("github.com", "raw.githubusercontent.com")
123 return Image(url, width=width)