Normalize a single-path. :returns: The normalized path.
(path: str, parent: str = os.curdir)
| 162 | |
| 163 | |
| 164 | def normalize_path(path: str, parent: str = os.curdir) -> str: |
| 165 | """Normalize a single-path. |
| 166 | |
| 167 | :returns: |
| 168 | The normalized path. |
| 169 | """ |
| 170 | # NOTE(sigmavirus24): Using os.path.sep and os.path.altsep allow for |
| 171 | # Windows compatibility with both Windows-style paths (c:\foo\bar) and |
| 172 | # Unix style paths (/foo/bar). |
| 173 | separator = os.path.sep |
| 174 | # NOTE(sigmavirus24): os.path.altsep may be None |
| 175 | alternate_separator = os.path.altsep or "" |
| 176 | if ( |
| 177 | path == "." |
| 178 | or separator in path |
| 179 | or (alternate_separator and alternate_separator in path) |
| 180 | ): |
| 181 | path = os.path.abspath(os.path.join(parent, path)) |
| 182 | return path.rstrip(separator + alternate_separator) |
| 183 | |
| 184 | |
| 185 | @functools.lru_cache(maxsize=1) |