forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
45 lines (35 loc) · 1.14 KB
/
Copy pathconfig.py
File metadata and controls
45 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""This module provides the RP To-Do config functionality."""
import configparser
from pathlib import Path
import typer
from rptodo import DB_WRITE_ERROR, DIR_ERROR, FILE_ERROR, SUCCESS, __app_name__
CONFIG_DIR_PATH = Path(typer.get_app_dir(__app_name__))
CONFIG_FILE_PATH = CONFIG_DIR_PATH / "config.ini"
def init_app(db_path: str) -> int:
"""Initialize the application."""
config_code = _init_config_file()
if config_code != SUCCESS:
return config_code
database_code = _create_database(db_path)
if database_code != SUCCESS:
return database_code
return SUCCESS
def _init_config_file() -> int:
try:
CONFIG_DIR_PATH.mkdir(exist_ok=True)
except OSError:
return DIR_ERROR
try:
CONFIG_FILE_PATH.touch(exist_ok=True)
except OSError:
return FILE_ERROR
return SUCCESS
def _create_database(db_path: str) -> int:
config_parser = configparser.ConfigParser()
config_parser["General"] = {"database": db_path}
try:
with CONFIG_FILE_PATH.open("w") as file:
config_parser.write(file)
except OSError:
return DB_WRITE_ERROR
return SUCCESS