Esempio n. 1
0
File: url.py Progetto: rsms/smisk
 def test_clean_strings(self):
   # Should be unmodified and retain pointers
   raw = 'hello/john'
   escaped = URL.escape(raw)
   self.assertEquals(escaped, raw)
   self.assertEquals(id(escaped), id(raw))
   
   raw = 'hello_john'
   encoded = URL.encode(raw)
   self.assertEquals(encoded, raw)
   self.assertEquals(id(encoded), id(raw))
Esempio n. 2
0
File: url.py Progetto: rsms/smisk
 def test_decompose_query(self):
   u = URL('http://a/?email=foo%[email protected]&&stale_key&&mos=abc&mos=123&&&')
   q = URL.decompose_query(u.query)
   self.assertEquals(q['email'], "[email protected]")
   self.assertEquals(q['stale_key'], None)
   self.assertEquals(q['mos'], ['abc', '123'])
   self.assertContains(q.keys(), ['email', 'stale_key', 'mos'])
Esempio n. 3
0
 def _call_leaf(self, args, params):
   '''
   Resolves and calls the appropriate leaf, passing args and params to it.
   
   :returns: Response structure or None
   :rtype:   dict
   '''
   # Add Content-Location response header if data encoding was deduced through
   # TCN or requested with a non-standard URI. (i.e. "/hello" instead of "/hello/")
   if self.response.serializer and not self.response.format:
     if self.destination.uri:
       path = URL.escape(self.request.cn_url.path) + '.' + self.response.serializer.extensions[0]
       self.response.headers.append('Content-Location: ' + \
         self.request.cn_url.to_s(scheme=0,user=0,host=0,port=0,path=path))
     # Always add the vary header, because we do (T)CN
     self.response.headers.append('Vary: Accept-Charset, Accept')
   else:
     # Always add the vary header, because we do (T)CN. Here we know this
     # request does not negotiate accept type, as response type was explicitly
     # set by the client, so we do not include "accept".
     self.response.headers.append('Vary: Accept-Charset')
   
   # Call leaf
   log.debug('calling destination %r with args %r and params %r', self.destination, args, params)
   if self._leaf_filter is not None:
     return self._leaf_filter(*args, **params)
   else:
     return self.destination(*args, **params)
Esempio n. 4
0
File: url.py Progetto: rsms/smisk
 def test_to_s_2_port(self):
   u = URL('http://fisk.tld:1983/some/path')
   self.assertEquals(u.to_s(port=0), 'http://fisk.tld/some/path')
   self.assertEquals(u.to_s(port80=0), 'http://fisk.tld:1983/some/path')
   self.assertEquals(u.to_s(port=0, port80=1), 'http://fisk.tld/some/path')
   u = URL('http://fisk.tld:80/some/path')
   self.assertEquals(u.to_s(port=0), 'http://fisk.tld/some/path')
   self.assertEquals(u.to_s(port80=0), 'http://fisk.tld/some/path')
   self.assertEquals(u.to_s(port=0, port80=1), 'http://fisk.tld/some/path')
Esempio n. 5
0
File: string.py Progetto: rsms/smisk
def normalize_url(url, ref_base_url=None):
  '''
  :param url:
    An absolute URL, absolute path or relative path.
  :type  url:
    URL | string
  :param ref_base_url:
    Default absolute URL used to expand a path to a full URL.
    Uses ``smisk.core.request.url`` if not set.
  :type  ref_base_url:
    URL
  :rtype:
    URL
  '''
  is_relative_uri = False
  if '/' not in url:
    is_relative_uri = True
    u = URL()
    u.path = url
    url = u
  else:
    url = URL(url) # make a copy so we don't modify the original
  
  if not ref_base_url:
    if Application.current and Application.current.request:
      ref_base_url = Application.current.request.url
    else:
      ref_base_url = URL()
  
  if url.scheme is None:
    url.scheme = ref_base_url.scheme
  if url.user is None:
    url.user = ref_base_url.user
    if url.user is not None and url.password is None:
      url.password = ref_base_url.password
  if url.host is None:
    url.host = ref_base_url.host
  if url.port == 0:
    url.port = ref_base_url.port
  if is_relative_uri:
    base_path = ref_base_url.path
    if not base_path:
      base_path = '/'
    elif not base_path.endswith('/'):
      base_path = base_path + '/'
    url.path = base_path + url.path
  
  return url
Esempio n. 6
0
File: string.py Progetto: rsms/smisk
def tokenize_path(path):
  '''Deconstruct a URI path into standardized tokens.
  
  :param path: A pathname
  :type  path: string
  :rtype: list'''
  tokens = []
  for tok in strip_filename_extension(path).split('/'):
    tok = URL.decode(tok)
    if len(tok):
      tokens.append(tok)
  return tokens
Esempio n. 7
0
File: url.py Progetto: rsms/smisk
 def test_encode_decode_string_type(self):
   self.assertEquals(type(URL.encode(u"[email protected]")), type(u"foo%[email protected]"))
   self.assertEquals(type(URL.encode("[email protected]")), type("foo%[email protected]"))
   self.assertEquals(type(URL.escape(u"[email protected]")), type(u"foo%[email protected]"))
   self.assertEquals(type(URL.escape("[email protected]")), type("foo%[email protected]"))
   self.assertEquals(type(URL.decode(u"foo%[email protected]")), type(u"[email protected]"))
   self.assertEquals(type(URL.decode("foo%[email protected]")), type("[email protected]"))
