def mock_gtk_version(tmp_path, monkeypatch): """Fixture to mock the xdg directories and fill them with gtk-version-like files.""" for name in ("cache", "config", "data"): directory = tmp_path / name directory.mkdir() monkeypatch.setenv(f"XDG_{name.upper()}_HOME", str(directory)) for directory in ( xdg.vimiv_config_dir(), xdg.vimiv_cache_dir(), xdg.vimiv_data_dir(), ): assert "home" not in directory, "patching the xdg directories failed" os.makedirs(directory) for basename in ("vimivrc", "keys.conf"): abspath = xdg.vimiv_config_dir(basename) with open(abspath, "w") as f: f.write("option: value\n") tag_dir = xdg.vimiv_data_dir("Tags") os.mkdir(tag_dir) tag_file = os.path.join(tag_dir, TAGFILE_NAME) with open(tag_file, "w") as f: for i in range(10): f.write(f"test_{i:02d}.jpg\n") yield migration.WelcomePopUp.gtk_installed = False
def init_directories(args: argparse.Namespace) -> None: """Create vimiv cache, config and data directories. The directories are either the directories defined in the freedesktop standard or located in a temporary base directory. Args: args: Arguments returned from parser.parse_args(). """ if args.temp_basedir: global _tmpdir _tmpdir = tempfile.TemporaryDirectory(prefix="vimiv-tempdir-") args.basedir = _tmpdir.name if args.basedir is not None: xdg.basedir = args.basedir xdg.makedirs(xdg.vimiv_cache_dir(), xdg.vimiv_config_dir(), xdg.vimiv_data_dir())
def backup() -> XdgDirectories: """Create a backup of all gtk vimiv directories.""" backup_str = "-gtk-backup" backup_dirs: List[Optional[str]] = [] for dirname in ( xdg.vimiv_cache_dir(), xdg.vimiv_config_dir(), xdg.vimiv_data_dir(), ): try: backup_dirname = dirname + backup_str shutil.move(dirname, backup_dirname) print(f"... Created backup of '{dirname}' at '{backup_dirname}'") backup_dirs.append(backup_dirname) except OSError as e: print(f"... Error backing up {dirname}: {e}") backup_dirs.append(None) return XdgDirectories(*backup_dirs)
def init_directories(args): """Create vimiv cache, config and data directories. The directories are either the directories defined in the freedesktop standard or located in a temporary base directory. Args: args: Arguments returned from parser.parse_args(). """ if args.temp_basedir: global _tmpdir _tmpdir = tempfile.TemporaryDirectory(prefix="vimiv-tempdir-") basedir = _tmpdir.name os.environ["XDG_CACHE_HOME"] = os.path.join(basedir, "cache") os.environ["XDG_CONFIG_HOME"] = os.path.join(basedir, "config") os.environ["XDG_DATA_HOME"] = os.path.join(basedir, "data") for directory in [ xdg.vimiv_cache_dir(), xdg.vimiv_config_dir(), xdg.vimiv_data_dir(), xdg.join_vimiv_config("styles"), ]: os.makedirs(directory, exist_ok=True)