Skip to content

Commit

Permalink
Add "exists" method to file to check for existence of file before "re…
Browse files Browse the repository at this point in the history
…open" creates it.
  • Loading branch information
pfeairheller committed Apr 2, 2024
1 parent eb69cf4 commit be192d1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/hio/base/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,66 @@ def remake(self, *, name="", base="", temp=None, headDirPath=None, perm=None,

os.chmod(path, perm) # set dir/file permissions

return (path, file)
return path, file

def exists(self, name="", base="", headDirPath=None, clean=False, filed=False, fext=None):
"""
Check if (path. file) exists for a given set of parameters for remake. Temp is not allowed.
Parameters:
name (str): unique name alias portion of path
base (str): optional base inserted before name in path
headDirPath (str): optional head directory pathname of main database
clean (bool): True means make path for cleaned version and remove
old directory or file at clean path if any.
False means make path normally (not clean)
filed (bool): True means .path is file path not directory path
False means .path is directiory path not file path
fext (str): File extension when .filed
Returns:
bool: True means path or alt path exists, false means neither exists
"""

# use class defaults here so can use makePath for other dirs and files
if headDirPath is None:
headDirPath = self.HeadDirPath

if fext is None:
fext = self.Fext

tailDirPath = self.CleanTailDirPath if clean else self.TailDirPath
altTailDirPath = self.AltCleanTailDirPath if clean else self.AltTailDirPath

if filed:
root, ext = os.path.splitext(name)
if not ext:
name = f"{name}.{fext}"

path = os.path.abspath(
os.path.expanduser(
os.path.join(headDirPath,
tailDirPath,
base,
name)))

# Check non-alt, if exists return True
if os.path.exists(path):
return True

# Now we must check the alt path to see if that exists.
headDirPath = self.AltHeadDirPath
path = os.path.abspath(
os.path.expanduser(
os.path.join(headDirPath,
altTailDirPath,
base,
name)))
return os.path.exists(path)

def close(self, clear=False):
"""
Expand Down
21 changes: 21 additions & 0 deletions tests/base/test_filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def test_filing():
Test Filer class
"""
dirpath = '/usr/local/var/hio/test'
filer = filing.Filer(name="test", reopen=False) # defaults
assert filer.exists(name="test") is False

filer = filing.Filer(name="test") # defaults
assert filer.exists(name="test") is True
assert filer.path.endswith("hio/test")
assert filer.opened
assert os.path.exists(filer.path)
Expand Down Expand Up @@ -50,7 +54,12 @@ def test_filing():

# Test with clean
dirpath = '/usr/local/var/hio/clean/test'
filer = filing.Filer(name="test", clean="true", reopen=False) # defaults
assert filer.exists(name="test", clean="true") is False

filer = filing.Filer(name="test", clean="true") # defaults
assert filer.exists(name="test", clean="true") is True

assert filer.path.endswith("hio/clean/test")
assert filer.opened
assert os.path.exists(filer.path)
Expand Down Expand Up @@ -86,7 +95,11 @@ def test_filing():
# test with alt
dirpath = '/Users/samuel/.hio/test'
# headDirPath that is not permitted to force using AltPath
filer = filing.Filer(name="test", headDirPath="/root/hio", reopen=False)
assert filer.exists(name="test", headDirPath="/root/hio") is False

filer = filing.Filer(name="test", headDirPath="/root/hio")
assert filer.exists(name="test", headDirPath="/root/hio") is True
assert filer.path.endswith(".hio/test")
assert filer.opened
assert os.path.exists(filer.path)
Expand Down Expand Up @@ -121,8 +134,12 @@ def test_filing():

# Test Filer with file not dir
filepath = '/usr/local/var/hio/conf/test.text'
assert os.path.exists(filepath) is False
filer = filing.Filer(name="test", base="conf", filed=True, reopen=False)
assert filer.exists(name="test", base="conf", filed=True) is False

filer = filing.Filer(name="test", base="conf", filed=True)
assert filer.exists(name="test", base="conf", filed=True) is True
assert filer.path.endswith("hio/conf/test.text")
assert filer.opened
assert os.path.exists(filer.path)
Expand Down Expand Up @@ -170,7 +187,11 @@ def test_filing():
# Test Filer with file not dir and with Alt path
filepath = '/Users/samuel/.hio/conf/test.text'
# force altPath by using headDirPath of "/root/hio" which is not permitted
filer = filing.Filer(name="test", base="conf", headDirPath="/root/hio", filed=True, reopen=False)
assert filer.exists(name="test", base="conf", headDirPath="/root/hio", filed=True) is False

filer = filing.Filer(name="test", base="conf", headDirPath="/root/hio", filed=True)
assert filer.exists(name="test", base="conf", headDirPath="/root/hio", filed=True) is True
assert filer.path.endswith(".hio/conf/test.text")
assert filer.opened
assert os.path.exists(filer.path)
Expand Down

0 comments on commit be192d1

Please sign in to comment.