Merge "Do not call sys.exit inside ZanataUtils"

This commit is contained in:
Jenkins 2015-07-06 20:38:37 +00:00 committed by Gerrit Code Review
commit e93630ac4b
2 changed files with 19 additions and 15 deletions

View File

@ -17,7 +17,6 @@ from lxml import etree
import os
import re
import requests
import sys
try:
import configparser
except ImportError:
@ -48,12 +47,13 @@ class IniConfig:
"""
if not os.path.isfile(self.inifile):
sys.exit('zanata.ini file not found.')
raise ValueError('zanata.ini file not found.')
config = configparser.ConfigParser()
try:
config.read(self.inifile)
except configparser.Error:
sys.exit('zanata.ini could not be parsed, please check format.')
raise ValueError('zanata.ini could not be parsed, please check '
'format.')
for item in config.items('servers'):
item_type = item[0].split('.')[1]
if item_type in ('username', 'key', 'url'):
@ -112,9 +112,9 @@ class ProjectConfig:
with open(self.xmlfile, 'r') as f:
xml = etree.parse(f)
except IOError:
sys.exit('Cannot load zanata.xml for this project')
raise ValueError('Cannot load zanata.xml for this project')
except etree.ParseError:
sys.exit('Cannot parse zanata.xml for this project')
raise ValueError('Cannot parse zanata.xml for this project')
root = xml.getroot()
tag_prefix = self._get_tag_prefix(root)
self.project = root.find('%sproject' % tag_prefix).text
@ -150,12 +150,12 @@ class ProjectConfig:
'X-Auth-Token': self.key}
r = requests.get(request_url, verify=verify, headers=headers)
except requests.exceptions.ConnectionError:
sys.exit("Connection error")
raise ValueError('Connection error')
if r.status_code != 200:
sys.exit('Got status code %s for %s' %
(r.status_code, request_url))
raise ValueError('Got status code %s for %s' %
(r.status_code, request_url))
if not r.content:
sys.exit('Did not recieve any data from %s' % request_url)
raise ValueError('Did not recieve any data from %s' % request_url)
return r.content
def _fetch_zanata_xml(self, verify=False):
@ -175,7 +175,7 @@ class ProjectConfig:
try:
xml = etree.parse(BytesIO(project_config), p)
except etree.ParseError:
sys.exit('Error parsing xml output')
raise ValueError('Error parsing xml output')
return xml
def _add_configuration(self, xml):
@ -229,4 +229,4 @@ class ProjectConfig:
try:
xml.write(self.xmlfile, pretty_print=True)
except IOError:
sys.exit('Error writing zanata.xml.')
raise ValueError('Error writing zanata.xml.')

View File

@ -14,6 +14,7 @@
import argparse
import os
import sys
from ZanataUtils import IniConfig, ProjectConfig
@ -37,11 +38,14 @@ def get_args():
def main():
args = get_args()
zc = IniConfig(os.path.expanduser('~/.config/zanata.ini'))
rules = args.rule or [('*.pot', '{locale}/LC_MESSAGES/{filename}.po')]
ProjectConfig(zc, args.file, rules, project=args.project,
version=args.version,
srcdir=args.srcdir, txdir=args.txdir)
try:
zc = IniConfig(os.path.expanduser('~/.config/zanata.ini'))
ProjectConfig(zc, args.file, rules, project=args.project,
version=args.version,
srcdir=args.srcdir, txdir=args.txdir)
except ValueError as e:
sys.exit(e)
if __name__ == '__main__':