ext/rest.py
changeset 10957 2fdf67ef3341
parent 10212 dccce2be3101
parent 10953 8b8193046600
child 10960 9e64fddebc89
equal deleted inserted replaced
10895:90c55e27aa87 10957:2fdf67ef3341
    32 * `rql-table`, create a table from a RQL query
    32 * `rql-table`, create a table from a RQL query
    33 
    33 
    34 """
    34 """
    35 __docformat__ = "restructuredtext en"
    35 __docformat__ = "restructuredtext en"
    36 
    36 
       
    37 import sys
    37 from itertools import chain
    38 from itertools import chain
    38 from logging import getLogger
    39 from logging import getLogger
    39 from os.path import join
    40 from os.path import join
    40 from urlparse import urlsplit
    41 from urlparse import urlsplit
    41 
    42 
    47 from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape
    48 from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape
    48 
    49 
    49 from cubicweb import UnknownEid
    50 from cubicweb import UnknownEid
    50 from cubicweb.ext.html4zope import Writer
    51 from cubicweb.ext.html4zope import Writer
    51 
    52 
    52 from cubicweb.web.views import vid_from_rset # XXX better not to import c.w.views here...
    53 from cubicweb.web.views import vid_from_rset  # XXX better not to import c.w.views here...
    53 
    54 
    54 # We provide our own parser as an attempt to get rid of
    55 # We provide our own parser as an attempt to get rid of
    55 # state machine reinstanciation
    56 # state machine reinstanciation
    56 
    57 
    57 import re
    58 import re
    65 mimetypes.add_type('text/rest', '.rest')
    66 mimetypes.add_type('text/rest', '.rest')
    66 mimetypes.add_type('text/rest', '.rst')
    67 mimetypes.add_type('text/rest', '.rst')
    67 
    68 
    68 
    69 
    69 LOGGER = getLogger('cubicweb.rest')
    70 LOGGER = getLogger('cubicweb.rest')
       
    71 
    70 
    72 
    71 def eid_reference_role(role, rawtext, text, lineno, inliner,
    73 def eid_reference_role(role, rawtext, text, lineno, inliner,
    72                        options={}, content=[]):
    74                        options={}, content=[]):
    73     try:
    75     try:
    74         try:
    76         try:
    95         ref = refedentity.absolute_url()
    97         ref = refedentity.absolute_url()
    96     set_classes(options)
    98     set_classes(options)
    97     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
    99     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
    98                             **options)], []
   100                             **options)], []
    99 
   101 
       
   102 
   100 def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   103 def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   101     """``:rql:`<rql-expr>``` or ``:rql:`<rql-expr>:<vid>```
   104     """``:rql:`<rql-expr>``` or ``:rql:`<rql-expr>:<vid>```
   102 
   105 
   103     Example: ``:rql:`Any X,Y WHERE X is CWUser, X login Y:table```
   106     Example: ``:rql:`Any X,Y WHERE X is CWUser, X login Y:table```
   104 
   107 
   128     except Exception as exc:
   131     except Exception as exc:
   129         content = 'an error occurred while interpreting this rql directive: %r' % exc
   132         content = 'an error occurred while interpreting this rql directive: %r' % exc
   130     set_classes(options)
   133     set_classes(options)
   131     return [nodes.raw('', content, format='html')], []
   134     return [nodes.raw('', content, format='html')], []
   132 
   135 
       
   136 
   133 def bookmark_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   137 def bookmark_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   134     """``:bookmark:`<bookmark-eid>``` or ``:bookmark:`<eid>:<vid>```
   138     """``:bookmark:`<bookmark-eid>``` or ``:bookmark:`<eid>:<vid>```
   135 
   139 
   136     Example: ``:bookmark:`1234:table```
   140     Example: ``:bookmark:`1234:table```
   137 
   141 
   184         content = view.render()
   188         content = view.render()
   185     except Exception, exc:
   189     except Exception, exc:
   186         content = 'An error occurred while interpreting directive bookmark: %r' % exc
   190         content = 'An error occurred while interpreting directive bookmark: %r' % exc
   187     set_classes(options)
   191     set_classes(options)
   188     return [nodes.raw('', content, format='html')], []
   192     return [nodes.raw('', content, format='html')], []
       
   193 
   189 
   194 
   190 def winclude_directive(name, arguments, options, content, lineno,
   195 def winclude_directive(name, arguments, options, content, lineno,
   191                        content_offset, block_text, state, state_machine):
   196                        content_offset, block_text, state, state_machine):
   192     """Include a reST file as part of the content of this reST file.
   197     """Include a reST file as part of the content of this reST file.
   193 
   198 
   249         return []
   254         return []
   250 
   255 
   251 winclude_directive.arguments = (1, 0, 1)
   256 winclude_directive.arguments = (1, 0, 1)
   252 winclude_directive.options = {'literal': directives.flag,
   257 winclude_directive.options = {'literal': directives.flag,
   253                               'encoding': directives.encoding}
   258                               'encoding': directives.encoding}
       
   259 
   254 
   260 
   255 class RQLTableDirective(Directive):
   261 class RQLTableDirective(Directive):
   256     """rql-table directive
   262     """rql-table directive
   257 
   263 
   258     Example:
   264     Example:
   412                                     # used *at import time*
   418                                     # used *at import time*
   413                 # dunno what's the max, severe is 4, and we never want a crash
   419                 # dunno what's the max, severe is 4, and we never want a crash
   414                 # (though try/except may be a better option...). May be the
   420                 # (though try/except may be a better option...). May be the
   415                 # above traceback option will avoid this?
   421                 # above traceback option will avoid this?
   416                 'halt_level': 10,
   422                 'halt_level': 10,
       
   423                 # disable stupid switch to colspan=2 if field name is above a size limit
       
   424                 'field_name_limit': sys.maxsize,
   417                 }
   425                 }
   418     if context:
   426     if context:
   419         if hasattr(req, 'url'):
   427         if hasattr(req, 'url'):
   420             base_url = req.url()
   428             base_url = req.url()
   421         elif hasattr(context, 'absolute_url'):
   429         elif hasattr(context, 'absolute_url'):