Package: dirct
dirctA dict that reflects the contents of a directory.InstallationYou can install this package with pip.$pipinstalldirctLinksUsageBasic usage>>>fromdirctimportDirct>>>d=Dirct("path/to/directory")>>>fromtypingimportMapping>>>isinstance(d,Mapping)TrueLoading filesThe directory may contain any number of files and subdirectories. Files ending in.toml,.json,.yaml, and.ymlare parsed correctly according to their respective formats, and all other files are read as text if it can be decoded or as bytes otherwise.You can also pass custom loader callables to theloadersparameter of theDirctconstructor. A loader callable takes thePathto a file and returns the value that file represents. TheDEFAULT_LOADERSconstant contains loaders for the aforementioned file formats.fromdirctimportDirct,DEFAULT_PARSERSdefcsv_loader(path:Path)->Sequence[Mapping[str,str]]:withpath.open()asf:returntuple(csv.DictReader(f))Dirct("path/to/directory",loaders=(csv_loader,*DEFAULT_LOADERS))Loaders are tried in order. If a loader raises anUnsupportedFileErrorthen the next loader is tried. If a loader raises any other type ofExceptionit is propagated. If no loader can load a file, then anUnsupportedFileErroris raised.Example directoryConsider the following directorypath/to/directory/that contains data about a video game.path/to/directory/
├── __self__.toml
├── publisher.toml
├── version
└── levels/
	├── castle.lvl.json
	├── dungeon.lvl.json
	└── forest.lvl.jsonCreating aDirctwith this directory would produce a mapping with the following structure.>>>d=Dirct("path/to/directory").to_dict(){"name":"Dungeons, Dungeons, and More Dungeons",# From __self__.toml"release_date":datetime.date(2021,1,1),# From __self__.toml"version":"1.0.0","publisher.toml":{"name":"Probabilitor the Annoying","founded":2015},"levels":{"castle.lvl.json":{"name":"Castle","enemies":["goblin","orc","ogre"]},"dungeon.lvl.json":{"name":"Dungeon","enemies":["skeleton","zombie","ghost"]},"forest.lvl.json":{"name":"Forest","enemies":["wolf","bear","dragon"]}}}Mapping files to/from keysConverting between file names and dictionary keys is handled by aKeyMapperobject, which can be passed to thekey_mapperargument of theDirctconstructor.Exact key mapper (default)The exact key mapper only recognizes keys that exactly match the file/directory names.>>>fromdirctimportExactKeyMapper>>>d=Dirct("path/to/directory",key_mapper=ExactKeyMapper())>>>tuple(d.keys())('publisher.toml','version','levels')>>>d["publisher.toml"]# Gets the contents of publisher.toml{'name':'Probabilitor the Annoying','founded':2015}>>>d["publisher"]KeyError:'publisher'>>>d[".publisher.toml"]KeyError:'.publisher.toml'>>>d["publisher.json"]KeyError:'publisher.json'Basename key mapperThe basename key mapper ignores leading dots and all trailing extensions. Ifhidden=Falseis passed to the constructor, then leading dots are not ignored.In case of a name collision when mapping a key to a path, anAmbiguityErroris raised unlessstrict=Falsewas passed to the constructor, in which case a warning is logged and one of the paths is chosen. (For a given directory's contents, the same path will be chosen consistently.)>>>d=Dirct("path/to/directory")# Default key mapper>>>tuple(d.keys())('publisher','version','levels')>>>d["publisher"]# Gets the contents of publisher.toml{'name':'Probabilitor the Annoying','founded':2015}>>>d["publisher.toml"]# Also works{'name':'Probabilitor the Annoying','founded':2015}>>>d[".publisher.foo.json"]# Also works because the leading dot and file extensions are ignored{'name':'Probabilitor the Annoying','founded':2015}__self__A directory may contain a single special file called__self__.*with any known extension. The contents of this file are parsed to a dictionary (anInvalidSelfErroris raised if it can't be parsed as a dictionary) which is then merged with the rest of the directory's contents (explicit files/subdirectories take precedence over__self__.*). This allows you to keep some of the keys in a single file and the rest in separate files or subdirectories.For example,__self__.tomlmay contain the following:name="Dungeons, Dungeons, and More Dungeons"release_date=2021-01-01>>>d=Dirct("path/to/directory")>>>d["name"]'Dungeons, Dungeons, and More Dungeons'