def __init__(self, app): """Create the necessary objects. Args: app: The main vimiv application to interact with. """ super().__init__(app) self.app = app # Create tags directory self.directory = os.path.join(GLib.get_user_data_dir(), "vimiv", "Tags") os.makedirs(self.directory, exist_ok=True) # If there are tags in the old location, move them to the new one # TODO remove this in a new version, assigned for 0.10 old_directory = os.path.expanduser("~/.vimiv/Tags") if os.path.isdir(old_directory): old_tags = os.listdir(old_directory) for f in old_tags: old_path = os.path.join(old_directory, f) new_path = os.path.join(self.directory, f) os.rename(old_path, new_path) message = "Moved tags from the old directory %s " \ "to the new location %s." % (old_directory, self.directory) error_message(message, running_tests=self.app.running_tests) os.rmdir(old_directory) self.last = ""
def parse_config(): """Check each configfile for settings and apply them. Return: Dictionary of modified settings. """ settings = set_defaults() configfiles = check_configs( ["/etc/vimiv/vimivrc", os.path.expanduser("~/.vimiv/vimivrc")]) # Error message, gets filled with invalid sections in the user's configfile. # If any exist, a popup is displayed at startup. message = "" # Iterate through the configfiles overwriting settings accordingly for configfile in configfiles: config = configparser.ConfigParser() config.read(configfile) keys = [key for key in config.keys() if key in ["GENERAL", "LIBRARY"]] for key in keys: settings, partial_message = overwrite_section( key, config, settings) message += partial_message settings, partial_message = add_aliases(config, settings) message += partial_message if message: error_message(message) return settings
def parse_config(commandline_config=None, running_tests=False): """Check each configfile for settings and apply them. Args: commandline_config: Configfile given by command line flag to parse. running_tests: If True, running from testsuite. Return: Dictionary of modified settings. """ configfiles = [] # We do not want to parse user configuration files when running the test # suite if not running_tests: configfiles += [ "/etc/vimiv/vimivrc", os.path.join(get_user_config_dir(), "vimiv/vimivrc"), os.path.expanduser("~/.vimiv/vimivrc")] if commandline_config: configfiles.append(commandline_config) # Error message, gets filled with invalid sections in the user's configfile. # If any exist, a popup is displayed at startup. message = "" # Let ConfigParser parse the list of configuration files config = configparser.ConfigParser() try: config.read(configfiles) except UnicodeDecodeError as e: message += "Could not decode configfile.\n" + str(e) except configparser.MissingSectionHeaderError as e: message += "Invalid configfile.\n" + str(e) except configparser.ParsingError as e: message += str(e) # Override settings with settings in config for header in ["GENERAL", "LIBRARY", "EDIT"]: if header not in config: continue section = config[header] for setting in section: try: settings.override(setting, section[setting]) except StringConversionError as e: message += str(e) + "\n" except SettingNotFoundError: message += "Unknown setting %s\n" % (setting) # Receive aliases aliases, partial_message = get_aliases(config) settings.set_aliases(aliases) message += partial_message if message: error_message(message, running_tests=running_tests) return settings
def __init__(self, app, settings): """Create the necessary objects and settings. Args: app: The main vimiv application to interact with. settings: Settings from configfiles to use. """ super().__init__(app) general = settings["GENERAL"] # Command line self.entry = Gtk.Entry() self.entry.connect("activate", self.handler) self.entry.connect("key_press_event", self.app["eventhandler"].run, "COMMAND") self.entry.connect("changed", self.check_close) self.entry.set_hexpand(True) # Cmd history from file datadir = os.path.join(GLib.get_user_data_dir(), "vimiv") os.makedirs(datadir, exist_ok=True) # If there is a history file in the old location move it and inform the # user # TODO remove this in a new version, assigned for 0.10 old_histfile = os.path.expanduser("~/.vimiv/history") new_histfile = os.path.join(datadir, "history") if os.path.isfile(old_histfile): if os.path.isfile(new_histfile): message = "There is a history file in the deprecated location" \ " %s and the new location %s. Using the new one. If you " \ "want the history from the old file you will have to " \ "merge it manually." % (old_histfile, new_histfile) error_message(message, running_tests=self.app.running_tests) else: os.rename(old_histfile, new_histfile) message = "Moved the old history file %s " \ "to the new location %s." % (old_histfile, new_histfile) error_message(message, running_tests=self.app.running_tests) self.history = read_file(os.path.join(datadir, "history")) # Defaults self.pos = 0 self.sub_history = [] self.search_positions = [] self.search_case = general["search_case_sensitive"] self.incsearch = general["incsearch"] if self.incsearch: self.entry.connect("changed", self.incremental_search) self.last_index = 0 self.running_processes = [] self.last_focused = ""
def __init__(self, app): """Create the necessary objects and settings. Args: app: The main vimiv application to interact with. """ super(CommandLine, self).__init__() self._app = app self.commands = None # Command line self.connect("activate", self._on_activate) self.connect("key_press_event", self._app["eventhandler"].on_key_press, "COMMAND") self.connect("changed", self._on_text_changed) self.set_hexpand(True) # Initialize search self.search = Search(self) # Cmd history from file datadir = os.path.join(get_user_data_dir(), "vimiv") os.makedirs(datadir, exist_ok=True) # If there is a history file in the old location move it and inform the # user # TODO remove this in a new version, assigned for 0.10 old_histfile = os.path.expanduser("~/.vimiv/history") new_histfile = os.path.join(datadir, "history") if os.path.isfile(old_histfile): if os.path.isfile(new_histfile): message = "There is a history file in the deprecated location" \ " %s and the new location %s. Using the new one. If you " \ "want the history from the old file you will have to " \ "merge it manually." % (old_histfile, new_histfile) error_message(message, running_tests=self._app.running_tests) else: os.rename(old_histfile, new_histfile) message = "Moved the old history file %s " \ "to the new location %s." % (old_histfile, new_histfile) error_message(message, running_tests=self._app.running_tests) self._history = read_file(os.path.join(datadir, "history")) # Defaults self._matching_history = [] self._last_index = 0 self._pos = 0 self.running_processes = [] self._last_widget = ""
def parse_config(commandline_config=None, running_tests=False): """Check each configfile for settings and apply them. Args: commandline_config: Configfile given by command line flag to parse. running_tests: If True, running from testsuite. Return: Dictionary of modified settings. """ settings = set_defaults() configfiles = [] # We do not want to parse user configuration files when running the test # suite if not running_tests: configfiles += [ "/etc/vimiv/vimivrc", os.path.join(GLib.get_user_config_dir(), "vimiv/vimivrc"), os.path.expanduser("~/.vimiv/vimivrc") ] if commandline_config: configfiles.append(commandline_config) # Error message, gets filled with invalid sections in the user's configfile. # If any exist, a popup is displayed at startup. message = "" # Let ConfigParser parse the list of configuration files config = configparser.ConfigParser() try: config.read(configfiles) except UnicodeDecodeError as e: message += "Could not decode configfile.\n" + str(e) except configparser.MissingSectionHeaderError as e: message += "Invalid configfile.\n" + str(e) except configparser.ParsingError as e: message += str(e) keys = [key for key in config if key in ["GENERAL", "LIBRARY"]] for key in keys: settings, partial_message = overwrite_section(key, config, settings) message += partial_message settings, partial_message = add_aliases(config, settings) message += partial_message if message: error_message(message, running_tests=running_tests) return settings
def parse_keys(): """Check for a keyfile and parse it. Return: Dictionary of keybindings. """ # Check which keyfile should be used keys = configparser.ConfigParser() try: if os.path.isfile(os.path.expanduser("~/.vimiv/keys.conf")): keys.read(os.path.expanduser("~/.vimiv/keys.conf")) elif os.path.isfile("/etc/vimiv/keys.conf"): keys.read("/etc/vimiv/keys.conf") else: message = "Keyfile not found. Exiting." error_message(message) sys.exit(1) except configparser.DuplicateOptionError as e: message = e.message + ".\n Duplicate keybinding. Exiting." error_message(message) sys.exit(1) # Get the keybinding dictionaries checking for errors try: keys_image = keys["IMAGE"] keys_thumbnail = keys["THUMBNAIL"] keys_library = keys["LIBRARY"] keys_manipulate = keys["MANIPULATE"] keys_command = keys["COMMAND"] except KeyError as e: message = "Missing section " + str(e) + " in keys.conf.\n" \ + "Refer to vimivrc(5) to fix your config." error_message(message) sys.exit(1) # Update the dictionaries of every window with the keybindings that apply # for more than one window keys_image.update(keys["GENERAL"]) keys_image.update(keys["IM_THUMB"]) keys_image.update(keys["IM_LIB"]) keys_thumbnail.update(keys["GENERAL"]) keys_thumbnail.update(keys["IM_THUMB"]) keys_library.update(keys["GENERAL"]) keys_library.update(keys["IM_LIB"]) # Generate one dictionary for all and return it keybindings = { "IMAGE": keys_image, "THUMBNAIL": keys_thumbnail, "LIBRARY": keys_library, "MANIPULATE": keys_manipulate, "COMMAND": keys_command } return keybindings
def test_error_message(self): """Error message popup.""" # Not much can happen here, if all attributes are set correctly it will # also run helpers.error_message("Test error", True)
def parse_keys(running_tests=False): """Check for a keyfile and parse it. Args: running_tests: If True running from testsuite. Do not show error popup. Return: Dictionary of keybindings. """ keyfiles = [ "/etc/vimiv/keys.conf", os.path.join(GLib.get_user_config_dir(), "vimiv/keys.conf"), os.path.expanduser("~/.vimiv/keys.conf") ] # Read the list of files keys = configparser.ConfigParser() try: # No file for keybindings found if not keys.read(keyfiles): message = "Keyfile not found. Exiting." error_message(message) sys.exit(1) except configparser.DuplicateOptionError as e: message = e.message + ".\n Duplicate keybinding. Exiting." error_message(message) sys.exit(1) # Get the keybinding dictionaries checking for errors try: keys_image = keys["IMAGE"] keys_thumbnail = keys["THUMBNAIL"] keys_library = keys["LIBRARY"] keys_manipulate = keys["MANIPULATE"] keys_command = keys["COMMAND"] except KeyError as e: message = "Missing section " + str(e) + " in keys.conf.\n" \ "Refer to vimivrc(5) to fix your config." error_message(message, running_tests=running_tests) sys.exit(1) # Update the dictionaries of every window with the keybindings that apply # for more than one window def update_keybindings(sections, keydict): """Add keybindings from generic sections to keydict.""" for section in sections: if section in keys: print( "Section", section, "is deprecated and will be removed in" " a future version.") keydict.update(keys[section]) update_keybindings(["GENERAL", "IM_THUMB", "IM_LIB"], keys_image) update_keybindings(["GENERAL", "IM_THUMB"], keys_thumbnail) update_keybindings(["GENERAL", "IM_LIB"], keys_library) # Generate one dictionary for all and return it keybindings = { "IMAGE": keys_image, "THUMBNAIL": keys_thumbnail, "LIBRARY": keys_library, "MANIPULATE": keys_manipulate, "COMMAND": keys_command } return keybindings
#!/usr/bin/env python3 # encoding: utf-8 """vimiv init: tests some imports and imports main.""" import sys try: from gi import require_version require_version('Gtk', '3.0') from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Pango from PIL import Image, ImageEnhance from vimiv.helpers import error_message except ImportError as import_error: message = import_error.msg + "\n" + "Are all dependencies installed?" error_message(message) sys.exit(1)
def __init__(self, app): """Generate commands and functions. Args: app: The main vimiv application to interact with. """ self._app = app self._commands = {} self.aliases = {} self._n = 0 # Add all commands self.add_command("accept_changes", self._app["manipulate"].finish, default_args=[True]) self.add_command("alias", self.add_alias, positional_args=["name", "command"], last_arg_allows_space=True) self.add_command("autorotate", self._app["transform"].rotate_auto) self.add_command("center", self._app["main_window"].center_window) self.add_command("copy_basename", self._app["clipboard"].copy_name, default_args=[False]) self.add_command("copy_abspath", self._app["clipboard"].copy_name, default_args=[True]) self.add_command("delete", self._app["transform"].delete) self.add_command("undelete", self._app["transform"].undelete, positional_args=["basename"], last_arg_allows_space=True) self.add_command("discard_changes", self._app["manipulate"].finish, default_args=[False]) self.add_command("edit", self._app["manipulate"].cmd_edit, positional_args=["manipulation"], optional_args=["value"]) self.add_command("first", self._app["image"].move_pos, default_args=[False], supports_count=True) self.add_command("first_lib", self._app["library"].move_pos, default_args=[False], supports_count=True) self.add_command("fit", self._app["image"].zoom_to, default_args=[0, "fit"]) self.add_command("fit_horiz", self._app["image"].zoom_to, default_args=[0, "horizontal"]) self.add_command("fit_vert", self._app["image"].zoom_to, default_args=[0, "vertical"]) self.add_command("flip", self._app["transform"].flip, positional_args=["direction"]) self.add_command("format", format_files, default_args=[self._app], positional_args=["formatstring"], last_arg_allows_space=True) self.add_command("fullscreen", self._app["window"].toggle_fullscreen) self.add_command("last", self._app["image"].move_pos, default_args=[True], supports_count=True) self.add_command("last_lib", self._app["library"].move_pos, default_args=[True], supports_count=True) self.add_command("library", self._app["library"].toggle) self.add_command("focus_library", self._app["library"].focus, default_args=[True]) self.add_command("unfocus_library", self._app["library"].focus, default_args=[False]) self.add_command("manipulate", self._app["manipulate"].toggle) self.add_command("mark", self._app["mark"].mark) self.add_command("mark_all", self._app["mark"].mark_all) self.add_command("mark_between", self._app["mark"].mark_between) self.add_command("mark_toggle", self._app["mark"].toggle_mark) self.add_command("move_up", self._app["library"].move_up, supports_count=True) self.add_command("next", self._app["image"].move_index, default_args=[True, True], supports_count=True) self.add_command("next!", self._app["image"].move_index, default_args=[True, True, 1, True], supports_count=True) self.add_command("prev", self._app["image"].move_index, default_args=[False, True], supports_count=True) self.add_command("prev!", self._app["image"].move_index, default_args=[False, True, 1, True], supports_count=True) self.add_command("q", self._app.quit_wrapper) self.add_command("q!", self._app.quit_wrapper, default_args=[True]) self.add_command("reload_lib", self._app["library"].reload, default_args=["."]) self.add_command("rotate", self._app["transform"].rotate, positional_args=["value"], supports_count=True) self.add_command("set", self.set, positional_args=["setting"], optional_args=["value"], supports_count=True) self.add_command("slideshow", self._app["slideshow"].toggle, supports_count=True) self.add_command("tag_load", self._app["tags"].load, positional_args=["tagname"], last_arg_allows_space=True) self.add_command("tag_remove", self._app["tags"].remove, positional_args=["tagname"], last_arg_allows_space=True) self.add_command("tag_write", self._app["tags"].write, default_args=[self._app["mark"].marked], positional_args=["tagname"], last_arg_allows_space=True) self.add_command("thumbnail", self._app["thumbnail"].toggle) self.add_command("version", self._app["information"].show_version_info) self.add_command("w", self._app["transform"].write) self.add_command("wq", self._app["transform"].write, default_args=[True]) self.add_command("zoom_in", self._app["window"].zoom, default_args=[True], optional_args=["steps"], supports_count=True) self.add_command("zoom_out", self._app["window"].zoom, default_args=[False], optional_args=["steps"], supports_count=True) self.add_command("zoom_to", self._app["image"].zoom_to, optional_args=["percent"], supports_count=True) # Hidden commands self.add_command("clear_status", self._app["statusbar"].clear_status, is_hidden=True) self.add_command("command", self._app["commandline"].enter, optional_args=["text"], is_hidden=True) self.add_command("complete", self._app["completions"].complete, default_args=[False], is_hidden=True) self.add_command("complete_inverse", self._app["completions"].complete, default_args=[True], is_hidden=True) self.add_command("discard_command", self._app["commandline"].leave, default_args=[True], is_hidden=True) self.add_command("focus_slider", self._app["manipulate"].focus_slider, positional_args=["name"], is_hidden=True) self.add_command("history_down", self._app["commandline"].history_search, default_args=[True], is_hidden=True) self.add_command("history_up", self._app["commandline"].history_search, default_args=[False], is_hidden=True) self.add_command("scroll", self._app["main_window"].scroll, positional_args=["direction"], supports_count=True, is_hidden=True) self.add_command("scroll_lib", self._app["library"].scroll, positional_args=["direction"], supports_count=True, is_hidden=True) self.add_command("search", self._app["commandline"].enter_search, is_hidden=True) self.add_command("search_next", self._app["commandline"].search_move, default_args=[True], supports_count=True, is_hidden=True) self.add_command("search_prev", self._app["commandline"].search_move, default_args=[False], supports_count=True, is_hidden=True) self.add_command("slider", self._app["manipulate"].change_slider, optional_args=["value"], supports_count=True, is_hidden=True) # Aliases aliases = settings.get_aliases() message = "" for alias in aliases: try: self._add_alias(alias, aliases[alias]) except AliasError as e: message += str(e) + "\n" if message: error_message(message, running_tests=self._app.running_tests) aliases.clear()
#!/usr/bin/env python3 # encoding: utf-8 """vimiv init: tests some imports and imports main.""" import sys try: from gi import require_version require_version('Gtk', '3.0') from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Pango from PIL import Image, ImageEnhance from vimiv.helpers import error_message except ImportError as import_error: message = import_error.msg + "\n" + "Are all dependencies installed?" error_message(message) sys.exit(1) import vimiv.main