Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Initializes logger based on path
"""
logger.setLevel(logging.DEBUG)
if not path:
file_handler = logging.FileHandler(LOG_FN)
else:
try:
file_handler = logging.FileHandler(os.path.join(path, CONF_DIR, LOG_FN))
# if on Windows system, set directory properties to hidden
if os.name == 'nt':
try:
subprocess.call(["attrib", "+H", os.path.join(path, CONF_DIR)])
except Exception as e:
logger.error("Error on init: "+str(e))
# logger.info("On Windows, make .ltk folder hidden")
# # Python 2
ret = ctypes.windll.kernel32.SetFileAttributesW(unicode(os.path.join(path, CONF_DIR)), HIDDEN_ATTRIBUTE)
# # End Python 2
# # Python 3
# # ret = ctypes.windll.kernel32.SetFileAttributesW(os.path.join(path, CONF_DIR), HIDDEN_ATTRIBUTE)
# # End Python 3
# if(ret != 1): # return value of 1 signifies success
# pass
except IOError as e:
#logger.info(e)
# todo error check when running init without existing conf dir
try:
os.mkdir(os.path.join(path, CONF_DIR))
# if on Windows system, make directory hidden
if os.name == 'nt':
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 get_watch_locales(self, document_id):
""" determine the locales that should be added for a watched doc """
locales = []
if self.detected_locales:
try:
locales = [self.detected_locales[document_id]]
except KeyError:
logger.error("Something went wrong. Could not detect a locale")
return locales
entry = self.doc_manager.get_doc_by_prop("id", document_id)
try:
locales = [locale for locale in self.watch_locales if locale not in entry['locales']]
except KeyError:
locales = self.watch_locales
return locales
def list(**kwargs):
""" Shows docs, workflows, locales, formats, or filters. By default lists added folders and docs. """
try:
action = list_action.ListAction(os.getcwd())
init_logger(action.path)
action.list_action(**kwargs)
except (UninitializedError, RequestFailedError) as e:
print_log(e)
logger.error(e)
return
def get_watch_locales(self, document_id):
""" determine the locales that should be added for a watched doc """
locales = []
if self.detected_locales:
try:
locales = [self.detected_locales[document_id]]
except KeyError:
logger.error("Something went wrong. Could not detect a locale")
return locales
entry = self.doc_manager.get_doc_by_prop("id", document_id)
try:
locales = [locale for locale in self.watch_locales if locale not in entry['locales']]
except KeyError:
locales = self.watch_locales
return locales
def watch(ignore, delimiter, timeout, no_folders, force_poll): # path, ignore, delimiter, timeout, no_folders):
"""
Watches local files added by ltk, and sends a PATCH when a document is changed.
Also watches remote files, and automatically downloads finished translations.
Automatically adds documents that are added to the watchfolder. Note: The add is performed without extra options (no srx id, no download folder, etc.)
"""
try:
action = WatchAction(os.getcwd(), timeout)
init_logger(action.path)
action.watch_action(ignore, delimiter, no_folders, force_poll) #path, ignore, delimiter, no_folders)
except (UninitializedError, RequestFailedError) as e:
print_log(e)
logger.error(e)
return
def download(auto_format, locales, locale_ext, no_ext, xliff, file_names):
""" Downloads translated content specified by filename for specified locales, or all locales if none are specified. Change download options and folders using ltk config."""
try:
action = download_action.DownloadAction(os.getcwd())
init_logger(action.path)
for name in file_names:
action.download_by_path(name, locales, locale_ext, no_ext, auto_format, xliff)
print("\n")
except (UninitializedError, ResourceNotFound, RequestFailedError) as e:
print_log(e)
logger.error(e)
return
def raise_error(json, error_message, is_warning=False, doc_id=None, file_name=None):
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)
logger.error("Document name specified for update doesn't exist: {0}".format(title))
return
if title:
response = self.api.document_update(document_id, file_name, title=title, **kwargs)
else:
response = self.api.document_update(document_id, file_name)
if response.status_code != 202:
raise_error(response.json(), "Failed to update document {0}".format(file_name), True)
self._update_document(relative_path)
return True
except Exception as e:
log_error(self.error_file_name, e)
if 'string indices must be integers' in str(e) or 'Expecting value: line 1 column 1' in str(e):
logger.error("Error connecting to Lingotek's TMS")
else:
logger.error("Error on updating document"+str(file_name)+": "+str(e))
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