2011-06-29 11 views
9

मैं पाइथन में एक आईएनआई स्टाइल कॉन्फ़िगर पार्सर की तलाश में हूं जो PHP में करता है Zend_Config_Ini के समान सेक्शन विरासत का समर्थन करता है।पायथन कॉन्फ़िगर पार्सर जो अनुभाग विरासत का समर्थन करता है?

क्या ऐसा मॉड्यूल मौजूद है या मुझे अपना खुद का रोल करने की आवश्यकता होगी?

उत्तर

12

पायथन का कॉन्फ़िगरर एकाधिक फाइल लोड कर सकता है। फ़ाइलों को बाद में पढ़ा जा सकता है पहली फ़ाइल से सेटिंग्स ओवरराइड कर सकते हैं।

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

मैं एक ही अनुभाग लेकिन विभिन्न मूल्यों से युक्त एक "environment.ini" फ़ाइल के साथ एक अलग सर्वर पर इन ओवरराइड:

उदाहरण के लिए, अपने आवेदन अपने आंतरिक डिफ़ॉल्ट विन्यास फाइल में डेटाबेस सेटिंग है :

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

अजगर में:

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

जानकारी के लिए धन्यवाद। दुर्भाग्य से यह मेरे लिए एक मास्टर फ़ाइल रखने की व्यावसायिक आवश्यकता के कारण काम नहीं करेगा जिसे कई प्रोग्रामिंग भाषाओं में पार्स किया जाएगा। ऐसा लगता है कि मुझे खुद को लागू करने की आवश्यकता होगी। – Maascamp

+0

मास्कैम्प: क्या आप सफल हुए? मेरे पास एक ही स्थिति है ... – xvga

+2

हां, मैंने अपनी आवश्यकताओं को पूरा करने के लिए एक लागू किया (Zend_Config_Ini शैली) और जहां संभव हो पाइथन देशी प्रकारों में परिवर्तित हो जाता है। यहां देखें [https://bitbucket.org/maascamp/pyconfigini ](https://bitbucket.org/maascamp/pyconfigini)। आशा करता हूँ की ये काम करेगा। – Maascamp

0

मुझे भी कोई तैयार समाधान नहीं मिला।

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 

प्रतिबंध:: इसे हल करने के लिए, मैं माता पिता खंड में बच्चे अनुभाग में खोज और बाद में करने के लिए ConfigParser का कार्य प्राप्त अनुकूलित

  • केवल रीड-ओनली पहुंच समर्थित config को
  • विरासत का केवल एक स्तर
1

यहां मैंने जो भी उपयोग किया है। extended_get विधि है जो आपको चाहिए - यह पदानुक्रमित वर्गों का समर्थन करता है।

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

धन्यवाद रोमन! किसी भी तरह पुरानी विधि का नाम कोड में घुस गया :) – xvga

संबंधित मुद्दे