在文件A中包含一个python文件B,其中B使用在A中定义的变量

时间:2016-08-26 12:25:04

标签: python django

我有一个Django项目,我有多个设置文件,其中包含大量冗余数据。结构如下

development.py

app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)

staging.py

app_one = 'http://staging.xyz.abc.com'
app_two = 'http://staging.pqr.abc.com'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)

production.py

app_one = 'http://production.xyz.abc.com'
app_two = 'http://production.pqr.abc.com'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)

在所有文件abc_urlxyz_url中基本相同的网址。唯一改变的是域名。

我期待的是,

  • 将所有网址放在名为app_one_urls.py和app_two_urls.py的单独文件中
  • 在我的开发,分段和制作文件中找到将此app_one_urls.py和app_two_urls.py包含在内的方法

最终结果如下:

app_one_urls.py

abc_url = '{0}/some-url/'.format(app_one)

app_two_urls.py

xyz_url = '{0}/some-url/'.format(app_two)

是两个单独的文件

在development.py中

我打算做以下

app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'
somehow get urls from app_one_urls and app_two_urls

可行且可行吗?如果是的话,我需要帮助来理解如何。

2 个答案:

答案 0 :(得分:1)

无需维护单独的文件,您可以根据环境类型使用key在字典中定义配置。

我在此基于hostname进行演示,因为我的服务器的主机名有:my-host-prodmy-host-stagingmy-host-dev您可以使用唯一定义服务器的条件。

import socket

def get_url_conf(my_host=socket.gethostname()):
    def get_conf_setting(env_type):
        return {'prod':    {'app1': 'app1_prod_url',
                            'app2': 'app2_prod_url'},
                'staging': {'app1': 'app1_staging_url',
                            'app2': 'app2_staging_url'},
                'dev':     {'app1': 'app1_dev_url',
                            'app2': 'app2_dev_url'},
                'local':   {'app1': 'app1_local_url',
                            'app2': 'app2_local_url'}
                }[env_type]
    if my_host.endswith('-prod'):
        server_key = 'prod'
    elif my_host.endswith('-staging'):
        server_key = 'prod'
    elif my_host.endswith('-dev'):
        server_key = 'dev'
    else:  # In case someone is running on local system
        server_key = 'local'
    return get_conf_setting(server_key)

现在,在您的设置文件中,您可以将其称为:

abc_url = '{0}/some-url/'.format(get_url_conf()[`app1`])
xyz_url = '{0}/some-url/'.format(get_url_conf()[`app2`]) 

答案 1 :(得分:0)

是的,这是可行的。 您需要将设置作为模块进行排列:

settings/
        /__init__.py
        /development.py
        /staging.py
        /production.py
        /base.py

base.py的内容:

SOME_ENVIRONMENT_INDEPENDENT_SETTING = 1

development.py的内容:

from base import *
SOME_DEVELOPMENT_SETTING = 1
app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'

production.py的内容:

    from base import *
    SOME_PRODUCTION_SETTING = 1
    app_one = 'http://production.xyz.abc.com'
    app_two = 'http://production.pqr.abc.com'

__init__.py的内容:

import os
#If you want to pull environment from an environment variable

#ENVIRONMENT = os.environ.get('CURR_ENV','PROD') 

ENVIRONMENT = "DEVELOPMENT"

if ENVIRONMENT == "PRODUCTION" :
    try:
        from production import *
    except:
        pass

elif ENVIRONMENT == "DEVELOPMENT" :
    try:
        from development import *
    except:
        pass

elif ENVIRONMENT == "STAGING":
    try:
        from staging import *
    except:
        pass

elif ENVIRONMENT.lower() == "DEVEL_LOCAL".lower():
    try:
        from devel_local import *
    except:
        pass

elif ENVIRONMENT.lower() == "PROD_PP".lower():
    try:
        from prod_pp import *
    except:
        pass


#common variables which change based on environment
abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)