forked from flask-kr/rosetta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
executable file
·308 lines (225 loc) · 10.1 KB
/
manage.py
File metadata and controls
executable file
·308 lines (225 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# -*- coding:utf8 -*-
import os
from framework import db
from application import app_factory
from urlparse import urlparse
from pypm import ProjectManager, FilterPattern
CONFIG_DIR_PATH = os.path.expandvars(
'$PROJECT_DIR/etc/configs')
DEFAULT_CONFIG_FILE_PATH = os.path.expandvars(
'$PROJECT_DIR/etc/configs/default_config.yml')
USER_CONFIG_FILE_PATH = os.path.expandvars(
'$PROJECT_DIR/etc/configs/user_config.yml')
ALEMBIC_CONFIG_FILE_PATH = os.path.expandvars(
'$PROJECT_DIR/alembic.ini')
ALEMBIC_REVISION_DIR_PATH = os.path.expandvars(
'$PROJECT_DIR/alembic/versions')
EXAMPLE_DATA_LOCALE_DIR_PATH = os.path.expandvars(
'$PROJECT_DIR/examples/data/locales')
def create_application(custom_config_file_path=USER_CONFIG_FILE_PATH):
if os.access(custom_config_file_path, os.R_OK):
return app_factory.create_application(
DEFAULT_CONFIG_FILE_PATH,
custom_config_file_path)
else:
print("NOT_FOUND_CUSTOM_CONFIG_FILE_PATH:%s" % custom_config_file_path)
return app_factory.create_application(
DEFAULT_CONFIG_FILE_PATH)
pm = ProjectManager()
@pm.command(package_names=dict(type=str, nargs='+', help='파이썬 패키지 이름'))
def install_package(package_names):
"""
파이썬 패키지 설치 후 requirement.txt 를 갱신합니다.
"""
if not package_names:
raise pm.ArgumentError('NO_PACKAGE_NAME')
for package_name in package_names:
pm.run_system_command('pip', ['install', package_name])
pm.run_system_command(
'pip', ['freeze', '> ./requirements.txt'])
@pm.command(config_hint=dict(type=str, nargs=1, help='설정 파일 경로 혹은 접두어'))
def switch_config(config_hint):
"""
유저 설정 파일을 교체합니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
pm.run_system_command(
'cp', [config_file_path, USER_CONFIG_FILE_PATH])
@pm.command(config_hint=dict(type=str, nargs=1, help='설정 파일 경로 혹은 접두어'))
def edit_config(config_hint):
"""
설정 파일을 수정 합니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
pm.run_system_command('$EDITOR', [config_file_path])
@pm.command(script_file_path=dict(type=str, nargs=1, help='스크립트 파일 경로'))
def run_script(script_file_path):
"""
스크립트를 실행합니다.
"""
script_dir_path, script_name = os.path.split(script_file_path)
import sys
sys.path.append(script_dir_path)
execfile(script_file_path, {'__name__': '__main__'})
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH, help='설정 파일 경로'))
def run_shell(config_hint):
"""
쉘을 실행합니다. app 과 db 에 접근할 수 있습니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
app = create_application(config_file_path)
pm.run_python_shell('Rosetta Shell', local_dict=dict(app=app, db=db))
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH, help='설정 파일 경로'))
def reset_all_dbs(config_hint):
"""
모든 데이터 베이스를 리셋합니다. 만약에 대비해 패스워드를 확인합니다.
전체 리셋 패스워드를 지정하지 않았다면 사용할 수 없습니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
app = create_application(config_file_path)
print "#### reset all databases"
print "* database uri: %s" % app.config['SQLALCHEMY_DATABASE_URI']
for key, value in sorted(app.config['SQLALCHEMY_BINDS'].iteritems()):
print " * bind_key: %s uri:%s" % (key, value)
print "* reset_all_password:",
config_password = app.config['DB_RESET_ALL_PASSWORD']
if not config_password:
raise pm.Error(
'NOT_FOUND_TO_DB_RESET_ALL_PASSWORD_IN_CONFIG_PATH:' +
config_file_path)
input_password = raw_input()
if input_password != config_password:
raise pm.Error('WRONG_DB_RESET_ALL_PASSWORD')
db.drop_all()
db.create_all()
from alembic.config import Config
alembic_config = Config(ALEMBIC_CONFIG_FILE_PATH)
from alembic import command
command.stamp(alembic_config, "head")
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH, help='설정 파일 경로'))
def connect_db(config_hint):
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
app = create_application(config_file_path)
db_uri = urlparse(app.config['SQLALCHEMY_DATABASE_URI'])
if db_uri.scheme == 'sqlite':
pm.run_system_command('sqlite3', [db_uri.path])
else:
print 'NOT_SUPPORT_DB_SCHEME:', db_uri.scheme
@pm.command(title=dict(type=str, nargs=1, help='DB 리비전 제목'))
def make_db_rev(title):
pm.run_system_command('alembic', ['revision', '--autogenerate', '-m', title])
@pm.command()
def list_db_revs():
pm.run_system_command('ls', [ALEMBIC_REVISION_DIR_PATH])
@pm.command(db_rev_hint=dict(type=str, nargs=1, help='DB 리비전 파일 이름 접두어'))
def edit_db_rev(db_rev_hint):
db_rev_file_path = pm.smart_find_file_path(
db_rev_hint, base_dir_path=ALEMBIC_REVISION_DIR_PATH)
pm.run_system_command('$EDITOR', [db_rev_file_path])
@pm.command(db_rev_hint=dict(type=str, nargs=1, help='DB 리비전 파일 이름 접두어'))
def remove_db_rev(db_rev_hint):
db_rev_file_path = pm.smart_find_file_path(
db_rev_hint, base_dir_path=ALEMBIC_REVISION_DIR_PATH)
pm.remove_file(db_rev_file_path, is_testing=False)
@pm.command(db_rev_hint=dict(type=str, nargs=1, help='DB 리비전 파일 이름 접두어'))
def apply_db_rev(db_rev_hint):
db_rev_file_path = pm.smart_find_file_path(
db_rev_hint, base_dir_path=ALEMBIC_REVISION_DIR_PATH)
db_rev_file_name = os.path.basename(db_rev_file_path).split('_')[0]
pm.run_system_command('alembic', ['upgrade', db_rev_file_name])
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH,
help='설정 파일 경로'),
port=dict(type=int, flag='-p', default=5000, help="포트 번호"))
def run_server(config_hint, port):
"""
서버를 실행합니다. 기본 포트는 5000번입니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
app = create_application(config_file_path)
from application.apis import api_bp
from application.pages import page_bp
app.register_blueprint(api_bp)
app.register_blueprint(page_bp)
if app.config['DEBUG_PAGES']:
from application.pages import page_debug_bp
app.register_blueprint(page_debug_bp)
app.run(port=port)
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH,
help='설정 파일 경로'),
locale_dir_path=dict(type=str, flag='-l',
default=EXAMPLE_DATA_LOCALE_DIR_PATH,
help='로케일 디렉토리 경로'),
po_hints=dict(type=str, nargs='+', help='번역 파일 경로들'))
def insert_po(config_hint, locale_dir_path, po_hints):
"""
po 번역 파일을 데이터 베이스에 추가합니다.
"""
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
create_application(config_file_path)
import polib
import email.utils
from urlparse import urlparse, urlunsplit
po_file_paths = list(pm.find_file_path_iter(
base_dir_path=locale_dir_path,
filter_file_name=FilterPattern(po_hints)))
from application.models import Site, Page, Sentence
from application.models import User, Translation, Selection
for po_file_path in po_file_paths:
print "insert_database_from_po_file:", po_file_path
po = polib.pofile(po_file_path)
site_url = urlparse(po.metadata['Project-Id-Version'])
site, is_site_created = Site.query\
.get_or_create(url=urlunsplit(
(site_url.scheme, site_url.netloc, '', '', '')))
page, is_page_created = Page.query\
.get_or_create(path=site_url.path, site=site)
user_name, user_email = email.utils\
.parseaddr(po.metadata['Last-Translator'])
user, is_user_created = User.query\
.get_or_create(uid=user_email, name=user_name)
for entry in po:
sentence, is_sentence_created = Sentence.query\
.get_or_create(text=entry.msgid, page=page)
translation, is_translation_created = Translation.query\
.get_or_create(text=entry.msgstr,
sentence=sentence,
user=user)
selection, is_selection_created = Selection.query\
.get_or_create(translation=translation,
sentence=sentence,
user=user)
db.session.commit()
@pm.command(config_hint=dict(type=str, flag='-c',
default=USER_CONFIG_FILE_PATH,
help='설정 파일 경로'),
texts=dict(type=str, nargs='+', help='원문 문장들'))
def translate(config_hint, texts):
config_file_path = pm.smart_find_file_path(
config_hint, base_dir_path=CONFIG_DIR_PATH)
create_application(config_file_path)
from application.models import Sentence
sentences = Sentence.query.filter(Sentence.text.in_(texts)).all()
for sentence in sentences:
print "original:", sentence.text
for translation in sentence.translations:
print "translation:", translation.text
print ''
if __name__ == '__main__':
if 0:
pm.run_command(['translate', 'Foreword for Experienced Programmers'])
else:
import sys
pm.run_command(sys.argv[1:])