Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
document_id = self.doc_manager.get_doc_by_prop('name', title)['id']
if self.locale_delimiter:
try:
# curr_locale = title.split(self.locale_delimiter)[1]
# todo locale detection needs to be more robust
curr_locale = title.split(self.locale_delimiter)[-2]
fixed_locale = map_locale(curr_locale)
if fixed_locale:
print ("fixed locale:", fixed_locale)
# self.watch_locales.add(fixed_locale)
self.detected_locales[document_id] = fixed_locale
else:
logger.warning('This document\'s detected locale: {0} is not supported.'.format(curr_locale))
except IndexError:
logger.warning('Cannot detect locales from file: {0}, not adding any locales'.format(title))
self.watch_add_target(title, document_id)
# logger.info('Added new document {0}'.format(title
def clone_action(self, folders, copy_root):
try:
if not len(self.watch_locales) or self.watch_locales == set(['[]']):
logger.warning("There are no locales for which to clone. You can add locales using 'ltk config -t'.")
return
folders_map = {}
are_added_folders = False
if not folders:
folders = self.folder_manager.get_file_names()
are_added_folders = True
for folder in folders:
folder_paths = folder.split(os.sep)
# print("current abs: "+str(self.get_current_abs(folder)))
if are_added_folders:
folder = os.path.join(self.path,folder)
# print("folder to be cloned: "+str(folder))
folders_map[folder_paths[len(folder_paths)-1]] = get_sub_folders(folder)
# print("folders: "+str(folders_map))
cloned_folders = False
for locale in self.watch_locales:
self.update_config_file('download_option', download_option, conf_parser, config_file_name, '')
elif clone_option == 'off':
if self.download_dir == '':
new_download_option = 'same'
self.download_option = new_download_option
self.update_config_file('clone_option', clone_option, conf_parser, config_file_name, log_info)
self.update_config_file('download_option', new_download_option, conf_parser, config_file_name, '')
self.update_config_file('download_folder', self.download_dir, conf_parser, config_file_name, '')
else:
new_download_option = 'folder'
self.download_option = new_download_option
self.update_config_file('clone_option', clone_option, conf_parser, config_file_name, log_info)
self.update_config_file('download_option', new_download_option, conf_parser, config_file_name, '')
else:
logger.warning('Error: Invalid value for "-c" / "--clone_option": Must be either "on" or "off"')
print_config = False
if 'target_locales' in kwargs and kwargs['target_locales']:
target_locales = kwargs['target_locales']
locales = []
for locale in target_locales:
locales.extend(locale.split(','))
if len(locales) > 0 and locales[0].lower() == 'none':
log_info = 'Removing all target locales'
self.update_config_file('watch_locales', '', conf_parser, config_file_name, log_info)
else:
target_locales = get_valid_locales(self.api,locales)
target_locales_str = ','.join(target for target in target_locales)
if len(target_locales_str) > 0:
log_info = 'Set target locales to {}'.format(target_locales_str)
self.update_config_file('watch_locales', target_locales_str, conf_parser, config_file_name, log_info)
def append_location(self, name, path_to_file, in_directory=False):
repo_directory = path_to_file
path_sep = os.sep
if not in_directory:
while repo_directory and repo_directory != "" and not (os.path.isdir(repo_directory + "/.ltk")):
repo_directory = repo_directory.split(path_sep)[:-1]
repo_directory = path_sep.join(repo_directory)
if repo_directory == "":
logger.warning('Error: File must be contained within an ltk-initialized directory')
return name
path_to_file = path_to_file.replace(repo_directory, '', 1).strip(os.sep)
config_file_name, conf_parser = self.init_config_file()
if not conf_parser.has_option('main', 'append_option'): self.update_config_file('append_option', 'none', conf_parser, config_file_name, 'Update: Added optional file location appending (ltk config --help)')
append_option = conf_parser.get('main', 'append_option')
if append_option == 'none': return name
elif append_option == 'full': return '{0} ({1})'.format(name, path_to_file.rstrip(name).rstrip(os.sep))
elif len(append_option) > 5 and append_option[:5] == 'name:':
folder_name = append_option[5:]
if folder_name in path_to_file:
return '{0} ({1})'.format(name, path_to_file[path_to_file.find(folder_name)+len(folder_name):].rstrip(name).strip(os.sep))
else: return '{0} ({1})'.format(name, path_to_file.rstrip(name).rstrip(os.sep))
elif len(append_option) > 7 and append_option[:7] == 'number:':
try: folder_number = int(append_option[7:])
except ValueError:
logger.warning('Error: Value after "number" must be an integer')
folder_name = append_option[5:]
if folder_name in path_to_file:
return '{0} ({1})'.format(name, path_to_file[path_to_file.find(folder_name)+len(folder_name):].rstrip(name).strip(os.sep))
else: return '{0} ({1})'.format(name, path_to_file.rstrip(name).rstrip(os.sep))
elif len(append_option) > 7 and append_option[:7] == 'number:':
try: folder_number = int(append_option[7:])
except ValueError:
logger.warning('Error: Value after "number" must be an integer')
return name
if(folder_number >=0):
return '{0} ({1})'.format(name, path_sep.join(path_to_file.rstrip(name).rstrip(os.sep).split(path_sep)[(-1*folder_number) if folder_number != 0 else len(path_to_file):]))
else:
logger.warning('Error: Value after "number" must be a non-negative integer')
return name
else:
logger.warning('Error: Invalid value listed for append option. Please update; see ltk config --help')
if doc:
document_id = doc['id']
else:
return
if self.locale_delimiter:
try:
# curr_locale = title.split(self.locale_delimiter)[1]
# todo locale detection needs to be more robust
curr_locale = title.split(self.locale_delimiter)[-2]
fixed_locale = map_locale(curr_locale)
if fixed_locale:
print ("fixed locale: ", fixed_locale)
# self.watch_locales.add(fixed_locale)
self.detected_locales[document_id] = fixed_locale
else:
logger.warning('This document\'s detected locale: {0} is not supported.'.format(curr_locale))
except IndexError:
logger.warning('Cannot detect locales from file: {0}, not adding any locales'.format(title))
self.watch_add_target(relative_path, document_id)
# logger.info('Added new document {0}'.format(title
# else:
# print("Skipping hidden file "+file_path)
except KeyboardInterrupt:
for observer in self.observers:
observer.stop()
# except Exception as err:
if 'locale_folder' in kwargs and kwargs['locale_folder']:
locale_folders = kwargs['locale_folder']
mult_folders = False
folders_count = len(locale_folders)
folders_string = ""
count = 0
log_info = ""
for folder in locale_folders:
count += 1
if not folder[0] or not folder[1]:
logger.warning("Please specify a valid locale and a directory for that locale.")
print_config = False
continue
locale = folder[0].replace("-","_")
if not is_valid_locale(self.api, locale):
logger.warning(str(locale+' is not a valid locale. See "ltk list -l" for the list of valid locales'))
print_config = False
continue
if folder[1] == '--none':
folders_count -= 1
if locale in self.locale_folders:
self.locale_folders.pop(locale, None)
logger.info("Removing download folder for locale "+str(locale)+"\n")
else:
logger.info("The locale "+str(locale)+" already has no download folder.\n")
print_config = False
continue
path = self.norm_path(os.path.abspath(folder[1]))
if os.path.exists(os.path.join(self.path,path)):
taken_locale = self.is_locale_folder_taken(locale, path)
if taken_locale:
logger.info("The folder "+str(path)+" is already taken by the locale "+str(taken_locale)+".\n")
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("The locale "+str(locale)+" already has no download folder.\n")
print_config = False
continue
path = self.norm_path(os.path.abspath(folder[1]))
if os.path.exists(os.path.join(self.path,path)):
taken_locale = self.is_locale_folder_taken(locale, path)
if taken_locale:
logger.info("The folder "+str(path)+" is already taken by the locale "+str(taken_locale)+".\n")
print_config = False
continue
else:
# print("path of new locale folder: "+path)
self.locale_folders[locale] = path
else:
logger.warning('Error: Invalid value for "-p" / "--locale_folder": Path "'+path+'" does not exist')
print_config = False
continue
folders_string += str(locale) + ": " + str(path)
if count < len(locale_folders):
folders_string += ", "
if len(folders_string):
if folders_count > 1:
log_info = 'Adding locale folders {0}'.format(folders_string)
else:
log_info = 'Adding locale folder for {0}'.format(folders_string)
locale_folders_str = json.dumps(self.locale_folders)
self.update_config_file('locale_folders', locale_folders_str, conf_parser, config_file_name, log_info)
if 'remove_locales' in kwargs and kwargs['remove_locales']:
clear_locales = kwargs['remove_locales']
log_info = "Removed all locale specific download folders."
self.locale_folders = {}
def get_valid_locales(api, entered_locales, operation_text):
"""Return the list of valid locales, checking locales either remotely or using a local list of locales."""
valid_locales = []
response = api.list_locales()
remote_check = False
if response.status_code == 200:
remote_check = True
locale_json = response.json()
for entry in locale_json:
valid_locales.append(locale_json[entry]['locale'])
locales = []
if(len(entered_locales) == 0 or (len(entered_locales) == 1 and entered_locales[0] == "[]")):
logger.warning('No locales have been assigned to this document. Please add them using \'ltk request\'.')
else:
for locale in entered_locales:
check_locale = locale.replace("-","_")
if remote_check and check_locale not in valid_locales or not remote_check and not check_locale in locale_list:
logger.warning('The locale code "'+str(locale)+'" failed to be '+operation_text+' since it is invalid (see "ltk list -l" for the list of valid codes).')
else:
locales.append(locale)
return locales