Univention Bugzilla – Attachment 4464 Details for
Bug 27716
UMC Modul-Entwickler sollten shortcuts an die Hand bekommen
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
univention-system-setup: Verschlankung der einfachen Funktionen
umcp_decorator.patch (text/plain), 4.45 KB, created by
Dirk Wiesenthal
on 2012-06-21 17:40:49 CEST
(
hide
)
Description:
univention-system-setup: Verschlankung der einfachen Funktionen
Filename:
MIME Type:
Creator:
Dirk Wiesenthal
Created:
2012-06-21 17:40:49 CEST
Size:
4.45 KB
patch
obsolete
>Index: umc/python/setup/__init__.py >=================================================================== >--- umc/python/setup/__init__.py (Revision 33668) >+++ umc/python/setup/__init__.py (Arbeitskopie) >@@ -55,6 +55,58 @@ > > PATH_SYS_CLASS_NET = '/sys/class/net' > >+def simple_response(function): >+ import inspect >+ from univention.management.console.protocol.message import InvalidArgumentsError >+ from univention.management.console.protocol.definitions import BAD_REQUEST_INVALID_ARGS >+ # argument names of the function, including 'self' >+ argspec = inspect.getargspec(function) >+ # remove self, use all the others >+ arguments = argspec.args[1:] >+ defaults = argspec.defaults >+ # use defaults as dict >+ if defaults: >+ defaults = dict(zip(arguments[-len(defaults):], defaults)) >+ else: >+ defaults = {} >+ def _response(self, request): >+ try: >+ try: >+ # extract arguments from request, raise if not given and no default >+ # pass arguments as **kwargs, not *args to be more flexible with defaults >+ kwargs = {} >+ for arg in arguments: >+ if arg in request.options: >+ kwargs[arg] = request.options[arg] >+ else: >+ kwargs[arg] = defaults[arg] >+ except KeyError, e: >+ # raise InvalidArgs if a mandatory argument was not given >+ raise InvalidArgumentsError(BAD_REQUEST_INVALID_ARGS, 'Required argument "%s" not found' % e) >+ else: >+ try: >+ try: >+ ret = function(self, **kwargs) >+ except InvalidArgumentsError, e: >+ if int(e.args[0]) and e.args[1]: >+ # re-raise a valid InvalidArgumentsError >+ raise e >+ else: >+ self.finished(request.id, ret) >+ except InvalidArgumentsError, e: >+ # this is the re-raised: re-re-raise >+ raise e >+ except: >+ raise InvalidArgumentsError(BAD_REQUEST_INVALID_ARGS, 'The arguments caused the command to fail') >+ # better raise 40x >+ # codes like CLIENT_ERR_NONFATAL 301 are mapped to 500 for HTTP requests. >+ # raise InvalidArgumentsError(CLIENT_ERR_NONFATAL, 'The arguments caused the command to fail') >+ except InvalidArgumentsError, e: >+ self.finished(request.id, None, message=e.args[1], success=False, status=e.args[0]) >+ # copy __doc__, otherwise it would not show up in api and such >+ _response.__doc__ = function.__doc__ >+ return _response >+ > class TimeoutError(Exception): > pass > >@@ -535,25 +587,18 @@ > > self.finished(request.id, locales) > >- def lang_default_timezone(self, request): >+ @simple_response >+ def lang_default_timezone(self, countrycode): > '''Returns default timezone for given locale.''' >- countrycode = request.options.get('countrycode', '') >- timezone = None >- file = open('/lib/univention-installer/locale/countrycode2timezone') >+ with open('/lib/univention-installer/locale/countrycode2timezone') as file: >+ reader = csv.reader(file, delimiter=' ') >+ for row in reader: >+ if row[0].startswith("#"): continue >+ if len(row) > 1: >+ if countrycode.upper() == row[0].upper(): >+ return row[1] >+ return 'Europe/Berlin' > >- reader = csv.reader(file, delimiter=' ') >- for row in reader: >- if row[0].startswith("#"): continue >- if len(row) > 1: >- if countrycode.upper() == row[0].upper(): >- timezone = row[1] >- break >- file.close() >- >- if timezone is None: >- timezone = 'Europe/Berlin' >- self.finished(request.id, timezone) >- > def lang_timezones(self, request): > '''Return a list of all available time zones.''' > try: >@@ -567,26 +612,18 @@ > > self.finished(request.id, timezones) > >- def lang_default_keymap(self, request): >+ @simple_response >+ def lang_default_keymap(self, countrycode): > '''Returns default timezone for given locale.''' >- # use "or ''" to be sure to not get None >- countrycode = (request.options.get('countrycode') or '').upper() >- keymap = None >- file = open('/lib/univention-installer/locale/default-kmaps') >+ with open('/lib/univention-installer/locale/default-kmaps') as file: >+ reader = csv.reader(file, delimiter=':') >+ for row in reader: >+ if row[0].startswith("#"): continue >+ if len(row) > 1: >+ if row[1].upper().startswith(countrycode.upper()): >+ return row[1] >+ return 'us' > >- reader = csv.reader(file, delimiter=':') >- for row in reader: >- if row[0].startswith("#"): continue >- if len(row) > 1: >- if row[1].upper().startswith(countrycode): >- keymap = row[1] >- break >- file.close() >- >- if keymap is None: >- keymap = 'us' >- self.finished(request.id, keymap) >- > def lang_keymaps(self, request): > '''Return a list of all available keyboard layouts.''' > try:
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 27716
: 4464