Skip to content

Commit

Permalink
shim for atomic/commit_on_success.
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinius committed Jun 25, 2015
1 parent c6a3919 commit a2d2233
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions eoxserver/resources/coverages/management/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@

import logging
import traceback
from urlparse import urlparse
from optparse import make_option, OptionValueError
from optparse import OptionValueError

from django.core.management.base import BaseCommand, CommandError
from django.db import transaction


Expand All @@ -54,17 +52,17 @@ def _variable_args_cb(option, opt_str, value, parser):
if getattr(parser.values, option.dest):
args.extend(getattr(parser.values, option.dest))
setattr(parser.values, option.dest, args)


class StringFormatCallback(object):
""" Small helper class to supply a variable number of arguments to a
callback function and store the resulting value in the `dest` field of the
parser.
""" Small helper class to supply a variable number of arguments to a
callback function and store the resulting value in the `dest` field of the
parser.
"""

def __init__(self, callback):
self.callback = callback

def __call__(self, option, opt_str, value, parser):
args = []
for arg in parser.rargs:
Expand All @@ -73,12 +71,12 @@ def __call__(self, option, opt_str, value, parser):
else:
del parser.rargs[:len(args)]
break

try:
setattr(parser.values, option.dest, self.callback(" ".join(args)))
except ValueError, e:
raise OptionValueError(str(e))
except ValueError, e:
raise OptionValueError(str(e))


class CommandOutputMixIn(object):
""" Helper mix-in class to ease the handling of user message reporting.
Expand All @@ -87,42 +85,40 @@ class CommandOutputMixIn(object):
def print_err(self, msg):
" Print an error message which is both logged and written to stderr. "

logger.error(msg)
self.stderr.write("ERROR: %s\n"%msg)

logger.error(msg)
self.stderr.write("ERROR: %s\n" % msg)

def print_wrn(self, msg):
""" Print a warning message, which is logged and posibly written to
""" Print a warning message, which is logged and posibly written to
stderr, depending on the set verbosity.
"""

logger.warning(msg)
logger.warning(msg)

if 0 < max(0, getattr(self, "verbosity", 1)):
if 0 < max(0, getattr(self, "verbosity", 1)):
self.stderr.write("WARNING: %s\n" % msg)


def print_msg(self, msg, level=1):
""" Print a basic message with a given level. The message is possibly
logged and/or written to stderr depending on the verbosity setting.
"""

# three basic level of info messages
# three basic level of info messages
# level == 0 - always printed even in the silent mode - not recommended
# level == 1 - normal info suppressed in silent mode
# level >= 2 - debuging message (additional levels allowed)
# level >= 2 - debuging message (additional levels allowed)
# messages ALWAYS logged (as either info or debug)

level = max(0, level)
verbosity = max(0, getattr(self, "verbosity", 1))

# everything with level 2 or higher is DEBUG
level = max(0, level)
verbosity = max(0, getattr(self, "verbosity", 1))

# everything with level 2 or higher is DEBUG
if level >= 2:
prefix = "DEBUG"
logger.debug(msg)

# levels 0 (silent) and 1 (default-verbose)
else:
else:
prefix = "INFO"
logger.info(msg)

Expand All @@ -136,11 +132,10 @@ def print_traceback(self, e, kwargs):
self.print_msg(traceback.format_exc())



def nested_commit_on_success(func):
"""Like commit_on_success, but doesn't commit existing transactions.
This decorator is used to run a function within the scope of a
This decorator is used to run a function within the scope of a
database transaction, committing the transaction on success and
rolling it back if an exception occurs.
Expand All @@ -152,10 +147,14 @@ def nested_commit_on_success(func):
From: https://djangosnippets.org/snippets/1343/
"""

commit_on_success = transaction.commit_on_success(func)
def _nested_commit_on_success(*args, **kwargs):
if transaction.is_managed():
return func(*args, **kwargs)
else:
return commit_on_success(*args, **kwargs)
return transaction.wraps(func)(_nested_commit_on_success)
try:
return transaction.atomic(func)
except AttributeError:
commit_on_success = transaction.commit_on_success(func)

def _nested_commit_on_success(*args, **kwargs):
if transaction.is_managed():
return func(*args, **kwargs)
else:
return commit_on_success(*args, **kwargs)
return transaction.wraps(func)(_nested_commit_on_success)

0 comments on commit a2d2233

Please sign in to comment.