summaryrefslogtreecommitdiff
path: root/pywal/util.py
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2017-06-26 12:09:35 +1000
committerDylan Araps <dylan.araps@gmail.com>2017-06-26 12:09:35 +1000
commit583a0c19ad2f67a54130a219eeeaccf53ea08ded (patch)
treea72b51a6cf219c46c55f8fe9033814c48d23d48b /pywal/util.py
parentd17282cdc01a41cc70e0919ace55715eac3dd6d1 (diff)
General: Split wal into multiple files.
Diffstat (limited to 'pywal/util.py')
-rw-r--r--pywal/util.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/pywal/util.py b/pywal/util.py
new file mode 100644
index 0000000..061c94e
--- /dev/null
+++ b/pywal/util.py
@@ -0,0 +1,54 @@
+"""
+Misc helper functions.
+"""
+import os
+import pathlib
+import subprocess
+import shutil
+
+from pywal import settings as s
+
+
+def read_file(input_file):
+ """Read colors from a file."""
+ return open(input_file).read().splitlines()
+
+
+def save_file(colors, export_file):
+ """Write the colors to the file."""
+ with open(export_file, "w") as file:
+ file.write(colors)
+
+
+def create_cache_dir():
+ """Alias to create the cache dir."""
+ pathlib.Path(s.CACHE_DIR / "schemes").mkdir(parents=True, exist_ok=True)
+
+
+def hex_to_rgb(color):
+ """Convert a hex color to rgb."""
+ red, green, blue = list(bytes.fromhex(color.strip("#")))
+ return f"{red},{green},{blue}"
+
+
+def fix_escape(string):
+ """Decode a string."""
+ return bytes(string, "utf-8").decode("unicode_escape")
+
+
+def notify(msg):
+ """Send arguements to notify-send."""
+ if shutil.which("notify-send") and s.Args.notify:
+ subprocess.Popen(["notify-send", msg],
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ preexec_fn=os.setpgrp)
+
+
+def disown(*cmd):
+ """Call a system command in the background,
+ disown it and hide it's output."""
+ subprocess.Popen(["nohup"] + list(cmd),
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ preexec_fn=os.setpgrp)