2011-06-29 27 views

Trả lời

12

Trình cấu hình ConfigParser của Python có thể tải nhiều tệp. Tệp được đọc sau có thể ghi đè cài đặt từ tệp đầu tiên.

Ví dụ, ứng dụng của tôi có các cài đặt cơ sở dữ liệu trong nội bộ mặc định tập tin cấu hình của nó:

[database] 
server = 127.0.0.1 
port = 1234 
... 

tôi ghi đè này trên một máy chủ khác nhau với một tập tin "environment.ini" chứa phần tương tự nhưng giá trị khác nhau :

[database] 
server = 192.168.0.12 
port = 2345 
... 

Trong Python:

import os 
from ConfigParser import ConfigParser 
dbconf = ConfigParser() 
dbconf.readfp(open('default.ini')) 
if os.path.exists('environment.ini'): 
    dbconf.readfp(open('environment.ini')) 
dbconf.get('database', 'server') # Returns 192.168.0.12 
+1

Cảm ơn thông tin. Thật không may điều này sẽ không làm việc cho tôi do yêu cầu kinh doanh của việc có một tệp chính sẽ được phân tích cú pháp bằng một số ngôn ngữ lập trình. Có vẻ như tôi sẽ cần phải thực hiện bản thân mình. – Maascamp

+0

Maascamp: bạn đã thành công chưa? Tôi có cùng một tình huống ... – xvga

+2

Có, tôi đã triển khai một yêu cầu đáp ứng các yêu cầu của tôi (kiểu Zend_Config_Ini) và chuyển đổi thành các kiểu gốc tự nhiên nếu có thể. Xem tại đây [https://bitbucket.org/maascamp/pyconfigini](https://bitbucket.org/maascamp/pyconfigini). Hy vọng nó giúp. – Maascamp

0

Tôi cũng không tìm thấy bất kỳ giải pháp đã sẵn sàng nào. Để giải quyết nó, tôi thích nghi chức năng get của ConfigParser để tìm kiếm trong phần con và sau đó trong phần cha mẹ:

config = SafeConfigParser() 
config.read(filenames) 
required_environment = "mysection" 

# determine fallback requirement in case parameter is not found in required environment 
fallback_environment = "default" 
# loop through all sections of config files 
for environment in config.sections(): 
    # check whether we find an inheritance based on the required section 
    if re.search(required_environment + " *: *\w+", environment): 
     # found inheritance, take parent as fallback section 
     fallback_environment = re.sub(required_environment + r" : (\w+)", r"\1", environment) 
     # take this name as requested section 
     required_environment = environment 

# override get method 
_config_parser_get = config.get 
def myConfigParserGet(id): 
    # check different sections for desired value 
    if config.has_option(required_environment, id): 
     return _config_parser_get(required_environment, id) 
    else: 
     return _config_parser_get(fallback_environment, id) 

config.get = myConfigParserGet 

Hạn chế:

  • chỉ truy cập chỉ đọc để cấu hình được hỗ trợ
  • chỉ có một cấp độ thừa kế
1

Đây là những gì tôi đã sử dụng. Phương pháp extended_get là những gì bạn cần - nó hỗ trợ các phần phân cấp.

import re 
import io 
import ConfigParser 

class ZendConfigParser(ConfigParser.ConfigParser): 
    def extended_get(self, section, key): 
     if self.has_option(section, key): 
      return self.get(section, key) 
     else: 
      orig_section, parent_section = self._get_orig_section(section) 
      if orig_section != None: 
       if self.has_option(orig_section,key): 
        return self.get(orig_section,key) 
       else: 
        return self.extended_get(parent_section,key) 
      else: 
       return None 



    def _get_orig_section(self, zend_section): 
     orig_section = None 
     parent_section = None 
     for section in self.sections(): 
      if re.search(r'^[ \t]*' + zend_section + '\\b', section) != None: 
       orig_section = section 
       #look for a parent section 
       match = re.match(r'\w+[ \t]*:[ \t]*(\w+)$', section) 
       if match != None: 
        parent_section = match.groups()[0] 
       break 

     return (orig_section, parent_section) 

config = ZendConfigParser() 
config.read(file) 
print(config.extended_get('production', 'database.params.host')) 
+0

cảm ơn Roman! Bằng cách nào đó tên phương thức cũ lẻn vào mã :) – xvga

Các vấn đề liên quan