Esempio n. 8
0
File: url.py Progetto: rsms/smisk
 def test_to_s_4(self):
   u = URL('http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme='ftp'), 
     'ftp://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(user='bob'),
     'http://bob:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(password='bob'),
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(host='bob'),
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(port=123),
     'http://john:[email protected]:123/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(user=0, path='/internets'),
     'http://fisk.tld:1983/internets?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(query='grekisk_afton=yes'),
     'http://john:[email protected]:1983/some/path.ext?grekisk_afton=yes#chapter5')
   self.assertEquals(u.to_s(fragment='m0'),
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#m0')
Esempio n. 9
0
File: url.py Progetto: rsms/smisk
 def test_decompose_query_decode(self):
   # explicitly decode iso-8859-1 text:
   u = URL('http://a/?name=%E5%E4%F6')
   q = URL.decompose_query(u.query, charset='latin_1', tolerant=False)
   self.assertTrue(isinstance(q['name'], unicode))
   self.assertEquals(q['name'], u'\xe5\xe4\xf6')
   
   # explicitly decode utf-8 text:
   u = URL('http://a/?name=%C3%A5%C3%A4%C3%B6%EF%A3%BF')
   q = URL.decompose_query(u.query, charset='utf-8')
   self.assertTrue(isinstance(q['name'], unicode))
   self.assertEquals(q['name'], u'\xe5\xe4\xf6\uf8ff')
   
   # fail to decode iso-8859-1 as utf-8 (tolerant=False):
   u = URL('http://a/?name=%E5%E4%F6')
   self.assertRaises(UnicodeDecodeError, 
     lambda: URL.decompose_query(u.query, charset='utf-8', tolerant=False))
   # repeating the above with tolerant=True (default value) should implicitly 
   # use the latin-1 charset:
   q = URL.decompose_query(u.query, charset='utf-8')
   self.assertTrue(isinstance(q['name'], unicode))
   self.assertEquals(q['name'], u'\xe5\xe4\xf6')
Esempio n. 10
0
File: url.py Progetto: rsms/smisk
 def test_encode_decode(self):
   raw = "http://abc.se:12/mos/jäger/grek land/hej.html?mos=japp&öland=nej#ge-mig/då";
   escaped = URL.escape(raw)
   self.assertEquals(escaped,
     'http%3A//abc.se%3A12/mos/j%C3%A4ger/grek%20land/hej.html'\
     '?mos=japp&%C3%B6land=nej%23ge-mig/d%C3%A5')
   encoded = URL.encode(raw)
   self.assertEquals(encoded,
     'http%3A%2F%2Fabc.se%3A12%2Fmos%2Fj%C3%A4ger%2Fgrek%20land%2Fhej.html%3Fmos%3Djapp'\
     '%26%C3%B6land%3Dnej%23ge-mig%2Fd%C3%A5')
   self.assertEquals(URL.decode(escaped), raw)
   self.assertEquals(URL.decode(encoded), raw)
   self.assertEquals(URL.unescape(escaped), URL.decode(escaped))
   self.assertEquals(URL.decode("foo%[email protected]"), "[email protected]")
Esempio n. 11
0
File: url.py Progetto: rsms/smisk
 def test_to_s_3(self):
   u = URL('http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   
   # meet and greet
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=1, port=1, path=1, query=1, fragment=1),
     'john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=1, port=1, path=1, query=1, fragment=1),
     'http://fisk.tld:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=1, port=1, path=1, query=1, fragment=1),
     'http://[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=0, port=1, path=1, query=1, fragment=1),
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=0, path=1, query=1, fragment=1),
     'http://john:[email protected]/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=0, query=1, fragment=1),
     'http://john:[email protected]:1983?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=1, query=0, fragment=1),
     'http://john:[email protected]:1983/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=1, query=1, fragment=0), 
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no scheme
   self.assertEquals(u.to_s(scheme=0, user=0, password=1, host=1, port=1, path=1, query=1, fragment=1),
     'fisk.tld:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=0, host=1, port=1, path=1, query=1, fragment=1),
     '[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=0, port=1, path=1, query=1, fragment=1),
     'john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=1, port=0, path=1, query=1, fragment=1),
     'john:[email protected]/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=1, port=1, path=0, query=1, fragment=1),
     'john:[email protected]:1983?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=1, port=1, path=1, query=0, fragment=1),
     'john:[email protected]:1983/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=0, user=1, password=1, host=1, port=1, path=1, query=1, fragment=0),
     'john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no user
   self.assertEquals(u.to_s(scheme=1, user=0, password=0, host=1, port=1, path=1, query=1, fragment=1),
     'http://fisk.tld:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=0, port=1, path=1, query=1, fragment=1),
     'http://:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=1, port=0, path=1, query=1, fragment=1),
     'http://fisk.tld/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=1, port=1, path=0, query=1, fragment=1),
     'http://fisk.tld:1983?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=1, port=1, path=1, query=0, fragment=1),
     'http://fisk.tld:1983/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=0, password=1, host=1, port=1, path=1, query=1, fragment=0),
     'http://fisk.tld:1983/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no password
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=0, port=1, path=1, query=1, fragment=1),
     'http://[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=1, port=0, path=1, query=1, fragment=1),
     'http://[email protected]/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=1, port=1, path=0, query=1, fragment=1),
     'http://[email protected]:1983?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=1, port=1, path=1, query=0, fragment=1),
     'http://[email protected]:1983/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=0, host=1, port=1, path=1, query=1, fragment=0),
     'http://[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no host
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=0, port=0, path=1, query=1, fragment=1),
     'http://john:[email protected]/some/path.ext?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=0, port=1, path=0, query=1, fragment=1),
     'http://john:[email protected]:1983?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=0, port=1, path=1, query=0, fragment=1),
     'http://john:[email protected]:1983/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=0, port=1, path=1, query=1, fragment=0),
     'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no port
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=0, path=0, query=1, fragment=1),
     'http://john:[email protected]?arg1=245&arg2=hej%20du#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=0, path=1, query=0, fragment=1),
     'http://john:[email protected]/some/path.ext#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=0, path=1, query=1, fragment=0),
     'http://john:[email protected]/some/path.ext?arg1=245&arg2=hej%20du')
   
   # no path
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=0, query=0, fragment=1),
     'http://john:[email protected]:1983#chapter5')
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=0, query=1, fragment=0),
     'http://john:[email protected]:1983?arg1=245&arg2=hej%20du')
   
   # no query
   self.assertEquals(u.to_s(scheme=1, user=1, password=1, host=1, port=1, path=1, query=0, fragment=0),
     'http://john:[email protected]:1983/some/path.ext')
