Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from pacstall/feat/config-parser
Browse files Browse the repository at this point in the history
feat: add initial config parser module
  • Loading branch information
wizard-28 authored Mar 27, 2022
2 parents 1f826b7 + 4641886 commit 7ea5b6a
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/codacy-coverage-reporter.yml
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
105 changes: 105 additions & 0 deletions libpacstall/config.py
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)
Loading

0 comments on commit 7ea5b6a

Please sign in to comment.