path.py
1 # This source file is part of the Swift.org open source project 2 # 3 # Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors 4 # Licensed under Apache License v2.0 with Runtime Library Exception 5 # 6 # See http://swift.org/LICENSE.txt for license information 7 # See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 8 # 9 10 import os 11 12 class Path: 13 _path = None 14 def __init__(self, path): 15 if isinstance(path, Path): 16 self._path = path.absolute() 17 elif os.path.isabs(path): 18 self._path = os.path.abspath(path) 19 else: 20 self._path = path 21 22 def relative(self, base=os.getcwd()): 23 return os.path.relpath(self._path, base) 24 25 def absolute(self): 26 return self._path 27 28 @staticmethod 29 def path(path): 30 if path is None: 31 return None 32 else: 33 return Path(path) 34 35 def path_by_appending(self, comps): 36 return Path.path(os.path.join(self._path, comps)) 37 38 def basename(self): 39 return os.path.basename(self._path) 40 41 def extension(self): 42 name, ext = os.path.splitext(self._path) 43 return ext 44 45 def parent(self): 46 path, _ = os.path.split(self._path) 47 return Path(path)