Python os.path.rsplit() Examples

The following are code examples for showing how to use os.path.rsplit(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: OGER   Author: OntoGene   File: router.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _load_postfilter(path):
        '''
        Import an external postfiltering function from an arbitrary module.
        '''
        # Get the function name, if defined.
        func = 'postfilter'  # default function name
        try:
            p, f = path.rsplit(':', 1)
        except ValueError:
            pass
        else:
            if f.isidentifier():
                path, func = p, f
        # Import the module and access the respective function.
        if path == 'builtin':
            import oger.post as m
        else:
            from importlib.machinery import SourceFileLoader
            m = SourceFileLoader('postfilter', path).load_module()
        return getattr(m, func) 
Example 2
Project: aliyun-tablestore-tutorials   Author: aexleader   File: package_index.py    Apache License 2.0 6 votes vote down vote up
def _vcs_split_rev_from_url(self, url, pop_prefix=False):
        scheme, netloc, path, query, frag = urlparse.urlsplit(url)

        scheme = scheme.split('+', 1)[-1]

        # Some fragment identification fails
        path = path.split('#',1)[0]

        rev = None
        if '@' in path:
            path, rev = path.rsplit('@', 1)

        # Also, discard fragment
        url = urlparse.urlunsplit((scheme, netloc, path, query, ''))

        return url, rev 
Example 3
Project: alt-src   Author: release-engineering   File: alt_src.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, options):
        self.options = options
        self.logger = logging.getLogger("altsrc")
        self.error_log = None
        self.checkout = None
        self.git_auth_set = False

        self.mmd = None
        self.mmd_parsed = None
        self.src_mmd_parsed = None

        self.logfile = None
        self.workdir = None
        self.lock = None
        self.nvr = "UNKNOWN"
        self.package = "UNKNOWN"

        self.headers = None
        self.release = None
        self.version = None
        self.summary = None

        if self.options.koji:
            if self.options.source.endswith('modulemd.src.txt'):
                # expected source arg: <nvr>:module.src.txt
                self.nvr = self.options.source.rsplit(':', 1)[0]
                self.source_file, self.mmd = self.get_koji_module_src()
                self.rpms_or_module_dir = 'modules'

            elif self.options.source.endswith('src.rpm'):
                self.source_file = self.get_koji_srpm()
                self.rpms_or_module_dir = 'rpms'
            else:
                raise StartupError("Unsupported source: %s" % self.options.source)

        else:
            self.source_file = self.options.source
            self.rpms_or_module_dir = 'rpms'

        if not os.path.isfile(self.source_file):
            raise StartupError, "No such file: %s" % self.source_file 
Example 4
Project: alt-src   Author: release-engineering   File: alt_src.py    GNU General Public License v3.0 5 votes vote down vote up
def for_lookaside(self, path):
        """Determine if a file should go to the lookaside cache

        Return True if file should go to the lookaside
        """
        # there are varying heuristics for what to place in the lookaside
        # Fedora/pyrpkg decides based on extension
        # Centos/nazar decides based on output of the file utility
        # We're using a hybrid approach
        ext = path.rsplit('.')[-1].lower()
        if ext in self.INCLUDE_EXTS:
            return False
        if ext in self.UPLOAD_EXTS:
            return True
        fstat = os.stat(path)
        if fstat.st_size < 1024:
            # the UPLOAD_EXTS check should catch most of what we want in
            # the lookaside, so we'll just include anything small
            return False
        #lastly, see what the file utility says
        cmd = ['file', '--brief', path]
        output, retval = self.get_output(cmd, stderr='keep', fatal=False)
        if retval:
            #nonfatal
            return False
        self.logger.debug("Source file %s: %s", path, output)
        return output.find('text') == -1 
Example 5
Project: bandit-ss   Author: zeroSteiner   File: utils.py    Apache License 2.0 5 votes vote down vote up
def namespace_path_split(path):
    '''Split the namespace path into a pair (head, tail).

    Tail will be the last namespace path component and head will
    be everything leading up to that in the path. This is similar to
    os.path.split.

    :param path: (String) A namespace path.
    :returns: (String, String) A tuple where the first component is the base
              path and the second is the last path component.
    '''
    return tuple(path.rsplit('.', 1)) 
