Skip to content

Commit 9360d52

Browse files
committed
Add function to check str path and return file path
1 parent dc6ddff commit 9360d52

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

pyntegrity/core.py

+42
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
"""
18+
import os
1819
import re
20+
import enum
21+
from pathlib import Path
1922

23+
from .exceptions import FileNotFoundException
24+
from .exceptions import ObjectNotAFileException
2025
from .exceptions import HashStrNotValidException
26+
from .exceptions import MissingFilePermissionException
2127
from .exceptions import HashAlgorithmNotSupportedException
2228

2329
from .config import SUPPORTED_HASH_ALGOS
2430

31+
HashAlgoEnum = enum.Enum(
32+
"HashAlgoEnum", {key: key for key in SUPPORTED_HASH_ALGOS.keys()}
33+
)
34+
2535

2636
def detect_hash_algo(checksum_str: str):
2737
"""
@@ -58,3 +68,35 @@ def validate_checksum_str(checksum_str: str):
5868
raise HashStrNotValidException(
5969
detected_hash_algo=hash_name, checksum_str=checksum_str
6070
)
71+
72+
73+
def get_file_path_from_str(str_path: str):
74+
"""
75+
Checks if path is a file and there is read access to it then return a Path object
76+
77+
:param str_path: provided file path
78+
:return: file path
79+
:rtype: Path
80+
"""
81+
path_object = Path(str_path)
82+
if path_object.is_file():
83+
path_object = path_object.resolve()
84+
if os.access(path_object, os.R_OK):
85+
return path_object
86+
else:
87+
raise MissingFilePermissionException(file_path=str_path)
88+
elif not path_object.exists():
89+
raise FileNotFoundException(file_path=str_path)
90+
else:
91+
raise ObjectNotAFileException(file_path=str_path)
92+
93+
94+
class IntegrityValidator:
95+
def __init__(self, str_path: str):
96+
self.file = get_file_path_from_str(str_path)
97+
98+
def validate_file_integrity(self):
99+
pass
100+
101+
def get_file_checksum(self):
102+
pass

pyntegrity/exceptions.py

+36
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,39 @@ def __init__(self, detected_hash_algo: str, checksum_str: str):
5757
f'seem valid for the detected algorithm"{detected_hash_algo}"'
5858
)
5959
super().__init__(self.message)
60+
61+
62+
class FileNotFoundException(Exception):
63+
def __init__(self, file_path: str):
64+
"""
65+
Exception raised when the file isn't found using the provided path;
66+
67+
:param file_path: provided file path
68+
"""
69+
self.file_path = file_path
70+
self.message = f'[!] file "{file_path}" not found!'
71+
super().__init__(self.message)
72+
73+
74+
class ObjectNotAFileException(Exception):
75+
def __init__(self, file_path: str):
76+
"""
77+
Exception raised when the provided path isn't a file;
78+
79+
:param file_path: provided file path
80+
"""
81+
self.file_path = file_path
82+
self.message = f'[!] the "{file_path}" is not a valid file!'
83+
super().__init__(self.message)
84+
85+
86+
class MissingFilePermissionException(Exception):
87+
def __init__(self, file_path: str):
88+
"""
89+
Exception raised when the process can't read the file;
90+
91+
:param file_path: provided file path
92+
"""
93+
self.file_path = file_path
94+
self.message = f'[!] the program can not read the file "{file_path}" !'
95+
super().__init__(self.message)

tests/data/test_file.dat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


tests/data/test_file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Just a test file

tests/test_core.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from pathlib import Path
12
from unittest import TestCase
23

34
from pyntegrity.core import detect_hash_algo
5+
from pyntegrity.core import get_file_path_from_str
46
from pyntegrity.core import validate_checksum_str
57

8+
from pyntegrity.exceptions import FileNotFoundException
9+
from pyntegrity.exceptions import ObjectNotAFileException
610
from pyntegrity.exceptions import HashStrNotValidException
711
from pyntegrity.exceptions import HashAlgorithmNotSupportedException
812

@@ -44,3 +48,17 @@ def test_validate_checksum_str_sha256_nok(self):
4448
)
4549
with self.assertRaises(HashStrNotValidException):
4650
validate_checksum_str(invalid_sha256)
51+
52+
53+
class TestGetFileFromStr(TestCase):
54+
def test_get_file_path_from_str_ok(self):
55+
obj = get_file_path_from_str("tests/data/test_file.txt")
56+
self.assertTrue(isinstance(obj, Path))
57+
58+
def test_get_file_path_from_str_nok_is_not_file(self):
59+
with self.assertRaises(ObjectNotAFileException):
60+
get_file_path_from_str("tests/data/")
61+
62+
def test_get_file_path_from_str_nok_not_found(self):
63+
with self.assertRaises(FileNotFoundException):
64+
get_file_path_from_str("tests/data/doesnt_exists.csv")

0 commit comments

Comments
 (0)