Last active
October 4, 2015 10:53
-
-
Save bencharb/4ee9277c0389481b5358 to your computer and use it in GitHub Desktop.
path helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urlparse | |
| import os | |
| class Path(unicode): | |
| def __init__(self, path): | |
| self.parsed_url = urlparse.urlparse(path) | |
| self.split = os.path.split(self.parsed_url.path) | |
| self.splitext = os.path.splitext(self.parsed_url.path) | |
| super(Path, self).__init__(path) | |
| @property | |
| def fragment(self): | |
| return self.parsed_url.fragment | |
| @property | |
| def query(self): | |
| return self.parsed_url.query | |
| @property | |
| def domain(self): | |
| return self.parsed_url.netloc | |
| @property | |
| def scheme(self): | |
| return self.parsed_url.scheme | |
| @property | |
| def is_uri(self): | |
| return self.scheme != '' | |
| @property | |
| def is_file_with_ext(self): | |
| return self.splitext[1] != '' | |
| @property | |
| def is_path(self): | |
| return self.split[1] == '' | |
| @property | |
| def is_file_no_ext(self): | |
| return self.splitext[1] == '' and self.split[1] != '' | |
| @property | |
| def ext(self): | |
| return self.splitext[1] | |
| @property | |
| def ext_value(self): | |
| val = self.ext | |
| if val is not '': | |
| return self.ext[1:] | |
| return '' | |
| @property | |
| def filename(self): | |
| return self.split[1] | |
| @property | |
| def filename_no_ext(self): | |
| val = self.split[1] | |
| if self.is_file_no_ext: | |
| return val | |
| without_ext = val[:-len(self.ext)] | |
| return without_ext | |
| @property | |
| def path(self): | |
| return self.split[0] | |
| @property | |
| def paths(self): | |
| vals = self.path.split(os.path.sep) | |
| if not self.is_uri: | |
| vals = [self.domain] + vals | |
| vals = filter(lambda v: v is not '', vals) | |
| return vals | |
| @property | |
| def parent_folder(self): | |
| paths = self.paths | |
| if len(paths) > 1 : | |
| return paths[-1] | |
| return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment