# -*- coding: utf-8 -*-

import os
from werkzeug.utils import import_string, find_modules
from flask import Flask, Blueprint


class App(Flask):
    """Custom Flask Class."""

    def __init__(
            self,
            import_name,
            config=None,
            packages=None,
            extensions=None,
            middlewares=None,
            errorhandlers=None):
        super(App, self).__init__(
            import_name,
            instance_path=os.getcwd(),
            instance_relative_config=True)

        self.packages = packages if packages else []
        self.extensions = extensions if extensions else []
        self.middlewares = middlewares if middlewares else []
        self.errorhandlers = errorhandlers if errorhandlers else[]

        # config
        if config:
            self.config.from_pyfile(config)
        elif os.getenv('FLASK') == 'dev':
            self.config.from_pyfile('config/development.conf')
            self.logger.info("Config: Development")
        elif os.getenv('FLASK') == 'test':
            self.config.from_pyfile('config/test.conf')
            self.logger.info("Config: Test")
        else:
            self.config.from_pyfile('config/production.conf')
            self.logger.info("Config: Production")

        self.configure_extensions(self.extensions)
        self.configure_middlewares(self.middlewares)
        self.configure_errorhandlers(self.errorhandlers)

        # register module
        self.configure_packages(self.packages)

    def configure_extensions(self, extensions):
        for extension in extensions.__dict__.items():
            if hasattr(extension, 'init_app'):
                extension.init_app(self)

    def configure_packages(self, packages):
        for package_name in packages:
            package_name = '%s.%s' % (self.import_name, package_name)
            modules = find_modules(package_name)
            for module in modules:
                __import__(module)

            package = import_string(package_name)
            for attr_name in dir(package):
                attr = getattr(package, attr_name)
                if isinstance(attr, Blueprint):
                    self.register_blueprint(attr)

    def configure_middlewares(self, middlewares):
        for middleware in middlewares:
            middleware(self)

    def configure_errorhandlers(self, errorhandlers):
        for errorhandler in errorhandlers:
            errorhandler(self)

@@ -28,16 +28,31 @@
 port os%0A
+import inspect%0A
 from wer
@@ -531,17 +531,16 @@
 g=True)%0A
-%0A
         
@@ -600,22 +600,16 @@
 self.ext
-ension
 s = exte
@@ -1318,22 +1318,16 @@
 self.ext
-ension
 s)%0A     
@@ -1603,12 +1603,13 @@
 t__.
-item
+value
 s():
@@ -1624,16 +1624,51 @@
      if 
+not inspect.isclass(extension) and 
 hasattr(

fix bugs