Resolve given path from relative into absolute form.
(path)
| 289 | |
| 290 | |
| 291 | def normalize_path(path): |
| 292 | """Resolve given path from relative into absolute form.""" |
| 293 | if './' not in path: |
| 294 | return path |
| 295 | |
| 296 | # Normalize the URL by removing ./ and ../ |
| 297 | atoms = [] |
| 298 | for atom in path.split('/'): |
| 299 | if atom == '.': |
| 300 | pass |
| 301 | elif atom == '..': |
| 302 | # Don't pop from empty list |
| 303 | # (i.e. ignore redundant '..') |
| 304 | if atoms: |
| 305 | atoms.pop() |
| 306 | elif atom: |
| 307 | atoms.append(atom) |
| 308 | |
| 309 | newpath = '/'.join(atoms) |
| 310 | # Preserve leading '/' |
| 311 | if path.startswith('/'): |
| 312 | newpath = '/' + newpath |
| 313 | |
| 314 | return newpath |
| 315 | |
| 316 | |
| 317 | #### |