core#
- class namefile.core.FileInfo(stem: str, suffix: None | str = None, tags: list[str] = <factory>, date: None | datetime.date = None, version: None | Version = None)#
Bases:
objectFileInfo ValueObj
we use this class to represent file meta info, such as stem, tags, date, version and suffix, and we can call
name()to generate file name by this meta info.Example
>>> fileinfo = FileInfo('foo', 'txt', ['bar', 'baz'], datetime.date(2020, 1, 1), Version('1.0.0')) >>> str(fileinfo) 'foo-bar-baz.20200101.1.0.0.txt'
- Parameters:
stem (str) – Stem of file, in
__post_init__(), it will be sanitized bysanitize_stem().suffix (str | None) – Suffix of file.
tags (list[str]) – Tags of file, in
__post_init__(), tags will be deduplicated and sorted.date (datetime.date | None) – Date of file.
version (packaging.version.Version | None) – Version of file.
Warning
stemmust not be empty.suffixmust be a valid suffix, which means it must be a string ends with a letter.
- name() str#
Generate file name by this file info.
generate file name by
stem,tags,date,versionandsuffix.__str__()is an alias of this method.Note
if
suffixis None, it will return a directory name, otherwise it will return a file name.if
dateis not None, it will be formatted as%Y%m%dand add to file name.if
versionis not None, it will be formatted asstrand add to file name.if
tagsis not empty, it will be joined by-and add to file name.
Note
this method encode all file meta info to a file name, and
parse()is the decode method.Example
>>> fileinfo = FileInfo('foo', 'txt', ['bar', 'baz'], datetime.date(2020, 1, 1), Version('1.0.0')) >>> fileinfo.name() 'foo-bar-baz.20200101.1.0.0.txt' >>> fileinfo.name() == str(fileinfo) True
- namefile.core.namefile(stem: str, suffix: None | str = None, tags: None | str | Iterable[str] = None, date: None | bool | datetime.date | datetime.datetime = False, version: None | str | Version = None) str#
Helper function to generate file name by
FileInfo.args are same as
FileInfo.Example
>>> fileinfo = namefile('foo', 'txt', ['bar', 'baz'], datetime.date(2020, 1, 1), Version('1.0.0')) >>> str(fileinfo) 'foo-bar-baz.20200101.1.0.0.txt'
- namefile.core.nameparse(file_name: str) FileInfo#
Helper function to parse file name to
FileInfo.Example
>>> fileinfo = nameparse('foo-bar-baz.20200101.1.0.0.txt') >>> fileinfo.stem 'foo' >>> fileinfo.tags ['bar', 'baz']