Created
January 11, 2010 12:06
-
-
Save laiso/274186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import pikzie | |
from sqlobject import ( | |
sqlhub, connectionForURI, | |
SQLObject, | |
) | |
sqlhub.processConnection = \ | |
connectionForURI('mysql://USER:PASSWORD@localhost/blog') | |
class Entry(SQLObject): | |
class sqlmeta(): | |
table = 'entries' | |
fromDatabase = True | |
class TD2(): | |
def __init__(self, entries, version="TDIARY2.01.00" , path=None): | |
self.TDIARY = version | |
self.entries = entries | |
self.path = path | |
def publish(self): | |
for e in self.entries: | |
if not os.path.exists(e.path): | |
self.__add_TD2header(e) | |
self.__add_entry(e) | |
def __add_TD2header(self, entry): | |
file = open(entry.path, 'w') | |
file.write(self.TDIARY + "\n") | |
file.close() | |
def __add_entry(self, entry): | |
file = open(entry.path, 'a') | |
file.writelines(( | |
"Date: "+entry.Date+"\n", | |
"Title: "+entry.Title+"\n", | |
"Last-Modified: \n", | |
"Visible: true\n", | |
"Format: tDiary\n\n", | |
entry.body+"\n", | |
".\n" | |
)) | |
file.close() | |
class TD2Entry(): | |
def __init__(self, \ | |
title=None, body=None, \ | |
date=None, last_modified=None, \ | |
format=None, visible=True, \ | |
base_dir="diary", path=None): | |
self.Title = title | |
self.body = body | |
self.datetime = date, | |
self.Date = self.__normarize_date(date) | |
self.LastModified = last_modified | |
self.Format = format | |
self.Visible = visible | |
self.base_dir = base_dir, | |
self.path = path | |
self.__appoint_filepath() | |
self.__setup_writable_path() | |
def __normarize_date(self, datetime_obj): | |
if not datetime_obj: | |
return "20090101" | |
return datetime_obj.strftime("%Y%m%d") | |
def __appoint_filepath(self): | |
dir = self.datetime[0].strftime("%Y") | |
path = self.datetime[0].strftime("%Y%m.td2") | |
self.base_dir = "%s/%s" % (self.base_dir[0], dir) | |
self.path = "%s/%s" % (self.base_dir, path) | |
def __setup_writable_path(self): | |
if not os.path.exists(self.base_dir): | |
os.makedirs(self.base_dir) | |
class MigHandler(): | |
def __init__(self): | |
self.entries = list() | |
def fetch_all_blog_entries(self): | |
self.entries = Entry.select() | |
def publish(self, entries): | |
TD2(entries).publish() | |
def to_TD2Entry(self): | |
return [self.__to_TD2Entry(e) for e in self.entries] | |
def __to_TD2Entry(self, entry): | |
return TD2Entry( | |
title=entry.title, | |
body=entry.html, | |
date=entry.published | |
) | |
class AllTests(pikzie.TestCase): | |
def setup(self): | |
self.mig = MigHandler() | |
self.mig.fetch_all_blog_entries() | |
self.entries = self.mig.to_TD2Entry() | |
def test_fetchAllEntries(self): | |
self.assert_equal( | |
unicode(self.mig.entries[0].title, 'utf-8'), | |
u"Tornadoのexampleアプリケーションをカスタマイズしてつかう") | |
def test_toTD2Entry(self): | |
self.assert_equal( | |
self.entries[0].Date, | |
"20090927" | |
) | |
def test_td2filepath(self): | |
path = self.entries[0].path | |
dir = self.entries[0].base_dir | |
self.assert_equal(dir, "diary/2009") | |
self.assert_equal(path, "diary/2009/200909.td2") | |
def test_publish(self): | |
self.mig.publish(self.entries) | |
def main(): | |
pass | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment