def task_workbench(monkeypatch, app_dir): """Setup the workbench in such a way that the task manager can be tested. """ def exit_err(self): if self._delayed: raise Exception('Unexpected exceptions occured :\n' + pformat(self._delayed)) monkeypatch.setattr(ErrorsPlugin, 'exit_error_gathering', exit_err) workbench = Workbench() workbench.register(CoreManifest()) workbench.register(AppManifest()) workbench.register(PreferencesManifest()) workbench.register(ErrorsManifest()) workbench.register(DependenciesManifest()) workbench.register(TasksManagerManifest()) yield workbench for m_id in ('ecpy.tasks', 'ecpy.app.dependencies', 'ecpy.app.errors', 'ecpy.app.preferences', 'ecpy.app'): try: workbench.unregister(m_id) except ValueError: pass
def measure_workbench(monkeypatch, app_dir): """Setup the workbench in such a way that the measure plugin can be tested. """ def exit_err(self): self._gathering_counter -= 1 if self._gathering_counter < 1: self._gathering_counter = 0 if self._delayed: msg = 'Unexpected exceptions occured :\n' raise ErrorDialogException(msg + pformat(self._delayed)) monkeypatch.setattr(ErrorsPlugin, 'exit_error_gathering', exit_err) workbench = Workbench() workbench.register(CoreManifest()) workbench.register(AppManifest()) workbench.register(PreferencesManifest()) workbench.register(ErrorsManifest()) workbench.register(DependenciesManifest()) workbench.register(MeasureManifest()) yield workbench for m_id in ('ecpy.measure', 'ecpy.app.dependencies', 'ecpy.app.errors', 'ecpy.app.preferences', 'ecpy.app'): try: workbench.unregister(m_id) except ValueError: pass
def setup(self): self.workbench = Workbench() self.workbench.register(CoreManifest()) self.workbench.register(AppManifest()) self.workbench.register(PreferencesManifest()) self.workbench.register(StateManifest()) self.workbench.register(LogManifest())
def test_app_startup3(self, tmpdir, windows): """Test app start-up when a preference file already exists. """ manifest = PreferencesManifest() self.workbench.register(manifest) # Create a trash app_directory.ini file. The global fixture ensure # that it cannot be a user file. Don't use app_dir fixture as I need to # test directory creation. app_pref = os.path.join(ecpy_path(), APP_PREFERENCES, APP_DIR_CONFIG) app_dir = str(tmpdir.join('ecpy')) conf = ConfigObj() conf.filename = app_pref conf['app_path'] = app_dir conf.write() assert not os.path.isdir(app_dir) # Start the app and fake a user answer. app = self.workbench.get_plugin('ecpy.app') app.run_app_startup(object()) assert os.path.isdir(app_dir)
def test_save3(self, app_dir, monkeypatch): """Test saving to a specific file. """ self.workbench.register(PreferencesManifest()) c_man = PrefContributor() self.workbench.register(c_man) contrib = self.workbench.get_plugin(c_man.id) contrib.string = 'test_save' prefs_path = os.path.join(app_dir, 'prefs') path = os.path.join(prefs_path, 'custom.ini') @classmethod def answer(*args, **kwargs): return path with enaml.imports(): from ecpy.app.preferences.manifest import FileDialogEx monkeypatch.setattr(FileDialogEx, 'get_save_file_name', answer) core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.save', { 'path': prefs_path, 'ask_user': True }) ref = {c_man.id: {'string': 'test_save', 'auto': ''}} assert os.path.isfile(path) assert ConfigObj(path).dict() == ref assert self.workbench.get_plugin(PLUGIN_ID).last_directory == \ prefs_path
def test_load4(self, app_dir, monkeypatch): """Test loading preferences from non-default file for started plugin. """ self.workbench.register(PreferencesManifest()) c_man = PrefContributor() self.workbench.register(c_man) contrib = self.workbench.get_plugin(c_man.id) prefs_path = os.path.join(app_dir, 'prefs') path = os.path.join(prefs_path, 'custom.ini') conf = ConfigObj(path) conf[c_man.id] = {} conf[c_man.id]['string'] = 'test' conf.write() assert contrib.string == '' @classmethod def answer(*args, **kwargs): return path with enaml.imports(): from ecpy.app.preferences.manifest import FileDialogEx monkeypatch.setattr(FileDialogEx, 'get_open_file_name', answer) core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.load', { 'path': prefs_path, 'ask_user': True }, self) assert contrib.string == 'test' assert self.workbench.get_plugin(PLUGIN_ID).last_directory == \ prefs_path
def test_lifecycle(self, app_dir): """Test the plugin lifecycle when no default.ini exist in app folder. """ pref_man = PreferencesManifest() self.workbench.register(pref_man) c_man = PrefContributor() self.workbench.register(c_man) # Start preferences plugin. prefs = self.workbench.get_plugin(PLUGIN_ID) assert prefs.app_directory == app_dir assert os.path.isdir(os.path.join(app_dir, 'prefs')) core = self.workbench.get_plugin('enaml.workbench.core') assert core.invoke_command('ecpy.app.preferences.get', dict(plugin_id='test.prefs')) is not None self.workbench.register(PrefContributor2()) assert core.invoke_command('ecpy.app.preferences.get', dict(plugin_id='test.prefs2')) is not None # Stopping self.workbench.unregister(c_man.id) with pytest.raises(KeyError): core.invoke_command('ecpy.app.preferences.get', dict(plugin_id='test.prefs')) self.workbench.unregister(pref_man.id) assert not prefs._prefs assert not prefs._pref_decls
def test_app_startup4(self, tmpdir, windows): """Test app start-up when user request to reset app folder. """ manifest = PreferencesManifest() self.workbench.register(manifest) app_dir = str(tmpdir.join('ecpy')) # Add a app_directory.ini file. app_pref = os.path.join(ecpy_path(), APP_PREFERENCES, APP_DIR_CONFIG) if not os.path.isfile(app_pref): conf = ConfigObj() conf.filename = app_pref conf['app_path'] = app_dir conf.write() # Start the app and fake a user answer. app = self.workbench.get_plugin('ecpy.app') class DummyArgs(object): reset_app_folder = True with handle_dialog(custom=lambda x: setattr(x, 'path', app_dir)): app.run_app_startup(DummyArgs) assert os.path.isfile(app_pref) assert ConfigObj(app_pref)['app_path'] == app_dir assert os.path.isdir(app_dir)
def task_workbench(workbench, monkeypatch, app_dir): """Setup the workbench in such a way that the task manager can be tested. """ monkeypatch.setattr(ErrorsPlugin, 'exit_error_gathering', exit_on_err) workbench.register(CoreManifest()) workbench.register(AppManifest()) workbench.register(PreferencesManifest()) workbench.register(IconManagerManifest()) workbench.register(ErrorsManifest()) workbench.register(StateManifest()) workbench.register(DependenciesManifest()) workbench.register(TasksManagerManifest()) yield workbench for m_id in ('ecpy.tasks', 'ecpy.app.dependencies', 'ecpy.app.errors', 'ecpy.app.icons', 'ecpy.app.preferences', 'ecpy.app'): try: workbench.unregister(m_id) except Exception: pass # Give some time to the os to release resources linked to file # monitoring. sleep(0.1)
def test_load2(self, app_dir): """Test loading preferences from non-existing file. """ self.workbench.register(PreferencesManifest()) self.workbench.register(PrefContributor()) core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.load', {'path': ''}, self) assert not self.workbench.get_plugin(PLUGIN_ID)._prefs
def icon_workbench(workbench, app_dir): """Register the icon mannager plugin and dependencies. """ workbench.register(CoreManifest()) workbench.register(PreferencesManifest()) workbench.register(IconManagerManifest()) yield workbench for m_id in ('ecpy.app.icons', 'ecpy.app.preferences'): try: workbench.unregister(m_id) except Exception: pass
def test_update_contrib_and_type_checking(self, app_dir): """Check that the contributions are correctly updated when a new plugin is registered and check that the contribution is of the right type. """ pref_man = PreferencesManifest() self.workbench.register(pref_man) c_man = PrefContributor() self.workbench.register(c_man) # Start preferences plugin. self.workbench.get_plugin(PLUGIN_ID) # Test observation of extension point and type checking. b_man = BadPrefContributor() with pytest.raises(TypeError): self.workbench.register(b_man)
def test_save2(self, app_dir): """Test saving to a specific file. """ self.workbench.register(PreferencesManifest()) c_man = PrefContributor() self.workbench.register(c_man) contrib = self.workbench.get_plugin(c_man.id) contrib.string = 'test_save' path = os.path.join(app_dir, 'prefs', 'custom.ini') core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.save', {'path': path}) ref = {c_man.id: {'string': 'test_save', 'auto': ''}} assert os.path.isfile(path) assert ConfigObj(path).dict() == ref
def test_app_startup2(self, tmpdir, windows): """Test app start-up when user quit app. """ manifest = PreferencesManifest() self.workbench.register(manifest) # Remove any trash app_directory.ini file. The global fixture ensure # that it cannot be a user file. app_pref = os.path.join(ecpy_path(), APP_PREFERENCES, APP_DIR_CONFIG) if os.path.isfile(app_pref): os.remove(app_pref) # Start the app and fake a user answer. app = self.workbench.get_plugin('ecpy.app') with pytest.raises(SystemExit): with handle_dialog('reject'): app.run_app_startup(object())
def test_load_defaultini(self, app_dir): """Test that a default.ini file found in the app folder under prefs is loaded on startup. """ prefs_path = os.path.join(app_dir, 'prefs') os.mkdir(os.path.join(app_dir, 'prefs')) conf = ConfigObj(os.path.join(prefs_path, 'default.ini')) c_man = PrefContributor() conf[c_man.id] = {} conf[c_man.id]['string'] = 'This is a test' conf.write() pref_man = PreferencesManifest() self.workbench.register(pref_man) self.workbench.register(c_man) c_pl = self.workbench.get_plugin(c_man.id) assert c_pl.string == 'This is a test'
def test_load3(self, app_dir): """Test loading preferences from non-default file for started plugin. """ self.workbench.register(PreferencesManifest()) c_man = PrefContributor() self.workbench.register(c_man) contrib = self.workbench.get_plugin(c_man.id) path = os.path.join(app_dir, 'prefs', 'custom.ini') conf = ConfigObj(path) conf[c_man.id] = {} conf[c_man.id]['string'] = 'test' conf.write() assert contrib.string == '' core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.load', {'path': path}, self) assert contrib.string == 'test'
def test_auto_sync(self, app_dir): """Check that auito_sync members are correctly handled. """ self.workbench.register(PreferencesManifest()) c_man = PrefContributor() self.workbench.register(c_man) contrib = self.workbench.get_plugin(c_man.id) contrib.auto = 'test_auto' ref = {c_man.id: {'auto': 'test_auto'}} path = os.path.join(app_dir, 'prefs', 'default.ini') assert os.path.isfile(path) assert ConfigObj(path).dict() == ref contrib.auto = 'test' ref = {c_man.id: {'auto': 'test'}} path = os.path.join(app_dir, 'prefs', 'default.ini') assert os.path.isfile(path) assert ConfigObj(path).dict() == ref
def test_load1(self, app_dir): """Test loading default preferences for unstarted plugin. """ # Register and start preferences plugin self.workbench.register(PreferencesManifest()) self.workbench.get_plugin(PLUGIN_ID) c_man = PrefContributor() self.workbench.register(c_man) path = os.path.join(app_dir, 'prefs', 'default.ini') conf = ConfigObj(path) conf[c_man.id] = {} conf[c_man.id]['string'] = 'test' conf.write() core = self.workbench.get_plugin('enaml.workbench.core') core.invoke_command('ecpy.app.preferences.load', {}) assert self.workbench.get_plugin(c_man.id, False) is None contrib = self.workbench.get_plugin(c_man.id) assert contrib.string == 'test'
def pulses_workbench(monkeypatch, app_dir): """Setup the workbench in such a way that the pulses manager can be tested. """ monkeypatch.setattr(ErrorsPlugin, 'exit_error_gathering', exit_on_err) workbench = Workbench() workbench.register(CoreManifest()) workbench.register(AppManifest()) workbench.register(PreferencesManifest()) workbench.register(ErrorsManifest()) workbench.register(StateManifest()) workbench.register(DependenciesManifest()) workbench.register(MeasureManifest()) workbench.register(PulsesManagerManifest()) yield workbench for m_id in ('ecpy.pulses', 'ecpy.app.dependencies', 'ecpy.app.errors', 'ecpy.app.preferences', 'ecpy.app'): try: workbench.unregister(m_id) except Exception: pass
def test_app_startup1(self, tmpdir, windows): """Test app start-up when no app_directory.ini exists. """ manifest = PreferencesManifest() self.workbench.register(manifest) # Remove any trash app_directory.ini file. The global fixture ensure # that it cannot be a user file. app_pref = os.path.join(ecpy_path(), APP_PREFERENCES, APP_DIR_CONFIG) if os.path.isfile(app_pref): os.remove(app_pref) # Start the app and fake a user answer. app = self.workbench.get_plugin('ecpy.app') app_dir = str(tmpdir.join('ecpy')) with handle_dialog(custom=lambda x: setattr(x, 'path', app_dir)): app.run_app_startup(object()) assert os.path.isfile(app_pref) assert ConfigObj(app_pref)['app_path'] == app_dir assert os.path.isdir(app_dir)
def main(cmd_line_args=None): """Main entry point of the Ecpy application. """ # Build parser from ArgParser and parse arguemnts parser = ArgParser() parser.add_choice('workspaces', 'ecpy.measure.workspace', 'measure') parser.add_argument("-s", "--nocapture", help="Don't capture stdout/stderr", action='store_true') parser.add_argument("-w", "--workspace", help='Select start-up workspace', default='measure', choices='workspaces') parser.add_argument("-r", "--reset-app-folder", help='Reset the application startup folder.', action='store_true') modifiers = [] for i, ep in enumerate(iter_entry_points('ecpy_cmdline_args')): try: modifier, priority = ep.load(require=False) modifiers.append((ep, modifier, priority, i)) except Exception as e: text = 'Error loading extension %s' % ep.name content = ('The following error occurred when trying to load the ' 'entry point {} :\n {}'.format(ep.name, e)) details = format_exc() display_startup_error_dialog(text, content, details) modifiers.sort(key=itemgetter(1, 2)) try: for m in modifiers: m[1](parser) except Exception as e: text = 'Error modifying cmd line arguments' content = ('The following error occurred when the entry point {} ' 'tried to add cmd line options :\n {}'.format(ep.name, e)) details = format_exc() display_startup_error_dialog(text, content, details) try: args = parser.parse_args(cmd_line_args) except BaseException as e: if e.args == (0,): sys.exit(0) text = 'Failed to parse cmd line arguments' content = ('The following error occurred when trying to parse the ' 'command line arguments :\n {}'.format(e)) details = format_exc() display_startup_error_dialog(text, content, details) # Patch Thread to use sys.excepthook setup_thread_excepthook() workbench = Workbench() workbench.register(CoreManifest()) workbench.register(UIManifest()) workbench.register(AppManifest()) workbench.register(StateManifest()) workbench.register(ErrorsManifest()) workbench.register(PreferencesManifest()) workbench.register(IconManagerManifest()) workbench.register(LogManifest()) workbench.register(PackagesManifest()) workbench.register(DependenciesManifest()) workbench.register(InstrumentManagerManifest()) workbench.register(TasksManagerManifest()) workbench.register(MeasureManifest()) workbench.register(TextMonitorManifest()) ui = workbench.get_plugin(u'enaml.workbench.ui') # Create the application try: app = workbench.get_plugin('ecpy.app') app.run_app_startup(args) except Exception as e: text = 'Error starting plugins' content = ('The following error occurred when executing plugins ' 'application start ups :\n {}'.format(e)) details = format_exc() display_startup_error_dialog(text, content, details) core = workbench.get_plugin('enaml.workbench.core') # Install global except hook. core.invoke_command('ecpy.app.errors.install_excepthook', {}) # Select workspace core.invoke_command('enaml.workbench.ui.select_workspace', {'workspace': args.workspace}, workbench) ui = workbench.get_plugin(u'enaml.workbench.ui') ui.show_window() ui.window.maximize() ui.start_application() core.invoke_command('enaml.workbench.ui.close_workspace', {}, workbench) # Unregister all contributed packages workbench.unregister('ecpy.app.packages') workbench.unregister('ecpy.measure.monitors.text_monitor') workbench.unregister('ecpy.measure') workbench.unregister('ecpy.tasks') workbench.unregister('ecpy.instruments') workbench.unregister('ecpy.app.icons') workbench.unregister('ecpy.app.preferences') workbench.unregister('ecpy.app.states') workbench.unregister('ecpy.app.dependencies') workbench.unregister('ecpy.app.errors') workbench.unregister('ecpy.app.logging') workbench.unregister('ecpy.app') workbench.unregister(u'enaml.workbench.ui') workbench.unregister(u'enaml.workbench.core')