Skip to content

Commit

Permalink
Merge pull request #52 from sundeep-co-in/master
Browse files Browse the repository at this point in the history
ZNTA-2791: Port Zanata Python client to Python 3
  • Loading branch information
sdp5 authored Mar 7, 2019
2 parents 1b23b9d + 14d64e9 commit ad3bf0c
Show file tree
Hide file tree
Showing 27 changed files with 113 additions and 115 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
language: python
dist: xenial
python:
- "2.6"
- "2.7"
- "3.5"
- "3.6"
- "3.7"
# command to install dependencies
install: "pip install -r requirements-dev.txt"
# command to run tests
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Fri Mar 08 2019 Sundeep Anand <suanand@redhat.com> - 1.5.3
- ZNTA-2791 - Port Zanata Python client to Python 3

* Mon Apr 16 2018 Sundeep Anand <suanand@redhat.com> - 1.5.2
- ZNTA-1390 - Python client fuzzies my strings
- ZNTA-1382 - Should "--push-trans-only" be deprecated in the python client?
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ clean:
python setup.py clean
rm -f zanataclient/VERSION-FILE

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +

run:
python zanata help

Expand All @@ -28,7 +33,7 @@ lint-report:
pylint --reports=n zanata zanataclient

flake8:
flake8 --ignore=E501,F403,F841,F401 zanataclient
flake8 --ignore=E402,E501,F403,F841,F401 zanataclient

test:
(cd zanataclient/test; nosetests ${NOSE_FLAGS} test_all.py)
Expand Down
21 changes: 6 additions & 15 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
-r requirements.txt
coverage==4.0.3
flake8==2.4.1
flake8-import-order==0.6.1
funcsigs==0.4
linecache2==1.0.0
mccabe==0.3.1
MiniMock==1.2.8
mock==1.3.0
nose==1.3.7
pbr==1.8.1
pep8==1.5.7
pyflakes==0.8.1
six==1.10.0
traceback2==1.4.0
unittest2==1.1.0
coverage
flake8<3.8
flake8-import-order
mock
nose
unittest2
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
future
httplib2
lxml
ordereddict
polib
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_client_version():
path = os.path.dirname(os.path.realpath(__file__))
version_file = os.path.join(path, 'zanataclient', 'VERSION-FILE')
try:
version = open(version_file, 'rb')
version = open(version_file, 'r')
client_version = version.read()
version.close()
version_number = client_version.rstrip().strip('version: ')
Expand Down Expand Up @@ -65,7 +65,9 @@ def get_client_version():
'License :: OSI Approved :: GNU Lesser General Public License (LGPL)',
'Operating System :: Unix',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
)
2 changes: 1 addition & 1 deletion zanataclient/VERSION-FILE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: 1.5.2
version: 1.5.3
2 changes: 1 addition & 1 deletion zanataclient/cmdbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def check_plural_support(self, server_version):

version = str(server_version.split('-')[0])
main_ver = version[:3]
version_number = string.atof(main_ver)
version_number = float(main_ver)

