From 67a6b1ec42cca52c41c4ef83661e0e6877f30c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Thu, 8 Aug 2024 12:28:10 +0200 Subject: [PATCH] feat: dump --- common/dump.py | 18 +++++++++++++++++- common/sync.py | 2 +- common/util.py | 15 +++++++++++++++ goog | 13 ++++++++----- 4 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 common/util.py diff --git a/common/dump.py b/common/dump.py index b80b22c..c63e8e0 100644 --- a/common/dump.py +++ b/common/dump.py @@ -1,2 +1,18 @@ -def dump(): +from glob import glob +from os.path import expanduser, isdir +from os import mkdir +from shutil import copy2 +import shutil + +def dump(stuff: list[str]): + for pattern in stuff: + expand = expanduser(pattern) + + for path in glob(expand, recursive=True): + normalized_path = path.removeprefix(expanduser("~/")) + if isdir(normalized_path): + mkdir(normalized_path) + else: + shutil.copy2(path, normalized_path) + exit(1) diff --git a/common/sync.py b/common/sync.py index 033cc64..85dd0dc 100644 --- a/common/sync.py +++ b/common/sync.py @@ -1,2 +1,2 @@ -def sync(): +def sync(stuff: list[str]): exit(1) diff --git a/common/util.py b/common/util.py new file mode 100644 index 0000000..df81fa5 --- /dev/null +++ b/common/util.py @@ -0,0 +1,15 @@ +import re + +def expand(pattern: str) -> list[str]: + match = re.search(r'\{([^{}]*)\}', pattern) + if not match: + return [pattern] + + pre, post = pattern[:match.start()], pattern[match.end():] + options = match.group(1).split(',') + + expanded_patterns = [] + for option in options: + expanded_patterns.extend(expand(pre + option + post)) + + return expanded_patterns diff --git a/goog b/goog index 8347d70..6e3a70b 100755 --- a/goog +++ b/goog @@ -5,12 +5,15 @@ import sys from common.dump import dump from common.sync import sync +from common.util import expand -stuff = { - "~/.config/{kitty,nvim,openbox,pipewire,tint2,zed}", - "~/.themes", - "~/.local/bin/{yarn}" -} +stuff: list[str] = [ + *expand("~/.config/{kitty,nvim,openbox,pipewire,tint2,zed}/**"), + "~/.themes/**", + *expand("~/.local/bin/{yarn}"), + "~/.bashrc", + "~/.xbindkeysrc" +] options, _ = getopt(sys.argv[1:], "ds", ["dump", "sync"])