def is_windows_path(path): win_split = win_splitdrive(path) # Note, that ntpath.splitdrive also deals with UNC paths. In that case # the "drive" wouldn't be a windows drive letter followed by a colon if win_split[0] and win_split[1] and \ win_split[0].endswith(":") and len(win_split[0]) == 2: # seems to be a windows path return True return False
def _guess_ri_cls(ri): """Factory function which would determine which type of a ri a provided string is""" TYPES = { 'url': URL, 'ssh': SSHRI, 'file': PathRI, 'datalad': DataLadRI } # go in exotic mode if this is an absolute windows path win_split = win_splitdrive(ri) # we need a drive and a path, otherwise this could be a false positive if win_split[0] and win_split[1]: # OMG we got something from windows lgr.log(5, "Detected file ri") return TYPES['file'] # We assume that it is a URL and parse it. Depending on the result # we might decide that it was something else ;) fields = URL._pr_to_fields(urlparse(ri)) lgr.log(5, "Parsed ri %s into fields %s" % (ri, fields)) type_ = 'url' # Special treatments # file:///path should stay file: if fields['scheme'] and fields['scheme'] not in {'file'} \ and not fields['hostname']: # dl+archive:... or just for ssh hostname:path/p1 if '+' not in fields['scheme']: type_ = 'ssh' lgr.log(5, "Assuming ssh style ri, adjusted: %s" % (fields,)) if not fields['scheme'] and not fields['hostname']: parts = _split_colon(ri) if fields['path'] and '@' in fields['path'] or len(parts) > 1: # user@host:path/sp1 # or host_name: (hence parts check) # TODO: we need a regex to catch those really, parts check is not suff type_ = 'ssh' elif ri.startswith('//'): # e.g. // or ///path type_ = 'datalad' else: type_ = 'file' if not fields['scheme'] and fields['hostname']: # e.g. //a/path type_ = 'datalad' cls = TYPES[type_] # just parse the ri according to regex matchint ssh "ri" specs lgr.log(5, "Detected %s ri" % type_) return cls
def posixpath(self): if is_windows_path(self.path): win_split = win_splitdrive(self.path) return "/" + win_split[0][0] + win_split[1].replace('\\', '/') else: return self.path