cwvreg.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 12 Jul 2010 12:30:36 +0200
branchstable
changeset 5956 1cccf88d6dfe
parent 5886 00a78298d30d
child 5890 141b935a38fc
permissions -rw-r--r--
Added tag cubicweb-version-3.8.7 for changeset a1a334d93439
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5306
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    18
""".. VRegistry:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    19
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    20
The `VRegistry`
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    21
---------------
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    22
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
    23
The `VRegistry` can be seen as a two-level dictionary. It contains
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    24
all dynamically loaded objects (subclasses of :ref:`appobject`) to
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    25
build a |cubicweb| application. Basically:
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    26
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    27
* the first level key returns a *registry*. This key corresponds to the
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    28
  `__registry__` attribute of application object classes
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    29
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    30
* the second level key returns a list of application objects which
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    31
  share the same identifier. This key corresponds to the `__regid__`
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    32
  attribute of application object classes.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    33
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    34
A *registry* holds a specific kind of application objects. There is
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    35
for instance a registry for entity classes, another for views, etc...
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    36
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    37
The `VRegistry` has two main responsibilities:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    38
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    39
- being the access point to all registries
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    40
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    41
- handling the registration process at startup time, and during automatic
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    42
  reloading in debug mode.
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    43
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    44
.. _AppObjectRecording:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    45
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    46
Details of the recording process
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    47
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    48
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    49
.. index::
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    50
   vregistry: registration_callback
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    51
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
    52
On startup, |cubicweb| loads application objects defined in its library
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    53
and in cubes used by the instance. Application objects from the
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    54
library are loaded first, then those provided by cubes are loaded in
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    55
dependency order (e.g. if your cube depends on an other, objects from
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
    56
the dependency will be loaded first). The layout of the modules or packages
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
    57
in a cube  is explained in :ref:`cubelayout`.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    58
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    59
For each module:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    60
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    61
* by default all objects are registered automatically
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    62
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    63
* if some objects have to replace other objects, or have to be
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    64
  included only if some condition is met, you'll have to define a
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    65
  `registration_callback(vreg)` function in your module and explicitly
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    66
  register **all objects** in this module, using the api defined
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
    67
  below.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    68
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    69
.. Note::
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    70
    Once the function `registration_callback(vreg)` is implemented in a module,
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    71
    all the objects from this module have to be explicitly registered as it
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    72
    disables the automatic objects registration.
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    73
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    74
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    75
API for objects registration
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    76
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    77
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    78
Here are the registration methods that you can use in the `registration_callback`
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    79
to register your objects to the `VRegistry` instance given as argument (usually
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    80
named `vreg`):
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    81
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    82
.. automethod:: cubicweb.cwvreg.CubicWebVRegistry.register_all
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    83
.. automethod:: cubicweb.cwvreg.CubicWebVRegistry.register_and_replace
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    84
.. automethod:: cubicweb.cwvreg.CubicWebVRegistry.register
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    85
.. automethod:: cubicweb.cwvreg.CubicWebVRegistry.register_if_interface_found
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    86
.. automethod:: cubicweb.cwvreg.CubicWebVRegistry.unregister
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    87
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    88
Examples:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    89
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    90
.. sourcecode:: python
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    91
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    92
   # web/views/basecomponents.py
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    93
   def registration_callback(vreg):
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    94
      # register everything in the module except SeeAlsoComponent
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    95
      vreg.register_all(globals().values(), __name__, (SeeAlsoVComponent,))
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    96
      # conditionally register SeeAlsoVComponent
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    97
      if 'see_also' in vreg.schema:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    98
          vreg.register(SeeAlsoVComponent)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   100
In this example, we register all application object classes defined in the module
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   101
except `SeeAlsoVComponent`. This class is then registered only if the 'see_also'
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   102
relation type is defined in the instance'schema.
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   103
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   104
.. sourcecode:: python
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   105
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   106
   # goa/appobjects/sessions.py
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   107
   def registration_callback(vreg):
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   108
      vreg.register(SessionsCleaner)
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   109
      # replace AuthenticationManager by GAEAuthenticationManager
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   110
      vreg.register_and_replace(GAEAuthenticationManager, AuthenticationManager)
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   111
      # replace PersistentSessionManager by GAEPersistentSessionManager
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   112
      vreg.register_and_replace(GAEPersistentSessionManager, PersistentSessionManager)
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   113
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   114
In this example, we explicitly register classes one by one:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   115
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   116
* the `SessionCleaner` class
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   117
* the `GAEAuthenticationManager` to replace the `AuthenticationManager`
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   118
* the `GAEPersistentSessionManager` to replace the `PersistentSessionManager`
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   119
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   120
If at some point we register a new appobject class in this module, it won't be
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   121
registered at all without modification to the `registration_callback`
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   122
implementation. The previous example will register it though, thanks to the call
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   123
to the `register_all` method.
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   124
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   125
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   126
.. _Selection:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   127
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   128
Runtime objects selection
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   129
~~~~~~~~~~~~~~~~~~~~~~~~~
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   130
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   131
Now that we have all application objects loaded, the question is : when
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   132
I want some specific object, for instance the primary view for a given
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   133
entity, how do I get the proper object ? This is what we call the
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   134
**selection mechanism**.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   135
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   136
As explained in the :ref:`Concepts` section:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   137
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   138
* each application object has a **selector**, defined by its
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   139
  `__select__` class attribute
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   140
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   141
* this selector is responsible to return a **score** for a given context
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   142
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   143
  - 0 score means the object doesn't apply to this context
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   144
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   145
  - else, the higher the score, the better the object suits the context
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   146
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   147
* the object with the highest score is selected.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   148
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   149
.. Note::
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   150
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   151
  When no single object has the highest score, an exception is raised in development
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   152
  mode to let you know that the engine was not able to identify the view to
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   153
  apply. This error is silenced in production mode and one of the objects with
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   154
  the highest score is picked.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   155
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   156
  In such cases you would need to review your design and make sure
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   157
  your selectors or appobjects are properly defined. Such an error is
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   158
  typically caused by either forgetting to change the __regid__ in a
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   159
  derived class, or by having copy-pasted some code.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   160
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   161
For instance, if you are selecting the primary (`__regid__ =
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   162
'primary'`) view (`__registry__ = 'views'`) for a result set
5306
763319a51e72 [doc/book] some fixes for vregistry, selectors & appobjects
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5275
diff changeset
   163
containing a `Card` entity, two objects will probably be selectable:
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   164
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   165
* the default primary view (`__select__ = implements('Any')`), meaning
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   166
  that the object is selectable for any kind of entity type
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   167
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   168
* the specific `Card` primary view (`__select__ = implements('Card')`,
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   169
  meaning that the object is selectable for Card entities
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   170
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   171
Other primary views specific to other entity types won't be selectable in this
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   172
case. Among selectable objects, the `implements('Card')` selector will return a higher
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   173
score since it's more specific, so the correct view will be selected as expected.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   174
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   175
.. _SelectionAPI:
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   176
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   177
API for objects selections
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   178
~~~~~~~~~~~~~~~~~~~~~~~~~~
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   179
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   180
Here is the selection API you'll get on every registry. Some of them, as the
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   181
'etypes' registry, containing entity classes, extend it. In those methods,
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   182
`*args, **kwargs` is what we call the **context**. Those arguments are given to
5608
f9ab62103ad4 proof read documentation
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5426
diff changeset
   183
selectors that will inspect their content and return a score accordingly.
5147
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   184
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   185
.. automethod:: cubicweb.vregistry.Registry.select
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   186
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   187
.. automethod:: cubicweb.vregistry.Registry.select_or_none
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   188
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   189
.. automethod:: cubicweb.vregistry.Registry.possible_objects
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   190
70181998897f more / cleaner / in code documentation of vreg, selectors and appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   191
.. automethod:: cubicweb.vregistry.Registry.object_by_id
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
"""
5886
00a78298d30d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5748
diff changeset
   193
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
__docformat__ = "restructuredtext en"
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
   195
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   197
from logilab.common.decorators import cached, clear_cache
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2476
diff changeset
   198
