def test_broken_configfiles(self): """Parse broken configfiles. Check for the error message in stdout. The parsed settings should be default as no valid updates are made. """ # Invalid because it does not have the setting: value structure text = "[GENERAL]\nsome useless text\n" configfile = self.create_configfile(text=text) parsed_settings = parser.parse_config(configfile, running_tests=True) # pylint:disable=no-member output = sys.stdout.getvalue().strip() self.assertIn("Source contains parsing errors", output) self.check_defaults(parsed_settings) # Is not plain text but rather a binary parsed_settings = parser.parse_config("vimiv/testimages/arch-logo.png", running_tests=True) output = sys.stdout.getvalue().strip() self.assertIn("Could not decode configfile", output) self.check_defaults(parsed_settings) # Start without a section header before the settings text = "shuffle: yes\n" configfile = self.create_configfile(text=text) parsed_settings = parser.parse_config(configfile, running_tests=True) output = sys.stdout.getvalue().strip() self.assertIn("Invalid configfile", output) self.check_defaults(parsed_settings)
def test_parse_config_empty(self): """Parse an empty configfile.""" # Should set default values path = os.path.join(self.configdir, "empty_vimivrc") with open(path, "w") as f: f.write("") settings = parser.parse_config(path, True) self.check_defaults(settings)
def run_check(settings, expected_output): """Run check for settings, check for expected output.""" configfile = self.create_configfile(settings=settings) parsed_settings = parser.parse_config(configfile, running_tests=True) # pylint:disable=no-member output = sys.stdout.getvalue().strip() self.assertIn(expected_output, output) self.check_defaults(parsed_settings)
def __init__(self, application_id="org.vimiv"): """Create the Gtk.Application and connect the activate signal. Args: application_id: The ID used to register vimiv. Default: org.vimiv. """ # Init application and set default values Gtk.Application.__init__(self, application_id=application_id) self.set_flags(Gio.ApplicationFlags.HANDLES_OPEN) self.connect("activate", self.activate_vimiv) self.settings = parse_config() self.paths = [] self.index = 0 self.widgets = {} self.directory = os.path.expanduser("~/.vimiv") self.commands = {} self.aliases = {} self.functions = {} # Set up all commandline options self.init_commandline_options()
def test_correct_configfile(self): """Parse correct non-default configfile with all settings.""" settings = { "GENERAL": { "start_fullscreen": "yes", "start_slideshow": "yes", "shuffle": "yes", "display_bar": "no", "default_thumbsize": "(256, 256)", "geometry": "400x400", "recursive": "yes", "rescale_svg": "yes", "overzoom": "yes", "search_case_sensitive": "yes", "incsearch": "no", "copy_to_primary": "no", "commandline_padding": 0, "completion_height": 100 }, "LIBRARY": { "show_library": "yes", "library_width": "200", "expand_lib": "no", "border_width": "12", "markup": "<span foreground=\"#FF0000\">", "show_hidden": "no", "desktop_start_dir": "~/.local", "file_check_amount": "10", "tilde_in_statusbar": "no" }, "ALIASES": { "testalias": "zoom_in" } } configfile = self.create_configfile(settings=settings) parsed_settings = parser.parse_config(configfile, True) # False positive # pylint: disable = unsubscriptable-object general = parsed_settings["GENERAL"] library = parsed_settings["LIBRARY"] aliases = parsed_settings["ALIASES"] self.assertEqual(general["start_fullscreen"], True) self.assertEqual(general["start_slideshow"], True) self.assertEqual(general["shuffle"], True) self.assertEqual(general["display_bar"], False) self.assertEqual(general["default_thumbsize"], (256, 256)) self.assertEqual(general["geometry"], "400x400") self.assertEqual(general["recursive"], True) self.assertEqual(general["rescale_svg"], True) self.assertEqual(general["overzoom"], True) self.assertEqual(general["search_case_sensitive"], True) self.assertEqual(general["incsearch"], False) self.assertEqual(general["copy_to_primary"], False) self.assertEqual(general["commandline_padding"], 0) self.assertEqual(general["completion_height"], 100) self.assertEqual(library["show_library"], True) self.assertEqual(library["library_width"], 200) self.assertEqual(library["expand_lib"], False) self.assertEqual(library["border_width"], 12) self.assertEqual(library["markup"], "<span foreground=\"#FF0000\">") self.assertEqual(library["show_hidden"], False) self.assertEqual(library["desktop_start_dir"], os.path.expanduser("~/.local")) self.assertEqual(library["file_check_amount"], 10) self.assertEqual(library["tilde_in_statusbar"], False) self.assertIn("testalias", aliases.keys()) self.assertEqual(aliases["testalias"], "zoom_in")
def test_unexisting_configfile(self): """Parse a non-existing configfile.""" path = os.path.join(self.configdir, "this_file_does_not_exist") # Should ignore it and set default values settings = parser.parse_config(path, True) self.check_defaults(settings)
def do_handle_local_options(self, options): """Handle commandline arguments. Args: options: The dictionary containing all options given to the commandline. Return: Exitcode or -1 to continue """ def set_option(name, section, setting, value=0): """Set a setting in self.settings according to commandline option. Args: name: Name of the commandline option section: Name of section in self.settings to modify. setting: Name of setting in self.settings to modify. value: One of 0, 1 or 2. 0: Set setting to False. 1: Set setting to True. 2: Set setting to value given in the commandline. """ if options.contains(name): valuedict = {0: False, 1: True} if value == 2: valuedict[2] = options.lookup_value(name).unpack() self.settings[section][setting] = valuedict[value] # Show Gtk warnings if the debug option is given if options.contains("debug"): self.debug = True # If the version option is given, print version and exit 0 if options.contains("version"): information = Information(self) print(information.get_version()) return 0 # Temp basedir removes all current settings and sets them to default if options.contains("temp-basedir"): self.tmpdir = tempfile.TemporaryDirectory(prefix="vimiv-") self.settings = set_defaults() tmp = self.tmpdir.name # Export environment variables for thumbnails, tags, history and # logs os.environ["XDG_CACHE_HOME"] = os.path.join(tmp, "cache") os.environ["XDG_CONFIG_HOME"] = os.path.join(tmp, "config") os.environ["XDG_DATA_HOME"] = os.path.join(tmp, "data") # Create settings as soon as we know which config files to use elif options.contains("config"): configfile = options.lookup_value("config").unpack() self.settings = parse_config(commandline_config=configfile, running_tests=self.running_tests) else: self.settings = parse_config(running_tests=self.running_tests) # If we start from desktop, move to the wanted directory # Else if the input does not come from a tty, e.g. find "" | vimiv, set # paths and index according to the input from the pipe if options.contains("start-from-desktop"): os.chdir(self.settings["LIBRARY"]["desktop_start_dir"]) elif not sys.stdin.isatty(): try: tty_paths = [] for line in sys.stdin: tty_paths.append(line.rstrip("\n")) self.paths, self.index = populate(tty_paths) except TypeError: pass # DebugConsoleStdIn is not iterable set_option("bar", "GENERAL", "display_bar", 1) set_option("no-bar", "GENERAL", "display_bar", 0) set_option("library", "LIBRARY", "show_library", 1) set_option("no-library", "LIBRARY", "show_library", 0) set_option("fullscreen", "GENERAL", "start_fullscreen", 1) set_option("no-fullscreen", "GENERAL", "start_fullscreen", 0) set_option("shuffle", "GENERAL", "shuffle", 1) set_option("no-shuffle", "GENERAL", "shuffle", 0) set_option("recursive", "GENERAL", "recursive", 1) set_option("no-recursive", "GENERAL", "recursive", 0) set_option("slideshow", "GENERAL", "start_slideshow", 1) set_option("slideshow-delay", "GENERAL", "slideshow_delay", 2) set_option("geometry", "GENERAL", "geometry", 2) return -1 # To continue
def setUp(self): self.config_settings = parser.parse_config()