コード例 #1
0
ファイル: base.py プロジェクト: scbarber/horriblepoems
 def generate(self, _ignore_req_list=False, _append_slash=False, **kargs):
     """Generate a URL from ourself given a set of keyword arguments
     
     Toss an exception if this
     set of keywords would cause a gap in the url.
     
     """
     # Verify that our args pass any regexp requirements
     if not _ignore_req_list:
         for key in self.reqs.keys():
             val = kargs.get(key)
             if val and not self.req_regs[key].match(self.make_unicode(val)):
                 return False
     
     # Verify that if we have a method arg, its in the method accept list. 
     # Also, method will be changed to _method for route generation
     meth = kargs.get('method')
     if meth:
         if self.conditions and 'method' in self.conditions \
             and meth.upper() not in self.conditions['method']:
             return False
         kargs.pop('method')
     
     routelist = self.routebackwards
     urllist = []
     gaps = False
     for part in routelist:
         if isinstance(part, dict) and part['type'] == ':':
             arg = part['name']
             
             # For efficiency, check these just once
             has_arg = kargs.has_key(arg)
             has_default = self.defaults.has_key(arg)
             
             # Determine if we can leave this part off
             # First check if the default exists and wasn't provided in the 
             # call (also no gaps)
             if has_default and not has_arg and not gaps:
                 continue
                 
             # Now check to see if there's a default and it matches the 
             # incoming call arg
             if (has_default and has_arg) and self.make_unicode(kargs[arg]) == \
                 self.make_unicode(self.defaults[arg]) and not gaps: 
                 continue
             
             # We need to pull the value to append, if the arg is None and 
             # we have a default, use that
             if has_arg and kargs[arg] is None and has_default and not gaps:
                 continue
             
             # Otherwise if we do have an arg, use that
             elif has_arg:
                 val = kargs[arg]
             
             elif has_default and self.defaults[arg] is not None:
                 val = self.defaults[arg]
             
             # No arg at all? This won't work
             else:
                 return False
             
             urllist.append(url_quote(val, self.encoding))
             if has_arg:
                 del kargs[arg]
             gaps = True
         elif isinstance(part, dict) and part['type'] == '*':
             arg = part['name']
             kar = kargs.get(arg)
             if kar is not None:
                 urllist.append(url_quote(kar, self.encoding))
                 gaps = True
         elif part and part[-1] in self.done_chars:
             if not gaps and part in self.done_chars:
                 continue
             elif not gaps:
                 urllist.append(part[:-1])
                 gaps = True
             else:
                 gaps = True
                 urllist.append(part)
         else:
             gaps = True
             urllist.append(part)
     urllist.reverse()
     url = ''.join(urllist)
     if not url.startswith('/'):
         url = '/' + url
     extras = frozenset(kargs.keys()) - self.maxkeys
     if extras:
         if _append_slash and not url.endswith('/'):
             url += '/'
         url += '?'
         fragments = []
         for key in extras:
             if key == 'action' or key == 'controller':
                 continue
             val = kargs[key]
             if isinstance(val, (tuple, list)):
                 for value in val:
                     fragments.append((key, value))
             else:
                 fragments.append((key, val))
             
         url += urllib.urlencode(fragments)
     elif _append_slash and not url.endswith('/'):
         url += '/'
     return url
