This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from pacstall/feat/config-parser
feat: add initial config parser module
- Loading branch information
Showing
5 changed files
with
485 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: codacy-coverage-reporter | ||
|
||
on: ["push"] | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install Python | ||
uses: actions/setup-python@master | ||
with: | ||
python-version: 3.8 | ||
- name: Setup Poetry Environment | ||
run: | | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
poetry install | ||
- name: Run tests | ||
run: | | ||
poetry run coverage run -m pytest . | ||
- name: Generate Coverage Report | ||
run: | | ||
poetry run coverage report -m | ||
poetry run coverage xml | ||
- name: Upload Coverage Report to Codacy | ||
uses: codacy/codacy-coverage-reporter-action@v1 | ||
with: | ||
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} | ||
coverage-reports: coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# ____ __ ____ | ||
# / __ \____ ___________/ /_____ _/ / / | ||
# / /_/ / __ `/ ___/ ___/ __/ __ `/ / / | ||
# / ____/ /_/ / /__(__ ) /_/ /_/ / / / | ||
# /_/ \__,_/\___/____/\__/\__,_/_/_/ | ||
# | ||
# Copyright (C) 2022-present | ||
# | ||
# This file is part of Pacstall | ||
# | ||
# Pacstall is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, version 3 of the License | ||
# | ||
# Pacstall is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Pacstall. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
"""Module for config parsing.""" | ||
|
||
from os import cpu_count, environ | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
from tomli import load | ||
|
||
|
||
class Settings: | ||
""" | ||
Facade for the file settings. | ||
Attributes | ||
---------- | ||
jobs | ||
The number of jobs to use for building. | ||
editor | ||
The editor to use for opening files. | ||
""" | ||
|
||
jobs: int | ||
editor: str | ||
|
||
def __init__(self, raw_config: Dict[str, Any]) -> None: | ||
""" | ||
Initialize the settings. | ||
Parameters | ||
---------- | ||
raw_config | ||
The raw config dictionary. | ||
""" | ||
|
||
settings = raw_config["settings"] | ||
self.jobs = settings.get("jobs", cpu_count()) | ||
|
||
# Loading order: | ||
# 1. `editor` config file value. | ||
# 2. `EDITOR` environment variable. | ||
# 3. `VISUAL` environment variable. | ||
# 4. `sensible-editor` | ||
self.editor = settings.get( | ||
"editor", environ.get("EDITOR", environ.get("VISUAL", "sensible-editor")) | ||
) | ||
|
||
|
||
class Config: | ||
""" | ||
Facade for the config file. | ||
Attributes | ||
---------- | ||
raw_config | ||
The raw config parsed dictionary. | ||
settings | ||
Facade for the config file settings. | ||
""" | ||
|
||
settings: Settings | ||
raw_config: Dict[str, Any] | ||
|
||
def __init__( | ||
self, | ||
config_file: Path = Path("/etc/pacstall/config.toml"), | ||
) -> None: | ||
""" | ||
Initialize the config. | ||
Parameters | ||
---------- | ||
config_file | ||
The config file to parse. | ||
""" | ||
|
||
config_file.touch(exist_ok=True) | ||
|
||
with config_file.open(mode="rb") as file: | ||
raw_config = load(file) | ||
|
||
self.raw_config = raw_config | ||
|
||
self.settings = Settings(raw_config) |
Oops, something went wrong.