from logilab.common.deprecation import  deprecated
2684
c84691380dd4 [autoreload] remove all modules under vregistry_path() from sys.modules before reloading
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2669
diff changeset
   199
from logilab.common.modutils import cleanup_sys_modules
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
from rql import RQLHelper
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
2651
3ad936634d2a [registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2613
diff changeset
   203
from cubicweb import (ETYPE_NAME_MAP, Binary, UnknownProperty, UnknownEid,
2655
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650 2651
diff changeset
   204
                      ObjectNotFound, NoSelectableObject, RegistryNotFound,
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   205
                      CW_EVENT_MANAGER, onevent)
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   206
from cubicweb.utils import dump_class
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2824
diff changeset
   207
from cubicweb.vregistry import VRegistry, Registry, class_regid
1769
fb91d2b8a441 fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents: 1752
diff changeset
   208
from cubicweb.rtags import RTAGS
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
5035
8469d0acd949 [i18ncube] clear_rtag_objects imported when generating cube's .po file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4996
diff changeset
   210
def clear_rtag_objects():
8469d0acd949 [i18ncube] clear_rtag_objects imported when generating cube's .po file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4996
diff changeset
   211
    for rtag in RTAGS:
8469d0acd949 [i18ncube] clear_rtag_objects imported when generating cube's .po file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4996
diff changeset
   212
        rtag.clear()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
776
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   214
def use_interfaces(obj):
3719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3706
diff changeset
   215
    """return interfaces used by the given object by searching for implements
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1044
diff changeset
   216
    selectors, with a bw compat fallback to accepts_interfaces attribute
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1044
diff changeset
   217
    """
776
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   218
    from cubicweb.selectors import implements
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   219
    try:
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   220
        # XXX deprecated
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   221
        return sorted(obj.accepts_interfaces)
776
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   222
    except AttributeError:
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   223
        try:
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   224
            impl = obj.__select__.search_selector(implements)
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   225
            if impl:
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   226
                return sorted(impl.expected_ifaces)
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   227
        except AttributeError:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2655
diff changeset
   228
            pass # old-style appobject classes with no accepts_interfaces
1044
3672a7c86784 print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents: 1037
diff changeset
   229
        except:
3672a7c86784 print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents: 1037
diff changeset
   230
            print 'bad selector %s on %s' % (obj.__select__, obj)
3672a7c86784 print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents: 1037
diff changeset
   231
            raise
776
29ba95ea7e7d no more used
sylvain.thenault@logilab.fr
parents: 717
diff changeset
   232
        return ()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   235
class CWRegistry(Registry):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   236
    def __init__(self, vreg):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   237
        super(CWRegistry, self).__init__(vreg.config)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   238
        self.vreg = vreg
2927
d249dd9000d9 use a property for schema, avoid bad schema reference
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2914
diff changeset
   239
d249dd9000d9 use a property for schema, avoid bad schema reference
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2914
diff changeset
   240
    @property
d249dd9000d9 use a property for schema, avoid bad schema reference
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2914
diff changeset
   241
    def schema(self):
d249dd9000d9 use a property for schema, avoid bad schema reference
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2914
diff changeset
   242
        return self.vreg.schema
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   243
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2898 2927
diff changeset
   244
    @deprecated('[3.6] select object, then use obj.render()')
3704
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   245
    def render(self, __oid, req, __fallback_oid=None, rset=None, initargs=None,
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   246
               **kwargs):
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   247
        """Select object with the given id (`__oid`) then render it.  If the
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   248
        object isn't selectable, try to select fallback object if
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   249
        `__fallback_oid` is specified.
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   250
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   251
        If specified `initargs` is expected to be a dictionnary containing
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   252
        arguments that should be given to selection (hence to object's __init__
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   253
        as well), but not to render(). Other arbitrary keyword arguments will be
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   254
        given to selection *and* to render(), and so should be handled by
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   255
        object's call or cell_call method..
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   256
        """
3704
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   257
        if initargs is None:
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   258
            initargs = kwargs
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   259
        else:
3706
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3704
diff changeset
   260
            initargs.update(kwargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   261
        try:
3704
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   262
            obj = self.select(__oid, req, rset=rset, **initargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   263
        except NoSelectableObject:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   264
            if __fallback_oid is None:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   265
                raise
3704
ddb10568f5f8 a way to distinguish __init__ vs render arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3702
diff changeset
   266
            obj = self.select(__fallback_oid, req, rset=rset, **initargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   267
        return obj.render(**kwargs)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   268
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2898 2927
diff changeset
   269
    @deprecated('[3.6] use select_or_none and test for obj.cw_propval("visible")')
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   270
    def select_vobject(self, oid, *args, **kwargs):
2770
356e9d7c356d R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2705
diff changeset
   271
        selected = self.select_or_none(oid, *args, **kwargs)
2818
326375561412 propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2815
diff changeset
   272
        if selected and selected.cw_propval('visible'):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   273
            return selected
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   274
        return None
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   275
2805
c6d10de521bc R deprecate CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2800
diff changeset
   276
    def poss_visible_objects(self, *args, **kwargs):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   277
        """return an ordered list of possible app objects in a given registry,
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   278
        supposing they support the 'visible' and 'order' properties (as most
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   279
        visualizable objects)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   280
        """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   281
        return sorted([x for x in self.possible_objects(*args, **kwargs)
2818
326375561412 propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2815
diff changeset
   282
                       if x.cw_propval('visible')],
326375561412 propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2815
diff changeset
   283
                      key=lambda x: x.cw_propval('order'))
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2898 2927
diff changeset
   284
    possible_vobjects = deprecated('[3.6] use poss_visible_objects()')(poss_visible_objects)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   285
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   286
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   287
VRegistry.REGISTRY_FACTORY[None] = CWRegistry
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   288
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   289
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   290
class ETypeRegistry(CWRegistry):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   291
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   292
    def initialization_completed(self):
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   293
        """on registration completed, clear etype_class internal cache
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   294
        """
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   295
        super(ETypeRegistry, self).initialization_completed()
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   296
        # clear etype cache if you don't want to run into deep weirdness
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   297
        clear_cache(self, 'etype_class')
4291
62a315e61adb clear vreg's parent_classes cache to fix etype cache / auto-reloading bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4252
diff changeset
   298
        clear_cache(self, 'parent_classes')
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   299
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   300
    def register(self, obj, **kwargs):
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2824
diff changeset
   301
        oid = kwargs.get('oid') or class_regid(obj)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   302
        if oid != 'Any' and not oid in self.schema:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   303
            self.error('don\'t register %s, %s type not defined in the '
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2824
diff changeset
   304
                       'schema', obj, oid)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   305
            return
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   306
        kwargs['clear'] = True
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   307
        super(ETypeRegistry, self).register(obj, **kwargs)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   308
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   309
    @cached
2824
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   310
    def parent_classes(self, etype):
2894
4b720d82a1e4 take care in case Any is given
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2829
diff changeset
   311
        if etype == 'Any':
4b720d82a1e4 take care in case Any is given
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2829
diff changeset
   312
            return [self.etype_class('Any')]
2824
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   313
        eschema = self.schema.eschema(etype)
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2898 2927
diff changeset
   314
        parents = [self.etype_class(e.type) for e in eschema.ancestors()]
2824
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   315
        parents.append(self.etype_class('Any'))
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   316
        return parents
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   317
3455f72010fe better as a method of the etypes registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2818
diff changeset
   318
    @cached
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   319
    def etype_class(self, etype):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   320
        """return an entity class for the given entity type.
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   321
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   322
        Try to find out a specific class for this kind of entity or default to a
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   323
        dump of the nearest parent class (in yams inheritance) registered.
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   324
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   325
        Fall back to 'Any' if not yams parent class found.
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   326
        """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   327
        etype = str(etype)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   328
        if etype == 'Any':
4149
ecde9f78a080 etype_class('Any') should return bare 'Any' class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   329
            objects = self['Any']
ecde9f78a080 etype_class('Any') should return bare 'Any' class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   330
            assert len(objects) == 1, objects
ecde9f78a080 etype_class('Any') should return bare 'Any' class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   331
            return objects[0]
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   332
        eschema = self.schema.eschema(etype)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   333
        baseschemas = [eschema] + eschema.ancestors()
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   334
        # browse ancestors from most specific to most generic and try to find an
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   335
        # associated custom entity class
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   336
        for baseschema in baseschemas:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   337
            try:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   338
                btype = ETYPE_NAME_MAP[baseschema]
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   339
            except KeyError:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   340
                btype = str(baseschema)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   341
            try:
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   342
                objects = self[btype]
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   343
                assert len(objects) == 1, objects
3137
5598bec9be7f fix so that we ensure issubclass(etype_class('SubNote'), etype_class('Note'))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   344
                if btype == etype:
3172
9341ab8f1d1a [vreg] we must ensure that parent classes are initialiazed first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3057
diff changeset
   345
                    cls = objects[0]
3137
5598bec9be7f fix so that we ensure issubclass(etype_class('SubNote'), etype_class('Note'))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   346
                else:
5598bec9be7f fix so that we ensure issubclass(etype_class('SubNote'), etype_class('Note'))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   347
                    # recurse to ensure issubclass(etype_class('Child'),
5598bec9be7f fix so that we ensure issubclass(etype_class('SubNote'), etype_class('Note'))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   348
                    #                              etype_class('Parent'))
5598bec9be7f fix so that we ensure issubclass(etype_class('SubNote'), etype_class('Note'))
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   349
                    cls = self.etype_class(btype)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   350
                break
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   351
            except ObjectNotFound:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   352
                pass
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   353
        else:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   354
            # no entity class for any of the ancestors, fallback to the default
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   355
            # one
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   356
            objects = self['Any']
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   357
            assert len(objects) == 1, objects
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   358
            cls = objects[0]
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   359
        # make a copy event if cls.__regid__ == etype, else we may have pb for
3035
2e4a381ea5b7 backport 3.5 branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968 3032
diff changeset
   360
        # client application using multiple connections to different
2e4a381ea5b7 backport 3.5 branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968 3032
diff changeset
   361
        # repositories (eg shingouz)
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   362
        cls = dump_class(cls, etype)
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   363
        cls.__regid__ = etype
2807
696ff03f9a58 __initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2804
diff changeset
   364
        cls.__initialize__(self.schema)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   365
        return cls
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   366
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   367
VRegistry.REGISTRY_FACTORY['etypes'] = ETypeRegistry
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   368
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   369
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   370
class ViewsRegistry(CWRegistry):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   371
3655
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3451
diff changeset
   372
    def main_template(self, req, oid='main-template', rset=None, **kwargs):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   373
        """display query by calling the given template (default to main),
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   374
        and returning the output as a string instead of requiring the [w]rite
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   375
        method as argument
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   376
        """
3655
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3451
diff changeset
   377
        obj = self.select(oid, req, rset=rset, **kwargs)
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3451
diff changeset
   378
        res = obj.render(**kwargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   379
        if isinstance(res, unicode):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   380
            return res.encode(req.encoding)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   381
        assert isinstance(res, str)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   382
        return res
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   383
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   384
    def possible_views(self, req, rset=None, **kwargs):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   385
        """return an iterator on possible views for this result set
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   386
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   387
        views returned are classes, not instances
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   388
        """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   389
        for vid, views in self.items():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   390
            if vid[0] == '_':
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   391
                continue
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   392
            try:
2770
356e9d7c356d R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2705
diff changeset
   393
                view = self._select_best(views, req, rset=rset, **kwargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   394
                if view.linkable():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   395
                    yield view
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   396
            except NoSelectableObject:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   397
                continue
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   398
            except Exception:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   399
                self.exception('error while trying to select %s view for %s',
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   400
                               vid, rset)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   401
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   402
VRegistry.REGISTRY_FACTORY['views'] = ViewsRegistry
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   403
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   404
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   405
class ActionsRegistry(CWRegistry):
5246
3246b1f88a18 [web] stop having actions configurable through cwproperties: this clutter site management and .po files for a useless fonctionnality (which, where and how actions are displayed is handled by code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   406
    def poss_visible_objects(self, *args, **kwargs):
3246b1f88a18 [web] stop having actions configurable through cwproperties: this clutter site management and .po files for a useless fonctionnality (which, where and how actions are displayed is handled by code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   407
        """return an ordered list of possible actions"""
3246b1f88a18 [web] stop having actions configurable through cwproperties: this clutter site management and .po files for a useless fonctionnality (which, where and how actions are displayed is handled by code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   408
        return sorted(self.possible_objects(*args, **kwargs),
3246b1f88a18 [web] stop having actions configurable through cwproperties: this clutter site management and .po files for a useless fonctionnality (which, where and how actions are displayed is handled by code)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   409
                      key=lambda x: x.order)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   410
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   411
    def possible_actions(self, req, rset=None, **kwargs):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   412
        if rset is None:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3376
diff changeset
   413
            actions = self.poss_visible_objects(req, rset=rset, **kwargs)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   414
        else:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   415
            actions = rset.possible_actions(**kwargs) # cached implementation
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   416
        result = {}
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   417
        for action in actions:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   418
            result.setdefault(action.category, []).append(action)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   419
        return result
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   420
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   421
VRegistry.REGISTRY_FACTORY['actions'] = ActionsRegistry
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   422
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   423
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   424
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   425
class CubicWebVRegistry(VRegistry):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2389
diff changeset
   426
    """Central registry for the cubicweb instance, extending the generic
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   427
    VRegistry with some cubicweb specific stuff.
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   428
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2389
diff changeset
   429
    This is one of the central object in cubicweb instance, coupling
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   430
    dynamically loaded objects with the schema and the configuration objects.
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   431
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   432
    It specializes the VRegistry by adding some convenience methods to access to
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   433
    stored objects. Currently we have the following registries of objects known
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2389
diff changeset
   434
    by the web instance (library may use some others additional registries):
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   435
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   436
    * etypes
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   437
    * views
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   438
    * components
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   439
    * actions
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   440
    * forms
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   441
    * formrenderers
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   442
    * controllers, which are directly plugged into the application
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   443
      object to handle request publishing XXX to merge with views
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   444
    * contentnavigation XXX to merge with components? to kill?
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   445
    """
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   446
169
0e031b66cb0b don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   447
    def __init__(self, config, debug=None, initlog=True):
0e031b66cb0b don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   448
        if initlog:
0e031b66cb0b don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   449
            # first init log service
0e031b66cb0b don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   450
            config.init_log(debug=debug)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   451
        super(CubicWebVRegistry, self).__init__(config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
        self.schema = None
2906
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   453
        self.initialized = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
        self.reset()
5080
cfc7c2b24f9e [cleanup] some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5035
diff changeset
   455
        # XXX give force_reload (or refactor [re]loading...)
4996
45ce27d538a1 [test] don't clear rtags during tests, may cause breakage with manually import appobjects modules (see unittest_tracker for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4966
diff changeset
   456
        if self.config.mode != 'test':
45ce27d538a1 [test] don't clear rtags during tests, may cause breakage with manually import appobjects modules (see unittest_tracker for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4966
diff changeset
   457
            # don't clear rtags during test, this may cause breakage with
45ce27d538a1 [test] don't clear rtags during tests, may cause breakage with manually import appobjects modules (see unittest_tracker for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4966
diff changeset
   458
            # manually imported appobject modules
5035
8469d0acd949 [i18ncube] clear_rtag_objects imported when generating cube's .po file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4996
diff changeset
   459
            CW_EVENT_MANAGER.bind('before-registry-reload', clear_rtag_objects)
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   460
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   461
    def setdefault(self, regid):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   462
        try:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   463
            return self[regid]
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   464
        except RegistryNotFound:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   465
            self[regid] = self.registry_class(regid)(self)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   466
            return self[regid]
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   467
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
    def items(self):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   469
        return [item for item in super(CubicWebVRegistry, self).items()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
                if not item[0] in ('propertydefs', 'propertyvalues')]
2669
cab66dfe0db6 to avoid confusions, implements iteritems/itervalues as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2659
diff changeset
   471
    def iteritems(self):
cab66dfe0db6 to avoid confusions, implements iteritems/itervalues as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2659
diff changeset
   472
        return (item for item in super(CubicWebVRegistry, self).iteritems()
cab66dfe0db6 to avoid confusions, implements iteritems/itervalues as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2659
diff changeset
   473
                if not item[0] in ('propertydefs', 'propertyvalues'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
    def values(self):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   476
        return [value for key, value in self.items()]
2669
cab66dfe0db6 to avoid confusions, implements iteritems/itervalues as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2659
diff changeset
   477
    def itervalues(self):
cab66dfe0db6 to avoid confusions, implements iteritems/itervalues as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2659
diff changeset
   478
        return (value for key, value in self.items())
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   479
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   480
    def reset(self):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   481
        super(CubicWebVRegistry, self).reset()
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   482
        self._needs_iface = {}
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   483
        # two special registries, propertydefs which care all the property
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   484
        # definitions, and propertyvals which contains values for those
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   485
        # properties
2906
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   486
        if not self.initialized:
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   487
            self['propertydefs'] = {}
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   488
            self['propertyvalues'] = self.eprop_values = {}
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   489
            for key, propdef in self.config.eproperty_definitions():
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2761
diff changeset
   490
                self.register_property(key, **propdef)
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   491
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
    def set_schema(self, schema):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2389
diff changeset
   493
        """set instance'schema and load application objects"""
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   494
        self._set_schema(schema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
        # now we can load application's web objects
5275
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   496
        self.reload(self.config.vregistry_path(), force_reload=False)
2273
daf6e178659f new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2223
diff changeset
   497
        # map lowered entity type names to their actual name
daf6e178659f new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2223
diff changeset
   498
        self.case_insensitive_etypes = {}
3692
54aa8d13aab4 clear eschema caches on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3641
diff changeset
   499
        for eschema in self.schema.entities():
54aa8d13aab4 clear eschema caches on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3641
diff changeset
   500
            etype = str(eschema)
2273
daf6e178659f new case_insensitive_etypes resource on the cubicweb registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2223
diff changeset
   501
            self.case_insensitive_etypes[etype.lower()] = etype
3692
54aa8d13aab4 clear eschema caches on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3641
diff changeset
   502
            clear_cache(eschema, 'ordered_relations')
54aa8d13aab4 clear eschema caches on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3641
diff changeset
   503
            clear_cache(eschema, 'meta_attributes')
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   504
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   505
    def reload_if_needed(self):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   506
        path = self.config.vregistry_path()
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   507
        if self.is_reload_needed(path):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   508
            self.reload(path)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   509
5275
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   510
    def reload(self, path, force_reload=True):
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   511
        """modification detected, reset and reload the vreg"""
4966
e968e0a7776b [registry] trigger reload events on set_schema()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   512
        CW_EVENT_MANAGER.emit('before-registry-reload')
5275
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   513
        if force_reload:
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   514
            cleanup_sys_modules(path)
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   515
            cubes = self.config.cubes()
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   516
            # if the fs code use some cubes not yet registered into the instance
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   517
            # we should cleanup sys.modules for those as well to avoid potential
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   518
            # bad class reference pb after reloading
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   519
            cfg = self.config
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   520
            for cube in cfg.expand_cubes(cubes, with_recommends=True):
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   521
                if not cube in cubes:
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   522
                    cpath = cfg.build_vregistry_cube_path([cfg.cube_dir(cube)])
32fbb13c06d3 [vreg] fix bug introduced in 5273:c4caef6f09c9 : we should not force_reload in vreg reset done because of a set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
   523
                    cleanup_sys_modules(cpath)
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   524
        self.reset()
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   525
        self.register_objects(path)
4966
e968e0a7776b [registry] trigger reload events on set_schema()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   526
        CW_EVENT_MANAGER.emit('after-registry-reload')
e968e0a7776b [registry] trigger reload events on set_schema()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   527
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   528
    def _set_schema(self, schema):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   529
        """set instance'schema"""
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   530
        self.schema = schema
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   531
        clear_cache(self, 'rqlhelper')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   532
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   533
    def update_schema(self, schema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   534
        """update .schema attribute on registered objects, necessary for some
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   535
        tests
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   536
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   537
        self.schema = schema
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   538
        for registry, regcontent in self.items():
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
            for objects in regcontent.values():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
                for obj in objects:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   541
                    obj.schema = schema
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   542
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   543
    def register_if_interface_found(self, obj, ifaces, **kwargs):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   544
        """register `obj` but remove it if no entity class implements one of
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   545
        the given `ifaces` interfaces at the end of the registration process.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   546
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   547
        Extra keyword arguments are given to the
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   548
        :meth:`~cubicweb.cwvreg.CubicWebVRegistry.register` function.
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   549
        """
785
e4f9fa3c891a more iface based cleanup fixes
sylvain.thenault@logilab.fr
parents: 777
diff changeset
   550
        self.register(obj, **kwargs)
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   551
        if not isinstance(ifaces,  (tuple, list)):
785
e4f9fa3c891a more iface based cleanup fixes
sylvain.thenault@logilab.fr
parents: 777
diff changeset
   552
            self._needs_iface[obj] = (ifaces,)
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   553
        else:
785
e4f9fa3c891a more iface based cleanup fixes
sylvain.thenault@logilab.fr
parents: 777
diff changeset
   554
            self._needs_iface[obj] = ifaces
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   555
3055
06814d57514f [vreg] keep positional arg compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2927
diff changeset
   556
    def register(self, obj, *args, **kwargs):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   557
        """register `obj` application object into `registryname` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   558
        `obj.__registry__` if not specified, with identifier `oid` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   559
        `obj.__regid__` if not specified.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   560
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   561
        If `clear` is true, all objects with the same identifier will be
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   562
        previously unregistered.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5080
diff changeset
   563
        """
3055
06814d57514f [vreg] keep positional arg compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2927
diff changeset
   564
        super(CubicWebVRegistry, self).register(obj, *args, **kwargs)
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   565
        # XXX bw compat
777
39695e98ba35 test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents: 776
diff changeset
   566
        ifaces = use_interfaces(obj)
666
8ad9885ea45a interface handling should be done here
sylvain.thenault@logilab.fr
parents: 631
diff changeset
   567
        if ifaces:
785
e4f9fa3c891a more iface based cleanup fixes
sylvain.thenault@logilab.fr
parents: 777
diff changeset
   568
            self._needs_iface[obj] = ifaces
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   569
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   570
    def register_objects(self, path):
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   571
        """overriden to give cubicweb's extrapath (eg cubes package's __path__)
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   572
        """
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   573
        super(CubicWebVRegistry, self).register_objects(
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5608
diff changeset
   574
            path, self.config.extrapath)
2651
3ad936634d2a [registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2613
diff changeset
   575
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   576
    def initialization_completed(self):
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   577
        """cw specific code once vreg initialization is completed:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   579
        * remove objects requiring a missing interface, unless
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   580
          config.cleanup_interface_sobjects is false
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   581
        * init rtags
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   582
        """
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   583
        # we may want to keep interface dependent objects (e.g.for i18n
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   584
        # catalog generation)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   585
        if self.config.cleanup_interface_sobjects:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2655
diff changeset
   586
            # remove appobjects that don't support any available interface
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   587
            implemented_interfaces = set()
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   588
            if 'Any' in self.get('etypes', ()):
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   589
                for etype in self.schema.entities():
3702
29cce43d6af2 skip final type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3692
diff changeset
   590
                    if etype.final:
29cce43d6af2 skip final type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3692
diff changeset
   591
                        continue
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   592
                    cls = self['etypes'].etype_class(etype)
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   593
                    for iface in cls.__implements__:
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   594
                        implemented_interfaces.update(iface.__mro__)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   595
                    implemented_interfaces.update(cls.__mro__)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   596
            for obj, ifaces in self._needs_iface.items():
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   597
                ifaces = frozenset(isinstance(iface, basestring)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   598
                                   and iface in self.schema
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   599
                                   and self['etypes'].etype_class(iface)
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   600
                                   or iface
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   601
                                   for iface in ifaces)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   602
                if not ('Any' in ifaces or ifaces & implemented_interfaces):
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2655
diff changeset
   603
                    self.debug('kicking appobject %s (no implemented '
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   604
                               'interface among %s)', obj, ifaces)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   605
                    self.unregister(obj)
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   606
        # clear needs_iface so we don't try to remove some not-anymore-in
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   607
        # objects on automatic reloading
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   608
        self._needs_iface.clear()
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   609
        super(CubicWebVRegistry, self).initialization_completed()
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   610
        for rtag in RTAGS:
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   611
            # don't check rtags if we don't want to cleanup_interface_sobjects
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   612
            rtag.init(self.schema, check=self.config.cleanup_interface_sobjects)
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   613
1638
6f9003a32ecc extract method
sylvain.thenault@logilab.fr
parents: 1513
diff changeset
   614
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   615
    # rql parsing utilities ####################################################
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   616
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   617
    @property
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   618
    @cached
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   619
    def rqlhelper(self):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   620
        return RQLHelper(self.schema,
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   621
                         special_relations={'eid': 'uid', 'has_text': 'fti'})
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   622
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   623
    def solutions(self, req, rqlst, args):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   624
        def type_from_eid(eid, req=req):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   625
            return req.describe(eid)[0]
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5147
diff changeset
   626
        return self.rqlhelper.compute_solutions(rqlst, {'eid': type_from_eid}, args)
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   627
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   628
    def parse(self, req, rql, args=None):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   629
        rqlst = self.rqlhelper.parse(rql)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   630
        try:
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   631
            self.solutions(req, rqlst, args)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   632
        except UnknownEid:
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   633
            for select in rqlst.children:
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   634
                select.solutions = []
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   635
        return rqlst
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   636
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   637
    # properties handling #####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   638
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   639
    def user_property_keys(self, withsitewide=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   640
        if withsitewide:
2223
59588e2d5cd1 close #343467, no sources sections in site properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   641
            return sorted(k for k in self['propertydefs']
59588e2d5cd1 close #343467, no sources sections in site properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   642
                          if not k.startswith('sources.'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   643
        return sorted(k for k, kd in self['propertydefs'].iteritems()
2223
59588e2d5cd1 close #343467, no sources sections in site properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   644
                      if not kd['sitewide'] and not k.startswith('sources.'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   645
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   646
    def register_property(self, key, type, help, default=None, vocabulary=None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   647
                          sitewide=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   648
        """register a given property"""
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   649
        properties = self['propertydefs']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   650
        assert type in YAMS_TO_PY
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   651
        properties[key] = {'type': type, 'vocabulary': vocabulary,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   652
                           'default': default, 'help': help,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   653
                           'sitewide': sitewide}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   654
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   655
    def property_info(self, key):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   656
        """return dictionary containing description associated to the given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   657
        property key (including type, defaut value, help and a site wide
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   658
        boolean)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   659
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   660
        try:
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   661
            return self['propertydefs'][key]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   662
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   663
            if key.startswith('system.version.'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   664
                soft = key.split('.')[-1]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   665
                return {'type': 'String', 'sitewide': True,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   666
                        'default': None, 'vocabulary': None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
                        'help': _('%s software version of the database') % soft}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   668
            raise UnknownProperty('unregistered property %r' % key)
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   669
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   670
    def property_value(self, key):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   671
        try:
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   672
            return self['propertyvalues'][key]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   673
        except KeyError:
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4440 4486
diff changeset
   674
            return self.property_info(key)['default']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   676
    def typed_value(self, key, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   677
        """value is an unicode string, return it correctly typed. Let potential
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
        type error propagates.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   680
        pdef = self.property_info(key)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   681
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
            value = YAMS_TO_PY[pdef['type']](value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
        except (TypeError, ValueError):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
            raise ValueError(_('bad value'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   685
        vocab = pdef['vocabulary']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   686
        if vocab is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
            if callable(vocab):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   688
                vocab = vocab(key, None) # XXX need a req object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   689
            if not value in vocab:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   690
                raise ValueError(_('unauthorized value'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   691
        return value
1475
5c1ec97f317e should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents: 1357
diff changeset
   692
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   693
    def init_properties(self, propvalues):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   694
        """init the property values registry using the given set of couple (key, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   695
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   696
        self.initialized = True
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   697
        values = self['propertyvalues']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   698
        for key, val in propvalues:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   699
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   700
                values[key] = self.typed_value(key, val)
3951
b64c73bdb37b fix name error if ValueError is raised
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3719
diff changeset
   701
            except ValueError, ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   702
                self.warning('%s (you should probably delete that property '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   703
                             'from the database)', ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
            except UnknownProperty, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
                self.warning('%s (you should probably delete that property '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   706
                             'from the database)', ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   707
3240
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   708
    # deprecated code ####################################################
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   709
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   710
    @deprecated('[3.4] use vreg["etypes"].etype_class(etype)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   711
    def etype_class(self, etype):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   712
        return self["etypes"].etype_class(etype)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   713
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   714
    @deprecated('[3.4] use vreg["views"].main_template(*args, **kwargs)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   715
    def main_template(self, req, oid='main-template', **context):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   716
        return self["views"].main_template(req, oid, **context)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   717
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   718
    @deprecated('[3.4] use vreg[registry].possible_vobjects(*args, **kwargs)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   719
    def possible_vobjects(self, registry, *args, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   720
        return self[registry].possible_vobjects(*args, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   721
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   722
    @deprecated('[3.4] use vreg["actions"].possible_actions(*args, **kwargs)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   723
    def possible_actions(self, req, rset=None, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   724
        return self["actions"].possible_actions(req, rest=rset, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   725
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   726
    @deprecated('[3.4] use vreg["boxes"].select_object(...)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   727
    def select_box(self, oid, *args, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   728
        return self['boxes'].select_object(oid, *args, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   729
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   730
    @deprecated('[3.4] use vreg["components"].select_object(...)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   731
    def select_component(self, cid, *args, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   732
        return self['components'].select_object(cid, *args, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   733
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   734
    @deprecated('[3.4] use vreg["actions"].select_object(...)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   735
    def select_action(self, oid, *args, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   736
        return self['actions'].select_object(oid, *args, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   737
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   738
    @deprecated('[3.4] use vreg["views"].select(...)')
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   739
    def select_view(self, __vid, req, rset=None, **kwargs):
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   740
        return self['views'].select(__vid, req, rset=rset, **kwargs)
8604a15995d1 refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3205
diff changeset
   741
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2025
diff changeset
   742
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   743
from datetime import datetime, date, time, timedelta
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
YAMS_TO_PY = {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
    'Boolean':  bool,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
    'String' :  unicode,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
    'Password': str,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
    'Bytes':    Binary,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
    'Int':      int,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
    'Float':    float,
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   752
    'Date':     date,
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   753
    'Datetime': datetime,
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   754
    'Time':     time,
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 823
diff changeset
   755
    'Interval': timedelta,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   756
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   757