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 rm_document(self, file_name, useID, force, doc_name=None): doc = None if not useID: relative_path = file_name.replace(self.path, '') doc = self.doc_manager.get_doc_by_prop('file_name', relative_path) title = os.path.basename(os.path.normpath(file_name)) # print("relative_path: "+relative_path) try: doc = self.doc_manager.get_doc_by_prop('file_name', relative_path) document_id = doc['id'] except TypeError: # Documents specified by name must be found in the local database to be removed. logger.warning("Document name specified for remove doesn't exist locally: {0}".format(relative_path)) return # raise exceptions.ResourceNotFound("Document name specified doesn't exist: {0}".format(document_name)) else: document_id = file_name doc = self.doc_manager.get_doc_by_prop('id', document_id) # raise exceptions.ResourceNotFound("Document name specified doesn't exist: {0}".format(document_name)) if doc: file_name = doc['file_name'] response = self.api.document_delete(document_id) #print (response) if response.status_code != 204: # raise_error(response.json(), "Failed to delete document {0}".format(document_name), True) logger.error("Failed to delete document {0} remotely".format(file_name)) else: if doc_name: logger.info("{0} ({1}) has been deleted remotely.".format(doc_name, file_name)) else: logger.info("{0} has been deleted remotely.".format(file_name)) if doc: if force: self.delete_local(file_name, document_id) self.doc_manager.remove_element(document_id)
def download_by_name(self, document_name, locale_code, auto_format): try: document_id = self.doc_manager.get_doc_by_prop('name', document_name)['id'] except TypeError: logger.error("Document name specified for download doesn't exist: {0}".format(document_name)) return self.download_action(document_id, locale_code, auto_format)
def add_action(self, file_patterns, **kwargs): # format will be automatically detected by extension but may not be what user expects # use current working directory as root for files instead of project root matched_files = get_files(file_patterns) if not matched_files: raise exceptions.ResourceNotFound("Could not find the specified file/pattern") for file_name in matched_files: # title = os.path.basename(os.path.normpath(file_name)).split('.')[0] title = os.path.basename(os.path.normpath(file_name)) relative_path = file_name.replace(self.path, '') if not self.doc_manager.is_doc_new(relative_path): if self.doc_manager.is_doc_modified(relative_path, self.path): if 'force' in kwargs and kwargs['force']: confirm = 'Y' else: confirm = 'not confirmed' while confirm != 'y' and confirm != 'Y' and confirm != 'N' and confirm != 'n' and confirm != '': confirm = input("This document already exists. Would you like to overwrite it? [y/N]: ") # confirm if would like to overwrite existing document in Lingotek Cloud if not confirm or confirm in ['n', 'N']: continue else: logger.info('Overwriting document: {0} in Lingotek Cloud...'.format(title)) self.update_document_action(file_name, title, **kwargs) continue else: logger.error("This document has already been added: {0}".format(title)) continue # todo separate function somewhere around here maybe.. self.add_document(file_name, title, **kwargs)
def add_document(self, file_name, title, doc_metadata={}, **kwargs): ''' adds the document to Lingotek cloud and the db ''' if ltk.check_connection.check_for_connection() == False: logger.warning( "Cannot connect to network. Documents added to the watch folder will be translated after you reconnect to the network." ) while ltk.check_connection.check_for_connection() == False: time.sleep(15) if self.is_hidden_file(file_name): return try: if not 'locale' in kwargs or not kwargs['locale']: locale = self.locale else: locale = kwargs['locale'] # add document to Lingotek cloud response = self.api.add_document( locale, file_name, self.project_id, self.append_location(title, file_name), doc_metadata, **kwargs) if response.status_code == 402: raise_error(response.json(), "", True) elif response.status_code != 202: raise_error(response.json(), "Failed to add document {0}\n".format(title), True) else: title = self.append_location(title, file_name) logger.info('Added document {0} with ID {1}\n'.format( title, response.json()['properties']['id'])) relative_path = self.norm_path(file_name) # add document to the db if 'download_folder' in kwargs and kwargs['download_folder']: self._add_document( relative_path, title, response.json()['properties']['id'], response.json()['properties']['process_id'], kwargs['download_folder']) else: self._add_document( relative_path, title, response.json()['properties']['id'], response.json()['properties']['process_id']) if 'translation_locale_code' in kwargs and kwargs[ 'translation_locale_code']: self._update_document(relative_path, None, kwargs['translation_locale_code']) except KeyboardInterrupt: raise_error("", "Canceled adding document\n") 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\n") else: logger.error("Error on adding document \n" + str(file_name) + ": " + str(e))
def request(doc_name, locales, to_delete, due_date, workflow): """ Add targets to document(s) to start translation; defaults to the entire project. Use ltk list -l to see possible locales """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.target_action(doc_name, locales, to_delete, due_date, workflow) except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def add(file_names, **kwargs): """ Adds content. Could be one or more files specified by a Unix shell pattern """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.add_action(file_names, **kwargs) except (UninitializedError, RequestFailedError, ResourceNotFound, AlreadyExistsError) as e: print_log(e) logger.error(e) return
def filter_add(filter_id, filename): """Update filter on Lingotek.""" try: action = filters_action.FiltersAction(os.getcwd()) init_logger(action.path) action.filter_save_action(filter_id, filename) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
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 push(): """ Sends updated content to Lingotek for documents that have been added """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.push_action() except UninitializedError as e: print_log(e) logger.error(e) return
def wrapper(*args, **kwargs): while True: try: return function(*args, **kwargs) except Exception as e: if e.__class__ in exec_type: logger.error("Connection has timed out. Retrying..") time.sleep(timeout) # sleep for some time then retry else: raise e
def status(**kwargs): """ Gets the status of a specific document or all documents """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.status_action(**kwargs) except (UninitializedError, ResourceNotFound) as e: print_log(e) logger.error(e) return
def filter_list(): """List default and custom filters.""" try: action = filters_action.FiltersAction(os.getcwd()) init_logger(action.path) action.filter_list_action() except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def config(locale, workflow_id, download_folder, watch_folder, target_locales): """ View or change local configuration """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.config_action(locale, workflow_id, download_folder, watch_folder, target_locales) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def filter_rm(filter_id): """Remove the filter specified by FILTER_ID.""" try: action = filters_action.FiltersAction(os.getcwd()) init_logger(action.path) action.filter_rm_action(filter_id) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def reference_add(filename, doc_id): """Adds reference material to a document on Lingotek.""" try: action = reference_action.ReferenceAction(os.getcwd()) init_logger(action.path) action.reference_add_action(filename, doc_id) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def reference_get(filename, doc_id, get_all, path): """Downloads reference material attached to a document on Lingotek. Defaults to downloading them to the root of the project""" try: action = reference_action.ReferenceAction(os.getcwd()) init_logger(action.path) action.reference_download_action(filename, doc_id, get_all, path) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def target_action(self, document_name, locales, to_delete, due_date, workflow, document_id=None): change_db_entry = True if to_delete: expected_code = 204 failure_message = 'Failed to delete target' info_message = 'Deleted locale' else: expected_code = 201 failure_message = 'Failed to add target' info_message = 'Added target' if not document_name and not document_id: for locale in locales: response = self.api.project_add_target(self.project_id, locale, due_date) if not to_delete \ else self.api.project_delete_target(self.project_id, locale) if response.status_code != expected_code: raise_error(response.json(), '{message} {locale} for project'.format(message=failure_message, locale=locale), True) change_db_entry = False continue logger.info('{message} {locale} for project {project_name}'.format(message=info_message, locale=locale, project_name=self.project_name)) document_ids = self.doc_manager.get_doc_ids() if change_db_entry: for document_id in document_ids: self._target_action_db(to_delete, locales, document_id) else: # todo: document name or file name? since file name will be relative to root # todo: clean this code up some if not document_id: entry = self.doc_manager.get_doc_by_prop('name', document_name) try: document_id = entry['id'] except TypeError: logger.error('Document name specified for target doesn\'t exist: {0}'.format(document_name)) return # raise exceptions.ResourceNotFound("Document name specified doesn't exist: {0}".format(document_name)) if not document_name: entry = self.doc_manager.get_doc_by_prop('id', document_id) try: document_name = entry['name'] except TypeError: logger.error('Document specified for target doesn\'t exist: {0}'.format(document_id)) return for locale in locales: response = self.api.document_add_target(document_id, locale, workflow, due_date) if not to_delete \ else self.api.document_delete_target(document_id, locale) if response.status_code != expected_code: raise_error(response.json(), '{message} {locale} for document'.format(message=failure_message, locale=locale), True) change_db_entry = False continue logger.info('{message} {locale} for document {name}'.format(message=info_message, locale=locale, name=document_name)) if change_db_entry: self._target_action_db(to_delete, locales, document_id)
def download(auto_format, locale, document_names): """ Downloads the translated content of document(s) """ try: action = actions.Action(os.getcwd()) init_logger(action.path) for name in document_names: action.download_by_name(name, locale, auto_format) except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def wrapper(*args, **kwargs): while True: try: return function(*args, **kwargs) except Exception as e: log_error(self.error_file_name, e) if e.__class__ in exec_type: logger.error("Connection has timed out. Retrying..") time.sleep(timeout) # sleep for some time then retry else: raise e
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 download(auto_format, locales, locale_ext, no_ext, 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 = actions.Action(os.getcwd()) init_logger(action.path) for name in file_names: action.download_by_path(name, locales, locale_ext, no_ext, auto_format) except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def watch(ignore, delimiter, timeout, no_folders, force_poll): # path, ignore, delimiter, timeout, no_folders): """ Watches local files added or imported by ltk, and sends a PATCH when a document is changed. Also watches remote files, and automatically downloads finished translations. """ 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)
def filter_get(filter_id, filename, info, overwrite): """Retrieve the filter specified by FILTER_ID from Lingotek and store it in the current working directly as the title (or as as the optional_filename when specified) of the filter""" try: action = filters_action.FiltersAction(os.getcwd()) init_logger(action.path) if info == True: action.filter_info_action(filter_id) else: action.filter_get_action(filter_id, filename, overwrite) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def push(test, title, files, metadata, metadata_only, **kwargs): #""" Sends updated content to Lingotek for documents that have been added. Fileglobs (e.g. *.txt) can be used to push all matching files """ """ Sends updated content to Lingotek for documents that have been added. Fileglobs (e.g. *.txt) can be used to push all matching files Metadata can be updated by launching the metadata wizard with the -m flag or by using flags for specific metadata. The metadata flags are --author_email, --author_name, --business_division, --business_unit, --campaign_id, --campaign_rating, --channel, --contact_email, --contact_name, --content_description, --content_type, --domain, --external_application_id, --external_document_id, --external_style_id, --job_id, --purchase_order, --reference_url, --region, --require_review, --category_id, and --note """ try: action = push_action.PushAction(os.getcwd(), test, title) init_logger(action.path) action.push_action(files=files, set_metadata=metadata, metadata_only=metadata_only, **kwargs) except UninitializedError as e: print_log(e) logger.error(e) return
def pull(auto_format, locale_ext, no_ext, locales): """ Pulls translations for all added documents for all locales or by specified locales """ try: action = actions.Action(os.getcwd()) init_logger(action.path) if locales: for locale in locales: action.pull_action(locale, locale_ext, no_ext, auto_format) else: action.pull_action(None, locale_ext, no_ext, auto_format) except UninitializedError as e: print_log(e) logger.error(e) return
def config(**kwargs): """ View or change local configuration """ try: action = actions.Action(os.getcwd()) init_logger(action.path) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.config_action(**kwargs) 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 init(host, access_token, path, project_name, workflow_id, locale, browserless, delete, reset): """ Connects a local project to Lingotek """ try: host = 'https://' + host if not path: path = os.getcwd() if not project_name: project_name = os.path.basename(os.path.normpath(path)) init_logger(path) actions.init_action(host, access_token, path, project_name, workflow_id, locale, browserless, delete, reset) except (ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def clean(force, dis_all, doc_name): """ Cleans up the associations between local documents and documents in Lingotek. By default, checks that local documents and remote documents line up. Use different options for different use cases """ try: action = actions.Action(os.getcwd()) init_logger(action.path) action.clean_action(force, dis_all, doc_name) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def config(**kwargs): """ View or change local configuration """ try: action = config_action.ConfigAction(os.getcwd()) init_logger(action.path) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.config_action(**kwargs) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
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 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 pull(auto_format, locale_ext, no_ext, locales): """ Pulls translations for all added documents for all locales or by specified locales """ try: download = download_action.DownloadAction(os.getcwd()) action = pull_action.PullAction(os.getcwd(), download) init_logger(action.path) if locales: for locale in locales: action.pull_translations(locale, locale_ext, no_ext, auto_format) else: action.pull_translations(None, locale_ext, no_ext, auto_format) except UninitializedError as e: print_log(e) logger.error(e) return
def update_document_action(self, file_name, title=None, **kwargs): relative_path = file_name.replace(self.path, '') entry = self.doc_manager.get_doc_by_prop('file_name', relative_path) try: document_id = entry['id'] except TypeError: logger.error("Document name specified for update doesn't exist: {0}".format(title)) print (file_name, entry['prolerty']) 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)
def status(**kwargs): """ Gets the status of a specific document or all documents """ try: action = status_action.StatusAction(os.getcwd()) init_logger(action.path) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.get_status(**kwargs) except (UninitializedError, ResourceNotFound) as e: print_log(e) logger.error(e) return
def request(doc_name, path, locales, to_cancel, to_delete, due_date, workflow): """ Add targets to document(s) to start translation; defaults to the entire project. If no locales are specified, Filesystem Connector will look for target watch locales set in ltk config. Use ltk list -l to see possible locales. """ try: action = request_action.RequestAction(os.getcwd(), doc_name, path, locales, to_cancel, to_delete, due_date, workflow) init_logger(action.path) if locales and isinstance(locales,str): locales = [locales] doc_name = remove_powershell_formatting(doc_name) path = remove_powershell_formatting(path) action.target_action() except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def status(**kwargs): """ Gets the status of a specific document or all documents """ try: action = actions.Action(os.getcwd()) init_logger(action.path) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.status_action(**kwargs) except (UninitializedError, ResourceNotFound) as e: print_log(e) logger.error(e) return
def request(doc_name, path, locales, to_delete, due_date, workflow): """ Add targets to document(s) to start translation; defaults to the entire project. If no locales are specified, Filesystem Connector will look for target watch locales set in ltk config. Use ltk list -l to see possible locales. """ try: action = actions.Action(os.getcwd()) init_logger(action.path) if locales and isinstance(locales,str): locales = [locales] doc_name = remove_powershell_formatting(doc_name) path = remove_powershell_formatting(path) action.target_action(doc_name, path, locales, to_delete, due_date, workflow) except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def rm(file_names, **kwargs): """ Disassociates local doc(s) from Lingotek Cloud and deletes the remote copy. If the remote copy should not be kept, please use ltk clean. """ try: action = actions.Action(os.getcwd()) init_logger(action.path) print(file_names) if not file_names and not ('all' in kwargs and kwargs['all']): logger.info("Usage: ltk rm [OPTIONS] FILE_NAMES...") return action.rm_action(file_names, **kwargs) 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: 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 add(file_names, **kwargs): """ Add files and folders for upload to Lingotek. Fileglobs (e.g. *.txt) can be used to add all matching files and/or folders. Added folders will automatically add the new files added or created inside of them. """ try: action = actions.Action(os.getcwd()) init_logger(action.path) file_names = remove_powershell_formatting(file_names) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.add_action(file_names, **kwargs) except (UninitializedError, RequestFailedError, ResourceNotFound, AlreadyExistsError) as e: print_log(e) logger.error(e) return
def clean(force, dis_all, file_paths): """ Cleans up the associations between local documents and documents in Lingotek. By default, removes documents from local tracking that have been cancelled or no longer exist locally or in the Lingotek Cloud. Enter file or directory names to cancel those documents and remove local associations of specific files or directories. """ try: action = clean_action.CleanAction(os.getcwd()) init_logger(action.path) if len(file_paths) > 0: file_paths = remove_powershell_formatting(file_paths) action.clean_action(force, dis_all, file_paths) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def clean(force, dis_all, file_paths): """ Cleans up the associations between local documents and documents in Lingotek. By default, checks that local documents and remote documents match up. Enter file or directory names to remove local associations of specific files or directories. """ try: action = actions.Action(os.getcwd()) init_logger(action.path) if len(file_paths) > 0: file_paths = remove_powershell_formatting(file_paths) action.clean_action(force, dis_all, file_paths) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def rm(file_names, **kwargs): """ Disassociates local doc(s) from Lingotek Cloud and removes them from the project by cancelling them. If the remote copy should be deleted, use the -r flag. """ try: action = rm_action.RmAction(os.getcwd()) init_logger(action.path) if not file_names and not (('all' in kwargs and kwargs['all']) or ('local' in kwargs and kwargs['local'])): logger.info("Usage: ltk rm [OPTIONS] FILE_NAMES...") return if len(file_names) > 0: file_names = remove_powershell_formatting(file_names) action.rm_action(file_names, **kwargs) except (UninitializedError, ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def import_command(import_all, force, path): """ Import documents from Lingotek Cloud, automatically downloading to the project root """ # todo import should show all documents # add a force option so can import all force -- overwrites all existing documents without prompting # check if doc id # if exist, prompt for overwrite # else automatically re-name # possibly have to patch title in Lingotek Cloud? try: # action = actions.Action(os.getcwd()) action = ImportAction(os.getcwd()) init_logger(action.path) action.import_action(import_all, force, path) except(UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def mv(source_path, destination_path): """ Moves specified local doc to a specified destination directory, moving both the file itself and file location stores in the local database. If SOURCE_PATH is a directory, all added files in the directory will be moved. """ try: # action = actions.Action(os.getcwd()) action = ImportAction(os.getcwd()) init_logger(action.path) source_path = remove_powershell_formatting(source_path) print("Source path " + str(source_path)) destination_path = remove_powershell_formatting(destination_path) print("Destination path "+str(destination_path)) action.mv_action(source_path, destination_path) except(UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def mv(source_path, destination_path): """ Moves specified local doc to a specified destination directory, moving both the file itself and file location stored in the local database. If SOURCE_PATH is a directory, all added files in the directory will be moved. """ try: # action = actions.Action(os.getcwd()) add = add_action.AddAction(os.getcwd()) action = move_action.MoveAction(add, os.getcwd()) init_logger(action.path) source_path = remove_powershell_formatting(source_path) #print("Source path " + str(source_path)) destination_path = remove_powershell_formatting(destination_path) #print("Destination path "+str(destination_path)) action.mv_action(source_path, destination_path) except(UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def add(file_names, **kwargs): #""" Add files and folders for upload to Lingotek. Fileglobs (e.g. *.txt) can be used to add all matching files and/or folders. Added folders will automatically add the new files added or created inside of them. """ """ Add files and folders for upload to Lingotek. Fileglobs (e.g. *.txt) can be used to add all matching files and/or folders. Added folders will automatically add the new files added or created inside of them. Metadata can be added by launching the metadata wizard with the -m flag or by using flags for specific metadata. The metadata flags are --author_email, --author_name, --business_division, --business_unit, --campaign_id, --campaign_rating, --channel, --contact_email, --contact_name, --content_description, --content_type, --domain, --external_application_id, --external_document_id, --external_style_id, --job_id, --purchase_order, --reference_url, --region, --require_review, --category_id, and --note """ try: action = add_action.AddAction(os.getcwd()) init_logger(action.path) file_names = remove_powershell_formatting(file_names) for f in kwargs: if kwargs[f]: temp = remove_powershell_formatting(kwargs[f]) kwargs[f] = temp action.add_action(file_names, **kwargs) except (UninitializedError, RequestFailedError, ResourceNotFound, AlreadyExistsError) as e: print_log(e) logger.error(e) return
def list_ids(id_type, hide_docs, title): """ Shows docs, workflows, locales, formats, or filters. By default lists added folders and docs. """ try: action = actions.Action(os.getcwd()) init_logger(action.path) if id_type == 'workflow': action.list_workflow_action() elif id_type == 'locale': action.list_locale_action() elif id_type == 'format': action.list_format_action() elif id_type == 'filter': action.list_filter_action() elif id_type == 'remote': action.list_remote_action() else: action.list_ids_action(hide_docs, title) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def init(host, access_token, client_id, path, project_name, workflow_id, locale, browser, delete, reset): """ Connects a local project to Lingotek """ try: host = 'https://' + host if not path: path = os.getcwd() if not project_name: project_name = os.path.basename(os.path.normpath(path)) init_logger(path) init = init_action.InitAction(os.getcwd()) init.init_action(host, access_token, client_id, path, project_name, workflow_id, locale, browser, delete, reset) if(init.turn_clone_on == False): # set the download option in config config = config_action.ConfigAction(os.getcwd()) config.set_clone_option('off', print_info=False) except (ResourceNotFound, RequestFailedError) as e: print_log(e) logger.error(e) return
def import_command(import_all, force, path, track, no_cancel): """ Import documents from Lingotek Cloud, by default downloading to the project's root folder """ # todo import should show all documents # add a force option so can import all force -- overwrites all existing documents without prompting # check if doc id # if exist, prompt for overwrite # else automatically re-name # possibly have to patch title in Lingotek Cloud? try: # action = actions.Action(os.getcwd()) action = import_action.ImportAction(os.getcwd()) init_logger(action.path) if path != None: path = remove_powershell_formatting(path) action.import_action(import_all, force, path, track, no_cancel) except(UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def clone(folders, copy_root): """ Copies the folder structure of added folders or specified folders for each target locale as specified in config. Folders are added to the locale folder specified if one has been specified, or by default a new folder will be created with the name of the locale. If only one root folder is being cloned, then the locale folder is used (instead of creating a new folder inside of the locale folder). """ try: action = clone_action.CloneAction(os.getcwd()) init_logger(action.path) if isinstance(folders,str): folders = [folders] if len(folders) > 0: folders = remove_powershell_formatting(folders) action.clone_action(folders, copy_root) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def clone(folders, copy_root): """ Copies the folder structure of added folders or specified folders for each target locale as specified in config. Folders are added to the locale folder specified if one has been specified, or by default a new folder will be created with the name of the locale. If only one root folder is being cloned, then the locale folder is used (instead of creating a new folder inside of the locale folder). """ try: action = actions.Action(os.getcwd()) init_logger(action.path) if isinstance(folders,str): folders = [folders] if len(folders) > 0: folders = remove_powershell_formatting(folders) action.clone_action(folders, copy_root) except (UninitializedError, RequestFailedError) as e: print_log(e) logger.error(e) return
def update_document_action(self, file_name, title=None, **kwargs): try: relative_path = self.norm_path(file_name) entry = self.doc_manager.get_doc_by_prop('file_name', relative_path) try: document_id = entry['id'] except TypeError as e: log_error(self.error_file_name, e) logger.error("Document name specified for update doesn't exist: {0}".format(title)) return if title: response, previous_doc_id = self.locked_doc_response_manager(self.api.document_update(document_id, file_name, title=title, **kwargs), document_id, file_name, title=title, **kwargs) else: response, previous_doc_id = self.locked_doc_response_manager(self.api.document_update(document_id, file_name), document_id, file_name, **kwargs) if response.status_code == 410: target_locales = entry['locales'] self.doc_manager.remove_element(previous_doc_id) print('Document has been archived. Reuploading...') self.add_document(file_name, title, self.default_metadata, translation_locale_code=target_locales) return True elif response.status_code == 402: raise_error(response.json(), "Community has been disabled. Please contact [email protected] to re-enable your community", True) elif response.status_code == 202: try: next_document_id = response.json()['next_document_id'] except Exception: next_document_id = None finally: self._update_document(relative_path, next_document_id) return True else: raise_error(response.json(), "Failed to update document {0}".format(file_name), True) return False 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)) return False
def init_logger(path): """ 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': 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: print(e.errno) print(e) file_handler = logging.FileHandler(os.path.join(path, CONF_DIR, LOG_FN)) console_handler = logging.StreamHandler(sys.stdout) file_handler.setLevel(API_LOG_LEVEL) file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s')) if quiet: console_handler.setLevel(logging.WARNING) elif verbosity: if verbosity > 1: console_handler.setLevel(API_RESPONSE_LOG_LEVEL) else: console_handler.setLevel(API_LOG_LEVEL) else: console_handler.setLevel(logging.INFO) custom_formatter = CustomFormatter() console_handler.setFormatter(custom_formatter) logger.addHandler(file_handler) logger.addHandler(console_handler)
def error(error_message): logger.error(error_message+"\n")
def error(error_message): logger.error(error_message + "\n")