uri_utils.py :  » Development » SnapLogic » snaplogic » common » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » common » uri_utils.py
#!/usr/bin/env python
# $SnapHashLicense:
# 
# SnapLogic - Open source data services
# 
# Copyright (C) 2008, SnapLogic, Inc.  All rights reserved.
# 
# See http://www.snaplogic.org for more information about
# the SnapLogic project. 
# 
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
# 
# "SnapLogic" is a trademark of SnapLogic, Inc.
# 
# 
# $
# $Id: uri_utils.py 4008 2008-08-22 03:26:34Z dhiraj $

"""
Utilities for working with URIs.
"""

import fnmatch

from snaplogic.common.snap_exceptions import SnapValueError

def _match_segments(pattern_segments, uri_segments):
    for i in xrange(len(pattern_segments)):
        if not fnmatch.fnmatchcase(uri_segments[i], pattern_segments[i]):
            return False
    
    return True
        
def path_glob_match(pattern, uri_list, recursive=False):
    """
    Runs a globbing match over a URI list and returns matches.
    
    A glob pattern much like those used in a shell against a filesystem is possible with this function. It uses
    the fnmatch python module to perform glob matches on the segments of a URI path. Like filesystem globbing,
    the glob pattern will not span URI segments as divided by the "/" character. For example the pattern "/foo*" could
    match "/foo" and "/foobar" but not "/foo/bar".
    
    If the recursive flag is given and True, the match will become recursive. If a pattern matches a prefix of segments
    in a URI and the recursive flag is True, that URI will be included. With the flag, The pattern "/foo*"
    would match "/foo", "/foobar", and "/foo/bar".
    
    @param pattern: A glob pattern.
    @type pattern: str
    
    @param uri_list: List of URIs to perform search against.
    @type uri_list: sequence
    
    @param recursive: A flag indicating if recursive pattern matching should be used.
    @type recursive: bool
    
    @return: List of URIs that match glob pattern.
    @rtype: list
    
    @raise SnapValueError: The pattern or uri_list are invalidly formatted.
    
    """
    if not pattern.startswith('/'):
        raise SnapValueError("URI path pattern '%s' must begin with '/'." % pattern)
    
    include_all = recursive and pattern == '/'
    
    # Chop off the first segment that will just be the empty string
    pattern_segments = pattern.split('/')[1:]
    matched_uris = []
    for uri in uri_list:
        if not uri.startswith('/'):
            raise SnapValueError("URI path '%s' must begin with '/'." % uri)
        
        if include_all:
            matched_uris.append(uri)
        else:
            # Chop leading empty string again.
            uri_segments = uri.split('/')[1:]
            
            # If the recursive flag is not set, the number of segments in the pattern and the URI must match.
            # With the recursive flag, the number of URI segments must be >= the number of pattern segments
            if recursive:
                if _match_segments(pattern_segments, uri_segments):
                    matched_uris.append(uri)
            else:
                if len(uri_segments) == len(pattern_segments) and _match_segments(pattern_segments, uri_segments):
                    matched_uris.append(uri)
                     
    return matched_uris

def extract_comp_name_from_uri(uri):
    """
    Extract component name from URI.
    
    This is a very application specific method used both by CC and server. It parses URIs to extract component
    name from them.
    
    @param uri: The URI being parsed
    @type uri:  str
    
    @return: component name
    @rtype:  str
    
    """
    if uri.endswith("/"):
        uri = uri[:-1]
        
    slist = uri.split("/")
    if len(slist) <= 1:
        # This cannot be a valid URI
        raise SnapException("URI for component is invalid")
    comp_name = slist[-1:][0]
    
    return comp_name
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.