From 66fa62d6bec7da8d3377f086760caac7d876141b Mon Sep 17 00:00:00 2001 From: Yax Date: Wed, 29 Apr 2015 20:20:07 +0200 Subject: [PATCH] Migration tools from Pecosys to Stacosys --- requirements.txt | 6 ++++ tools/config.json | 3 ++ tools/pecosys2stacosys.py | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 requirements.txt create mode 100644 tools/config.json create mode 100644 tools/pecosys2stacosys.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b11b0cb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +Flask==0.10.1 +itsdangerous==0.24 +Jinja2==2.7.3 +MarkupSafe==0.23 +SQLAlchemy==1.0.2 +Werkzeug==0.10.4 diff --git a/tools/config.json b/tools/config.json new file mode 100644 index 0000000..ffdef07 --- /dev/null +++ b/tools/config.json @@ -0,0 +1,3 @@ +{ + "comments": "/home/yannic/work/coding/blog/blogduyax/comments" +} diff --git a/tools/pecosys2stacosys.py b/tools/pecosys2stacosys.py new file mode 100644 index 0000000..c8da9c7 --- /dev/null +++ b/tools/pecosys2stacosys.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- + +import os +import re +import json +import logging +from clize import clize, run + + +# configure logging +level = logging.DEBUG +logger = logging.getLogger(__name__) +logger.setLevel(level) +ch = logging.StreamHandler() +ch.setLevel(level) +formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') +ch.setFormatter(formatter) +logger.addHandler(ch) + +# regex +regex = re.compile(r"(\w+):\s*(.*)") + + +def convert_comment(config, filename): + logger.info('convert %s' % filename) + d = {} + with open(filename) as f: + for line in f: + match = regex.match(line) + if match: + d[match.group(1)] = match.group(2) + else: + break + logger.debug(d) + + +def convert(config): + comment_dir = config['comments'] + logger.info('Comment directory %s' % comment_dir) + for dirpath, dirs, files in os.walk(comment_dir): + for filename in files: + if filename.endswith(('.md',)): + comment_file = '/'.join([dirpath, filename]) + convert_comment(config, comment_file) + else: + logger.debug('ignore file %s' % filename) + + +def load_config(config_pathname): + logger.info("Load config from %s" % config_pathname) + with open(config_pathname, 'rt') as config_file: + config = json.loads(config_file.read()) + return config + + +@clize +def pecosys2stacosys(config_pathname): + config = load_config(config_pathname) + convert(config) + + +if __name__ == '__main__': + run(pecosys2stacosys)