Example #1
0
    def read(self):
        if not self.filename:
            raise FilenameError

        try:
            handler = open(self.filename, self.filemode_to_read)
        except (IOError, OSError), e:
            message = 'Cannot open %r file for read in %r mode' % \
                      (self.filename, self.filemode_to_read)
            logger.error(message)

            return {}
Example #2
0
    def save(self):
        if not self.filename:
            raise FilenameError

        try:
            handler = open(self.filename, self.filemode_to_save)
        except (IOError, OSError), e:
            message = 'Cannot open %r file for write in %r mode' % \
                      (self.filename, self.filemode_to_save)
            logger.error(message)

            raise e
Example #3
0
    def autoconf(self):
        """
        Auto configure ``setman`` library.
        """
        # Do we work with Django?
        try:
            from django.conf import settings
            settings.SETTINGS_MODULE
        except ImportError:
            pass
        else:
            return self.configure(framework='setman.frameworks.django_setman')

        message = 'Not enough data to auto configure setman library. Please, '\
                  'call ``settings.configure`` manually.'
        logger.error(message)

        raise ImproperlyConfigured(message)
Example #4
0
    def autoconf(self):
        """
        Auto configure ``setman`` library.
        """
        # Do we work with Django?
        try:
            from django.conf import settings
            settings.SETTINGS_MODULE
        except ImportError:
            pass
        else:
            return self.configure(framework='setman.frameworks.django_setman')

        message = 'Not enough data to auto configure setman library. Please, '\
                  'call ``settings.configure`` manually.'
        logger.error(message)

        raise ImproperlyConfigured(message)
Example #5
0
    def configure(self, backend=None, framework=None, **kwargs):
        """
        Setup which backend will be used for reading and saving settings data
        and which framework will be used for searching for available settings.
        """
        assert not self._configured, '``LazySettings`` instance already ' \
                                     'configured. Backend: %r, framework: ' \
                                     '%r' % (self._backend, self._framework)

        if framework:
            # Import framework module by the Python path
            try:
                framework_module = importlib.import_module(framework)
            except ImportError:
                message = 'Cannot import framework module from %r path' % \
                          framework
                logger.error(message)

                raise ImproperlyConfigured(message)

            # Load framework class from module if possible
            try:
                framework_klass = getattr(framework_module, 'Framework')
            except AttributeError, e:
                message = 'Cannot import framework class from %r module' % \
                          framework
                logger.error(message)

                raise ImproperlyConfigured(message)

            # Framework class should be an instance of ``SetmanFramework``
            if not issubclass(framework_klass, SetmanFramework):
                message = '%r is not a subclass of %r' % \
                          (framework_klass, SetmanFramework)
                logger.error(message)

                raise ImproperlyConfigured(message)
Example #6
0
    def configure(self, backend=None, framework=None, **kwargs):
        """
        Setup which backend will be used for reading and saving settings data
        and which framework will be used for searching for available settings.
        """
        assert not self._configured, '``LazySettings`` instance already ' \
                                     'configured. Backend: %r, framework: ' \
                                     '%r' % (self._backend, self._framework)

        if framework:
            # Import framework module by the Python path
            try:
                framework_module = importlib.import_module(framework)
            except ImportError:
                message = 'Cannot import framework module from %r path' % \
                          framework
                logger.error(message)

                raise ImproperlyConfigured(message)

            # Load framework class from module if possible
            try:
                framework_klass = getattr(framework_module, 'Framework')
            except AttributeError, e:
                message = 'Cannot import framework class from %r module' % \
                          framework
                logger.error(message)

                raise ImproperlyConfigured(message)

            # Framework class should be an instance of ``SetmanFramework``
            if not issubclass(framework_klass, SetmanFramework):
                message = '%r is not a subclass of %r' % \
                          (framework_klass, SetmanFramework)
                logger.error(message)

                raise ImproperlyConfigured(message)
Example #7
0
            if not issubclass(framework_klass, SetmanFramework):
                message = '%r is not a subclass of %r' % \
                          (framework_klass, SetmanFramework)
                logger.error(message)

                raise ImproperlyConfigured(message)
        else:
            # If no framework would be set - we will use default class
            framework_klass = SetmanFramework

        if backend:
            try:
                backend_module = importlib.import_module(backend)
            except ImportError, e:
                message = 'Cannot import backend module from %r path' % backend
                logger.error(message)

                raise ImproperlyConfigured(message)

            try:
                backend_klass = getattr(backend_module, 'Backend')
            except AttributeError, e:
                message = 'Cannot import backend class from %r module' % \
                          backend
                logger.error(message)

                raise ImproperlyConfigured(message)

            if not issubclass(backend_klass, SetmanBackend):
                message = '%r is not a subclass of %r' % \
                          (backend_klass, SetmanBackend)
