1
0
Эх сурвалжийг харах

strip down config to plain dictionaries

Oz N Tiram 9 жил өмнө
parent
commit
f64b211d33
3 өөрчлөгдсөн 34 нэмэгдсэн , 37 устгасан
  1. 26 4
      blogit/blogit.py
  2. 5 30
      conf.py
  3. 3 3
      tests/test_all.py

+ 26 - 4
blogit/blogit.py

@@ -37,7 +37,23 @@ import tinydb
 from tinydb import Query, where
 
 sys.path.insert(0, os.getcwd())
-from conf import CONFIG, ARCHIVE_SIZE, GLOBAL_TEMPLATE_CONTEXT, KINDS
+from conf import CONFIG, GLOBAL_TEMPLATE_CONTEXT
+
+# with this config, pages are rendered to the location of their title
+KINDS = {
+    'writing': {
+        'name': 'writing', 'name_plural': 'writings',
+    },
+    'note': {
+        'name': 'note', 'name_plural': 'notes',
+    },
+    'link': {
+        'name': 'link', 'name_plural': 'links',
+    },
+    'photo': {
+        'name': 'photo', 'name_plural': 'photos',
+    },
+}
 
 jinja_env = Environment(lstrip_blocks=True, trim_blocks=True,
                         loader=FileSystemLoader(CONFIG['templates']))
@@ -131,6 +147,8 @@ class Tag(object):
         _render(context, 'tag_index.html', os.path.join(render_to, 'index.html'))
         # render atom.xml
         context['entries'] = context['entries'][:10]
+        context['last_build'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
+
         _render(context, 'atom.xml', os.path.join(render_to, 'atom.xml'))
         return True
 
@@ -359,13 +377,14 @@ def update_index(entries):
     """
     context = GLOBAL_TEMPLATE_CONTEXT.copy()
     context['entries'] = entries
+    context['last_build'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
 
     map(lambda x: _render(
         context, x[0], os.path.join(CONFIG['output_to'], x[1])),
         (('entry_index.html', 'index.html'), ('atom.xml', 'atom.xml')))
 
 
-def build():
+def build(config):
     """Incremental build of the website"""
 
     print("\nRendering website now...\n")
@@ -399,7 +418,7 @@ def build():
     #    os.path.join(CONFIG['content_root'], e.get('filename'))) for e in
     #    DB.posts.all()]
 
-    render_archive(_sort_entries(entries, reversed=True)[ARCHIVE_SIZE:])
+    render_archive(_sort_entries(entries, reversed=True)[config['ARCHIVE_SIZE']:])
 
 
 
@@ -522,13 +541,16 @@ def main():   # pragma: no coverage
 
     args = parser.parse_args()
 
+    if not os.path.exists(os.path.join(CONFIG['content_root'])):
+        os.makedirs(os.path.join(CONFIG['content_root']))
+
     if len(sys.argv) < 2:
         parser.print_help()
         sys.exit()
     if args.clean:
         clean()
     if args.build:
-        build()
+        build(CONFIG)
     if args.dist:
         dist()
     if args.preview:

+ 5 - 30
conf.py

@@ -5,10 +5,9 @@ python code as a configuration language instead of choosing the
 ini, yaml, or what ever DSL for configuration.
 """
 
-import datetime
-import os
-from collections import namedtuple
-import tinydb
+# ARCHIVE SIZE
+# 0 Means that all the entries will be in the archive
+# 10 meas that all the entries except the last 10
 
 CONFIG = {
     'content_root': 'content',  # where the markdown files are
@@ -20,41 +19,17 @@ CONFIG = {
     'http_port': 3030,
     'content_encoding': 'utf-8',
     'author': 'Oz Nahum Tiram',
-    'editor': 'editor'
+    'editor': 'editor',
+    'ARCHIVE_SIZE': 10
     }
 
-if not os.path.exists(os.path.join(CONFIG['content_root'])):
-    os.makedirs(os.path.join(CONFIG['content_root']))
-
-
-# EDIT THIS PARAMETER TO CHANGE ARCHIVE SIZE
-# 0 Means that all the entries will be in the archive
-# 10 meas that all the entries except the last 10
-ARCHIVE_SIZE = 0
-
 GLOBAL_TEMPLATE_CONTEXT = {
     'media_base': '/media/',
     'media_url': '../media/',
     'site_url': 'http://oz123.github.com',
-    'last_build': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
     'twitter': 'https://twitter.com/#!/OzNTiram',
     'stackoverflow': "http://stackoverflow.com/users/492620/oz123",
     'github': "https://github.com/oz123",
 }
 
 
-# with this config, pages are rendered to the location of their title
-KINDS = {
-    'writing': {
-        'name': 'writing', 'name_plural': 'writings',
-    },
-    'note': {
-        'name': 'note', 'name_plural': 'notes',
-    },
-    'link': {
-        'name': 'link', 'name_plural': 'links',
-    },
-    'photo': {
-        'name': 'photo', 'name_plural': 'photos',
-    },
-}

+ 3 - 3
tests/test_all.py

@@ -258,13 +258,13 @@ def test_render_index():
 
 
 def test_build():
-    build()
+    build(CONFIG)
     # check that the index really contains the last 10 entries
     with open(os.path.join(CONFIG['output_to'], 'index.html')) as html_index:
         soup = BeautifulSoup(html_index.read(), 'html.parser')
         assert len(soup.find_all(class_='clearfix entry')) == 10
 
-    # pages should not be in the archive, but archive size here is different
+    # pages should not be in the archive
     with open(os.path.join(CONFIG['output_to'], 'archive', 'index.html')) as html_index:
         soup = BeautifulSoup(html_index.read(), 'html.parser')
-        assert len(soup.find_all(class_='post')) == 22
+        assert len(soup.find_all(class_='post')) == 12