if version_number >= 1.6:
return True
Expand Down
2 changes: 1 addition & 1 deletion zanataclient/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def handle_program(
path = os.path.dirname(os.path.realpath(__file__))
version_file = os.path.join(path, 'VERSION-FILE')
try:
version = open(version_file, 'rb')
version = open(version_file, 'r')
client_version = version.read()
version.close()
version_number = client_version.rstrip().strip('version: ')
Expand Down
18 changes: 11 additions & 7 deletions zanataclient/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _update_client_version(self):
version_file = os.path.join(path, client_version_file)

try:
version = open(version_file, 'rb')
version = open(version_file, 'r')
client_version = version.read()
version.close()
version_number = client_version.rstrip()[len('version: '):]
Expand Down Expand Up @@ -224,7 +224,7 @@ def _update_locale_mapping(self):
locale_map = self.process_locales(locales)
self.remote_config.update({'locale_map': locale_map})

def _update_project_type(self):
def update_project_type(self):
"""
This fetches project_type from server
"""
Expand Down Expand Up @@ -271,7 +271,7 @@ def get_remote_configs(self):
"""
context_remote_configs = {
'default': [self._update_server_version, self._update_locale_mapping,
self._update_project_type],
self.update_project_type],
'init': [],
}
return context_remote_configs[self.mode]
Expand All @@ -287,14 +287,18 @@ def get_context_data(self):
"""
updates context_data with remote_config, local_config and command_dict
"""

def merge_dicts(i, j):
z = i.copy()
z.update(j)
return z

build_configs = [self.build_local_config,
self.build_remote_config]
[method() for method in build_configs]
# lowest to higest
# lowest to highest
precedence = [self.remote_config, self.local_config, self.command_dict]
context_data = functools.reduce(
lambda option, value: dict(option.items() + value.items()), precedence
)
context_data = functools.reduce(lambda option, value: merge_dicts(option, value), precedence)
return self.filter_context_data(context_data)

def filter_context_data(self, data):
Expand Down
1 change: 1 addition & 0 deletions zanataclient/csvconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def convert_to_json(self, filepath, locale_map, comments_header):
# glossary = {'source-locales':srclocales, 'glossary-entries':entries, 'target-locales':targetlocales}
return json.dumps(glossary)


if __name__ == "__main__":
converter = CSVConverter()
converter.convert_to_json("~/Downloads/test_data.csv", {'es': 'es-ES'}, ["description", "pos"])
38 changes: 16 additions & 22 deletions zanataclient/initcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@
import fnmatch
import os
import sys
from datetime import datetime

from .cmdbase import CommandsBase, CommandsInit
from .context import ContextBase
from .parseconfig import ZanataConfig
from .zanatalib.logger import Logger, TextColour
from .zanatalib.projectutils import ToolBox


try:
from collections import OrderedDict
from builtins import input
except ImportError:
from ordereddict import OrderedDict
from __builtin__ import input
from collections import OrderedDict
from datetime import datetime

from zanataclient.cmdbase import CommandsBase, CommandsInit
from zanataclient.context import ContextBase
from zanataclient.parseconfig import ZanataConfig
from zanataclient.zanatalib.logger import Logger, TextColour
from zanataclient.zanatalib.projectutils import ToolBox

log = Logger()

try:
input = raw_input
except NameError:
pass
log = Logger()


class ZanataInit(CommandsInit, ContextBase):
Expand All @@ -69,7 +63,7 @@ def get_init_stages(self):
self._update_user_config, self._update_http_headers,
self._update_client_version, self.check_config_exists,
self.set_zanata_command, self.project_options,
self.version_options, self._update_project_type,
self.version_options, self.update_project_type,
self.post_project_version, self.source_target_dirs,
self.dump_project_config, self.whats_next]
}
Expand Down Expand Up @@ -170,8 +164,8 @@ def _create_new_project(self):
log.info("Creating project on the server...")
if not self.zanata_cmd.create_project(input_project_id, input_project_name,
input_project_desc, project_type):
raise
except:
raise Exception
except Exception as e:
if self.print_yes_no("[?] Do you want to try again (y/n)? "):
return self._create_new_project()
else:
Expand Down Expand Up @@ -214,8 +208,8 @@ def _create_new_version(self):
log.info("Creating version on the server...")
if not self.zanata_cmd.create_version(self.local_config.get('project_id'),
input_version_id):
raise
except:
raise Exception
except Exception as e:
if self.print_yes_no("[?] Do you want to try again (y/n)? "):
return self._create_new_version()
else:
Expand Down Expand Up @@ -335,7 +329,7 @@ def dump_project_config(self):
xmldoc = ToolBox.dict2xml('config', project_config_dict)
try:
with open('zanata.xml', 'w') as project_config_file:
project_config_file.write(xmldoc)
project_config_file.write(xmldoc.decode("utf-8"))
except IOError:
log.error("Something went wrong. Try Again.")
else:
Expand Down
7 changes: 2 additions & 5 deletions zanataclient/parseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)

import os.path
from collections import OrderedDict
from xml.dom import minidom

from .zanatalib.logger import Logger
Expand All @@ -34,10 +35,6 @@
except ImportError:
from configparser import ConfigParser

try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict

project_config = {}

Expand All @@ -52,7 +49,7 @@ def set_userconfig(self, path):
try:
self.configparser = ConfigParser()
self._config = self.configparser.read(['zanata.ini', path])
except:
except Exception:
self.log.error("Invalid User Config")

def get_server(self, url):
Expand Down
2 changes: 1 addition & 1 deletion zanataclient/publicanutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def create_txtflowtarget(self, pofile):

def validate_content_type(self, content_type, object_type):
PATTERN = r'.+? charset=([\w_\-:\.]+)'
rxt = re.compile(unicode(PATTERN))
rxt = re.compile(PATTERN)

match = rxt.search(content_type)
if match:
Expand Down
15 changes: 5 additions & 10 deletions zanataclient/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@

import unittest

from test_client import RestHandleTest

from test_context import ProjectContextTest

from test_parseconfig import ConfigTest

from test_publicanutil import PublicanUtilityTest

from test_service import ServiceTest
from zanataclient.test.test_client import RestHandleTest
from zanataclient.test.test_context import ProjectContextTest
from zanataclient.test.test_parseconfig import ConfigTest
from zanataclient.test.test_publicanutil import PublicanUtilityTest
from zanataclient.test.test_service import ServiceTest

# from test_zanata import ZanataTest

# from test_zanatacmd import ZanataCmdTest

suite = unittest.TestSuite()
Expand Down
8 changes: 5 additions & 3 deletions zanataclient/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def previous(self):
def status(self):
return 200


response = FalseDict()
response.update({'status': '200', 'access-control-allow-headers': 'X-Requested-With, Content-Type, Accept,',
'content-location': 'http://localhost:8080/zanata/seam/resource/restv1/projects',
Expand All @@ -67,9 +68,9 @@ def status(self):
'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET',
'content-type': 'application/json'})

content = u'[{"id":"black-silver","defaultType":"","name":"Black Silver",' \
u'"links":[{"href":"p/black-silver","rel":"self","type":"application/vnd.zanata.project+json"}],' \
u'"status":"ACTIVE"}]'
content = b'[{"id":"black-silver","defaultType":"","name":"Black Silver",' \
b'"links":[{"href":"p/black-silver","rel":"self","type":"application/vnd.zanata.project+json"}],' \
b'"status":"ACTIVE"}]'


class RestHandleTest(unittest.TestCase):
Expand Down Expand Up @@ -98,5 +99,6 @@ def test_get_response_content(self, mock_call_request):
self.assertTrue('links' in response_content[1], 'links should be in content')
self.assertTrue('status' in response_content[1], 'project status should be in content')


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions zanataclient/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,6 @@ def test_init_context(self):
self.assertTrue('servers' in context_data)
self.assertTrue('http://localhost:8080/zanata' in context_data['servers'])


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions zanataclient/test/test_parseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ def test_project_config(self):
self.assertEqual(project_config['file_mapping_rules']['**/po/*.pot'],
'{path}/{locale_with_underscore}.po')


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions zanataclient/test/test_publicanutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ def test_msgstrplural(self):
self.assertEqual(result['extensions'], expect_json['extensions'])
json_data.close()
"""


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions zanataclient/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ def test_messages_status_401_503(self):
self.service.messages(SERVICE_RESPONSE_503, RESPONSE_CONTENT)
self.assertEqual(ex.exception.code, 1)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions zanataclient/test/test_zanatacmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ def test_pull_command(self):
def test_push_command(self):
pass


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit ad3bf0c

Please sign in to comment.