@@ -86,7 +86,7 @@ def init(config):
86
86
print (" - Language added: {}" .format (lang ))
87
87
88
88
89
- def pull (config ):
89
+ def pull (config , languages = None ):
90
90
"""
91
91
Pulls translations from the POEditor API.
92
92
"""
@@ -103,15 +103,57 @@ def pull(config):
103
103
104
104
for o in options :
105
105
if o .startswith ("trans." ):
106
+ language = o [6 :]
107
+ if languages and language not in languages :
108
+ continue
109
+
106
110
export_path = config .get (s , o )
107
111
parent_dir = os .path .dirname (export_path )
108
112
if not os .path .exists (parent_dir ):
109
113
os .makedirs (parent_dir )
110
- language = o [ 6 :]
114
+
111
115
print ("Language: {}" .format (language ))
112
116
client .export (project_id , language_code = language , file_type = file_type , local_file = export_path )
113
117
114
118
119
+ def push (config , languages = None , overwrite = False , sync_terms = False ):
120
+ """
121
+ Push terms and languages
122
+ """
123
+ assert config
124
+ client = POEditorAPI (api_token = config .get ("main" , "apikey" ))
125
+ sections = config .sections ()
126
+
127
+ for section in sections :
128
+ if not section .startswith ("project." ):
129
+ continue
130
+
131
+ print ("Project: {}" .format (section ))
132
+ project_id = config .get (section , "project_id" )
133
+ options = config .options (section )
134
+
135
+ for option in options :
136
+ if option .startswith ("trans." ):
137
+ import_path = config .get (section , option )
138
+ language = option .split ('.' , 1 )[- 1 ]
139
+ if languages and language not in languages :
140
+ continue
141
+
142
+ if not os .path .exists (import_path ):
143
+ print ("Error: {path} doesn't exist: ignoring language '{language}'"
144
+ .format (path = import_path , language = language ))
145
+ continue
146
+
147
+ print (" Pushing language '{}'..." .format (language ))
148
+ client .update_terms_definitions (
149
+ project_id ,
150
+ language_code = language ,
151
+ file_path = import_path ,
152
+ overwrite = overwrite ,
153
+ sync_terms = sync_terms
154
+ )
155
+
156
+
115
157
def pushTerms (config ):
116
158
"""
117
159
Pushes new terms to POEditor
@@ -129,29 +171,71 @@ def pushTerms(config):
129
171
client .update_terms (project_id , terms )
130
172
131
173
174
+ def status (config ):
175
+ """
176
+ Status is a simple task that displays the existing project configuration in a more human readable format.
177
+ It lists all resources that have been initialized under the local project and all their associated translation
178
+ files.
179
+ """
180
+ assert config
181
+ client = POEditorAPI (api_token = config .get ("main" , "apikey" ))
182
+ sections = config .sections ()
183
+
184
+ print ("Api key: {}" .format (config .get ("main" , "apikey" )))
185
+
186
+ for s in sections :
187
+ if s .startswith ("project." ):
188
+ project_id = config .get (s , "project_id" )
189
+ details = client .view_project_details (project_id )
190
+ terms = config .get (s , 'terms' , None ) if config .has_option (s , 'terms' ) else None
191
+ options = config .options (s )
192
+
193
+ print ("\n Project: {0} ({1})" .format (details ['name' ], details ['id' ]))
194
+ print ("Terms: {0}" .format (terms ))
195
+
196
+ for option in options :
197
+ if option .startswith ("trans." ):
198
+ import_path = config .get (s , option )
199
+ language = option .split ('.' , 1 )[- 1 ]
200
+ print (" - Language {0}: {1}" .format (language , import_path ))
201
+
202
+
132
203
def main ():
133
204
"""
134
205
./test.py init -f examples/.poeditor0
135
206
"""
136
207
parser = argparse .ArgumentParser ()
137
208
parser .add_argument ('--config-file' , '-f' , default = FILENAME )
138
- parser .add_argument ('command' , choices = ('init' , 'generate' , 'pull' , 'pushTerms' ))
209
+ parser .add_argument ('--overwrite' , default = False , action = "store_true" ,
210
+ help = "Overwrites definitions when pushing a file." )
211
+ parser .add_argument ('--sync-terms' , default = False , action = "store_true" ,
212
+ help = "Syncing terms deletes terms that are not found in the pushed file and adds new ones." )
213
+ parser .add_argument ('command' , choices = ('init' , 'generate' , 'pull' , 'push' , 'pushTerms' , 'status' ))
214
+ parser .add_argument ('languages' , default = None , nargs = "*" )
139
215
140
216
args = parser .parse_args ()
141
217
config = _load_config (args .config_file )
218
+ languages = args .languages [1 :] if args .languages else None
142
219
143
220
if "init" == args .command :
144
221
print ("Initialize project" )
145
222
init (config )
146
223
147
224
if 'generate' == args .command :
148
- print ("Generate configuration file" )
225
+ print ("Generate example configuration file" )
149
226
generate ()
150
227
151
228
elif "pull" == args .command :
152
229
print ("Download translations" )
153
- pull (config )
230
+ pull (config , languages = languages )
231
+
232
+ elif "push" == args .command :
233
+ print ("Push languages" )
234
+ push (config , languages = languages , overwrite = args .overwrite , sync_terms = args .sync_terms )
154
235
155
236
elif "pushTerms" == args .command :
156
237
print ("Push terms" )
157
238
pushTerms (config )
239
+
240
+ elif "status" == args .command :
241
+ status (config )
0 commit comments