Oz N Tiram преди 9 години
родител
ревизия
00ca3acb54
променени са 4 файла, в които са добавени 51 реда и са изтрити 26 реда
  1. 21 6
      blogit2.py
  2. 14 6
      conf.py
  3. 15 11
      tests/test_blogit2.py
  4. 1 3
      tests/test_tag.py

+ 21 - 6
blogit2.py

@@ -72,19 +72,32 @@ except ImportError, e:
         print "try: sudo pip install markdown2"
         sys.exit(1)
 
+import tinydb
 from tinydb import Query
 sys.path.insert(0, os.getcwdu())
-from conf import CONFIG, ARCHIVE_SIZE, GLOBAL_TEMPLATE_CONTEXT, KINDS, DB
+from conf import CONFIG, ARCHIVE_SIZE, GLOBAL_TEMPLATE_CONTEXT, KINDS
 jinja_env = Environment(loader=FileSystemLoader(CONFIG['templates']))
 
 
+class DataBase(object):
+
+    def __init__(self, path):
+        _db = tinydb.TinyDB(path)
+        self.posts = _db.table('posts')
+        self.tags = _db.table('tags')
+        self.pages = _db.table('pages')
+        self.templates = _db.table('templates')
+        self._db = _db
+
+DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
+
 class Tag(object):
 
     def __init__(self, name):
         self.name = name
         self.prepare()
         self.permalink = GLOBAL_TEMPLATE_CONTEXT["site_url"]
-        self.table = DB['tags']
+        self.table = DB.tags
 
         # todo: fix this
         #try:
@@ -129,7 +142,9 @@ class Tag(object):
         _entries = []
         Posts = Query()
         for id in self.posts:
-            post = DB['posts'].get(eid=id)
+            post = DB.posts.get(eid=id)
+            if not post:
+                raise ValueError("no post found for eid %s" % id)
             entry = Entry(os.path.join(CONFIG['content_root'],
                                        post['filename']))
             _entries.append(entry)
@@ -373,9 +388,9 @@ def find_new_posts(posts_table):
 
 
 def _get_last_entries():
-    eids = [post.eid for post in DB['posts'].all()]
+    eids = [post.eid for post in DB.posts.all()]
     eids = sorted(eids, reverse=True)[-10:]
-    entries = [Entry(DB['posts'].get(eid=eid)['filename']) for eid in eids]
+    entries = [Entry(DB.posts.get(eid=eid)['filename']) for eid in eids]
     return entries
 
 
@@ -420,7 +435,7 @@ def new_build():
     entries = list()
     tags = dict()
     root = CONFIG['content_root']
-    for post_id, post in find_new_posts(DB['posts']):
+    for post_id, post in find_new_posts(DB.posts):
         try:
             entry = Entry(os.path.join(root, post))
             if entry.render():

+ 14 - 6
conf.py

@@ -6,12 +6,10 @@ ini, yaml, or what ever DSL for configuration.
 """
 
 import datetime
+import os
+from collections import namedtuple
 import tinydb
 
-db = tinydb.TinyDB('blogit.db')
-
-
-
 CONFIG = {
     'content_root': 'content',  # where the markdown files are
     'output_to': 'oz123.github.com',
@@ -25,11 +23,21 @@ CONFIG = {
     'editor': 'editor'
     }
 
+if not os.path.exists(os.path.join(CONFIG['content_root'])):
+    os.makedirs(os.path.join(CONFIG['content_root']))
+
+
+#_db = tinydb.TinyDB(os.path.join(CONFIG['content_root'], 'blogit.db'))
 
 # TODO replace this with a namedtuple for a more convinient access and safety
-DB = {'posts': db.table('posts'), 'tags': db.table('tags'),
-      'pages': db.table('pages'), 'templates': db.table('templates') }
+#_DB = {'posts': _db.table('posts'), 'tags': _db.table('tags'),
+#       'pages': _db.table('pages'), 'templates': _db.table('templates') }
+
+#BlogDB = namedtuple('BlogDB', 'posts tags pages templates db')
 
+#DB = BlogDB(posts=_db.table('posts'), tags=_db.table('tags'),
+#            pages=_db.table('pages'), templates=_db.table('templates'),
+#            db=_db)
 
 # EDIT THIS PARAMETER TO CHANGE ARCHIVE SIZE
 # 0 Means that all the entries will be in the archive

+ 15 - 11
tests/test_blogit2.py

@@ -1,9 +1,8 @@
 import os
 import shutil
 from tinydb import Query
-from blogit2 import find_new_posts, DB, Entry, Tag
+from blogit2 import find_new_posts, DataBase, Entry, Tag
 from blogit2 import CONFIG, new_build
-from conf import db
 
 
 post_dummy = """title: Blog post {}
@@ -19,10 +18,10 @@ summary: |
 
 This is the body of post {}
 """
-def insert_single():
+def insert_single(DB):
     Posts = Query()
-    if not DB['posts'].contains(Posts.filename == 'post4.md'):
-        DB['posts'].insert({'filename': 'post4.md'})
+    if not DB.posts.contains(Posts.filename == 'post4.md'):
+        DB.posts.insert({'filename': 'post4.md'})
 
 
 def create_posts():
@@ -38,12 +37,13 @@ def clean_posts():
         shutil.rmtree('content')
 
 def test_find_new_posts():
-    db.purge_tables()
-    insert_single()
     clean_posts()
     create_posts()
-    new =  list(find_new_posts(DB['posts']))
-    assert len(DB['posts'].all()) == 4
+    DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
+    DB._db.purge_tables()
+    insert_single(DB)
+    new =  list(find_new_posts(DB.posts))
+    assert len(DB.posts.all()) == 4
     assert len(new) == 3
 
 
@@ -56,7 +56,8 @@ def test_tags():
 
 
 def test_new_build():
-    db.purge_tables()
+    DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
+    DB._db.purge_tables()
     clean_posts()
     create_posts()
     new_build()
@@ -86,4 +87,7 @@ def test_new_build2():
     # bug: creating a new post with existing tags
     # removes older tags ...
 
-os.unlink('blogit.db')
+try:
+    os.unlink('blogit.db')
+except OSError:
+    pass

+ 1 - 3
tests/test_tag.py

@@ -3,7 +3,6 @@ import shutil
 from tinydb import Query
 from blogit2 import find_new_posts, DB, Entry, Tag
 from blogit2 import CONFIG, new_build
-from conf import db
 
 
 post_dummy = """title: Blog post {}
@@ -20,7 +19,6 @@ summary: |
 This is the body of post {}
 """
 
-
 def create_posts():
     os.mkdir('content')
     os.chdir('content')
@@ -31,7 +29,7 @@ def create_posts():
 
 
 def test_tag():
-    new =  list(find_new_posts(DB['posts']))
+    new =  list(find_new_posts(DB.posts))
     t = Tag('python')
     t.posts = [1,2,3]
     t.render()