22import os
33import pathlib
44from functools import wraps
5+ from flask import current_app
56
67import 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