-
Notifications
You must be signed in to change notification settings - Fork 0
added a configuration file and library #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
waridh
wants to merge
6
commits into
main
Choose a base branch
from
bach/feature/config_parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d0b39bc
added a configuration file and library
waridh ecc5fd4
Merge branch 'main' into bach/feature/config_parser
waridh 80792a5
updated the ports to match the defined defaults, and updated the test…
waridh a5cce85
added the doc string for the newly added test
waridh d85cede
swapped format on a regular string to an f-string
waridh 4d0e7c9
fixed the other format string and swapped it to an f-string
waridh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,19 @@ | ||
| """This file contains definitions that will be used throughout the entire | ||
| project | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from enum import Enum, auto | ||
|
|
||
| PROJECT_ROOT = Path(__file__).parent | ||
|
|
||
|
|
||
| class Subsystems(Enum): | ||
| """enum of the simulated subsystems.""" | ||
|
|
||
| ADCS = auto() | ||
| EPS = auto() | ||
| IRIS = auto() | ||
| UHF = auto() |
This file contains hidden or 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,21 @@ | ||
| # Definitions Module | ||
|
|
||
| ## Overview | ||
|
|
||
| This module contains immutable definitions that are shared | ||
| throughout the project. Things such as the root path of the | ||
| project directory on any file system, and `enum` that | ||
| represents the different simulated subsystems. | ||
|
|
||
| ## Content | ||
|
|
||
| ### PROJECT_ROOT | ||
|
|
||
| This is a `pathlib.Path` object that contains the path to | ||
| the root of the project. This value is dynamic, as it | ||
| will update between machines. | ||
|
|
||
| ### Subsystems | ||
|
|
||
| This `enum` contains the fixed grouping of subsystems | ||
| supported by this project. |
This file contains hidden or 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,24 @@ | ||
| # Simconfig Module | ||
|
|
||
| ## Overview | ||
|
|
||
| This is a helper library that can read the `simulated_config.ini` | ||
| configuration files. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Obtaining root config | ||
|
|
||
| The following Python snippet is how the configuration | ||
| dictionary can be obtained from the configuration file | ||
| in the root. | ||
|
|
||
| ```python | ||
| from simconfig import get_simulated_config | ||
| from definitions import TEST_PATH, Subsystems | ||
|
|
||
| config_dictionary = get_simulated_config(TEST_PATH, Subsystems.ADCS) | ||
|
|
||
| config_dictionary | ||
| # {"port": "61200"} | ||
| ``` |
This file contains hidden or 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,23 @@ | ||
| # Testing | ||
|
|
||
| ## Dependencies | ||
|
|
||
| | Module name | version | | ||
| | --- | --- | | ||
| | Python | >=3.10 | | ||
| | pytest | | | ||
|
|
||
| ## Running Tests | ||
|
|
||
| To run tests in this repository, navigate to the project root, and run the | ||
| following command: | ||
| ```bash | ||
| pytest | ||
| ``` | ||
|
|
||
| ## Writing Tests | ||
|
|
||
| Simply add a new file with a name that starts with the word `test` in the | ||
| `test` directory. You do not need to import `pytest`, only the module | ||
| that you are trying to test. To get a quick overview of python modules, | ||
| refer to [this document](https://docs.python.org/3.10/tutorial/modules.html). |
This file contains hidden or 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,52 @@ | ||
| """This library provides the tools required to read configuration from the filesystem. | ||
| Using the INI format as it has broad compatabilities. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from configparser import ConfigParser | ||
| from pathlib import Path | ||
|
|
||
| from definitions import Subsystems | ||
|
|
||
|
|
||
| def get_simulated_config( | ||
| config_file_path: Path, subsystem: Subsystems | ||
| ) -> dict[str, str]: | ||
| """This function will read the configuration | ||
| file defined by config_file_path and obtain | ||
| the configuration for the defined subsystem. | ||
| """ | ||
| config = ConfigParser() | ||
| config.read(config_file_path) | ||
|
|
||
| subsystem_name = subsystem.name.lower() | ||
| return dict(config[subsystem_name]) | ||
|
|
||
|
|
||
| def make_fresh_config(file_path: Path) -> None: | ||
| """This function creates a new subsystem_config.ini file, in the case that | ||
| a reset must occur. | ||
|
|
||
| Will create configuration for all the subsystems that are defined in the | ||
| definitions.py file. | ||
| """ | ||
| config = ConfigParser() | ||
| for index, subsystem in enumerate(Subsystems): | ||
| config[subsystem.name.lower()] = _make_default_subsystem_values(index) | ||
|
|
||
| with open(file_path, "w", encoding="utf-8") as configfile: | ||
| config.write(configfile) | ||
|
|
||
|
|
||
| def _make_default_subsystem_values(i: int) -> dict[str, str]: | ||
| """This makes the default port for the simulated subsystem. Takes a port | ||
| offset as parameter. | ||
|
|
||
| NOTE: | ||
| - Left as a function for potential future extensions. | ||
| - The port is in the ephemeral range. | ||
| """ | ||
| sim_port = 61200 + i | ||
|
|
||
| return {"port": str(sim_port)} |
This file contains hidden or 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,12 @@ | ||
| [adcs] | ||
| port = 61200 | ||
|
|
||
| [eps] | ||
| port = 61201 | ||
|
|
||
| [iris] | ||
| port = 61202 | ||
|
|
||
| [uhf] | ||
| port = 61203 | ||
|
|
Empty file.
This file contains hidden or 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,44 @@ | ||
| """Tests for the config reader helper library""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from configparser import ConfigParser | ||
|
|
||
| import definitions | ||
| from simconfig import get_simulated_config, make_fresh_config | ||
|
|
||
| TEST_PATH = definitions.PROJECT_ROOT / "test.ini" | ||
|
|
||
|
|
||
| def test_make_default_simconfig() -> None: | ||
| """Tests if we could make a clean configuration file""" | ||
| checks = tuple(x.name.lower() for x in definitions.Subsystems) | ||
| make_fresh_config(TEST_PATH) | ||
| read_config = ConfigParser() | ||
| read_config.read(TEST_PATH) | ||
|
|
||
| for index, subsystem in enumerate(checks): | ||
| config_port = read_config[subsystem]["port"] | ||
| # There is a hardcoded testing value for the port. At risk of change | ||
| assert int(config_port) == 61200 + index, f"{subsystem}" | ||
|
|
||
| TEST_PATH.unlink() | ||
|
|
||
|
|
||
| def test_obtain_default_ports() -> None: | ||
| """Testing if we could read from the config file, and | ||
| obtain the ports""" | ||
| checks = tuple(x for x in definitions.Subsystems) | ||
| make_fresh_config(TEST_PATH) | ||
| read_config = ConfigParser() | ||
| read_config.read(TEST_PATH) | ||
|
|
||
| for index, subsystem in enumerate(checks): | ||
| config_dict = get_simulated_config(TEST_PATH, subsystem) | ||
| # There is a hardcoded testing value for the port. At risk of change | ||
| assert ( | ||
| "port" in config_dict | ||
| ), f"Missing port for {subsystem.name}, a required field" | ||
| assert int(config_dict["port"]) == 61200 + index, f"{subsystem}" | ||
|
|
||
| TEST_PATH.unlink() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.