Conversion tool is near from complete. Use Peewee as ORM
This commit is contained in:
parent
b38d47228a
commit
7b5c0822ff
7 changed files with 140 additions and 19 deletions
|
|
@ -1,3 +0,0 @@
|
|||
{
|
||||
"comments": "/home/yannic/work/coding/blog/blogduyax/comments"
|
||||
}
|
||||
|
|
@ -1,12 +1,24 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import logging
|
||||
from clize import clize, run
|
||||
|
||||
# add necessary directories to PATH
|
||||
current_path = os.path.realpath('.')
|
||||
parent_path = os.path.abspath(os.path.join(current_path, '..'))
|
||||
paths = [current_path, parent_path]
|
||||
for path in paths:
|
||||
if path not in sys.path:
|
||||
sys.path.insert(0, path)
|
||||
|
||||
# import database models
|
||||
from app.services.database import provide_db
|
||||
from app.models.site import Site
|
||||
from app.models.comment import Comment
|
||||
|
||||
# configure logging
|
||||
level = logging.DEBUG
|
||||
|
|
@ -22,9 +34,10 @@ logger.addHandler(ch)
|
|||
regex = re.compile(r"(\w+):\s*(.*)")
|
||||
|
||||
|
||||
def convert_comment(config, filename):
|
||||
def convert_comment(db, site, filename):
|
||||
logger.info('convert %s' % filename)
|
||||
d = {}
|
||||
content = ''
|
||||
with open(filename) as f:
|
||||
for line in f:
|
||||
match = regex.match(line)
|
||||
|
|
@ -32,32 +45,60 @@ def convert_comment(config, filename):
|
|||
d[match.group(1)] = match.group(2)
|
||||
else:
|
||||
break
|
||||
is_header = True
|
||||
for line in f:
|
||||
if is_header:
|
||||
if line.strip():
|
||||
is_header = False
|
||||
else:
|
||||
continue
|
||||
content = content + line
|
||||
logger.debug(d)
|
||||
logger.debug(content)
|
||||
|
||||
# create DB record
|
||||
comment = Comment(site=site, author_name=d['author'], content=content)
|
||||
if 'email' in d:
|
||||
comment.author_email = d['email']
|
||||
if 'site' in d:
|
||||
comment.author_site = d['site']
|
||||
if 'url' in d:
|
||||
comment.url = d['url']
|
||||
# else:
|
||||
# comment.url = d['article']
|
||||
if 'date' in d:
|
||||
comment.date = d['date']
|
||||
comment.save()
|
||||
|
||||
|
||||
def convert(config):
|
||||
comment_dir = config['comments']
|
||||
@provide_db
|
||||
def convert(db, site_name, url, comment_dir):
|
||||
|
||||
# create DB tables if needed
|
||||
db.create_tables([Site, Comment], safe=True)
|
||||
|
||||
# delete site record
|
||||
try:
|
||||
site = Site.select().where(Site.name == site_name).get()
|
||||
site.delete_instance(recursive=True)
|
||||
except Site.DoesNotExist:
|
||||
pass
|
||||
|
||||
site = Site.create(name=site_name, url=url, token='')
|
||||
|
||||
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)
|
||||
convert_comment(db, site, 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)
|
||||
def pecosys2stacosys(site, url, comment_dir):
|
||||
convert(site, url, comment_dir)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue