Coverage for src / ezcompiler / utils / validators / path_validators.py: 75.00%

12 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 06:49 +0000

1# /////////////////////////////////////////////////////////////// 

2# PATH_VALIDATORS - Path validation utilities 

3# Project: ezcompiler 

4# /////////////////////////////////////////////////////////////// 

5 

6""" 

7Path validators - Validation utilities for file and directory paths. 

8 

9This module provides validation functions for validating file and directory 

10paths, checking for valid characters and structure. 

11""" 

12 

13from __future__ import annotations 

14 

15# /////////////////////////////////////////////////////////////// 

16# IMPORTS 

17# /////////////////////////////////////////////////////////////// 

18# Standard library imports 

19from pathlib import Path 

20 

21# /////////////////////////////////////////////////////////////// 

22# FUNCTIONS 

23# /////////////////////////////////////////////////////////////// 

24 

25 

26def validate_file_path(path: str | Path) -> bool: 

27 """ 

28 Validate a file path. 

29 

30 Args: 

31 path: File path to validate 

32 

33 Returns: 

34 bool: True if path format is valid, False otherwise 

35 

36 Note: 

37 Checks for valid characters and structure, not existence. 

38 

39 Example: 

40 >>> validate_file_path("path/to/file.txt") 

41 True 

42 >>> validate_file_path("invalid<path>") 

43 False 

44 """ 

45 try: 

46 path_obj = Path(path) 

47 # Check if path has valid characters and structure 

48 path_str = str(path_obj) 

49 invalid_chars = ["<", ">", ":", '"', "|", "?", "*"] 

50 return len(path_str) > 0 and not any(char in path_str for char in invalid_chars) 

51 except Exception: 

52 return False 

53 

54 

55def validate_directory_path(path: str | Path) -> bool: 

56 """ 

57 Validate a directory path. 

58 

59 Args: 

60 path: Directory path to validate 

61 

62 Returns: 

63 bool: True if path format is valid, False otherwise 

64 

65 Note: 

66 Uses same validation as file path validation. 

67 

68 Example: 

69 >>> validate_directory_path("path/to/directory") 

70 True 

71 >>> validate_directory_path("invalid<path>") 

72 False 

73 """ 

74 return validate_file_path(path)