Skip to content

Commit ac26b1f

Browse files
committed
fix(api): use a more reliable approach to find project root
1 parent ee17524 commit ac26b1f

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ config.yml
1313
compose.yml
1414
.env
1515

16-
.dockerignore
16+
cdata

api/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ managed = true
3131
dev-dependencies = [
3232
"pytest>=8.3.2",
3333
"pytest-mock>=3.14.0",
34+
"ipdb>=0.13.13",
3435
]
3536

3637
[tool.hatch.metadata]

api/requirements-dev.lock

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
-e file:.
1313
aniso8601==9.0.1
1414
# via flask-restful
15+
asttokens==2.4.1
16+
# via stack-data
1517
certifi==2024.7.4
1618
# via requests
1719
charset-normalizer==3.3.2
@@ -20,8 +22,13 @@ click==8.1.7
2022
# via flask
2123
confluent-kafka==2.0.2
2224
# via rkapi
25+
decorator==5.1.1
26+
# via ipdb
27+
# via ipython
2328
environs==9.5.0
2429
# via rkapi
30+
executing==2.0.1
31+
# via stack-data
2532
flask==2.2.3
2633
# via flask-cors
2734
# via flask-restful
@@ -36,8 +43,13 @@ idna==3.7
3643
# via requests
3744
iniconfig==2.0.0
3845
# via pytest
46+
ipdb==0.13.13
47+
ipython==8.26.0
48+
# via ipdb
3949
itsdangerous==2.2.0
4050
# via flask
51+
jedi==0.19.1
52+
# via ipython
4153
jinja2==3.1.4
4254
# via flask
4355
libknot==3.2.5
@@ -47,13 +59,27 @@ markupsafe==2.1.5
4759
# via werkzeug
4860
marshmallow==3.21.3
4961
# via environs
62+
matplotlib-inline==0.1.7
63+
# via ipython
5064
packaging==24.1
5165
# via marshmallow
5266
# via pytest
67+
parso==0.8.4
68+
# via jedi
69+
pexpect==4.9.0
70+
# via ipython
5371
pluggy==1.5.0
5472
# via pytest
73+
prompt-toolkit==3.0.47
74+
# via ipython
5575
psycopg2==2.9.5
5676
# via rkapi
77+
ptyprocess==0.7.0
78+
# via pexpect
79+
pure-eval==0.2.3
80+
# via stack-data
81+
pygments==2.18.0
82+
# via ipython
5783
pytest==8.3.2
5884
# via pytest-mock
5985
pytest-mock==3.14.0
@@ -69,10 +95,20 @@ requests==2.28.2
6995
setuptools==72.1.0
7096
# via gunicorn
7197
six==1.16.0
98+
# via asttokens
7299
# via flask-cors
73100
# via flask-restful
101+
stack-data==0.6.3
102+
# via ipython
103+
traitlets==5.14.3
104+
# via ipython
105+
# via matplotlib-inline
106+
typing-extensions==4.12.2
107+
# via ipython
74108
urllib3==1.26.19
75109
# via requests
110+
wcwidth==0.2.13
111+
# via prompt-toolkit
76112
werkzeug==2.2.2
77113
# via flask
78114
# via rkapi

api/src/rkapi/app/controllers/api/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class MetaVersion(Resource):
99
def get(self):
10-
build = helpers.read_version("pyproject.toml", "version")
10+
build = helpers.read_version()
1111

1212
data = {"build": build}
1313
return response(200, data=data, message="OK")

api/src/rkapi/app/helpers/helpers.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import pathlib
44
from functools import wraps
5+
from flask import current_app
56

67
import yaml
78

@@ -78,21 +79,25 @@ def decorated_function(*args, **kwargs):
7879
return decorated_function
7980

8081

81-
def read_file(other_file_name, filename):
82-
root_dir = pathlib.Path(other_file_name).resolve().parent
83-
path = root_dir.joinpath(filename)
82+
def root_path() -> pathlib.Path:
83+
"""Get root path of the app"""
84+
return pathlib.Path(os.path.dirname(current_app.instance_path))
8485

86+
87+
def read_file(path) -> str | None:
88+
path = pathlib.Path(path)
8589
if path.is_file():
8690
with open(path, "rb") as f:
8791
content = f.read().decode("utf-8")
8892
return content
8993

9094

91-
def read_version(other_file_name, filename):
95+
def read_version() -> str:
9296
"""Read the the current version or build of the app"""
9397
version = ""
9498

95-
version = read_file(other_file_name, filename)
99+
path = root_path().joinpath("version")
100+
version = read_file(path)
96101
if version:
97102
version = version.rstrip()
98103

@@ -102,21 +107,20 @@ def read_version(other_file_name, filename):
102107
return version
103108

104109

105-
def config_file():
110+
def config_file() -> pathlib.Path:
106111
"""Return config file path."""
107-
path = os.environ.get("RESTKNOT_CONFIG_FILE")
112+
path = os.environ.get("RESTKNOT_CONFIG_FILE") # custom path
108113
if not path:
109-
current_path = pathlib.Path(__file__)
110-
path = current_path.parents[2].joinpath("config.yml")
114+
path = root_path().joinpath("config.yml")
111115

112-
is_exists = os.path.exists(path)
113-
if is_exists:
116+
path = pathlib.Path(path)
117+
if path.exists():
114118
return path
115119
else:
116120
raise ValueError(f"Config File Not Found: {path}")
117121

118122

119-
def get_config():
123+
def get_config() -> str:
120124
"""Return config file content."""
121125
file_ = config_file()
122126
config = yaml.safe_load(open(file_))

0 commit comments

Comments
 (0)