def get_storage_subclass(self): """ Import a storage class and return a subclass that will always return eq True to avoid creating a new migration when for runtime storage class changes. """ try: imported_storage_class = import_string( dotted_path=self.dotted_path) except Exception as exception: message = self.error_message or _( 'Unable to initialize storage: %(name)s. Check the storage ' 'backend dotted path and arguments.') % { 'name': self.name } logger.fatal(message) raise_from(value=TypeError(message), from_value=exception) class DynamicStorageSubclass(imported_storage_class): def __init__(self, *args, **kwargs): return super(DynamicStorageSubclass, self).__init__(*args, **kwargs) def __eq__(self, other): return True def deconstruct(self): return ('mayan.apps.storage.classes.FakeStorageSubclass', (), {}) return DynamicStorageSubclass
def _parse_install_system(self, command_line): parser = argparse.ArgumentParser() parser.add_argument('-n', dest='name') parser.add_argument('-m', '--memory') parser.add_argument('-d', '--disks', action='append') args, rest = parser.parse_known_args(command_line.split()) try: total_disk = 0 for disk in args.disks: expr_disk = disk.split(':') try: disk_size, = expr_disk except ValueError: _, disk_size = expr_disk total_disk += _scientific_int(disk_size) except TypeError as exc: six.raise_from(serializers.ValidationError('No disk specified'), exc) if args.memory is None: raise serializers.ValidationError('No mem specified') else: memory = _scientific_int(args.memory) if args.name is None: raise serializers.ValidationError('No name specified') else: name = args.name return name, memory, total_disk
def _send(self, email_message): """A helper method that does the actual sending.""" if not email_message.recipients(): return False from_email = sanitize_address(email_message.from_email, email_message.encoding) recipients = [sanitize_address(addr, email_message.encoding) for addr in email_message.recipients()] try: r = requests.post( self._api_url + "messages.mime", auth=("api", self._access_key), data={ "to": ", ".join(recipients), "from": from_email, }, files={ "message": StringIO( email_message.message().as_bytes(linesep="\r\n")) } ) except requests.exceptions.RequestException as e: if not self.fail_silently: six.raise_from(smtplib.SMTPException("Could not send mail"), e) return False if r.status_code != 200: if not self.fail_silently: raise smtplib.SMTPException("Mailgun server returned code {" "}".format(r.status_code)) return False return True
def get_header(self, msg, header): # msg parameter to maintain compatibility with # modoboa_webmail.lib.imapemail.ImapEmail if header in msg: decoded_values = [] for value, encoding in email.header.decode_header(msg[header]): if not len(value): continue if encoding: value = smart_text(value, encoding=encoding) elif isinstance(value, six.binary_type): # SMTPUTF8 fallback (most of the time) # Address contains non ASCII chars but is not RFC2047 # encoded... encoding = chardet.detect(value) try: value = value.decode(encoding["encoding"], "replace") except (TypeError, UnicodeDecodeError) as exc: six.raise_from( InternalError( _("unable to determine encoding of string") ), exc ) decoded_values.append(value) return "".join(decoded_values) return ""
def get_template(self, template_name): try: lookup_template = self.lookup.get_template(template_name) return ContextProcessorTemplate(self, lookup_template) except TemplateLookupException as exc: six.raise_from(TemplateDoesNotExist, exc) except SyntaxException as exc: six.raise_from(TemplateSyntaxError, exc)
def get_storage_instance(self): try: return self.get_storage_subclass()(**self.kwargs) except Exception as exception: message = self.error_message or _( 'Unable to initialize storage: %(name)s. Check the storage ' 'backend dotted path and arguments.') % { 'name': self.name } logger.fatal(message) raise_from(value=TypeError(message), from_value=exception)
def __init__(self, *args, **kwargs): access_key, server_name = (kwargs.pop('access_key', None), kwargs.pop('server_name', None)) super(MailgunBackend, self).__init__(*args, **kwargs) try: self._access_key = access_key or getattr(settings, 'MAILGUN_ACCESS_KEY') self._server_name = server_name or getattr(settings, 'MAILGUN_SERVER_NAME') except AttributeError as e: six.raise_from(ImproperlyConfigured(*e.args), e) self._api_url = "https://api.mailgun.net/v3/%s/" % self._server_name
def decode(value_bytes, encoding, append_to_error=""): """Try to decode the given string.""" assert isinstance(value_bytes, six.binary_type),\ "value_bytes should be of type %s" % six.binary_type.__name__ if len(value_bytes) == 0: # short circuit for empty strings return "" try: value = value_bytes.decode(encoding) except UnicodeDecodeError: encoding = chardet.detect(value_bytes) try: value = value_bytes.decode(encoding["encoding"], "replace") except (TypeError, UnicodeDecodeError) as exc: six.raise_from( InternalError( _("unable to determine encoding of string") + append_to_error), exc) return value
def resolve_field(model_field, lookup_expr): """ Resolves a ``lookup_expr`` into its final output field, given the initial ``model_field``. The lookup expression should only contain transforms and lookups, not intermediary model field parts. Note: This method is based on django.db.models.sql.query.Query.build_lookup For more info on the lookup API: https://docs.djangoproject.com/en/1.9/ref/models/lookups/ """ query = model_field.model._default_manager.all().query lhs = Expression(model_field) lookups = lookup_expr.split(LOOKUP_SEP) assert len(lookups) > 0 try: while lookups: name = lookups[0] args = (lhs, name) if django.VERSION < (2, 0): # rest_of_lookups was removed in Django 2.0 args += (lookups,) # If there is just one part left, try first get_lookup() so # that if the lhs supports both transform and lookup for the # name, then lookup will be picked. if len(lookups) == 1: final_lookup = lhs.get_lookup(name) if not final_lookup: # We didn't find a lookup. We are going to interpret # the name as transform, and do an Exact lookup against # it. lhs = query.try_transform(*args) final_lookup = lhs.get_lookup('exact') return lhs.output_field, final_lookup.lookup_name lhs = query.try_transform(*args) lookups = lookups[1:] except FieldError as e: six.raise_from(FieldLookupError(model_field, lookup_expr), e)
def handle(self, *args, **options): """Command entry point.""" exts_pool.load_all() for filename in options["files"]: try: with transaction.atomic(): self._import(filename, options) except CommandError as exc: raise exc except UnicodeDecodeError: self.stdout.write(self.style.NOTICE( _("CSV file is not encoded in UTF-8, attempting to guess " "encoding") )) detector = UniversalDetector() with io.open(filename, "rb") as fp: for line in fp: detector.feed(line) if detector.done: break detector.close() self.stdout.write(self.style.NOTICE( _("Reading CSV file using %(encoding)s encoding") % detector.result )) try: with transaction.atomic(): self._import( filename, options, encoding=detector.result["encoding"] ) except UnicodeDecodeError as exc: six.raise_from( CommandError( _("Unable to decode CSV file using %(encoding)s " "encoding") % detector.result ), exc )
def decode(value_bytes, encoding, append_to_error=""): """Try to decode the given string.""" assert isinstance(value_bytes, six.binary_type),\ "value_bytes should be of type %s" % six.binary_type.__name__ if len(value_bytes) == 0: # short circuit for empty strings return "" try: value = value_bytes.decode(encoding) except UnicodeDecodeError: encoding = chardet.detect(value_bytes) try: value = value_bytes.decode( encoding["encoding"], "replace" ) except (TypeError, UnicodeDecodeError) as exc: six.raise_from( InternalError( _("unable to determine encoding of string") + append_to_error ), exc ) return value
def operation_clear_old_cache(apps, schema_editor): try: storage_documentimagecache = get_storage_subclass( dotted_path=setting_documentimagecache_storage.value)( **setting_documentimagecache_storage_arguments.value) except Exception as exception: message = ( 'Unable to initialize the document image cache storage. ' 'Check the settings {} and {} for formatting errors.'.format( setting_documentimagecache_storage.global_name, setting_documentimagecache_storage_arguments.global_name)) logger.fatal(message) raise_from(value=TypeError(message), from_value=exception) DocumentPageCachedImage = apps.get_model( app_label='documents', model_name='DocumentPageCachedImage') for cached_image in DocumentPageCachedImage.objects.using( schema_editor.connection.alias).all(): # Delete each cached image directly since the model doesn't exists and # will not trigger the physical deletion of the stored file storage_documentimagecache.delete(cached_image.filename) cached_image.delete()
from __future__ import unicode_literals import logging from django.utils.six import raise_from from mayan.apps.storage.utils import get_storage_subclass from .settings import ( setting_staging_file_image_cache_storage, setting_staging_file_image_cache_storage_arguments, ) logger = logging.getLogger(__name__) try: storage_staging_file_image_cache = get_storage_subclass( dotted_path=setting_staging_file_image_cache_storage.value)( **setting_staging_file_image_cache_storage_arguments.value) except Exception as exception: message = ( 'Unable to initialize the staging file image cache storage. Check ' 'the settings {} and {} for formatting errors.'.format( setting_staging_file_image_cache_storage.global_name, setting_staging_file_image_cache_storage_arguments.global_name)) logger.fatal(message) raise_from(value=TypeError(message), from_value=exception)
def from_string(self, template_code): try: return ContextProcessorTemplate(self, Template(template_code)) except SyntaxException as exc: six.raise_from(TemplateSyntaxError, exc)