Skip to content

Commit c2a2338

Browse files
committed
Merge pull request #4 from lukin0110/v0.0.4
V0.0.4
2 parents eb03978 + 0bfa2f4 commit c2a2338

File tree

8 files changed

+128
-8
lines changed

8 files changed

+128
-8
lines changed

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: python
2+
3+
python:
4+
- "2.7"
5+
6+
install:
7+
- "pip install . --use-mirrors"
8+
9+
script:
10+
- ./run_tests.py

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
poeditor-client
22
===============
3+
[![Build Status](https://travis-ci.org/lukin0110/poeditor-client.svg)](https://travis-ci.org/lukin0110/poeditor-client)
4+
35
A command line tool which downloads translations from [POEditor](https://poeditor.com) based on a configuration file.
46

57
1. Install
@@ -33,7 +35,6 @@ terms = App/src/main/res/values/strings.xml
3335
trans.en = App/src/main/res/values/strings.xml
3436
trans.nl = App/src/main/res/values-nl/strings.xml
3537
trans.fr = App/src/main/res/values-fr/strings.xml
36-
trans.pl = App/src/poland/res/values-pl/strings.xml
3738
```
3839

3940
Parameter | Description
@@ -60,11 +61,21 @@ Download translations:
6061
poeditor pull
6162
```
6263

64+
Upload languages:
65+
```
66+
poeditor push
67+
```
68+
6369
Add terms:
6470
```
6571
poeditor pushTerms
6672
```
6773

74+
Check project status:
75+
```
76+
poeditor status
77+
```
78+
6879
License
6980
=======
7081

main.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Run cmd.main() locally. Once installed through pip you'll have to use the 'poeditor' cmd.
5+
if __name__ == "__main__":
6+
from poeditor_client import cmd
7+
cmd.main()

poeditor_client/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__author__ = 'maartenhuijsmans'
1+
__version__ = '0.0.4'

poeditor_client/cmd.py

+89-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def init(config):
8686
print(" - Language added: {}".format(lang))
8787

8888

89-
def pull(config):
89+
def pull(config, languages=None):
9090
"""
9191
Pulls translations from the POEditor API.
9292
"""
@@ -103,15 +103,57 @@ def pull(config):
103103

104104
for o in options:
105105
if o.startswith("trans."):
106+
language = o[6:]
107+
if languages and language not in languages:
108+
continue
109+
106110
export_path = config.get(s, o)
107111
parent_dir = os.path.dirname(export_path)
108112
if not os.path.exists(parent_dir):
109113
os.makedirs(parent_dir)
110-
language = o[6:]
114+
111115
print("Language: {}".format(language))
112116
client.export(project_id, language_code=language, file_type=file_type, local_file=export_path)
113117

114118

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+
115157
def pushTerms(config):
116158
"""
117159
Pushes new terms to POEditor
@@ -129,29 +171,71 @@ def pushTerms(config):
129171
client.update_terms(project_id, terms)
130172

131173

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("\nProject: {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+
132203
def main():
133204
"""
134205
./test.py init -f examples/.poeditor0
135206
"""
136207
parser = argparse.ArgumentParser()
137208
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="*")
139215

140216
args = parser.parse_args()
141217
config = _load_config(args.config_file)
218+
languages = args.languages[1:] if args.languages else None
142219

143220
if "init" == args.command:
144221
print("Initialize project")
145222
init(config)
146223

147224
if 'generate' == args.command:
148-
print("Generate configuration file")
225+
print("Generate example configuration file")
149226
generate()
150227

151228
elif "pull" == args.command:
152229
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)
154235

155236
elif "pushTerms" == args.command:
156237
print("Push terms")
157238
pushTerms(config)
239+
240+
elif "status" == args.command:
241+
status(config)

run_tests.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
import unittest
3+
4+
if __name__ == '__main__':
5+
unittest.main()

setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from setuptools import setup, find_packages
22

3+
34
long_description = """
45
The POEditor Client is a command line tool that enables you to easily manage your translation files within a project.
56
"""
@@ -10,9 +11,11 @@
1011
Fork: https://github.com/lukin0110/python-poeditor
1112
'''
1213

14+
import poeditor_client
15+
1316
setup(
1417
name="poeditor_client",
15-
version="0.0.3",
18+
version=poeditor_client.__version__,
1619
url='https://github.com/lukin0110/poeditor-client',
1720
license='LICENSE.txt',
1821
description='The POEditor Client',

test.py tests/__init__.py

File renamed without changes.

0 commit comments

Comments
 (0)