From: Oleg Broytman Date: Mon, 10 Apr 2017 03:16:17 +0000 (+0300) Subject: Split blog loading/saving from reindex_blog.py to blog_db.py X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=f85b54d41b553cd6d9cdc43a2b16c2675bb99216;p=phdru.name%2Fphdru.name.git Split blog loading/saving from reindex_blog.py to blog_db.py Also remove parameters - it is always Russian blog. --- diff --git a/blog_db.py b/blog_db.py new file mode 100755 index 0000000..5850e76 --- /dev/null +++ b/blog_db.py @@ -0,0 +1,30 @@ +#! /usr/bin/env python + +import sys, os + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2017 PhiloSoft Design" + +blog_data_root = os.path.dirname(__file__) +blog_root = os.path.join(blog_data_root, '../htdocs/phdru.name/Russian/blog') +blog_filename = os.path.join(blog_data_root, "ru/blog_dict.pickle") + +try: + import cPickle as pickle +except ImportError: + import pickle + +def load_blog(): + try: + blog_file = open(blog_filename, "rb") + except IOError: + blog = {} + else: + blog = pickle.load(blog_file) + blog_file.close() + return blog + +def save_blog(blog): + blog_file = open(blog_filename, "wb") + pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL) + blog_file.close() diff --git a/reindex_blog.py b/reindex_blog.py index ac22002..c2e16ce 100755 --- a/reindex_blog.py +++ b/reindex_blog.py @@ -5,30 +5,12 @@ __author__ = "Oleg Broytman " __copyright__ = "Copyright (C) 2006-2017 PhiloSoft Design" import sys, os -from Cheetah.compat import string_type - -blog_data_root = sys.argv[1] -blog_root = sys.argv[2] -blog_filename = os.path.join(blog_data_root, "blog_dict.pickle") - -try: - import cPickle as pickle -except ImportError: - import pickle - from Cheetah.Template import Template +from Cheetah.compat import string_type +from blog_db import blog_root, load_blog, save_blog -# Load old blog - -try: - blog_file = open(blog_filename, "rb") -except IOError: - old_blog = {} -else: - old_blog = pickle.load(blog_file) - blog_file.close() - +old_blog = load_blog() # blog is a dictionary mapping # (year, month, day) => [list of (file, title, lead, tags)] @@ -87,10 +69,8 @@ for dirpath, dirs, files in os.walk(blog_root): bodies[key] = body # Need to save the blog? -if blog <> old_blog: - blog_file = open(blog_filename, "wb") - pickle.dump(blog, blog_file, pickle.HIGHEST_PROTOCOL) - blog_file.close() +if blog != old_blog: + save_blog(blog) # Localized month names diff --git a/reindex_blog_ru b/reindex_blog_ru index e51558d..a02f190 100755 --- a/reindex_blog_ru +++ b/reindex_blog_ru @@ -6,4 +6,4 @@ LC_TIME=ru_RU.KOI8-R export LC_TIME cd ../htdocs/phdru.name && -exec ../../phdru.name/reindex_blog.py ../../phdru.name/ru Russian/blog +exec ../../phdru.name/reindex_blog.py