Skip to content

Commit

Permalink
Add util module
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitronx committed Aug 10, 2023
1 parent 7b29644 commit 4cb062e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/waybar_check_gmail/util/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import pathlib
import stat
import sys
import textwrap


def msgfmt(msg, prefix=""):
lines = []
for line in msg.splitlines():
lines += textwrap.wrap(line, 80 - len(prefix))
return "\n".join([prefix + line for line in lines])


def warn(msg):
print(msgfmt(msg, "warning: "))


def die(msg):
sys.exit(msgfmt(msg, "error: "))


def mkdir_p(dir):
try:
pathlib.Path(dir).mkdir(parents=True, exist_ok=True)
except FileExistsError:
die("{} is not a directory".format(dir))


def is_group_or_other_writable(f):
st = os.stat(f)
return bool(st.st_mode & (stat.S_IWGRP | stat.S_IWOTH))


def is_group_or_other_readable(f):
st = os.stat(f)
return bool(st.st_mode & (stat.S_IRGRP | stat.S_IROTH))


def check_runtime_dir(dir):
try:
pathlib.Path(dir)
except FileExistsError:
die("{} is not a directory".format(dir))

0 comments on commit 4cb062e

Please sign in to comment.