Skip to content

Commit abdbec2

Browse files
committed
enhancement #27: adding example with configuration files
1 parent 2674bed commit abdbec2

File tree

7 files changed

+91
-1
lines changed

7 files changed

+91
-1
lines changed

setup.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ include_package_data = True
1616
package_dir=
1717
=src
1818
install_requires =
19-
numpy
19+
numpy==1.25.1
2020

2121
[options.packages.find]
2222
where=src
2323

24+
[options.package_data]
25+
config_files =
26+
*.json
27+
2428
[options.extras_require]
2529
dev = black==23.12.1
2630
flake8==7.0.0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"socket_to_name": {
3+
"CAM_B": "right",
4+
"CAM_C": "left"
5+
},
6+
"inverted": false,
7+
"fisheye": true,
8+
"mono": true
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"socket_to_name": {
3+
"CAM_B": "right",
4+
"CAM_C": "left"
5+
},
6+
"inverted": false,
7+
"fisheye": true,
8+
"mono": false
9+
}

src/config_files/__init__.py

Whitespace-only changes.

src/example/cam_config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
from importlib.resources import files
3+
from typing import Any, List
4+
5+
6+
class CamConfig:
7+
def __init__(
8+
self,
9+
cam_config_json: str,
10+
) -> None:
11+
self.cam_config_json = cam_config_json
12+
13+
config = json.load(open(self.cam_config_json, "rb"))
14+
self.socket_to_name = config["socket_to_name"]
15+
self.inverted = config["inverted"]
16+
self.fisheye = config["fisheye"]
17+
self.mono = config["mono"]
18+
19+
def to_string(self) -> str:
20+
ret_string = "Camera Config: \n"
21+
ret_string += "Inverted: {}\n".format(self.inverted)
22+
ret_string += "Fisheye: {}\n".format(self.fisheye)
23+
ret_string += "Mono: {}\n".format(self.mono)
24+
25+
return ret_string
26+
27+
28+
def get_config_files_names() -> List[str]:
29+
path = files("config_files")
30+
return [file.stem for file in path.glob("**/*.json")] # type: ignore[attr-defined]
31+
32+
33+
def get_config_file_path(name: str) -> Any:
34+
path = files("config_files")
35+
for file in path.glob("**/*"): # type: ignore[attr-defined]
36+
if file.stem == name:
37+
return file.resolve()
38+
return None

src/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import sys
44

5+
from example.cam_config import CamConfig, get_config_file_path, get_config_files_names
56
from example.celcius import Celsius
67
from example.foo import Foo
78
from example.xterrabot import XTerraBot
@@ -26,6 +27,10 @@ def main(args: argparse.Namespace) -> int:
2627
xt_bot = XTerraBot()
2728
logging.info(xt_bot.get_object_in_gripper_frame())
2829

30+
# usage of data stored in config_files
31+
cam_conf = CamConfig(get_config_file_path(args.config))
32+
logging.info(cam_conf.to_string())
33+
2934
return 0
3035

3136

@@ -37,8 +42,19 @@ def main(args: argparse.Namespace) -> int:
3742
parser.add_argument("--int_param", type=int, default=5)
3843
parser.add_argument("--float_param", type=float, default=2.5)
3944
parser.add_argument("--verbose", action="store_true", default=False)
45+
46+
valid_configs = get_config_files_names()
47+
parser.add_argument(
48+
"--config",
49+
type=str,
50+
required=True,
51+
choices=valid_configs,
52+
help=f"Configutation file name : {valid_configs}",
53+
)
54+
4055
args = parser.parse_args()
4156

57+
# activate the --verbose to see more output
4258
if args.verbose:
4359
logging.basicConfig(level=logging.INFO)
4460

tests/test_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from src.example.cam_config import (
4+
CamConfig,
5+
get_config_file_path,
6+
get_config_files_names,
7+
)
8+
9+
10+
def test_config() -> None:
11+
list_config = get_config_files_names()
12+
for conf in list_config:
13+
c = CamConfig(get_config_file_path(conf))
14+
assert c is not None

0 commit comments

Comments
 (0)