Changeset 277:04e83a1043d9
- 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
| r276 |
r277 |
|
| 30 | 30 | |
|---|
| 31 | 31 | |
|---|
| | 32 | class SessionError(Exception): |
|---|
| | 33 | pass |
|---|
| | 34 | |
|---|
| | 35 | |
|---|
| 32 | 36 | class Session(object): |
|---|
| 33 | | def __init__(self, params): |
|---|
| | 37 | def __init__(self, params, defaults=None): |
|---|
| 34 | 38 | """ |
|---|
| 35 | 39 | 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`. |
|---|
| 37 | 47 | """ |
|---|
| | 48 | if not params: |
|---|
| | 49 | raise SessionError("parameter list must not be empty") |
|---|
| 38 | 50 | 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 |
|---|
| 39 | 57 | |
|---|
| 40 | 58 | def get_from_url(self, url): |
|---|
| … | … | |
| 52 | 70 | |
|---|
| 53 | 71 | 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). |
|---|
| 55 | 75 | """ |
|---|
| 56 | 76 | query_string = urlparse.urlparse(url)[4] |
|---|
| … | … | |
| 69 | 89 | value = value[0] |
|---|
| 70 | 90 | 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] |
|---|
| 71 | 95 | return result |
|---|
| 72 | 96 | |
|---|
| … | … | |
| 83 | 107 | >>> s.add_to_url("/page", {'a': u"Test"}) |
|---|
| 84 | 108 | '/page?a=Test' |
|---|
| | 109 | |
|---|
| | 110 | If a parameter is set to its default value, don't include it |
|---|
| | 111 | in the URL. |
|---|
| 85 | 112 | """ |
|---|
| 86 | 113 | parsed_url = list(urlparse.urlparse(url)) |
|---|
| … | … | |
| 91 | 118 | if param in params: |
|---|
| 92 | 119 | value = params[param] |
|---|
| | 120 | # don't include defaults |
|---|
| | 121 | if (param in self.defaults) and (self.defaults[param] == value): |
|---|
| | 122 | continue |
|---|
| 93 | 123 | # make sure the value is a list |
|---|
| 94 | 124 | if isinstance(value, basestring): |
|---|
| r220 |
r277 |
|
| 12 | 12 | >>> from websourcebrowser import session |
|---|
| 13 | 13 | |
|---|
| | 14 | Testing the constructor |
|---|
| | 15 | ----------------------- |
|---|
| | 16 | |
|---|
| | 17 | The constructor takes one obligatory and one optional argument. The |
|---|
| | 18 | first is a list of names (strings) which are part of the session. The |
|---|
| | 19 | second argument, `defaults` is a mapping from the names to their |
|---|
| | 20 | default string values. |
|---|
| | 21 | |
|---|
| | 22 | >>> s = session.Session([]) #doctest: +ELLIPSIS |
|---|
| | 23 | Traceback (most recent call last): |
|---|
| | 24 | ... |
|---|
| | 25 | SessionError: ... |
|---|
| | 26 | |
|---|
| | 27 | This is ok:: |
|---|
| | 28 | |
|---|
| | 29 | >>> s = session.Session(['var']) |
|---|
| | 30 | >>> s = session.Session(['var'], {'var': "3"}) |
|---|
| | 31 | |
|---|
| | 32 | Of course, the mapping can only contain names which are in the |
|---|
| | 33 | parameter 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 | |
|---|
| 14 | 41 | Testing the ``get_from_url`` method |
|---|
| 15 | 42 | ----------------------------------- |
|---|
| 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 | | {} |
|---|
| 23 | 43 | |
|---|
| 24 | 44 | The keys are usually strings, the values are always unicode strings, |
|---|
| … | … | |
| 47 | 67 | >>> s.get_from_url("/page?b=2") |
|---|
| 48 | 68 | {'b': u'2'} |
|---|
| | 69 | |
|---|
| | 70 | Unless 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 |
|---|
| 49 | 78 | |
|---|
| 50 | 79 | If a parameter is given multiple times, a list is used as the |
|---|
| … | … | |
| 100 | 129 | on dictionary "order" and module implementations.) |
|---|
| 101 | 130 | |
|---|
| | 131 | If a parameter value matches its default (i. e. compares equal), it's |
|---|
| | 132 | omitted 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 | |
|---|