Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def reinit(host, project_path, delete, reset):
if is_initialized(project_path) and not reset:
logger.warning('This project is already initialized!')
if not delete:
return False
confirm = 'not confirmed'
while confirm != 'y' and confirm != 'Y' and confirm != 'N' and confirm != 'n' and confirm != '':
confirm = input(
"Are you sure you want to delete the current project? "
"This will also delete the project in your community. [y/N]: ")
# confirm if would like to delete existing folder
if not confirm or confirm in ['n', 'N']:
return False
else:
# delete the corresponding project online
logger.info('Deleting old project folder and creating new one...')
config_file_name = os.path.join(project_path, CONF_DIR, CONF_FN)
if os.path.isfile(config_file_name):
old_config = configparser.ConfigParser()
def update_config_file(self, option, value, conf_parser, config_file_name, log_info):
conf_parser.set('main', option, value)
with open(config_file_name, 'w') as new_file:
conf_parser.write(new_file)
self._initialize_self()
if (len(log_info)):
logger.info(log_info+"\n")
def update_config_file(self, option, value, conf_parser, config_file_name, log_info):
conf_parser.set('main', option, value)
with open(config_file_name, 'w') as new_file:
conf_parser.write(new_file)
self._initialize_self()
if (len(log_info)):
logger.info(log_info+"\n")
def handleError(self):
if self.watch:
logger.warning("Could not connect to Lingotek")
restart("Restarting watch", self.timeout)
else:
raise ConnectionFailed("Could not connect to Lingotek")
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
def log_api(method, uri, response):
try:
content_length = response.headers['content-length']
except KeyError:
content_length = 'No content-length header'
log = '{method} {uri} {status} {length}'.format(method=method, uri=uri, status=response.status_code,
length=content_length)
logger.api_call(log)
if 'content-type' in response.headers and response.headers['content-type'].find("json") != -1:
try:
import json
response_info = json.dumps(response.json(), indent=4, separators=(',', ': '))
except ValueError:
response_info = 'No json response available'
logger.api_response(response_info)
elif 'content-length' in response.headers and response.headers['content-length'] != '0':
if response.headers['content-type'] == 'application/zip':
logger.api_response('Zip file')
else:
logger.api_response(response.content.decode('UTF-8'))
def delete_local(self, title, document_id, message=None):
# print('local delete:', title, document_id)
if not title:
title = document_id
message = '{0} has been deleted locally.'.format(title) if not message else message
try:
file_name = self.doc_manager.get_doc_by_prop('id', document_id)['file_name']
except TypeError:
logger.info('Document to remove not found in the local database')
return
try:
os.remove(os.path.join(self.path, file_name))
logger.info(message)
except OSError:
logger.info('Something went wrong trying to delete the local file.')
def handleError(self):
if self.watch:
logger.warning("Could not connect to Lingotek")
restart("Restarting watch", self.timeout)
else:
raise ConnectionFailed("Could not connect to Lingotek")
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