Skip to content

Commit 9fa8580

Browse files
committed
document new stuff
1 parent 274b30e commit 9fa8580

7 files changed

Lines changed: 114 additions & 9 deletions

File tree

comprehensiveconfig/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from . import configio
66
from . import spec
7+
from . import validators
78
from .json import JsonWriter
89
from .toml import TomlWriter
910

@@ -147,6 +148,7 @@ def reset_global(cls):
147148
"_ConfigSpecMeta",
148149
"_ConfigSpecABCMeta",
149150
"spec",
151+
"validators",
150152
"configio",
151153
"JsonWriter",
152154
"TomlWriter",

comprehensiveconfig/validators.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import sys
44

5+
# unix
56
NULL_BYTE = "\x00"
6-
7+
# windows
78
DEVICE_PATH_PREFIX = "\\\\?\\"
89
DEVICE_PATH_NORMALIZED_PREFIX = "\\\\.\\"
910
BANNED_WINDOWS_NAMES = (
@@ -36,10 +37,7 @@
3637

3738
def validate_path_unix(path: str) -> bool:
3839
"""Takes in a str filepath and validates whether or not it is valid on unix/linux"""
39-
if NULL_BYTE in path: # only invalid character
40-
return False
41-
42-
return True
40+
return NULL_BYTE not in path
4341

4442

4543
def validate_path_windows(path: str) -> bool:
@@ -109,3 +107,11 @@ def validate_path_sys_aware(path: str) -> bool:
109107
return validate_path_windows(path)
110108
else:
111109
return validate_path_unix(path)
110+
111+
112+
__all__ = [
113+
"validate_path_unix",
114+
"validate_path_windows",
115+
"validate_path_agnostic",
116+
"validate_path_sys_aware",
117+
]

docs/source/fields.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ Module
120120
.. py:attribute:: _holds
121121
:type: str
122122

123+
.. py:attribute:: _regex_pattern
124+
:type: str
125+
123126

124127
.. py:class:: comprehensiveconfig.spec.List[T](default_value: list[T] = [], /, inner_type: AnyConfigField | None = None, **kwargs)
125128
@@ -133,6 +136,57 @@ Module
133136

134137
Might require manual annotation if your default value remains an empty list
135138

139+
.. py:class:: comprehensiveconfig.spec.PathField(default_value: Path | str | _NoDefaultValueT = NoDefaultValue, /, path_type: PathField.PathType = PathField.directory_or_file, path_validator: PathField.PathValidator = PathValidator.agnostic, **kwargs)
140+
141+
:param Path | str | _NoDefaultValueT default_value: The default for this field.
142+
:param PathField.PathType path_type: Whether you want to only accept files, directories, or both
143+
:param PathField.PathValidator path_validator: Determines how it should validate the path string.
144+
145+
.. py:attribute:: _holds
146+
:type: Path
147+
148+
.. py:attribute:: _path_type
149+
:type: PathField.PathType
150+
151+
.. py:attribute:: _path_validator
152+
:type: PathField.PathValidator
153+
154+
.. py:class:: comprehensiveconfig.spec.PathField.PathType
155+
156+
This is an enum representing the types of paths we would like to support in this field.
157+
158+
.. py:data:: directory
159+
160+
We only want paths to point to directories!
161+
162+
.. py:data:: file
163+
164+
We only want paths to point to files!
165+
166+
.. py:data:: directory_or_file
167+
168+
Disables specific checks for what the paths are pointing to.
169+
170+
.. py:class:: comprehensiveconfig.spec.PathField.PathValidator
171+
172+
This is an enum representing the different path validators defined in :py:mod:`comprehensiveconfig.validators`
173+
174+
.. py:data:: windows
175+
176+
Paths should only be valid Windows paths.
177+
178+
.. py:data:: unix
179+
180+
Paths should only be valid Unix Paths
181+
182+
.. py:data:: agnostic
183+
184+
Checks for validity on Unix or Windows (only one needs to be valid).
185+
186+
.. py:data:: current_system
187+
188+
Uses the validator corresponding to the current current system.
189+
136190
.. py:class:: comprehensiveconfig.spec.Table[K, V](default_value: dict[K, V] = {}, /, key_type: AnyConfigField | None = None, value_type: AnyConfigField | None = None, **kwargs)
137191
138192
:param dict[K, V] default_value: The default value of the field. This always default to an empty dict (required for static type checking)

docs/source/globaltoc.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
:maxdepth: 2
2626
:caption: Utilities:
2727

28-
utilities.rst
28+
utilities.rst
29+
validators.rst

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ loading as well as complex validators for incoming configuration values.
4141
:caption: Utilities:
4242

4343
utilities.rst
44+
validators.rst
4445

4546
Module
4647
********

docs/source/validators.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Validators
2+
===========
3+
4+
This file primarily contains various validators used throughout the spec.
5+
6+
.. py:currentmodule:: comprehensiveconfig.validators
7+
8+
9+
Module
10+
********
11+
12+
.. py:function:: validate_path_unix(path: str) -> bool
13+
14+
This function takes a string path and validates whether or not it would be valid on a Unix system.
15+
This is a simple check to see if the path contains a null byte (:code:`\x00`).
16+
17+
:param str path: The path as a :bold:`string`. Path objects do not work here!
18+
19+
20+
.. py:function:: validate_path_windows(path: str) -> bool
21+
22+
This function takes a string path and validates whether or not it would be valid on a windows system.
23+
This is a muchmore complex function than the one for Unix systems.
24+
25+
:param str path: The path as a :bold:`string`. Path objects do not work here!
26+
27+
28+
.. py:function:: validate_path_agnostic(path: str) -> bool
29+
30+
This function checks if a path would be valid on Unix OR Windows. It first runs the check for Unix (its less expensive) before then checking for validity on Windows.
31+
32+
:param str path: The path as a :bold:`string`. Path objects do not work here!
33+
34+
.. py:function:: validate_path_sys_aware(path: str) -> bool
35+
36+
Another path validator function but this one determines the system you are currently on before choosing which validator to use.
37+
38+
:param str path: The path as a :bold:`string`. Path objects do not work here!

readme.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ A simple configuration library that lets you create a pydantic-like model for yo
1313
- [x] Supports static type checking
1414
- [x] toml writer
1515
- [x] json writer
16-
- [x] Number Fields
17-
- [x] Text Fields (with regex filtering)
16+
- [x] Number fields
17+
- [x] Text fields (with regex filtering)
18+
- [x] File/Folder path fields (`PathField`)
19+
- [x] Ability to change validator (unix/linux, windows, agnostic, and current system)
20+
- [x] Validate for only files, folder, or accept both.
1821
- [x] List fields
1922
- [x] Table fields
2023
- [x] TableSpec (Model) fields
2124
- [x] Sections
2225
- [x] Include doc comments in Section
2326
- [x] auto loading
2427
- [x] initialize default config (with auto loader)
25-
- [ ] yaml writer
28+
- [N/a] yaml writer (This will likely be moved to a different library as an extension of comprehensiveconfig!)
2629
- [ ] Tests targetting mypy and other static type checkers to ensure EVERYTHING looks good across IDE's
2730
- [x] section list (via a Table field)
2831
- [x] Field type unions (overwriting normal union syntax)

0 commit comments

Comments
 (0)