Esempio n. 12
0
File: url.py Progetto: rsms/smisk
 def test_to_s_1(self):
   raw = 'http://john:[email protected]:1983/some/path.ext?arg1=245&arg2=hej%20du#chapter5'
   u = URL(raw)
   self.assertEquals(u.to_s(), raw)
   self.assertEquals(str(u), raw)
   self.assertEquals(unicode(u), unicode(raw))
Esempio n. 13
0
File: model.py Progetto: rsms/smisk
 def smisk_mvc_metadata(conf):
   '''This config filter configures the underlying Elixir 
   and SQLAlchemy modules.
   '''
   global log
   conf = conf.get('smisk.mvc.model')
   if not conf:
     return
   
   # Aquire required parameter "url"
   try:
     url = conf['url']
   except KeyError:
     log.warn('missing required "url" parameter in "smisk.mvc.model" config')
     return
   
   # Parse url into an accessible structure
   from smisk.core import URL, Application
   url_st = URL(url)
   
   # Make a copy of the default options
   engine_opts = default_engine_opts.copy()
   
   # MySQL
   if url_st.scheme.lower() == 'mysql':
     if 'poolclass' not in conf:
       conf['poolclass'] = MySQLConnectionPool
       log.debug('MySQL: setting poolclass=%r', conf['poolclass'])
       if 'pool_size' in conf:
         log.debug('MySQL: disabling pool_size')
         del conf['pool_size']
       if 'pool_size' in engine_opts:
         del engine_opts['pool_size']
     elif 'pool_recycle' not in conf and 'pool_recycle' not in engine_opts:
       # In case of user-configured custom pool_class
       conf['pool_recycle'] = 3600
       log.debug('MySQL: setting pool_recycle=%r', conf['pool_recycle'])
   elif 'poolclass' not in conf:
     # Others than MySQL should also use a kind of static pool
     conf['poolclass'] = SingleProcessPool
   
   # Demux configuration
   elixir_opts = {}
   for k,v in conf.items():
     if k.startswith('elixir.'):
       elixir_opts[k[7:]] = v
     elif k != 'url':
       engine_opts[k] = v
   
   # Apply Elixir default options
   if elixir_opts:
     log.info('applying Elixir default options %r', elixir_opts)
     # We apply by iteration since options_defaults is not 
     # guaranteed to be a real dict.
     for k,v in elixir_opts.items():
       options_defaults[k] = v
   
   # Mask out password, since we're logging this
   if url_st.password:
     url_st.password = '***'
   
   def rebind_model_metadata():
     # Dispose any previous connection
     if metadata.bind and hasattr(metadata.bind, 'dispose'):
       log.debug('disposing old connection %r', metadata.bind)
       try:
         metadata.bind.dispose()
       except Exception, e:
         if e.args and e.args[0] and 'SQLite objects created in a thread' in e.args[0]:
           log.debug('SQLite connections can not be disposed from other threads'\
             ' -- simply leaving it to the GC')
         else:
           log.warn('failed to properly dispose the connection', exc_info=True)
   
     # Create, configure and bind engine
     if engine_opts:
       log.info('binding to %r with options %r', str(url_st), engine_opts)
     else:
       log.info('binding to %r', str(url_st))
     metadata.bind = sql.create_engine(url, **engine_opts)