internal cleanup

This commit is contained in:
Elan Ruusamäe 2012-09-17 23:42:09 +02:00
parent d7ee835ce5
commit 800196e92d

View file

@ -46,14 +46,11 @@ def get_path_names(moin_pages_dir, basenames = False):
return pathnames return pathnames
def readfile(filename): def readfile(filename):
return file(filename, 'r').readlines()
def readfile2(filename):
with open(filename, 'r') as f: with open(filename, 'r') as f:
text = f.read() text = f.read()
return unicode(text.decode('utf-8')) return unicode(text.decode('utf-8'))
def writefile2(filename, content, overwrite=False): def writefile(filename, content, overwrite=False):
dir = os.path.split(filename)[0] dir = os.path.split(filename)[0]
if not isdir(dir): if not isdir(dir):
os.makedirs(dir); os.makedirs(dir);
@ -65,24 +62,12 @@ def writefile2(filename, content, overwrite=False):
f.write(content) f.write(content)
f.close() f.close()
def writefile(filename, content, overwrite=False):
dir = os.path.split(filename)[0]
if not isdir(dir):
os.makedirs(dir);
if os.path.exists(filename) and overwrite == False:
raise OSError, 'File already exists: %s' % filename
f = file(filename, 'w')
f.writelines([it.rstrip() + '\n' for it in content if it])
f.close()
def get_current_revision(pagedir): def get_current_revision(pagedir):
rev_dir = os.path.join(pagedir, 'revisions') rev_dir = os.path.join(pagedir, 'revisions')
# try "current" file first # try "current" file first
f = os.path.join(pagedir, 'current') f = os.path.join(pagedir, 'current')
if os.path.exists(f): if os.path.exists(f):
rev = readfile(f)[0].rstrip() rev = readfile(f).rstrip()
try: try:
int(rev) int(rev)
except ValueError, e: except ValueError, e:
@ -257,7 +242,7 @@ def convertfile(pagedir, overwrite = False):
print "SKIP %s: internal page" % pagedir print "SKIP %s: internal page" % pagedir
return return
content = readfile2(curr_rev) content = readfile(curr_rev)
# print "content:[%s]" % content # print "content:[%s]" % content
# content = convert_markup(pagename, content) # content = convert_markup(pagename, content)
content = moin2doku(pagename, content) content = moin2doku(pagename, content)
@ -265,7 +250,7 @@ def convertfile(pagedir, overwrite = False):
out_file = os.path.join(output_dir, dw.wikiFN(pagename)) out_file = os.path.join(output_dir, dw.wikiFN(pagename))
print "dokuname: [%s]" % out_file print "dokuname: [%s]" % out_file
try: try:
writefile2(out_file, content, overwrite = overwrite) writefile(out_file, content, overwrite = overwrite)
except OSError, e: except OSError, e:
print e print e
return 0 return 0