corrlib/corrlib/initialization.py
Justus Kuhlmann 8162758cec
Some checks failed
Ruff / ruff (push) Waiting to run
Pytest / pytest (3.12) (push) Successful in 1m15s
Pytest / pytest (3.13) (push) Has been cancelled
Mypy / mypy (push) Successful in 1m13s
Pytest / pytest (3.14) (push) Has been cancelled
use pathlib.Path for directories and files
2026-03-23 16:15:55 +01:00

121 lines
3.5 KiB
Python

from configparser import ConfigParser
import sqlite3
import os
from .tracker import save, init
from pathlib import Path
def _create_db(db: Path) -> None:
"""
Create the database file and the table.
Parameters
----------
db: str
Path of the database file.
"""
conn = sqlite3.connect(db)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS backlogs
(id INTEGER PRIMARY KEY,
name TEXT,
ensemble TEXT,
code TEXT,
path TEXT,
project TEXT,
customTags TEXT,
parameters TEXT,
parameter_file TEXT,
created_at TEXT,
updated_at TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS projects
(id TEXT PRIMARY KEY,
aliases TEXT,
customTags TEXT,
owner TEXT,
code TEXT,
created_at TEXT,
updated_at TEXT)''')
conn.commit()
conn.close()
return
def _create_config(path: Path, tracker: str, cached: bool) -> ConfigParser:
"""
Create the config file construction for backlogger.
Parameters
----------
path: str
The path of the libaray to create.
tracker: str
Type of the tracker to use for the library (only DataLad is supported at the moment).
cached: bool
Whether or not the library will create a cache folder for multiple reads when downloaded.
Returns
-------
config: ConfigParser
Cpnfig parser with the default configuration printed.
"""
config = ConfigParser()
config['core'] = {
'version': '1.0',
'tracker': tracker,
'cached': str(cached),
}
config['paths'] = {
'db': 'backlogger.db',
'projects_path': 'projects',
'archive_path': 'archive',
'toml_imports_path': 'toml_imports',
'import_scripts_path': 'import_scripts',
}
return config
def _write_config(path: Path, config: ConfigParser) -> None:
"""
Write the config file to disk.
Parameters
----------
path: str
The path of the libaray to create.
config: ConfigParser
The configuration to be used as a ConfigParser, e.g. generated by _create_config.
"""
with open(os.path.join(path, '.corrlib'), 'w') as configfile:
config.write(configfile)
return
def create(path: Path, tracker: str = 'datalad', cached: bool = True) -> None:
"""
Create folder of backlogs.
Parameters
----------
path: str
The path at which the library will be created.
tracker: str, optional
The tracker to use for the library. The delauft is DataLad, which is also the only one that is supported at the moment.
cached: bool, optional
Whether or not hte librarby will be cached. By default, it does cache already read entries.
"""
config = _create_config(path, tracker, cached)
init(path, tracker)
_write_config(path, config)
_create_db(path / config['paths']['db'])
os.chmod(path / config['paths']['db'], 0o666)
os.makedirs(path / config['paths']['projects_path'])
os.makedirs(path / config['paths']['archive_path'])
os.makedirs(path / config['paths']['toml_imports_path'])
os.makedirs(path / config['paths']['import_scripts_path'] / 'template.py')
with open(path / ".gitignore", "w") as fp:
fp.write(".cache")
fp.close()
save(path, message="Initialized correlator library")
return