"""Tools for setting up and managing PUDL workspaces."""importosfrompathlibimportPathfromtypingimportSelffrompydanticimportDirectoryPath,NewPath,model_validatorfrompydantic_settingsimportBaseSettings,SettingsConfigDictimportpudl.logging_helpers
[docs]classPudlPaths(BaseSettings):"""These settings provide access to various PUDL directories. It is primarily configured via PUDL_INPUT and PUDL_OUTPUT environment variables. Other paths of relevance are derived from these. """
[docs]defcreate_directories(self:Self):"""Create PUDL input and output directories if they don't already exist."""self.input_dir.mkdir(parents=True,exist_ok=True)self.output_dir.mkdir(parents=True,exist_ok=True)
@property
[docs]definput_dir(self)->Path:"""Path to PUDL input directory."""returnPath(self.pudl_input).absolute()
@property
[docs]defoutput_dir(self)->Path:"""Path to PUDL output directory."""returnPath(self.pudl_output).absolute()
@property
[docs]defsettings_dir(self)->Path:"""Path to directory containing settings files."""returnself.input_dir.parent/"settings"
@property
[docs]defdata_dir(self)->Path:"""Path to PUDL data directory."""# TODO(janrous): possibly deprecate this in favor of input_dirreturnself.input_dir
@property
[docs]defpudl_db(self)->str:"""Returns url of locally stored pudl sqlite database."""returnself.sqlite_db_uri("pudl")
[docs]defsqlite_db_uri(self,name:str)->str:"""Returns url of locally stored pudl sqlite database with given name. The name is expected to be the name of the database without the .sqlite suffix. E.g. pudl, ferc1 and so on. """# SQLite URI has 3 slashes - 2 to separate URI scheme, 1 to separate creds# sqlite://{credentials}/{db_path}returnf"sqlite:///{self.sqlite_db_path(name)}"
[docs]defparquet_path(self,table_name:str|None=None)->Path:"""Return path to parquet file for given database and table."""iftable_nameisNone:returnself.output_dir/"parquet"returnself.output_dir/"parquet"/f"{table_name}.parquet"
[docs]defsqlite_db_path(self,name:str)->Path:"""Return path to locally stored SQLite DB file."""returnself.output_dir/f"{name}.sqlite"
[docs]defoutput_file(self,filename:str)->Path:"""Path to file in PUDL output directory."""returnself.output_dir/filename