mirror of
https://codeberg.org/privacy1st/de-p1st-monitor
synced 2024-11-21 19:33:18 +01:00
feat: include configuration files in package
This commit is contained in:
parent
a40fb97aca
commit
a9b648f029
@ -21,14 +21,18 @@ classifiers =
|
|||||||
Operating System :: Unix
|
Operating System :: Unix
|
||||||
|
|
||||||
[options]
|
[options]
|
||||||
|
packages = find:
|
||||||
package_dir =
|
package_dir =
|
||||||
= src
|
= src
|
||||||
packages = find:
|
include_package_data = True
|
||||||
python_requires = >=3.6.9
|
python_requires = >=3.10.0
|
||||||
|
|
||||||
[options.packages.find]
|
[options.packages.find]
|
||||||
where = src
|
where = src
|
||||||
|
|
||||||
|
[options.package_data]
|
||||||
|
de.p1st.monitor = data/*.ini
|
||||||
|
|
||||||
[options.entry_points]
|
[options.entry_points]
|
||||||
; https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
|
; https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
|
||||||
console_scripts=
|
console_scripts=
|
||||||
|
@ -2,22 +2,34 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import configparser
|
import configparser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import importlib.resources as importlib_resources
|
||||||
|
|
||||||
_cfg: configparser.ConfigParser | None = None
|
_cfg: configparser.ConfigParser | None = None
|
||||||
|
|
||||||
|
|
||||||
def init_cfg(config_file: Path = None):
|
def init_cfg(config_file: Path = None):
|
||||||
global _cfg
|
global _cfg
|
||||||
|
|
||||||
if _cfg is not None:
|
if _cfg is not None:
|
||||||
raise ValueError('already initialized')
|
raise ValueError('Already initialized.')
|
||||||
|
|
||||||
|
# Use the given config file.
|
||||||
|
|
||||||
|
# Otherwise, use config file from /etc/de-p1st-monitor/.
|
||||||
if config_file is None:
|
if config_file is None:
|
||||||
import socket
|
import socket
|
||||||
hostname = socket.gethostname()
|
hostname: str = socket.gethostname()
|
||||||
config_file = Path(f'/etc/de-p1st-monitor/{hostname}.ini')
|
config_file = Path(f'/etc/de-p1st-monitor/{hostname}.ini')
|
||||||
|
if not config_file.is_file():
|
||||||
|
config_file = None
|
||||||
|
|
||||||
if not config_file.exists():
|
# Otherwise, use packaged config file.
|
||||||
|
if config_file is None:
|
||||||
|
pkg = importlib_resources.files('de.p1st.monitor')
|
||||||
|
config_file = pkg / 'data' / f'{hostname}.ini'
|
||||||
|
if not config_file.is_file():
|
||||||
|
config_file = None
|
||||||
|
|
||||||
|
if config_file is None or not config_file.is_file():
|
||||||
raise Exception(f'Configuration file does not exist! {config_file}')
|
raise Exception(f'Configuration file does not exist! {config_file}')
|
||||||
|
|
||||||
_cfg = configparser.ConfigParser()
|
_cfg = configparser.ConfigParser()
|
||||||
|
Loading…
Reference in New Issue
Block a user