Module API - Anchors
superconf.anchors
Path anchoring and manipulation library for flexible path resolution.
This module provides classes for handling paths with anchor points, allowing for flexible path resolution relative to different base directories. It's particularly useful for configuration management and file organization where paths need to be resolved relative to different root directories.
Main Components
- PathAnchor: Base class for handling directory paths with anchor points
- FileAnchor: Extension of PathAnchor for handling file paths specifically
Key Features
- Path resolution relative to anchor points
- Support for both absolute and relative path modes
- Chain of anchor points for complex path hierarchies
- Clean path normalization
- Flexible path representation
Examples:
Basic path anchoring:
>>> project_dir = "/fake/prj"
>>> root = PathAnchor(project_dir)
>>> conf = PathAnchor("../../common_conf", anchor=root, mode="abs")
>>> inventory = PathAnchor("inventory/", anchor=conf, mode="rel")
>>>
>>> # Get paths in different modes
>>> conf.get_dir() # Returns absolute path
'/fake/common_conf'
>>> inventory.get_dir() # Returns relative path
'../../inventory'
File handling:
>>> root = PathAnchor("/fake/prj")
>>> config_file = FileAnchor("subconf/myfile.yml", anchor=root)
>>> config_file.get_path() # Full path
'/fake/prj/subconf/myfile.yml'
>>> config_file.get_file() # Just filename
'myfile.yml'
>>> config_file.get_dir() # Just directory
'/fake/prj/subconf'
Complex path resolution:
>>> project = PathAnchor("/fake_root/project")
>>> path = PathAnchor("subdir2/../../subdir2/file", anchor=project)
>>> path.get_dir(clean=True) # Normalizes the path
'/fake_root/subdir2/file'
FileAnchor
Bases: PathAnchor
A class representing a file path with optional anchor point and display mode.
This class extends PathAnchor to handle file paths specifically, maintaining separate tracking of directory and filename components.
Attributes:
Name | Type | Description |
---|---|---|
path_dir |
The directory component of the path |
|
path_file |
The filename component of the path |
|
path_anchor |
The anchor point for relative paths |
|
path_mode |
The display mode ('abs' or 'rel') |
Source code in superconf/anchors.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
__init__(path, directory=None, filename=None, mode=None, anchor=None)
Initialize a FileAnchor object.
The path can be specified either as a full path or as separate directory and file components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The complete file path |
required |
directory
|
Optional[str]
|
The directory component. If provided with filename, path parameter is ignored. Defaults to None. |
None
|
filename
|
Optional[str]
|
The filename component. Defaults to None. |
None
|
mode
|
Optional[str]
|
Display mode for path rendering. Can be 'abs' for absolute paths or 'rel' for relative paths. If None, the mode remains unchanged. Defaults to None. |
None
|
anchor
|
Optional[PathAnchor]
|
Another PathAnchor object to use as reference point. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
AssertionError
|
If the provided path components don't match the expected format |
Source code in superconf/anchors.py
get_file()
Get the filename component of the path.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The filename without directory path |
get_path(**kwargs)
Get the complete file path.
This method combines the directory path from get_dir() with the filename. All kwargs are passed to get_dir().
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The complete file path (directory + filename) |
Source code in superconf/anchors.py
PathAnchor
A class representing a path with an optional anchor point and display mode.
This class handles path operations with support for anchored paths, allowing paths to be relative to a specified anchor point. It also supports different display modes (absolute or relative paths).
Attributes:
Name | Type | Description |
---|---|---|
path_anchor |
The anchor point for relative paths |
|
path_dir |
The directory path |
|
path_mode |
The display mode ('abs' or 'rel') |
Source code in superconf/anchors.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
__init__(path, mode=None, anchor=None)
Initialize a PathAnchor object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to use |
required |
mode
|
Optional[str]
|
Display mode for path rendering. Can be 'abs' for absolute paths or 'rel' for relative paths. If None, the mode remains unchanged. Defaults to None. |
None
|
anchor
|
Optional[PathAnchor]
|
Another PathAnchor object to use as reference point. Defaults to None. |
None
|
Source code in superconf/anchors.py
__repr__()
Return a string representation of the PathAnchor object.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representation showing the path, anchor (if present), and mode (if set) |
Source code in superconf/anchors.py
get_dir(mode=None, clean=False, start=None, anchor=None)
Get the directory path according to specified parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
Optional[str]
|
Output path format. Can be 'abs' for absolute path or 'rel' for relative path. If None, uses the object's mode setting. Defaults to None. |
None
|
clean
|
Optional[bool]
|
If True, normalizes the path by resolving '..' and '.' components. Defaults to False. |
False
|
start
|
Optional[str]
|
Base directory for relative path calculation when mode is 'rel'. Defaults to current working directory. |
None
|
anchor
|
Optional[PathAnchor]
|
Override the anchor point for this operation. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The processed directory path |
Raises:
Type | Description |
---|---|
AssertionError
|
If an invalid mode is specified |
Source code in superconf/anchors.py
get_mode(lvl=0)
Get the effective display mode for this path.
If this object has no mode set but has an anchor, it will recursively check the anchor's mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lvl
|
int
|
Current recursion level. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The effective mode ('abs' or 'rel') or None if no mode is set |
Source code in superconf/anchors.py
get_parents(itself=False)
Get a list of all parent anchors in the anchor chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
itself
|
bool
|
Include the current object in the result if True. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
List
|
list[PathAnchor]: List of parent PathAnchor objects, ordered from current to root |
Source code in superconf/anchors.py
get_path(**kwargs)
Get the path using the same parameters as get_dir.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The processed path |