コード例 #2
0
ファイル: route.py プロジェクト: jamslevy/baseinacase
 def generate_minimized(self, kargs):
     """Generate a minimized version of the URL"""
     routelist = self.routebackwards
     urllist = []
     gaps = False
     for part in routelist:
         if isinstance(part, dict) and part['type'] == ':':
             arg = part['name']
             
             # For efficiency, check these just once
             has_arg = kargs.has_key(arg)
             has_default = self.defaults.has_key(arg)
             
             # Determine if we can leave this part off
             # First check if the default exists and wasn't provided in the 
             # call (also no gaps)
             if has_default and not has_arg and not gaps:
                 continue
                 
             # Now check to see if there's a default and it matches the 
             # incoming call arg
             if (has_default and has_arg) and self.make_unicode(kargs[arg]) == \
                 self.make_unicode(self.defaults[arg]) and not gaps: 
                 continue
             
             # We need to pull the value to append, if the arg is None and 
             # we have a default, use that
             if has_arg and kargs[arg] is None and has_default and not gaps:
                 continue
             
             # Otherwise if we do have an arg, use that
             elif has_arg:
                 val = kargs[arg]
             
             elif has_default and self.defaults[arg] is not None:
                 val = self.defaults[arg]
             
             # No arg at all? This won't work
             else:
                 return False
             
             urllist.append(url_quote(val, self.encoding))
             if has_arg:
                 del kargs[arg]
             gaps = True
         elif isinstance(part, dict) and part['type'] == '*':
             arg = part['name']
             kar = kargs.get(arg)
             if kar is not None:
                 urllist.append(url_quote(kar, self.encoding))
                 gaps = True
         elif part and part[-1] in self.done_chars:
             if not gaps and part in self.done_chars:
                 continue
             elif not gaps:
                 urllist.append(part[:-1])
                 gaps = True
             else:
                 gaps = True
                 urllist.append(part)
         else:
             gaps = True
             urllist.append(part)
     urllist.reverse()
     url = ''.join(urllist)
     return url
コード例 #3
0
    def generate(self, _ignore_req_list=False, _append_slash=False, **kargs):
        """Generate a URL from ourself given a set of keyword arguments
        
        Toss an exception if this
        set of keywords would cause a gap in the url.
        
        """
        # Verify that our args pass any regexp requirements
        if not _ignore_req_list:
            for key in self.reqs.keys():
                val = kargs.get(key)
                if val and not self.req_regs[key].match(unicode(val)):
                    return False

        # Verify that if we have a method arg, its in the method accept list.
        # Also, method will be changed to _method for route generation
        meth = kargs.get('method')
        if meth:
            if self.conditions and 'method' in self.conditions \
                and meth.upper() not in self.conditions['method']:
                return False
            kargs.pop('method')

        routelist = self.routebackwards
        urllist = []
        gaps = False
        for part in routelist:
            if isinstance(part, dict) and part['type'] == ':':
                arg = part['name']

                # For efficiency, check these just once
                has_arg = kargs.has_key(arg)
                has_default = self.defaults.has_key(arg)

                # Determine if we can leave this part off
                # First check if the default exists and wasn't provided in the
                # call (also no gaps)
                if has_default and not has_arg and not gaps:
                    continue

                # Now check to see if there's a default and it matches the
                # incoming call arg
                if (has_default and has_arg) and unicode(kargs[arg]) == \
                    unicode(self.defaults[arg]) and not gaps:
                    continue

                # We need to pull the value to append, if the arg is None and
                # we have a default, use that
                if has_arg and kargs[arg] is None and has_default and not gaps:
                    continue

                # Otherwise if we do have an arg, use that
                elif has_arg:
                    val = kargs[arg]

                elif has_default and self.defaults[arg] is not None:
                    val = self.defaults[arg]

                # No arg at all? This won't work
                else:
                    return False

                urllist.append(url_quote(val, self.encoding))
                if has_arg:
                    del kargs[arg]
                gaps = True
            elif isinstance(part, dict) and part['type'] == '*':
                arg = part['name']
                kar = kargs.get(arg)
                if kar is not None:
                    urllist.append(url_quote(kar, self.encoding))
                    gaps = True
            elif part and part[-1] in self.done_chars:
                if not gaps and part in self.done_chars:
                    continue
                elif not gaps:
                    urllist.append(part[:-1])
                    gaps = True
                else:
                    gaps = True
                    urllist.append(part)
            else:
                gaps = True
                urllist.append(part)
        urllist.reverse()
        url = ''.join(urllist)
        if not url.startswith('/'):
            url = '/' + url
        extras = frozenset(kargs.keys()) - self.maxkeys
        if extras:
            if _append_slash and not url.endswith('/'):
                url += '/'
            url += '?'
            url += urllib.urlencode([(key, kargs[key]) for key in kargs \
                                     if key in extras and \
                                     (key != 'action' or key != 'controller')])
        elif _append_slash and not url.endswith('/'):
            url += '/'
        return url