mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-05 22:03:01 -06:00
1b4eab0411
This patch reverts the use of pygettext for i18n string extraction. It was originally introduced because the help documentation for commands are in the class docstring and module docstring. Docstrings are a Python construct whereby any string which immediately follows a class declaration, function/method declaration or appears first in a module is taken to be the documentation for that object. Python automatically assigns that string to the __doc__ variable associated with the object. Explicitly assigning to the __doc__ variable is equivalent and permitted. We mark strings in the source for i18n translation by embedding them in _() or ngettext(). Specialized extraction tools (e.g. xgettext) scan the source code looking for strings with those markers and extracts the string for inclusion in a translation catalog. It was mistakingly assumed one could not mark for translation Python docstrings. Since some docstrings are vital for our command help system some method had to be devised to extract docstrings for the translation catalog. pygettext has the ability to locate and extract docstrings and it was introduced to acquire the documentation for our commands located in module and class docstrings. However pygettext was too large a hammer for this task, it lacked any fined grained ability to extract only the docstrings we were interested in. In practice it extracted EVERY docstring in each file it was presented with. This caused a large number strings to be extracted for translation which had no reason to be translated, the string might have been internal code documentation never meant to be seen by users. Often the superfluous docstrings were long, complex and likely difficult to translate. This placed an unnecessary burden on our volunteer translators. Instead what is needed is some method to extract only those strings intended for translation. We already have such a mechanism and it is already widely used, namely wrapping strings intended for translation in calls to _() or _negettext(), i.e. marking a string for i18n translation. Thus the solution to the docstring translation problem is to mark the docstrings exactly as we have been doing, it only requires that instead of a bare Python docstring we instead assign the marked string to the __doc__ variable. Using the hypothetical class foo as an example. class foo(Command): ''' The foo command takes out the garbage. ''' Would become: class foo(Command): __doc__ = _('The foo command takes out the garbage.') But which docstrings need to be marked for translation? The makeapi tool knows how to iterate over every command in our public API. It was extended to validate every command's documentation and report if any documentation is missing or not marked for translation. That information was then used to identify each docstring in the code which needed to be transformed. In summary what this patch does is: * Remove the use of pygettext (modification to install/po/Makefile.in) * Replace every docstring with an explicit assignment to __doc__ where the rhs of the assignment is an i18n marking function. * Single line docstrings appearing in multi-line string literals (e.g. ''' or """) were replaced with single line string literals because the multi-line literals were introducing unnecessary whitespace and newlines in the string extracted for translation. For example: ''' The foo command takes out the garbage. ''' Would appear in the translation catalog as: "\n The foo command takes out the garbage.\n " The superfluous whitespace and newlines are confusing to translators and requires us to strip leading and trailing whitespace from the translation at run time. * Import statements were moved from below the docstring to above it. This was necessary because the i18n markers are imported functions and must be available before the the doc is parsed. Technically only the import of the i18n markers had to appear before the doc but stylistically it's better to keep all the imports together. * It was observed during the docstring editing process that the command documentation was inconsistent with respect to the use of periods to terminate a sentence. Some doc had a trailing period, others didn't. Consistency was enforced by adding a period to end of every docstring if one was missing.
218 lines
6.4 KiB
Python
218 lines
6.4 KiB
Python
# Authors:
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
#
|
|
# Copyright (C) 2010 Red Hat
|
|
# see file 'COPYING' for use and warranty information
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import copy
|
|
from ipalib import api, _, ngettext
|
|
from ipalib import Flag, Str, List
|
|
from ipalib.request import context
|
|
from ipalib import api, crud, errors
|
|
from ipalib import output
|
|
from ipalib import Object, Command
|
|
|
|
__doc__ = _("""
|
|
Self-service Permissions
|
|
|
|
A permission enables fine-grained delegation of permissions. Access Control
|
|
Rules, or instructions (ACIs), grant permission to permissions to perform
|
|
given tasks such as adding a user, modifying a group, etc.
|
|
|
|
A Self-service permission defines what an object can change in its own entry.
|
|
|
|
|
|
EXAMPLES:
|
|
|
|
Add a self-service rule to allow users to manage their address:
|
|
ipa selfservice-add --permissions=write --attrs=street,postalCode,l,c,st "Users manage their own address"
|
|
|
|
When managing the list of attributes you need to include all attributes
|
|
in the list, including existing ones. Add telephoneNumber to the list:
|
|
ipa selfservice-mod --attrs=street,postalCode,l,c,st,telephoneNumber "Users manage their own address"
|
|
|
|
Display our updated rule:
|
|
ipa selfservice-show "Users manage their own address"
|
|
|
|
Delete a rule:
|
|
ipa selfservice-del "Users manage their own address"
|
|
""")
|
|
|
|
ACI_PREFIX=u"selfservice"
|
|
|
|
def is_selfservice(aciname):
|
|
"""
|
|
Determine if the ACI is a Self-service ACI and raise an exception if it
|
|
isn't.
|
|
|
|
Return the result if it is a self-service ACI.
|
|
"""
|
|
result = api.Command['aci_show'](aciname, aciprefix=ACI_PREFIX)['result']
|
|
if 'selfaci' not in result or result['selfaci'] == False:
|
|
raise errors.NotFound(reason=_('Self-service permission \'%(permission)s\' not found') % dict(permission=aciname))
|
|
return result
|
|
|
|
class selfservice(Object):
|
|
"""
|
|
Selfservice object.
|
|
"""
|
|
|
|
bindable = False
|
|
object_name = _('self service permission')
|
|
object_name_plural = _('self service permissions')
|
|
label = _('Self Service Permissions')
|
|
label_singular = _('Self Service Permission')
|
|
|
|
takes_params = (
|
|
Str('aciname',
|
|
cli_name='name',
|
|
label=_('Self-service name'),
|
|
doc=_('Self-service name'),
|
|
primary_key=True,
|
|
),
|
|
List('permissions?',
|
|
cli_name='permissions',
|
|
label=_('Permissions'),
|
|
doc=_('Comma-separated list of permissions to grant ' \
|
|
'(read, write). Default is write.'),
|
|
),
|
|
List('attrs',
|
|
cli_name='attrs',
|
|
label=_('Attributes'),
|
|
doc=_('Comma-separated list of attributes'),
|
|
normalizer=lambda value: value.lower(),
|
|
),
|
|
)
|
|
|
|
def __json__(self):
|
|
json_friendly_attributes = (
|
|
'label', 'label_singular', 'takes_params', 'bindable', 'name',
|
|
'object_name', 'object_name_plural',
|
|
)
|
|
json_dict = dict(
|
|
(a, getattr(self, a)) for a in json_friendly_attributes
|
|
)
|
|
json_dict['primary_key'] = self.primary_key.name
|
|
json_dict['methods'] = [m for m in self.methods]
|
|
return json_dict
|
|
|
|
api.register(selfservice)
|
|
|
|
|
|
class selfservice_add(crud.Create):
|
|
__doc__ = _('Add a new self-service permission.')
|
|
|
|
msg_summary = _('Added selfservice "%(value)s"')
|
|
|
|
def execute(self, aciname, **kw):
|
|
if not 'permissions' in kw:
|
|
kw['permissions'] = (u'write',)
|
|
kw['selfaci'] = True
|
|
kw['aciprefix'] = ACI_PREFIX
|
|
result = api.Command['aci_add'](aciname, **kw)['result']
|
|
del result['aciprefix'] # do not include prefix in result
|
|
|
|
return dict(
|
|
result=result,
|
|
value=aciname,
|
|
)
|
|
|
|
api.register(selfservice_add)
|
|
|
|
|
|
class selfservice_del(crud.Delete):
|
|
__doc__ = _('Delete a self-service permission.')
|
|
|
|
has_output = output.standard_boolean
|
|
msg_summary = _('Deleted selfservice "%(value)s"')
|
|
|
|
def execute(self, aciname, **kw):
|
|
is_selfservice(aciname)
|
|
kw['aciprefix'] = ACI_PREFIX
|
|
result = api.Command['aci_del'](aciname, **kw)
|
|
|
|
return dict(
|
|
result=True,
|
|
value=aciname,
|
|
)
|
|
|
|
api.register(selfservice_del)
|
|
|
|
|
|
class selfservice_mod(crud.Update):
|
|
__doc__ = _('Modify a self-service permission.')
|
|
|
|
msg_summary = _('Modified selfservice "%(value)s"')
|
|
|
|
def execute(self, aciname, **kw):
|
|
is_selfservice(aciname)
|
|
if 'attrs' in kw and kw['attrs'] is None:
|
|
raise errors.RequirementError(name='attrs')
|
|
|
|
kw['aciprefix'] = ACI_PREFIX
|
|
result = api.Command['aci_mod'](aciname, **kw)['result']
|
|
del result['aciprefix'] # do not include prefix in result
|
|
return dict(
|
|
result=result,
|
|
value=aciname,
|
|
)
|
|
|
|
api.register(selfservice_mod)
|
|
|
|
|
|
class selfservice_find(crud.Search):
|
|
__doc__ = _('Search for a self-service permission.')
|
|
|
|
msg_summary = ngettext(
|
|
'%(count)d selfservice matched', '%(count)d selfservices matched', 0
|
|
)
|
|
|
|
def execute(self, term, **kw):
|
|
kw['selfaci'] = True
|
|
kw['aciprefix'] = ACI_PREFIX
|
|
result = api.Command['aci_find'](term, **kw)['result']
|
|
|
|
for aci in result:
|
|
del aci['aciprefix'] # do not include prefix in result
|
|
|
|
return dict(
|
|
result=result,
|
|
count=len(result),
|
|
truncated=False,
|
|
)
|
|
|
|
api.register(selfservice_find)
|
|
|
|
|
|
class selfservice_show(crud.Retrieve):
|
|
__doc__ = _('Display information about a self-service permission.')
|
|
|
|
has_output_params = (
|
|
Str('aci',
|
|
label=_('ACI'),
|
|
),
|
|
)
|
|
|
|
def execute(self, aciname, **kw):
|
|
result = is_selfservice(aciname)
|
|
del result['aciprefix'] # do not include prefix in result
|
|
return dict(
|
|
result=result,
|
|
value=aciname,
|
|
)
|
|
|
|
api.register(selfservice_show)
|