app
Functions/classes for application specific usage
caller
get_caller
The elevaso_spine.app.caller.get_caller() function retrieves the root code file and path. This is helpful if you have an application that references nested modules and you need to know the initiating code path.
For example, you create a python module named json_logger that can be used by multiple python applications/scripts. You create a file called log_setup.py with code checks for a file named log_config.json stored at the application root directory. The module code looks like:
import json
import os
import elevaso_spine
def setup():
_, path = elevaso_spine.app.caller.get_caller() # Get the path of the calling file
log_config_path = os.path.join(path, "log_config.json")
if os.path.exists(log_config_path): # Check if file exists at application path
print(f"File found at {log_config_path}")
with open(log_config_path, "r") as f: # Read file
log_config = json.loads(f) # Load to variable
else:
print(f"No file found at {log_config_path}")
log_config = {}
return log_config
Next you create a python directory called test_app with a file called app.py with the following code:
import json_logger
json_logger.setup()
The directory structure would look like this (assuming it’s in your ~/code/ directory):
1.
2├── json_logger # json_logger Python module
3 └── log_setup.py # Setup logging
4└── test_app # Application code
5 └── app.py # Main application file
If you ran the code as-is, it would print No file found at ~/code/test_app/.
If you add a file to the test_app directory called log_config.json, the directory structure would look like:
1.
2├── json_logger # json_logger Python module
3 └── log_setup.py # Setup logging
4└── test_app # Application code
5 ├── app.py # Main application file
6 └── log_config.json # Configuration Json file for logging
Running the code now would print File found at ~/code/test_app/.