Commits

Olemis Lang committed a029230

TracRpc: Bug fixed: Docs not displayed using browser.

Comments (0)

Files changed (2)

 
 # http://trac-hacks.org/ticket/1055
 # Implementing wiki.listLinks() method (as in Wiki RPC specification)
-#t1055/t1055-list_links-r7194.diff
+# TODO: Add timeline links next to attachments
+# t1055/t1055-list_links-r7194.diff
 
 # Make protocols pluggable (including default supported ones)
 # TODO: Types cleanup (Binary + datetime + all method docs)
 # TODO: Make protocol documentation from plugin => HTML docs
 # TODO: Nicer display of enabled/disabled for known protocols
 # TODO: Abstract _send_error() using regular http status codes, and let each make the appropriate type of request
-# TODO: Really want to remove responding to other paths than /rpc and /login/rpc (change new API)
+# TODO: Really want to remove responding to other paths than /rpc and /login/rpc (change new API) 
+#         - Not possible !
 t5437/t5437-protocol_api-r7194.diff
 

t5437/t5437-protocol_api-r7194.diff

 +                    headers={'Content-Type': 'text/plain'})
 +        try:
 +            resp = urllib2.urlopen(req)
-+            self.fail("Excpected urllib2.HTTPError")
++            self.fail("Expected urllib2.HTTPError")
 +        except urllib2.HTTPError, e:
 +            self.assertEquals(e.code, 415)
 +            self.assertEquals(e.msg, "Unsupported Media Type")
 diff --git a/trunk/tracrpc/web_ui.py b/trunk/tracrpc/web_ui.py
 --- a/trunk/tracrpc/web_ui.py
 +++ b/trunk/tracrpc/web_ui.py
