forked from piobogiwo/IPython-plotly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
94 lines (77 loc) · 2.93 KB
/
common.py
File metadata and controls
94 lines (77 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import sys
import os
import delightfulsoup as ds
# -------------------------------------------------------------------------------
class PathHandler():
def __init__(self, name):
self.NAME = name
self.GLOBALS = self._get_globals()
self.nbs = self.load_references()['notebooks']
self.args = self._get_args()
def _get_globals(self):
return ds.load_json('_makescripts/data/globals.json')
def _is_valid(self, arg_or_args):
if isinstance(arg_or_args, list):
return all([arg in self.nbs for arg in arg_or_args])
else:
return arg in self.nbs
def _get_args(self):
NOTEBOOKS = self.GLOBALS['NOTEBOOKS']
_args = sys.argv[1:]
if not _args:
raise Exception(
"No nb argument sent\n\n"
"python {NAME}.py nb\n"
"python {NAME}.py nb1 nb2 ... nbN\n"
"The available directories are:\n"
" {nbs}".format(NAME=self.NAME,
nbs='\n '.join(nbs))
)
elif not self._is_valid(_args):
raise Exception(
'Invalid notebook directory.\n\n'
'The available notebook directories are:\n'
' {}'.format('\n '.join(self.nbs))
)
else:
return [os.path.dirname(arg + '/') for arg in _args]
def get_path(self, arg):
NOTEBOOKS = self.GLOBALS['NOTEBOOKS']
return os.path.join(NOTEBOOKS, arg) + '/'
def get_file(self, arg, ext):
NOTEBOOKS = self.GLOBALS['NOTEBOOKS']
return os.path.join(NOTEBOOKS, arg, arg) + ext
def load_config(self, arg):
path_config = os.path.join(self.get_path(arg), 'config.json')
return ds.load_json(path_config)
def load_references(self):
NOTEBOOKS = self.GLOBALS['NOTEBOOKS']
path_references = os.path.join(NOTEBOOKS, 'references.json')
return ds.load_json(path_references)
def get_tree(self, arg):
join = os.path.join
PUBLISHED = self.GLOBALS['PUBLISHED']
INCLUDES = join(PUBLISHED, 'includes')
tree = join(INCLUDES, arg)
if not os.path.exists(tree):
os.makedirs(tree)
return dict(
urls=join(PUBLISHED, 'urls.py'),
sitemaps=join(PUBLISHED, 'sitemaps.py'),
redirects=join(PUBLISHED, 'redirects.py'),
includes=dict(
references=join(INCLUDES, 'references.json'),
nb=dict(
body=join(INCLUDES, arg, 'body.html'),
config=join(INCLUDES, arg, 'config.json')
)
),
static=dict(
image=join(PUBLISHED, 'static', 'image')
)
)
def get_relative_urls(self):
relative_urls = []
for nb in self.nbs:
relative_urls += [self.load_config(nb)['relative_url']]
return relative_urls