Skip to content

Commit c09787a

Browse files
committedAug 30, 2016
Better support for a whole tree of data files
1 parent 6497f53 commit c09787a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed
 

‎CHANGES.txt

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
New in master
2+
=============
3+
4+
Features
5+
--------
6+
7+
* Better support for a tree of files in ``data/``
8+
19
New in v7.8.0
210
=============
311

‎nikola/nikola.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from copy import copy
3333
from pkg_resources import resource_filename
3434
import datetime
35-
import glob
3635
import locale
3736
import os
3837
import json
@@ -1136,12 +1135,13 @@ def _set_global_context_from_config(self):
11361135

11371136
def _set_global_context_from_data(self):
11381137
"""Load files from data/ and put them in the global context."""
1139-
self._GLOBAL_CONTEXT['data'] = {}
1140-
for fname in glob.glob('data/*'):
1141-
data = utils.load_data(fname)
1142-
key = os.path.basename(fname)
1143-
key = os.path.splitext(key)[0]
1144-
self._GLOBAL_CONTEXT['data'][key] = data
1138+
self._GLOBAL_CONTEXT['data'] = defaultdict(defaultdict)
1139+
for root, dirs, files in os.walk('data', followlinks=True):
1140+
for fname in files:
1141+
fname = os.path.join(root, fname)
1142+
data = utils.load_data(fname)
1143+
key = os.path.splitext(fname.split(os.sep, 1)[1])[0]
1144+
self._GLOBAL_CONTEXT['data'][key] = data
11451145

11461146
def _activate_plugins_of_category(self, category):
11471147
"""Activate all the plugins of a given category and return them."""

‎nikola/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,7 @@ def prefixed_lines():
19231923
def load_data(path):
19241924
"""Given path to a file, load data from it."""
19251925
ext = os.path.splitext(path)[-1]
1926+
loader = None
19261927
if ext in {'.yml', '.yaml'}:
19271928
loader = yaml
19281929
if yaml is None:
@@ -1935,5 +1936,7 @@ def load_data(path):
19351936
req_missing(['toml'], 'use TOML data files')
19361937
return {}
19371938
loader = toml
1939+
if loader is None:
1940+
return
19381941
with io.open(path, 'r', encoding='utf8') as inf:
19391942
return loader.load(inf)

0 commit comments

Comments
 (0)
Please sign in to comment.