Example #8
0
    def _parse_choices(self, value):
        """
        Convert string value to valid choices tuple.

        **Supported formats:**

        * a, b, c
        * (a, A), (b, B), (c, C)
        * a { b, c }, d { e, f }
        * A { (b, B), (c, C) }, D { (e, E), (f, F) }
        * path.to.CHOICES
        * path.to.Model.CHOICES

        """
        # Start parsing with internal choices
        if not ',' in value and '.' in value:
            # Choices tuple should be last part of value
            path, attr = value.rsplit('.', 1)

            # Load choices from module
            try:
                module = importlib.import_module(path)
            except ImportError:
                # Or from module class
                try:
                    module = load_from_path(path)
                except (AttributeError, ImportError):
                    logger.exception('Cannot load choices from %r path', value)
                    return ()

            # Try to get choices attr in module
            try:
                choices = getattr(module, attr)
            except AttributeError:
                logger.exception('Cannot load choices from %r path', value)
                return ()
        elif not '{' in value and not '}' in value:
            # Parse choice with labels
            label_re = re.compile(r'\(([^,]+),\s+([^\)]+)\)', re.M)
            found = label_re.findall(value)

            if found:
                choices = found
            # If nothing found by regex, just split value by comma and
            # duplicate resulted items
            else:
                choices = map(lambda item: (item.strip(), item.strip()),
                              value.split(','))
        else:
            # Parse groups
            groups_re = re.compile(r'([^{]+){([^}]+)},?', re.M)
            found = groups_re.findall(value)

            if found:
                choices = []

                for group, data in found:
                    group = group.strip()
                    choices.append((group, self._parse_choices(data.strip())))
            else:
                logger.error('Cannot parse choices from %r', value)
                return ()

        return tuple(choices)
Example #9
0
            if not issubclass(framework_klass, SetmanFramework):
                message = '%r is not a subclass of %r' % \
                          (framework_klass, SetmanFramework)
                logger.error(message)

                raise ImproperlyConfigured(message)
        else:
            # If no framework would be set - we will use default class
            framework_klass = SetmanFramework

        if backend:
            try:
                backend_module = importlib.import_module(backend)
            except ImportError, e:
                message = 'Cannot import backend module from %r path' % backend
                logger.error(message)

                raise ImproperlyConfigured(message)

            try:
                backend_klass = getattr(backend_module, 'Backend')
            except AttributeError, e:
                message = 'Cannot import backend class from %r module' % \
                          backend
                logger.error(message)

                raise ImproperlyConfigured(message)

            if not issubclass(backend_klass, SetmanBackend):
                message = '%r is not a subclass of %r' % \
                          (backend_klass, SetmanBackend)
Example #10
0
    def _parse_choices(self, value):
        """
        Convert string value to valid choices tuple.

        **Supported formats:**

        * a, b, c
        * (a, A), (b, B), (c, C)
        * a { b, c }, d { e, f }
        * A { (b, B), (c, C) }, D { (e, E), (f, F) }
        * path.to.CHOICES
        * path.to.Model.CHOICES

        """
        # Start parsing with internal choices
        if not ',' in value and '.' in value:
            # Choices tuple should be last part of value
            path, attr = value.rsplit('.', 1)

            # Load choices from module
            try:
                module = importlib.import_module(path)
            except ImportError:
                # Or from module class
                try:
                    module = load_from_path(path)
                except (AttributeError, ImportError):
                    logger.exception('Cannot load choices from %r path',
                                     value)
                    return ()

            # Try to get choices attr in module
            try:
                choices = getattr(module, attr)
            except AttributeError:
                logger.exception('Cannot load choices from %r path', value)
                return ()
        elif not '{' in value and not '}' in value:
            # Parse choice with labels
            label_re = re.compile(r'\(([^,]+),\s+([^\)]+)\)', re.M)
            found = label_re.findall(value)

            if found:
                choices = found
            # If nothing found by regex, just split value by comma and
            # duplicate resulted items
            else:
                choices = map(lambda item: (item.strip(), item.strip()),
                              value.split(','))
        else:
            # Parse groups
            groups_re = re.compile(r'([^{]+){([^}]+)},?', re.M)
            found = groups_re.findall(value)

            if found:
                choices = []

                for group, data in found:
                    group = group.strip()
                    choices.append((group, self._parse_choices(data.strip())))
            else:
                logger.error('Cannot parse choices from %r', value)
                return ()

        return tuple(choices)