def load_config(self, parser): import ConfigParser config_name = 'config.ini' config = ConfigParser.ConfigParser() configs = [] path = os.path.join(os.path.dirname(sys.executable), config_name) if os.path.exists(path): configs.append(path) if os.path.exists(config_name): configs.append(config_name) user_ini = os.path.expanduser( os.path.join(Options.nxdrive_home, config_name)) if os.path.exists(user_ini): configs.append(user_ini) if configs: config.read(configs) args = AbstractOSIntegration.get(None).get_system_configuration() if config.has_option(ConfigParser.DEFAULTSECT, 'env'): env = config.get(ConfigParser.DEFAULTSECT, 'env') for item in config.items(env): if item[0] == 'env': continue value = item[1] if value == '': continue if '\n' in item[1]: # Treat multiline option as a set value = tuple(sorted(item[1].split())) args[item[0].replace('-', '_')] = value if args: Options.update(args, setter='local') parser.set_defaults(**args)
def test_batch_update_from_dict_with_unknown_option(): options = {"debug": True, "foo": 42} with pytest.raises(RuntimeError): Options.update(options, setter="local") assert Options.debug assert not Options.foo
def test_batch_update_from_dict(): """ Simulate local and server conf files. """ options = {"debug": True, "locale": "fr"} Options.update(options, setter="local") assert Options.debug assert Options.locale == "fr"
def load_config(self, parser): import ConfigParser config_name = 'config.ini' config = ConfigParser.ConfigParser() configs = [] path = os.path.join(os.path.dirname(sys.executable), config_name) if os.path.exists(path): configs.append(path) if os.path.exists(config_name): configs.append(config_name) user_ini = os.path.expanduser(os.path.join(Options.nxdrive_home, config_name)) if os.path.exists(user_ini): configs.append(user_ini) if configs: config.read(configs) args = AbstractOSIntegration.get(None).get_system_configuration() if config.has_option(ConfigParser.DEFAULTSECT, 'env'): env = config.get(ConfigParser.DEFAULTSECT, 'env') for item in config.items(env): if item[0] == 'env': continue value = item[1] if value == '': continue if '\n' in item[1]: # Treat multiline option as a set value = tuple(sorted(item[1].split())) args[item[0].replace('-', '_')] = value if args: Options.update(args, setter='local') parser.set_defaults(**args)
def test_batch_update_from_dict_with_unknown_option(): options = { 'foo': 42, 'debug': True, } with pytest.raises(KeyError): Options.update(options, setter='local', fail_on_error=True) assert Options.debug assert not Options.foo
def test_batch_update_from_argparse(): """ Simulate CLI args. """ parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--debug', default=True, action='store_true') parser.add_argument('--delay', default=0, type=int) options = parser.parse_args([]) Options.update(options, setter='cli') assert Options.debug assert not Options.delay
def test_batch_update_from_dict(): """ Simulate local and server conf files. """ options = { 'debug': True, 'locale': 'fr', } Options.update(options, setter='local') assert Options.debug assert Options.locale == 'fr'
def test_batch_update_from_argparse(): """ Simulate CLI args. """ parser = argparse.ArgumentParser(add_help=False) parser.add_argument("--debug", default=True, action="store_true") parser.add_argument("--delay", default=0, type=int) options = parser.parse_args([]) Options.update(options, setter="cli") assert Options.debug assert not Options.delay
def handle(self, argv): """ Parse options, setup logs and manager and dispatch execution. """ options = self.parse_cli(argv) if hasattr(options, 'local_folder'): options.local_folder = normalized_path(options.local_folder) # 'launch' is the default command if None is provided command = getattr(options, 'command', 'launch') if command != 'uninstall': # Configure the logging framework, except for the tests as they # configure their own. # Don't need uninstall logs either for now. self._configure_logger(command, options) self.log = get_logger(__name__) self.log.debug("Command line: argv=%r, options=%r", ' '.join(argv), options) # Update default options Options.update(options, setter='cli') if command != 'uninstall': # Install utility to help debugging segmentation faults self._install_faulthandler() # Initialize a manager for this process self.manager = self.get_manager() # Find the command to execute based on the handler = getattr(self, command, None) if not handler: raise NotImplementedError('No handler implemented for command ' + command) try: return handler(options) except Exception as e: if Options.debug: # Make it possible to use the postmortem debugger raise msg = e.msg if hasattr(e, 'msg') else e self.log.error("Error executing '%s': %s", command, msg, exc_info=True)
def handle(self, argv): """ Parse options, setup logs and manager and dispatch execution. """ options = self.parse_cli(argv) if hasattr(options, 'local_folder'): options.local_folder = normalized_path(options.local_folder) # 'launch' is the default command if None is provided command = getattr(options, 'command', 'launch') if command != 'uninstall': # Configure the logging framework, except for the tests as they # configure their own. # Don't need uninstall logs either for now. self._configure_logger(command, options) self.log = get_logger(__name__) self.log.debug("Command line: argv=%r, options=%r", ' '.join(argv), options) # Update default options Options.update(options, setter='cli') if command != 'uninstall': # Install utility to help debugging segmentation faults self._install_faulthandler() # Initialize a manager for this process self.manager = self.get_manager() # Find the command to execute based on the handler = getattr(self, command, None) if not handler: raise NotImplementedError( 'No handler implemented for command ' + command) try: return handler(options) except Exception as e: if Options.debug: # Make it possible to use the postmortem debugger raise msg = e.msg if hasattr(e, 'msg') else e self.log.error("Error executing '%s': %s", command, msg, exc_info=True)
def _poll(self): # type: () -> bool """ Check for the configuration file and apply updates. """ for _, engine in self.manager._engines.items(): client = engine.get_remote_doc_client() if not client: continue try: raw, _ = client.do_get(client.rest_api_url + 'drive/configuration') conf = json.loads(raw, encoding='utf-8') except (URLError, ValueError): continue else: Options.update(conf, setter='server', fail_on_error=True) break return True
def test_disabled_features(caplog): """Simple test for disabled features.""" assert Options.feature_auto_update is True assert Options.feature_s3 is False with patch("nxdrive.options.DisabledFeatures", new=["auto_update"]): options = {"feature_auto_update": False, "feature_s3": True} Options.update(options, setter="manual") # feature_auto_update has been ignored as it is in DisabledFeatures assert Options.feature_auto_update is True # feature_s3 has been modified as expected assert Options.feature_s3 is True # Check that a warning has been fired record = caplog.records[0] assert record.levelname == "WARNING" assert record.message == "'feature_auto_update' cannot be changed."
def test_batch_update_from_dict_with_unknown_option(): options = {"debug": True, "foo": 42} with pytest.raises(RuntimeError) as err: Options.update(options, setter="local") msg = err.value.args[0] assert "foo" in msg assert "test.ini" not in msg assert "debugging" not in msg # Test the 'section' arg with pytest.raises(RuntimeError) as err: Options.update(options, setter="local", file="test.ini") msg = err.value.args[0] assert "foo" in msg assert "test.ini" in msg assert "debugging" not in msg # Test `file` and `section` args with pytest.raises(RuntimeError) as err: Options.update(options, setter="local", file="test.ini", section="debugging") msg = err.value.args[0] assert "foo" in msg assert "test.ini" in msg assert "debugging" in msg assert Options.debug assert not Options.foo
def test_list_conversion_and_original_values_updated(): assert isinstance(Options.ignored_suffixes, tuple) assert "azerty" not in Options.ignored_suffixes current_len = len(Options.ignored_suffixes) Options.set("ignored_suffixes", ["azerty"], setter="manual") assert isinstance(Options.ignored_suffixes, tuple) assert "azerty" in Options.ignored_suffixes assert len(Options.ignored_suffixes) == current_len + 1 new_values = { "ignored_files": ["bim", "bam", "boom", "zzzzzzzzzz"], "force_locale": "zh", } Options.update(new_values, setter="manual") assert isinstance(Options.ignored_files, tuple) assert "bim" in Options.ignored_files assert "bam" in Options.ignored_files assert "boom" in Options.ignored_files assert len(Options.ignored_files) > 4 # Check it is sorted assert Options.ignored_files[-1] == "zzzzzzzzzz"
def _poll(self): # type: () -> bool """ Check for the configuration file and apply updates. """ for _, engine in self.manager._engines.items(): client = engine.get_remote_doc_client() if not client: continue try: raw, _ = client.do_get(client.server_url + SERVER_CONF_URL) conf = json.loads(raw, encoding='utf-8') except HTTPError as exc: if exc.code == 404: self._enable = False log.info('Disabling server configuration updater thread') break except (URLError, ValueError): continue else: Options.update(conf, setter='server', fail_on_error=True) break return True