Changeset 277:04e83a1043d9

Show
Ignore:
Timestamp:
2007-08-25 18:45:06 (1 year ago)
Author:
Stefan Schwarzer <sschwarzer@sschwarzer.net>
branch:
default
Message:
When parsing an URL and a parameter isn't present in the URL, set it
to its default if it was given upon construction.

If a parameter is set to its default value, don't include it in the
URL.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • session.py

    r276 r277  
    3030 
    3131 
     32class SessionError(Exception): 
     33    pass 
     34 
     35 
    3236class Session(object): 
    33     def __init__(self, params): 
     37    def __init__(self, params, defaults=None): 
    3438        """ 
    3539        Initialize the `Session` instance by providing a list of 
    36         parameter names as argument `params`. 
     40        parameter names as argument `params`. If the list is empty, 
     41        raise a `SessionError`. 
     42 
     43        If the argument `defaults` is given, it must be a mapping from 
     44        parameter names to their default string values. If the mapping 
     45        contains a name, which isn't in the list `params`, raise a 
     46        `SessionError`. 
    3747        """ 
     48        if not params: 
     49            raise SessionError("parameter list must not be empty") 
    3850        self.params = params 
     51        if defaults is None: 
     52            defaults = {} 
     53        for name in defaults: 
     54            if name not in params: 
     55                raise SessionError("default name isn't present in params list") 
     56        self.defaults = defaults 
    3957 
    4058    def get_from_url(self, url): 
     
    5270 
    5371        If one of the parameter names isn't present in the URL at all, 
    54         the parameter name will not occur in the dictionary. 
     72        the parameter name will not occur in the dictionary, unless 
     73        the name is present in the `defaults` dictionary (see 
     74        `__init__` method). 
    5575        """ 
    5676        query_string = urlparse.urlparse(url)[4] 
     
    6989                    value = value[0] 
    7090                result[param] = value 
     91        # try to use a default if a parameter wasn't set 
     92        for param in self.defaults: 
     93            if param not in result: 
     94                result[param] = self.defaults[param] 
    7195        return result 
    7296 
     
    83107        >>> s.add_to_url("/page", {'a': u"Test"}) 
    84108        '/page?a=Test' 
     109 
     110        If a parameter is set to its default value, don't include it 
     111        in the URL. 
    85112        """ 
    86113        parsed_url = list(urlparse.urlparse(url)) 
     
    91118            if param in params: 
    92119                value = params[param] 
     120                # don't include defaults 
     121                if (param in self.defaults) and (self.defaults[param] == value): 
     122                    continue 
    93123                # make sure the value is a list 
    94124                if isinstance(value, basestring): 
  • test_session.txt

    r220 r277  
    1212    >>> from websourcebrowser import session 
    1313 
     14Testing the constructor 
     15----------------------- 
     16 
     17The constructor takes one obligatory and one optional argument. The 
     18first is a list of names (strings) which are part of the session. The 
     19second argument, `defaults` is a mapping from the names to their 
     20default string values. 
     21 
     22    >>> s = session.Session([])   #doctest: +ELLIPSIS 
     23    Traceback (most recent call last): 
     24        ... 
     25    SessionError: ... 
     26 
     27This is ok:: 
     28 
     29    >>> s = session.Session(['var']) 
     30    >>> s = session.Session(['var'], {'var': "3"}) 
     31 
     32Of course, the mapping can only contain names which are in the 
     33parameter list. Otherwise, a `SessionError` will be raised:: 
     34 
     35    >>> s = session.Session(['var'], {'var2': "7"}) 
     36    ... #doctest: +ELLIPSIS 
     37    Traceback (most recent call last): 
     38        ... 
     39    SessionError: ... 
     40 
    1441Testing the ``get_from_url`` method 
    1542----------------------------------- 
    16  
    17 If no parameters are defined, the resulting dictionary should be 
    18 empty:: 
    19  
    20     >>> s = session.Session([]) 
    21     >>> s.get_from_url("http://localhost/page?a=1&b=2") 
    22     {} 
    2343 
    2444The keys are usually strings, the values are always unicode strings, 
     
    4767    >>> s.get_from_url("/page?b=2") 
    4868    {'b': u'2'} 
     69 
     70Unless there's a default that can be used instead:: 
     71 
     72    >>> s = session.Session(['a', 'b'], defaults={'a': "abc"}) 
     73    >>> params = s.get_from_url("/page?b=2") 
     74    >>> params['a'] == "abc" 
     75    True 
     76    >>> params['b'] == u"2" 
     77    True 
    4978 
    5079If a parameter is given multiple times, a list is used as the 
     
    100129on dictionary "order" and module implementations.) 
    101130 
     131If a parameter value matches its default (i. e. compares equal), it's 
     132omitted from the query string of the constructed URL:: 
     133 
     134    >>> s = session.Session(['a'], defaults={'a': "7"}) 
     135    >>> s.add_to_url("/page?c=1", {'a': "7"}) 
     136    '/page?c=1' 
     137