Example 6
Project: exoplanet-ml   Author: google-research   File: prediction_fns.py    Apache License 2.0 5 votes vote down vote up
def _get_step_from_checkpoint_path(path):
  """Extracts the global step from a checkpoint path."""
  split_path = path.rsplit("model.ckpt-", 1)
  if len(split_path) != 2:
    raise ValueError("Unrecognized checkpoint path: {}".format(path))
  return int(split_path[1]) 
Example 7
Project: bandit   Author: PyCQA   File: utils.py    Apache License 2.0 5 votes vote down vote up
def namespace_path_split(path):
    '''Split the namespace path into a pair (head, tail).

    Tail will be the last namespace path component and head will
    be everything leading up to that in the path. This is similar to
    os.path.split.

    :param path: (String) A namespace path.
    :returns: (String, String) A tuple where the first component is the base
              path and the second is the last path component.
    '''
    return tuple(path.rsplit('.', 1)) 
Example 8
Project: chat-room   Author: savithruml   File: chat_server.py    GNU General Public License v2.0 5 votes vote down vote up
def sendfile(sender_username, client, command):
    message = 'File from ' + sender_username + ': '   

    receiver = command[1]                           #Destination clientname
    path = command[2]                               #File path
    receiver_is_logged_in = False
    
    if os.path.isfile(path):
        #print "file exists"                        #Check if file exists 
        a = path.rsplit('/')
        b = a[4]
        #print b
        logging.info("File {} Sent".format(b))


        rd = open(path, "rb")
        data = rd.read()
        #print data
        
        for user_tuple in logged_in_users:
            if user_tuple[0] == receiver:
                #user_tuple[1].send(message)           #Send to user
                user_tuple[1].sendall(data)
                receiver_is_logged_in = True

    else:
        client.sendall('File does not exists.\n')       #Send this if file does not exist

#LOGOUT FUNCTION 
Example 9
Project: kiwi   Author: OSInside   File: schema_parser.py    GNU General Public License v3.0 5 votes vote down vote up
def get_parent_paths(self):
        parents = []
        for path in filter(lambda x: '.' in x, self.paths):
            parent = path.rsplit('.' + self.get_name(), 1)[0]
            if len(parent):
                parents.append(parent)
        return parents 
Example 10
Project: pyecore   Author: pyecore   File: resource.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def can_resolve(self, uri_path, from_resource=None):
        uri_path = Resource.normalize(uri_path)
        fragment = uri_path.rsplit('#', maxsplit=1)
        nb_fragments = len(fragment)
        uri_str = ''
        if nb_fragments == 2:
            uri_str, fragment = fragment
            if uri_str in self.resources:
                return True
        start = from_resource.uri.normalize() if from_resource else '.'
        apath = path.dirname(start)
        uri = URI(path.join(apath, uri_str))
        return uri.normalize() in self.resources 
Example 11
Project: pyecore   Author: pyecore   File: resource.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def resolve(self, uri, from_resource=None):
        upath = URIMapper.translate(Resource.normalize(uri), from_resource)
        uri_str, fragment = upath.rsplit('#', maxsplit=1)
        if uri_str in self.resources:
            root = self.resources[uri_str]
        else:
            start = from_resource.uri.normalize() if from_resource else '.'
            apath = path.dirname(start)
            uri = URI(path.join(apath, uri_str))
            root = self.resources[uri.normalize()]
        if isinstance(root, Resource):
            root_number, fragment = Resource.extract_rootnum_and_frag(fragment)
            root = root.contents[root_number]
        return Resource._navigate_from(fragment, root) 
Example 12
Project: pyecore   Author: pyecore   File: resource.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def split_path(path):
        path = Resource.normalize(path)
        fragment = path.rsplit('#', maxsplit=1)
        if len(fragment) == 2:
            uri, fragment = fragment
        else:
            uri = None
        return uri, fragment 
Example 13
Project: pyecore   Author: pyecore   File: resource.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def resolve(path, registry):
        path = Resource.normalize(path)
        uri, fragment = path.rsplit('#', maxsplit=1)
        epackage = registry[uri]
        return Resource._navigate_from(fragment, epackage) 
Example 14
Project: pyecore   Author: pyecore   File: resource.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _is_external(self, path):
        path = self.normalize(path)
        uri, fragment = (path.rsplit('#', maxsplit=1)
                         if '#' in path else (None, path))
        return uri, fragment