fobj
Functions/classes for working with file objects
find_obj
check
The elevaso_spine.fobj.find_obj.check() function will expand the path provided and validate it is a file, returning True or False.
find
The elevaso_spine.fobj.find_obj.find() function will look for a specific file in the path provided and N parent directories where N is provided to the function.
For example, if your directory structure looks like the following
1.
2└── code # Main code directory
3 └── applications # Sub-directory for application code
4 ├── config.json # Configuration file for all applications
5 └── test_app # Test application
6 └── app.py # Main application file
In your app.py you want to load configuration that is global to all other application directories. You create the file config.json and store it in the applications sub-directory. You also want to add support for local projects overriding the global configuration.
The code to your app.py would look like:
import json
import os
import elevaso_spine
def load_config():
file_path = elevaso_spine.fobj.find_obj.find(os.path.dirname(__file__), "config.json")
if file_path is None:
raise Exception("Configuration file not found!")
else:
with open(file_path, "r") as f:
config = json.loads(f)
return config
The elevaso_spine.fobj.find_obj.find() function would first check the base directory where app.py exists (code/applications/test_app/). If no config.json file is found, it will move up one director and search in code/applications/. It will repeat this process until a file is found or the number of search_dirs has been reached.
- elevaso_spine.fobj.find_obj.find(path: str, file_name: str, search_dirs: int = 4) str[source]
Find a file by traversing the parent
- Args:
file_path (str): Starting path
file_name (str): Name of file to find
search_dirs (int, Optional): Number of parent directories to traverse, defaults to 4
- Returns:
str: Path of file found, None if file is not found