Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
if json:
error = json['messages'][0]
file_name = file_name.replace("Status of ", "")
if file_name is not None and doc_id is not None:
error = error.replace(doc_id, file_name+" ("+doc_id+")")
# Sometimes api returns vague errors like 'Unknown error'
if error == 'Unknown error':
error = error_message
if not is_warning:
raise exceptions.RequestFailedError(error)
# warnings.warn(error)
logger.error(error)
except (AttributeError, IndexError):
if not is_warning:
raise exceptions.RequestFailedError(error_message)
# warnings.warn(error_message)
logger.error(error_message)
def raise_error(json, error_message, is_warning=False, doc_id=None, file_name=None):
try:
error = ""
if json:
error = json['messages'][0]
if file_name:
file_name = file_name.replace("Status of ", "")
if file_name is not None and doc_id is not None:
error = error.replace(doc_id, file_name+" ("+doc_id+")")
# Sometimes api returns vague errors like 'Unknown error'
if error == 'Unknown error':
error = error_message
if not is_warning:
raise exceptions.RequestFailedError(error)
# warnings.warn(error)
if error:
error = error+"\n"
logger.error(error+error_message)
except (AttributeError, IndexError):
if not is_warning:
raise exceptions.RequestFailedError(error_message)
# warnings.warn(error_message)
logger.error(error_message)
def get_project_info(self, community_id):
response = self.list_projects(community_id)
info = {}
if response.status_code == 200:
if int(response.json()['properties']['total']) == 0:
return info
entities = response.json()['entities']
for entity in entities:
info[entity['properties']['id']] = entity['properties']['title']
return info
elif response.status_code == 204:
return info
else:
raise RequestFailedError("Unable to get existing projects")
def import_locale_info(self, document_id, poll=False):
locale_progress = {}
response = self.api.document_translation_status(document_id)
if response.status_code != 200:
if poll:
return {}
else:
# raise_error(response.json(), 'Failed to get locale details of document', True)
raise exceptions.RequestFailedError('Failed to get locale details of document')
try:
for entry in response.json()['entities']:
curr_locale = entry['properties']['locale_code']
curr_progress = int(entry['properties']['percent_complete'])
curr_locale = curr_locale.replace('-', '_')
locale_progress[curr_locale] = curr_progress
except KeyError:
pass
return locale_progress
def import_locale_info(self, document_id, poll=False):
locale_progress = {}
response = self.api.document_translation_status(document_id)
if response.status_code != 200:
if poll:
return {}
else:
# raise_error(response.json(), 'Failed to get locale details of document', True)
raise exceptions.RequestFailedError('Failed to get locale details of document')
try:
for entry in response.json()['entities']:
curr_locale = entry['properties']['locale_code']
curr_progress = int(entry['properties']['percent_complete'])
curr_locale = curr_locale.replace('-', '_')
locale_progress[curr_locale] = curr_progress
except KeyError:
pass
return locale_progress
def import_locale_info(self, document_id, poll=False, include_cancelled=False):
locale_progress = {}
response = self.api.document_translation_status(document_id)
if response.status_code != 200:
if poll or response.status_code == 404:
return {}
else:
# raise_error(response.json(), 'Failed to get locale details of document', True)
raise exceptions.RequestFailedError('Failed to get locale details of document')
try:
for entry in response.json()['entities']:
curr_locale = entry['properties']['locale_code']
curr_progress = int(entry['properties']['percent_complete'])
curr_status = entry['properties']['status']
curr_locale = curr_locale.replace('-', '_')
if include_cancelled or (curr_status.upper() != 'CANCELLED'):
locale_progress[curr_locale] = curr_progress
except KeyError:
pass
return locale_progress
def list_locale_action(self):
locale_info = []
response = self.api.list_locales()
if response.status_code != 200:
raise exceptions.RequestFailedError("Failed to get locale codes")
locale_json = response.json()
for entry in locale_json:
locale_code = locale_json[entry]['locale']
language = locale_json[entry]['language_name']
country = locale_json[entry]['country_name']
locale_info.append((locale_code, language, country))
for locale in sorted(locale_info):
if not len(locale[2]): # Arabic
print ("{0} ({1})".format(locale[0], locale[1]))
else:
print ("{0} ({1}, {2})".format(locale[0], locale[1], locale[2]))
def reference_remove(filename, doc_id, remove_all):
"""Deletes reference material attached to a document on Lingotek."""
try:
action = reference_action.ReferenceAction(os.getcwd())
init_logger(action.path)
action.reference_remove_action(filename, doc_id, remove_all)
except (UninitializedError, RequestFailedError) as e:
print_log(e)
logger.error(e)
return
def import_locale_info(self, document_id, poll=False):
locale_progress = {}
response = self.api.document_translation_status(document_id)
if response.status_code != 200:
if poll or response.status_code == 404:
return {}
else:
# raise_error(response.json(), 'Failed to get locale details of document', True)
raise exceptions.RequestFailedError('Failed to get locale details of document')
try:
for entry in response.json()['entities']:
curr_locale = entry['properties']['locale_code']
curr_progress = int(entry['properties']['percent_complete'])
curr_locale = curr_locale.replace('-', '_')
locale_progress[curr_locale] = curr_progress
except KeyError:
pass
return locale_progress