-@@ -6,132 +6,67 @@
+@@ -6,305 +6,110 @@
  (c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
  """
  
 -from trac.perm import PermissionError
 -from trac.util.datefmt import utc
 -from trac.util.text import to_unicode
-+from trac.web.api import RequestDone
++from trac.web.api import RequestDone, HTTPUnsupportedMediaType
  from trac.web.main import IRequestHandler
 -from trac.web.chrome import ITemplateProvider, add_stylesheet
 +from trac.web.chrome import ITemplateProvider, add_stylesheet, add_script
 -        req.write(response)
 +        """ Look for available protocols serving at requested path and
 +            content-type. """
++        req.args['protocol'] = None    # Precaution ... just in case
 +        content_type = req.get_header('Content-Type') or 'text/html'
 +        must_handle_request = False
 +        for protocol in self.protocols:
 +        return must_handle_request
  
      def process_request(self, req):
--
 +        protocol = req.args.get('protocol', None)
-         content_type = req.get_header('Content-Type') or 'text/html'
--        if not self.content_type_re.match(content_type):
--            # Dump RPC functions
++        content_type = req.get_header('Content-Type') or 'text/html'
 +        if protocol:
 +            # Perform the method call
 +            protocol.rpc_process(req, content_type)
 +            raise RequestDone
-+        elif not content_type.startswith('text/html'):
++        elif accepts_mimetype(req, 'text/html'):
++            return self._dump_docs(req)
++        else:
 +            # Attempt at API call gone wrong. Raise a plain-text 415 error
 +            req.send_response(UNSUPPORTED_MEDIA_TYPE)
 +            body = "No protocol matching Content-Type '%s' at path '%s'." % (
 +                                                content_type, req.path_info)
-+            req.send_header('Content-Type', 'text/plain')
++            body = body.encode('utf-8')
++            self.log.warning(body)
++            req.send_header('Content-Type', 'text/plain;utf-8')
 +            req.send_header('Content-Length', len(body))
-+            req.write(body.encode('utf-8'))
++            req.write(body)
 +            raise RequestDone
-+        else: 
-+            # Dump RPC documentation
-             req.perm.require('XML_RPC') # Need at least XML_RPC
-             namespaces = {}
-             for method in XMLRPCSystem(self.env).all_methods(req):
-@@ -139,172 +74,37 @@
-                 if namespace not in namespaces:
-                     namespaces[namespace] = {
-                         'description' : wiki_to_oneliner(
+ 
+-        content_type = req.get_header('Content-Type') or 'text/html'
+-        if not self.content_type_re.match(content_type):
+-            # Dump RPC functions
+-            req.perm.require('XML_RPC') # Need at least XML_RPC
+-            namespaces = {}
+-            for method in XMLRPCSystem(self.env).all_methods(req):
+-                namespace = method.namespace.replace('.', '_')
+-                if namespace not in namespaces:
+-                    namespaces[namespace] = {
+-                        'description' : wiki_to_oneliner(
 -                                            method.namespace_description,
 -                                            self.env, req=req),
-+                                        method.namespace_description,
-+                                        self.env, req=req),
-                         'methods' : [],
-                         'namespace' : method.namespace,
-                         }
-                 try:
-                     namespaces[namespace]['methods'].append(
+-                        'methods' : [],
+-                        'namespace' : method.namespace,
+-                        }
+-                try:
+-                    namespaces[namespace]['methods'].append(
 -                                (method.signature,
 -                                wiki_to_oneliner(
 -                                    method.description, self.env, req=req),
 -                                method.permission))
-+                            (method.signature,
-+                            wiki_to_oneliner(
-+                                method.description, self.env, req=req),
-+                            method.permission))
-                 except Exception, e:
+-                except Exception, e:
 -                    from StringIO import StringIO
-+                    from util import StringIO
-                     import traceback
-                     out = StringIO()
-                     traceback.print_exc(file=out)
+-                    import traceback
+-                    out = StringIO()
+-                    traceback.print_exc(file=out)
 -                    raise Exception('%s: %s\n%s' % (method.name, str(e), out.getvalue()))
-+                    raise Exception('%s: %s\n%s' % (method.name,
-+                                                    str(e), out.getvalue()))
-             add_stylesheet(req, 'common/css/wiki.css')
+-            add_stylesheet(req, 'common/css/wiki.css')
 -            return ('xmlrpclist.html', {'xmlrpc': {'functions': namespaces,
 -                                    'json': json and True or False}}, None)
 -
 -            try: # JSON encoding
 -                self.log.debug("RPC(json) result: %s" % repr(response))
 -                response = json.dumps(response, cls=TracRpcJSONEncoder)
--            except Exception, e:
++    def _dump_docs(self, req):
++        # Dump RPC documentation
++        req.perm.require('XML_RPC') # Need at least XML_RPC
++        namespaces = {}
++        for method in XMLRPCSystem(self.env).all_methods(req):
++            namespace = method.namespace.replace('.', '_')
++            if namespace not in namespaces:
++                namespaces[namespace] = {
++                    'description' : wiki_to_oneliner(
++                                    method.namespace_description,
++                                    self.env, req=req),
++                    'methods' : [],
++                    'namespace' : method.namespace,
++                    }
++            try:
++                namespaces[namespace]['methods'].append(
++                        (method.signature,
++                        wiki_to_oneliner(
++                            method.description, self.env, req=req),
++                        method.permission))
+             except Exception, e:
 -                response = json.dumps(self._json_error(e, r_id=r_id),
 -                                        cls=TracRpcJSONEncoder)
 -        except PermissionError, e:
 -            else:
 -                new_result.append(res)
 -        return new_result
-+            add_stylesheet(req, 'rpc/css/no-theme/ui.all.css')
-+            add_script(req, 'rpc/js/jquery-ui-1.6.custom.min.js')
-+            return ('xmlrpclist.html', 
-+                        {'rpc': {'functions': namespaces,
-+                        'protocols': [p.rpc_info() for p in self.protocols],
-+                        'proto_id' : protocol \
-+                                        and protocol.rpc_info()[0] or None}},
-+                      None)
++                from util import StringIO
++                import traceback
++                out = StringIO()
++                traceback.print_exc(file=out)
++                raise Exception('%s: %s\n%s' % (method.name,
++                                                str(e), out.getvalue()))
++        add_stylesheet(req, 'common/css/wiki.css')
++        add_stylesheet(req, 'rpc/css/no-theme/ui.all.css')
++        add_script(req, 'rpc/js/jquery-ui-1.6.custom.min.js')
++        return ('xmlrpclist.html', 
++                    {'rpc': {'functions': namespaces,
++                    'protocols': [p.rpc_info() for p in self.protocols],
++                    'proto_id' : None}},
++                  None)
  
      # ITemplateProvider
      def get_htdocs_dirs(self):
 +        yield ('rpc', resource_filename(__name__, 'htdocs'))
  
      def get_templates_dirs(self):
-         return [resource_filename(__name__, 'templates')]
+-        return [resource_filename(__name__, 'templates')]
++        yield resource_filename(__name__, 'templates')
 diff --git a/trunk/tracrpc/wiki.py b/trunk/tracrpc/wiki.py
 --- a/trunk/tracrpc/wiki.py
 +++ b/trunk/tracrpc/wiki.py