# -*- coding: utf-8; mode: structured-text -*-

= Module Names =

Wiking modules (and the Pytis specifications published on the web) are referred
to by names.  This chapter explains how the modules get their names, how they
are found by name and how an application or its extension replaces a module of
Wiking (or of another extension).

The examples use an e-shop.  Its web interface is a Wiking application in the
package =eshop.web=.  Its back office is a Pytis wx application with
specifications in the package =eshop.defs= -- the specifications =Invoices= and
=InvoiceItems= in the module =eshop.defs.billing= and =Customers= and =Orders=
in =eshop.defs.sales=.  The web publishes some of the back office
specifications too.

@TOC@


== Names and identity ==

Each Wiking module has a name, such as =Users= or =billing.Invoices=, returned
by its method =name()=.  The name is the identity of the module within the
application:

  * The URI mapping of the application refers to modules by names (see
    =_MAPPING= of =wiking.Application=).
  * The codebooks, bindings and nested rows of specifications refer to other
    modules (specifications) by names.
  * Forms send the name of their module in the request parameter =form_name=,
    so it is also visible to external clients posting forms.
  * The CSS classes of forms are derived from it (=billing-invoices= for
    =billing.Invoices=).
  * Wiking CMS stores the names of the modules embedded in pages in the
    database.

There is exactly one module (one class and one instance for given constructor
arguments) for each name.  The instances are obtained by =wiking.module=, such
as =wiking.module('billing.Invoices')= or =wiking.module.Users= (the attribute
access only works for names without dots).


== Where the names come from == search-modules

The configuration option =search_modules= names the Python packages providing
the modules of the application:

-----
search_modules = ('eshop.web', 'eshop.defs', 'wiking.cms')
-----

The packages are scanned (together with their modules) when the first module
is needed.  Each Wiking module class and each Pytis specification found gets a
name -- its shortest path relative to the package where it is defined:

  * A class in the top level namespace of the package (defined in the
    package's =\__init__.py= or imported there, which is the usual way of
    exporting the public classes) is named just by the class name, such as
    =Users=.
  * A class only available in a module of the package is named by its path
    within the package, such as =billing.Invoices= for the class =Invoices= in
    the module =eshop.defs.billing=.

The rules in detail:

  * Only the modules of the package are scanned, not its subpackages.
    Whether a subpackage appears in the namespace of its parent depends on
    whether anybody imported it, so the result would not be stable.  Name the
    subpackage in =search_modules= separately when needed (its classes are
    then named relative to the subpackage).
  * Private names (starting with an underscore) are skipped, so base classes
    such as =_Tasks= don't get names.
  * Importing a class into another module or package doesn't change its name.
    The name is given by the package where the class is defined.  Only
    classes defined outside all the named packages (such as the modules of
    =wiking= exported by =wiking.cms=) are named by the shortest path where
    they were found.
  * The order of the packages in =search_modules= doesn't matter for the
    modules.  It only determines the precedence of the resources,
    translations and documentation derived from the packages (see the option
    =resource_path=).
  * Two different classes with the same name are an error.  The error is
    reported when the name is used, so that two helper classes of the same
    name which are never used by name don't matter.  A module replaces
    another module only explicitly (see below).


== Full names ==

A module may also be referred to by the full Python name of its class, such as
=wiking.cms.Users=.  The full name leads to the same module (and the same
instance) as its name, including its possible replacement.  So the following
references are all equivalent in an application which replaces =Users= of
Wiking CMS by =eshop.web.Users=:

-----
wiking.module.Users
wiking.module('Users')
wiking.module('wiking.cms.Users')
wiking.module('eshop.web.Users')
-----

The name of the module (its identity) is =Users= in all cases.  Thus the
strings in references (specifications, the URI mapping) are not necessarily
the names of the modules.  Compare the modules themselves or get the name of
the referred module by the resolver (=wiking.cfg.resolver=) before comparing it
with =name()=:

-----
if wiking.module(binding.name()) is self:
    ...
if req.param('form_name') == wiking.cfg.resolver.name(binding.name()):
    ...
-----

The resolver accepts the name, the full name or the class of a module in all
its methods: =name()= returns the name of the module (the same as =name()= of
its instance, without creating it), =module_class()= its class (the replacing
class when the module is replaced), =module()= its instance (this is what
=wiking.module= uses) and =modules()= returns the classes of all modules
available by name.

Wiking modules outside =search_modules= are also available by their full names,
which are then also their names.  This is practical for small extensions which
are only used by full names and have no resources, translations or
documentation of their own (these are derived from =search_modules=).  Pytis
specifications outside =search_modules= are not available, because naming
their package in =search_modules= is the decision to publish them (see
below).


== Replacing modules == overrides

An application often needs to customize a module of Wiking CMS or of another
extension, such as the management of users.  The replacement is declared
explicitly by the decorator =wiking.overrides=:

-----
import wiking
import wiking.cms

@wiking.overrides(wiking.cms.Users)
class Users(wiking.cms.Users):
    ...
-----

