ext/rest.py
changeset 10960 9e64fddebc89
parent 10708 d5e3fffa9c07
parent 10957 2fdf67ef3341
equal deleted inserted replaced
10956:208c9ac8edbb 10960:9e64fddebc89
    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 
    41 
    41 from six import text_type
    42 from six import text_type
    49 from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape
    50 from logilab.mtconverter import ESC_UCAR_TABLE, ESC_CAR_TABLE, xml_escape
    50 
    51 
    51 from cubicweb import UnknownEid
    52 from cubicweb import UnknownEid
    52 from cubicweb.ext.html4zope import Writer
    53 from cubicweb.ext.html4zope import Writer
    53 
    54 
    54 from cubicweb.web.views import vid_from_rset # XXX better not to import c.w.views here...
    55 from cubicweb.web.views import vid_from_rset  # XXX better not to import c.w.views here...
    55 
    56 
    56 # We provide our own parser as an attempt to get rid of
    57 # We provide our own parser as an attempt to get rid of
    57 # state machine reinstanciation
    58 # state machine reinstanciation
    58 
    59 
    59 import re
    60 import re
    67 mimetypes.add_type('text/rest', '.rest')
    68 mimetypes.add_type('text/rest', '.rest')
    68 mimetypes.add_type('text/rest', '.rst')
    69 mimetypes.add_type('text/rest', '.rst')
    69 
    70 
    70 
    71 
    71 LOGGER = getLogger('cubicweb.rest')
    72 LOGGER = getLogger('cubicweb.rest')
       
    73 
    72 
    74 
    73 def eid_reference_role(role, rawtext, text, lineno, inliner,
    75 def eid_reference_role(role, rawtext, text, lineno, inliner,
    74                        options={}, content=[]):
    76                        options={}, content=[]):
    75     try:
    77     try:
    76         try:
    78         try:
    97         ref = refedentity.absolute_url()
    99         ref = refedentity.absolute_url()
    98     set_classes(options)
   100     set_classes(options)
    99     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
   101     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
   100                             **options)], []
   102                             **options)], []
   101 
   103 
       
   104 
   102 def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   105 def rql_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   103     """``:rql:`<rql-expr>``` or ``:rql:`<rql-expr>:<vid>```
   106     """``:rql:`<rql-expr>``` or ``:rql:`<rql-expr>:<vid>```
   104 
   107 
   105     Example: ``:rql:`Any X,Y WHERE X is CWUser, X login Y:table```
   108     Example: ``:rql:`Any X,Y WHERE X is CWUser, X login Y:table```
   106 
   109 
   130     except Exception as exc:
   133     except Exception as exc:
   131         content = 'an error occurred while interpreting this rql directive: %r' % exc
   134         content = 'an error occurred while interpreting this rql directive: %r' % exc
   132     set_classes(options)
   135     set_classes(options)
   133     return [nodes.raw('', content, format='html')], []
   136     return [nodes.raw('', content, format='html')], []
   134 
   137 
       
   138 
   135 def bookmark_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   139 def bookmark_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
   136     """``:bookmark:`<bookmark-eid>``` or ``:bookmark:`<eid>:<vid>```
   140     """``:bookmark:`<bookmark-eid>``` or ``:bookmark:`<eid>:<vid>```
   137 
   141 
   138     Example: ``:bookmark:`1234:table```
   142     Example: ``:bookmark:`1234:table```
   139 
   143 
   186         content = view.render()
   190         content = view.render()
   187     except Exception as exc:
   191     except Exception as exc:
   188         content = 'An error occurred while interpreting directive bookmark: %r' % exc
   192         content = 'An error occurred while interpreting directive bookmark: %r' % exc
   189     set_classes(options)
   193     set_classes(options)
   190     return [nodes.raw('', content, format='html')], []
   194     return [nodes.raw('', content, format='html')], []
       
   195 
   191 
   196 
   192 def winclude_directive(name, arguments, options, content, lineno,
   197 def winclude_directive(name, arguments, options, content, lineno,
   193                        content_offset, block_text, state, state_machine):
   198                        content_offset, block_text, state, state_machine):
   194     """Include a reST file as part of the content of this reST file.
   199     """Include a reST file as part of the content of this reST file.
   195 
   200 
   251         return []
   256         return []
   252 
   257 
   253 winclude_directive.arguments = (1, 0, 1)
   258 winclude_directive.arguments = (1, 0, 1)
   254 winclude_directive.options = {'literal': directives.flag,
   259 winclude_directive.options = {'literal': directives.flag,
   255                               'encoding': directives.encoding}
   260                               'encoding': directives.encoding}
       
   261 
   256 
   262 
   257 class RQLTableDirective(Directive):
   263 class RQLTableDirective(Directive):
   258     """rql-table directive
   264     """rql-table directive
   259 
   265 
   260     Example:
   266     Example:
   414                                     # used *at import time*
   420                                     # used *at import time*
   415                 # dunno what's the max, severe is 4, and we never want a crash
   421                 # dunno what's the max, severe is 4, and we never want a crash
   416                 # (though try/except may be a better option...). May be the
   422                 # (though try/except may be a better option...). May be the
   417                 # above traceback option will avoid this?
   423                 # above traceback option will avoid this?
   418                 'halt_level': 10,
   424                 'halt_level': 10,
       
   425                 # disable stupid switch to colspan=2 if field name is above a size limit
       
   426                 'field_name_limit': sys.maxsize,
   419                 }
   427                 }
   420     if context:
   428     if context:
   421         if hasattr(req, 'url'):
   429         if hasattr(req, 'url'):
   422             base_url = req.url()
   430             base_url = req.url()
   423         elif hasattr(context, 'absolute_url'):
   431         elif hasattr(context, 'absolute_url'):