Flask method in mind a set of configuration items!

In doing the project, the way encountered a setting configuration, mainly because the company wrote to all of the configuration of a proprietary configuration database (resources addresses, account number, etc.), the on-line project after what configuration is reads the configuration from this library. But it can not be used in the development, so when developing custom configuration required, and can be removed easily configure the development environment when on-line.

We implement class package files / custom file as in :( myjenkins.py)


class FlaskJenkins(object):

    def __init__(self, app=None):
        self.app = app
        self._j = None
        if app:
            self.init_app(app)

    def init_app(self, app):
        app.config.setdefault('JENKINS_URL', 'https://jenkins.hahha.com')        #设置默认的 配置信息,如果没有配置才会设置成功,如已有自定义配置则不会设置这些默认配置。(实际上公司的配置库里有这个配置,在初始化 app 时就已经在app.config中存在这些配置了,这里是示范。)
        app.config.setdefault('JENKINS_USERNAME', 'username')
        app.config.setdefault('JENKINS_PASSWORD', 'password')
        self._j = Jenkins(
            url=app.config['JENKINS_URL'],
            username=app.config['JENKINS_USERNAME'],
            password=app.config['JENKINS_PASSWORD']
        )

    def get_job_info(self, name, depth=0, fetch_all_builds=False):
        return self._j.get_job_info(name, depth, fetch_all_builds)

    def others_function(*args, **kwargs)
        pass

In extentions.py in:


from myjenkins import FlaskJenkins


jenkins = FlaskJenkins()

app to read custom configuration from the configuration file to see if there is a custom configuration. Profile config.py as follows:

class Config(object):

    CONFIG_CENTER = 'jarvis/config'

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:123456@localhost:3306/jarvis?charset=utf8"

    JENKINS_USERNAME = "root"
    JENKINS_PASSWORD = "passwd"
    JENKINS_URL = "http://localhost:9090"

In the initialization of resources in the project file, to initialize our this jenkins (such as jenkins address, account number), if the above is not configured in a custom configuration file, it will read the default configuration settings.
(If not written in config.py in JENKINS_USERNAME, JENKINS_PASSWORD, JENKINS_URL custom, the default configuration will take effect in FlaskJenkins)


from extentions import jenkins


jenkins.inint_app(app)

In other documents the need to use jenkins, jenkins import directly from extentions.py in:


from extentions import jenkins


# 调用方法
jenkins.other_functions()

Guess you like

Origin www.cnblogs.com/ChangAn223/p/11431009.html