Last active
January 5, 2018 10:08
-
-
Save papr/24c3b1101beb9e0e151984461e0ceb1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
(*)~--------------------------------------------------------------------------- | |
Pupil - eye tracking platform | |
Copyright (C) 2012-2017 Pupil Labs | |
Distributed under the terms of the GNU | |
Lesser General Public License (LGPL v3.0). | |
See COPYING and COPYING.LESSER for license details. | |
---------------------------------------------------------------------------~(*) | |
''' | |
import os | |
import json | |
from time import strftime, localtime | |
from plugin import Plugin | |
from pyglui import ui | |
import logging | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.INFO) | |
class Icon_Validator(Plugin): | |
def init_ui(self): | |
self.add_menu() | |
self.menu.label = self.pretty_class_name | |
self.menu.append(ui.Info_Text('The configuration will be exported to the user settings folder.')) | |
self.menu.append(ui.Button('Export current configuration', self.export_config)) | |
self.menu.append(ui.Info_Text('Drag and drop the config file to validate\ | |
it against the current configuration. A log\ | |
file containing the results will be created\ | |
next to the config file.')) | |
def export_config(self): | |
export_time = strftime("%Y_%m_%d__%H_%M_%S", localtime()) | |
export_name = 'icon_config_{}.json'.format(export_time) | |
export_path = os.path.join(self.g_pool.user_dir, export_name) | |
with open(export_path, 'w') as json_file: | |
json.dump(self.current_config(), json_file, ensure_ascii=True, | |
indent=4, sort_keys=True) | |
logger.info('Exported current config to {}'.format(export_path)) | |
def current_config(self): | |
return {p.pretty_class_name: {'icon_chr': p.icon_chr, 'icon_font': p.icon_font} | |
for p in self.g_pool.plugins} | |
def on_drop(self, paths): | |
for p in paths: | |
try: | |
with open(p) as json_file: | |
config = json.load(json_file) | |
except (json.decoder.JSONDecodeError, UnicodeDecodeError) as e: | |
logger.error('Config file {} could not be decoded. ({}: {})'.format(p, type(e), e)) | |
except IsADirectoryError: | |
logger.error('Path {} is a directory'.format(p)) | |
except: | |
import traceback | |
logger.error('Error while loading {}'.format(p)) | |
logger.error(traceback.format_exc()) | |
else: | |
self.validate(p, config) | |
def validate(self, path, config): | |
logger.info('Validating {}'.format(path)) | |
log = ['INFO: Validating {}'.format(path)] | |
log_path = os.path.splitext(path)[0] + '.log' | |
current = self.current_config() | |
config_plugins = set(config.keys()) | |
current_plugins = set(current.keys()) | |
only_in_config = config_plugins - current_plugins | |
only_in_current = current_plugins - config_plugins | |
warnings = 0 | |
errors = 0 | |
if only_in_config: | |
log.append('WARNING: The following plugins are present in the config but are currently not loaded:') | |
log.append('\t' + ', '.join((k for k in only_in_config))) | |
warnings += 1 | |
if only_in_current: | |
log.append('WARNING: The following plugins are currently loaded but are not present in the config:') | |
log.append('\t' + ', '.join((k for k in only_in_current))) | |
warnings += 1 | |
for p in config_plugins & current_plugins: | |
for k in ('icon_font', 'icon_chr'): | |
if config[p][k] != current[p][k]: | |
log.append('ERROR: For {}[{}] expected {} (got {})'.format(p, k, config[p][k], current[p][k])) | |
errors += 1 | |
log.append('INFO: Finished validating {}'.format(path)) | |
with open(log_path, 'a') as log_file: | |
log_file.write('\n'.join(log)) | |
logger.info('Finished validating. {} errors and {} warnings encountered.'.format(errors, warnings)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment