Code

DjangoDocumentKoreanTranslation: parse_draft.py

File parse_draft.py, 2.5 KB (added by spike, 7 years ago)
Line 
1# -*- coding: utf-8 -*-
2"""
3 Copyright 2005 Spike^ekipS <spikeekips@gmail.com>
4
5    This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18"""
19
20import os, re, sys
21
22def print_usage () :
23    print """
24%s <document filepath> <target, orig|trans>
25""" % sys.argv[0]
26
27if len(sys.argv) < 2 :
28    print_usage()
29    sys.exit()
30
31RE_TRANS_START_ORIG = re.compile("^>>\!$")
32RE_TRANS_END_ORIG = re.compile("^<<!$")
33RE_TRANS_START_TRANS = re.compile("^>>!!$")
34RE_TRANS_END_TRANS = re.compile("^<<!!$")
35
36started_orig = False
37ended_orig = False
38started_trans = False
39ended_trans = False
40
41lines_trans = list()
42lines_orig = list()
43
44filename = sys.argv[1]
45path_filename = os.path.dirname(filename)
46document_name = os.path.splitext(os.path.basename(filename))[0]
47
48for i in file(filename) :
49    i = i.rstrip()
50
51    if RE_TRANS_START_ORIG.findall(i) :
52        started_orig = True
53        started_trans = False
54        continue
55    elif started_orig and RE_TRANS_END_ORIG.findall(i) :
56        started_orig = False
57        started_trans = False
58        continue
59
60    if RE_TRANS_START_TRANS.findall(i) :
61        started_orig = False
62        started_trans = True
63        continue
64    elif started_trans and RE_TRANS_END_TRANS.findall(i) :
65        started_orig = False
66        started_trans = False
67        continue
68
69    if started_orig :
70        lines_orig.append(i)
71    elif started_trans :
72        lines_trans.append(i)
73    elif not started_orig and not started_trans :
74        lines_orig.append(i)
75        lines_trans.append(i)
76
77fd = file(os.path.join(path_filename, "%s-en.txt" % document_name), "w")
78fd.write("\n".join(lines_orig))
79fd.close()
80
81fd = file(os.path.join(path_filename, "%s-ko.txt" % document_name), "w")
82fd.write("\n".join(lines_trans))
83fd.close()
84
85"""
86Description
87-----------
88
89
90ChangeLog
91---------
92
93
94Usage
95-----
96
97
98"""
99
100__author__ =  "Spike^ekipS <spikeekips@gmail.com>"
101__version__=  "0.1"
102__nonsense__ = ""
103
104
105