Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "exists" method to Filer #33

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions tests/base/test_filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
tests.help.test_filing module

"""
import shutil

import pytest
import os

Expand All @@ -15,7 +17,14 @@ def test_filing():
Test Filer class
"""
dirpath = '/usr/local/var/hio/test'
if os.path.exists(dirpath):
shutil.rmtree(dirpath)

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 +59,15 @@ def test_filing():

# Test with clean
dirpath = '/usr/local/var/hio/clean/test'
if os.path.exists(dirpath):
shutil.rmtree(dirpath)

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 @@ -85,8 +102,15 @@ def test_filing():

# test with alt
dirpath = '/Users/samuel/.hio/test'
if os.path.exists(dirpath):
shutil.rmtree(dirpath)

# 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 +145,15 @@ def test_filing():

# Test Filer with file not dir
filepath = '/usr/local/var/hio/conf/test.text'
if os.path.exists(filepath):
os.remove(filepath)

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 @@ -169,8 +200,15 @@ def test_filing():

# Test Filer with file not dir and with Alt path
filepath = '/Users/samuel/.hio/conf/test.text'
if os.path.exists(filepath):
os.remove(filepath)

# 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
Loading