summaryrefslogtreecommitdiff
path: root/share/python/lib/files.py
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-02-25 08:31:29 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-02-25 08:31:29 +0000
commitc3a17851fd70a2462511ec6942627d0720cf6c5a (patch)
tree880bf662661864a1558548c8375bf6bb617b43f4 /share/python/lib/files.py
parentb5b2350106b5f352fe52cf745e693f2eda008758 (diff)
parent736daa8bbcd56eb80c0f18cf8e2a2f78d6432e99 (diff)
downloadsubplot-c3a17851fd70a2462511ec6942627d0720cf6c5a.tar.gz
Merge branch 'files' into 'main'
feat: directory handling steps for Python lib/files Closes #157 See merge request larswirzenius/subplot!140
Diffstat (limited to 'share/python/lib/files.py')
-rw-r--r--share/python/lib/files.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/share/python/lib/files.py b/share/python/lib/files.py
index ec37b9d..dd5b9f8 100644
--- a/share/python/lib/files.py
+++ b/share/python/lib/files.py
@@ -1,10 +1,12 @@
import logging
import os
import re
+import shutil
import time
def files_create_from_embedded(ctx, filename=None):
+ files_make_directory(ctx, path=os.path.dirname(filename) or ".")
files_create_from_embedded_with_other_name(
ctx, filename_on_disk=filename, embedded_filename=filename
)
@@ -14,15 +16,29 @@ def files_create_from_embedded_with_other_name(
ctx, filename_on_disk=None, embedded_filename=None
):
get_file = globals()["get_file"]
+
+ files_make_directory(ctx, path=os.path.dirname(filename_on_disk) or ".")
with open(filename_on_disk, "wb") as f:
f.write(get_file(embedded_filename))
def files_create_from_text(ctx, filename=None, text=None):
+ files_make_directory(ctx, path=os.path.dirname(filename) or ".")
with open(filename, "w") as f:
f.write(text)
+def files_make_directory(ctx, path=None):
+ path = "./" + path
+ if not os.path.exists(path):
+ os.makedirs(path)
+
+
+def files_remove_directory(ctx, path=None):
+ path = "./" + path
+ shutil.rmtree(path)
+
+
def files_file_exists(ctx, filename=None):
assert_eq = globals()["assert_eq"]
assert_eq(os.path.exists(filename), True)
@@ -33,6 +49,26 @@ def files_file_does_not_exist(ctx, filename=None):
assert_eq(os.path.exists(filename), False)
+def files_directory_exists(ctx, path=None):
+ assert_eq = globals()["assert_eq"]
+ assert_eq(os.path.isdir(path), True)
+
+
+def files_directory_does_not_exist(ctx, path=None):
+ assert_eq = globals()["assert_eq"]
+ assert_eq(os.path.isdir(path), False)
+
+
+def files_directory_is_empty(ctx, path=None):
+ assert_eq = globals()["assert_eq"]
+ assert_eq(os.listdir(path), [])
+
+
+def files_directory_is_not_empty(ctx, path=None):
+ assert_ne = globals()["assert_ne"]
+ assert_ne(os.listdir(path), False)
+
+
def files_only_these_exist(ctx, filenames=None):
assert_eq = globals()["assert_eq"]
filenames = filenames.replace(",", "").split()