The decorated class takes over the name of the replaced module, so all the
references to =Users= (including =wiking.cms.Users= and the references from the
specifications of Wiking CMS) lead to the replacement.  The rules:

  * The replacement only takes effect when the replacing class is found in a
    package named in =search_modules=.  Importing the class doesn't replace
    anything.
  * The replacing class may be defined anywhere and named anyhow.  It is
    usually derived from the replaced class, but it doesn't have to be.
  * Deriving a class without the decorator never replaces anything.  The
    derived class is just another module, which may be used for something
    else entirely.  If it has the same name as the original module, the name
    is in conflict.
  * A replacement may be replaced again (a chain of replacements, such as an
    extension of an application customizing the application's own
    replacement of a CMS module).  Each replacement names the class it
    replaces directly.  Two independent replacements of the same module are an
    error reported at once (when the packages are scanned).
  * =Application= is replaced the same way as any other module:

-----
@wiking.overrides(wiking.cms.Application)
class Application(wiking.cms.Application):
    ...
-----


== Pytis specifications on the web == specifications

Wiking not only uses Pytis as the infrastructure of its database modules (the
database abstraction and the specifications), it is also a web interface for
Pytis applications.  The specifications of a Pytis wx application may be
published on the web without changes.

When a package named in =search_modules= contains Pytis specifications, they
get names by the same rules as Wiking modules.  The specifications of a wx
application, which refer to each other as =billing.Invoices= or
=sales.Customers= (relative to its Pytis =search_modules=), thus have the same
names on the web when the same package is named in Wiking's =search_modules=:

-----
search_modules = ('eshop.web', 'eshop.defs', 'wiking.cms')
-----

A Wiking module is created automatically for each specification which has no
Wiking module of its own.  The module derives from =DefaultModule= and its
specification derives from both =wiking.Specification= and the Pytis
specification.  Its name is the name of the specification, so the codebooks,
bindings and nested rows work on the web without any changes of the
specifications.

The modules are not accessible on the web until the application maps them to
URIs (or until the bindings of the accessible modules lead to them):

-----
@wiking.overrides(wiking.Application)
class Application(wiking.Application):
    _MAPPING = dict(wiking.Application._MAPPING, invoices='billing.Invoices')
-----

=DefaultModule= allows reading to anyone and writing to no one.  The
application changes the behavior of all the automatically created modules by
replacing =DefaultModule=:

-----
@wiking.overrides(wiking.DefaultModule)
class DefaultModule(wiking.DefaultModule):
    def _authorized(self, req, action, **kwargs):
        return req.check_roles(wiking.Roles.AUTHENTICATED)
-----

A single module is customized by replacing the specification:

-----
@wiking.overrides(eshop.defs.billing.Invoices)
class Invoices(DefaultModule):
    class Spec(wiking.Specification, eshop.defs.billing.Invoices):
        ...
-----

See =pytis.demo.web= for a complete example publishing the specifications of
Pytis Demo.


== Independently developed extensions == extensions

The modules of an extension are named relative to the package of the extension
named in =search_modules=, so an extension developed independently of the
applications using it (and of Wiking CMS) should take care of its names:

  * Give the modules distinctive names, such as =PaymentTransactions= rather
    than =Transactions= in an extension =payments= for the e-shop.  A conflict
    of names is reported, but it can not be resolved by the application other
    than by replacing one of the modules.
  * Refer to the extension's own modules by their full names (such as
    =payments.PaymentTransactions=) within the extension.  The full names work
    no matter whether the extension is named in =search_modules=.
  * The application may replace any module of the extension by
    =wiking.overrides= without depending on the structure of the extension's
    package.

An extension must be named in =search_modules= when it replaces other modules,
publishes Pytis specifications or has resources, translations or documentation
of its own.  Otherwise it is enough to have it installed and refer to its
modules by full names.  An extension which is a subpackage of another package
named in =search_modules= must be named there separately too, because
subpackages are not scanned (see above).


== Design notes ==

=== Why does the order of the packages not matter? ===

If the order decided which module is used under a name, an application could
replace a module just by listing its package first.  But an unrelated module of
the same name would then silently shadow another module (or be silently
shadowed) depending on the order, and the same code would behave differently in
different configurations.  An explicit replacement says what is replaced and
the conflicts are reported.

=== Why is a conflict an error and not decided by the order? ===

Deciding by the order would bring the silent shadowing described above and make
the explicit replacements optional, so they would not be used consistently.
The error is reported only when the conflicting name is used, so the conflicts
of names nobody uses (such as helper base classes) don't matter.

=== Why are the names not the full Python names everywhere? ===

The names are visible outside the code -- stored in the database (Wiking CMS),
sent in requests by external clients and used in stylesheets.  The full Python
name of the place where a class is defined would make the internal structure of
the source files a public interface: moving a class into another file would
require a database migration and changes of stylesheets.  The full name of the
public location (such as =wiking.cms.Users=) can not be derived from the class
itself.  The full names would not avoid =search_modules= either -- it is still
needed to activate the replacements, to publish specifications and to find the
resources and translations.  Full names may still be used in references
wherever practical (see above).

=== Why are the names given by the package where the class is defined? ===

So that importing a class elsewhere doesn't change its name.  For example a
module of the application importing a specification to derive from it must not
rename the specification for all the others.

== Examples ==

The executable examples of the rules described in this chapter are in the
docstring of =\_resolver_examples= in =wiking/test.py= (run as a part of the
test suite).
