ANameGiver¶
|
Sets filename parts with new values. |
|
Returns a |
Returns this name givers validator providing the file naming convention. |
|
Sets the value of a convention’s filename part. |
|
Disassembles the filename returning ANameGiver. |
- class namefiles.ANameGiver(**filename_parts)¶
A Name Giver is the abstract base class, which can be used to define a custom file naming convention. This can achived subclassing ANameGiver and overriding its classmethod get_filename_validator, which needs to return a jsonschema.IValidator.
Notes
jsonschema has no declaration of IValidator. The called methods within namefiles are declared within JsonschemaValidator as a substitution.
- Parameters
**filename_parts – Filename parts for the implemented file name convention.
Examples
To enable a custom filename convention you subclass
namefiles.ANameGiver
and override thenamefiles.ANameGiver.get_filename_validator()
providing your file naming convention. In this example the naming convention ofnamefiles
is used, which uses the jsonschema draft 7 specification.>>> from doctestprinter import doctest_print >>> from jsonschema import Draft7Validator >>> from namefiles import ANameGiver, get_filename_convention >>> class MyFilenameParts(ANameGiver): ... CUSTOM_VALIDATOR = Draft7Validator(get_filename_convention()) ... @classmethod ... def get_filename_validator(cls) -> FilenameConvention: ... # Put your custom file naming convention (jsonschema) here ... return cls.CUSTOM_VALIDATOR >>> sample_parts = MyFilenameParts.disassemble("A#NAME.txt") >>> sample_parts MyFilenameParts(root_path: ., identifier: A, extension: .txt, sub_id: NAME) >>> str(sample_parts) 'A#NAME.txt' >>> sample_parts.set_parts( ... identifier="Zebra", vargroup=["in", "the"], extension=".zoo" ... ) >>> str(sample_parts) 'Zebra#NAME#_in_the.zoo' >>> sample_parts.set_parts( ... identifier="Z", sub_id="BRA", vargroup="", extension="" ... ) >>> str(sample_parts) 'Z#BRA'
Implements collections.abc.Mapping
>>> converted_into_dict = dict(sample_parts) >>> doctest_print(converted_into_dict, max_line_width=70) {'root_path': '.', 'identifier': 'Z', 'extension': '', 'source_id': '', 'sub_id': 'BRA', 'context': '', 'vargroup': ''}
>>> len(sample_parts) 7 >>> sample_parts["sub_id"] 'BRA'
Disassembling of path and filename
>>> sample_parts = MyFilenameParts.disassemble("/a/path/Z#BRA.txt") >>> sample_parts MyFilenameParts(root_path: /a/path, identifier: Z, extension: .txt, sub_id: BRA) >>> str(sample_parts.to_path()) '/a/path/Z#BRA.txt' >>> str(sample_parts.to_path(root_path="/another/path")) '/another/path/Z#BRA.txt'