def load_languages(self):
     self.translations = {}
     tool.touch_folder(self.language_folder)
     for file in tool.list_file(self.language_folder, LANGUAGE_FILE_SUFFIX):
         language = os.path.basename(file).rstrip(LANGUAGE_FILE_SUFFIX)
         with open(file, encoding='utf8') as f:
             self.translations[language] = yaml.round_trip_load(f)
Example #2
0
	def __init__(self, server, plugin_folder):
		self.plugin_folder = plugin_folder
		self.server = server
		self.logger = server.logger
		self.plugins = []
		self.thread_pool = PluginThreadPool(self.server, max_thread=constant.PLUGIN_THREAD_POOL_SIZE)
		tool.touch_folder(self.plugin_folder)
		tool.touch_folder(constant.PLUGIN_CONFIG_FOLDER)
Example #3
0
 def check_update(self, reply_func=None):
     if reply_func is None:
         reply_func = self.server.logger.info
     try:
         response = requests.get(constant.GITHUB_API_LATEST).json()
         latest_version = response['tag_name']
         url = response['html_url']
         download_url = response['assets'][0]['browser_download_url']
     except Exception as e:
         reply_func(
             self.server.t('update_helper._check_update.check_fail',
                           repr(e)))
     else:
         cmp_result = tool.version_compare(constant.VERSION, latest_version)
         if cmp_result == 0:
             reply_func(
                 self.server.t(
                     'update_helper._check_update.is_already_latest'))
         elif cmp_result == 1:
             reply_func(
                 self.server.t(
                     'update_helper._check_update.newer_than_latest',
                     constant.VERSION, latest_version))
         else:
             reply_func(
                 self.server.t(
                     'update_helper._check_update.new_version_detected',
                     latest_version))
             reply_func(
                 self.server.t(
                     'update_helper._check_update.new_version_url', url))
             if self.server.config['download_update']:
                 try:
                     tool.touch_folder(constant.UPDATE_DOWNLOAD_FOLDER)
                     file_name = os.path.join(
                         constant.UPDATE_DOWNLOAD_FOLDER,
                         os.path.basename(download_url))
                     if not os.path.isfile(file_name):
                         file_data = requests.get(download_url)
                         with open(file_name, 'wb') as file:
                             file.write(file_data.content)
                     reply_func(
                         self.server.
                         t('update_helper._check_update.download_finished',
                           file_name))
                 except:
                     reply_func(
                         self.server.t(
                             'update_helper._check_update.download_fail'))
                 else:
                     return True
     return False
	def set_file(self, file_name):
		if self.file_handler is not None:
			self.logger.removeHandler(self.file_handler)
		tool.touch_folder(os.path.dirname(file_name))
		if os.path.isfile(file_name):
			modify_time = time.strftime('%Y-%m-%d', time.localtime(os.stat(file_name).st_mtime))
			counter = 0
			while True:
				counter += 1
				zip_file_name = '{}/{}-{}.zip'.format(os.path.dirname(file_name), modify_time, counter)
				if not os.path.isfile(zip_file_name):
					break
			zipf = zipfile.ZipFile(zip_file_name, 'w')
			zipf.write(file_name, arcname=os.path.basename(file_name), compress_type=zipfile.ZIP_DEFLATED)
			zipf.close()
			os.remove(file_name)
		self.file_handler = logging.FileHandler(file_name, encoding='utf8')
		self.file_handler.setFormatter(self.file_fmt)
		self.logger.addHandler(self.file_handler)
Example #5
0
 def __check_update(self, reply_func):
     acquired = self.update_lock.acquire(blocking=False)
     if not acquired:
         reply_func(
             self.server.t('update_helper.check_update.already_checking'))
         return False
     try:
         if reply_func is None:
             reply_func = self.server.logger.info
         response = None
         try:
             response = requests.get(constant.GITHUB_API_LATEST,
                                     timeout=5).json()
             latest_version = response['tag_name']
             url = response['html_url']
             download_url = response['assets'][0]['browser_download_url']
             update_log = response['body']
         except Exception as e:
             reply_func(
                 self.server.t('update_helper.check_update.check_fail',
                               repr(e)))
             if isinstance(e, KeyError) and type(
                     response) is dict and 'message' in response:
                 reply_func(response['message'])
                 if 'documentation_url' in response:
                     reply_func(
                         RText(response['documentation_url'],
                               color=RColor.blue,
                               styles=RStyle.underlined).h(
                                   response['documentation_url']).c(
                                       RAction.open_url,
                                       response['documentation_url']))
         else:
             cmp_result = tool.version_compare(constant.VERSION,
                                               latest_version)
             if cmp_result == 0:
                 reply_func(
                     self.server.t(
                         'update_helper.check_update.is_already_latest'))
             elif cmp_result == 1:
                 reply_func(
                     self.server.t(
                         'update_helper.check_update.newer_than_latest',
                         constant.VERSION, latest_version))
             else:
                 reply_func(
                     self.server.t(
                         'update_helper.check_update.new_version_detected',
                         latest_version))
                 for line in update_log.splitlines():
                     reply_func('    {}'.format(line))
                 reply_func(
                     self.server.t(
                         'update_helper.check_update.new_version_url', url))
                 if self.server.config['download_update']:
                     try:
                         tool.touch_folder(constant.UPDATE_DOWNLOAD_FOLDER)
                         file_name = os.path.join(
                             constant.UPDATE_DOWNLOAD_FOLDER,
                             os.path.basename(download_url))
                         if not os.path.isfile(file_name):
                             file_data = requests.get(download_url,
                                                      timeout=5)
                             with open(file_name, 'wb') as file:
                                 file.write(file_data.content)
                         reply_func(
                             self.server.t(
                                 'update_helper.check_update.download_finished',
                                 file_name))
                     except:
                         reply_func(
                             self.server.
                             t('update_helper.check_update.download_fail'))
                     else:
                         return True
         return False
     finally:
         self.update_lock.release()