当前位置: 首页>>代码示例>>Python>>正文


Python ConfigParser.has_section方法代码示例

本文整理汇总了Python中exe.engine.configparser.ConfigParser.has_section方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigParser.has_section方法的具体用法?Python ConfigParser.has_section怎么用?Python ConfigParser.has_section使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在exe.engine.configparser.ConfigParser的用法示例。


在下文中一共展示了ConfigParser.has_section方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Config

# 需要导入模块: from exe.engine.configparser import ConfigParser [as 别名]
# 或者: from exe.engine.configparser.ConfigParser import has_section [as 别名]

#.........这里部分代码省略.........
        force creation of the config file in the top dir
        """
        # If there's an EXECONF environment variable, use it
        self.configPath = None
        configFileOptions = map(Path, self._getConfigPathOptions())
        if "EXECONF" in os.environ:
            envconf = Path(os.environ["EXECONF"])
            if envconf.isfile():
                self.configPath = os.environ["EXECONF"]
        # Otherwise find the most appropriate existing file
        if self.configPath is None:
            for confPath in configFileOptions:
                if confPath.isfile():
                    self.configPath = confPath
                    break
            else:
                # If no config files exist, create and use the
                # first one on the list
                self.configPath = configFileOptions[0]
                folder = self.configPath.abspath().dirname()
                if not folder.exists():
                    folder.makedirs()
                self.configPath.touch()
        # Now make our configParser
        self.configParser.read(self.configPath)
        self.configParser.autoWrite = True


    def upgradeFile(self):
        """
        Called before loading the config file,
        removes or upgrades any old settings.
        """
        if self.configParser.has_section('system'):
            system = self.configParser.system
            if system.has_option('appDataDir'):
                # Older config files had configDir stored as appDataDir
                self.configDir = Path(system.appDataDir)
                self.stylesDir =Path(self.configDir)/'style'
                # We'll just upgrade their config file for them for now...
                system.configDir = self.configDir
                system.stylesDir =Path(self.configDir)/'style'
                del system.appDataDir
                
                self.audioMediaConverter_au = system.audioMediaConverter_au
                self.audioMediaConverter_wav = system.audioMediaConverter_wav
                self.videoMediaConverter_ogv = system.videoMediaConverter_ogv
                self.videoMediaConverter_3gp = system.videoMediaConverter_3gp
                self.videoMediaConverter_avi = system.videoMediaConverter_avi
                self.videoMediaConverter_mpg = system.videoMediaConverter_mpg
                self.audioMediaConverter_ogg = system.audioMediaConverter_ogg
                self.audioMediaConverter_mp3 = system.audioMediaConverter_mp3
                self.ffmpegPath = system.ffmpegPath
            
            self.mediaProfilePath = system.mediaProfilePath
                
            if system.has_option('greDir'):
                # No longer used, system should automatically support
                del system.greDir


    def loadSettings(self):
        """
        Loads the settings from the exe.conf file.
        Overrides the defaults set in __init__
        """
开发者ID:kohnle-lernmodule,项目名称:exe201based,代码行数:70,代码来源:config.py

示例2: __init__

# 需要导入模块: from exe.engine.configparser import ConfigParser [as 别名]
# 或者: from exe.engine.configparser.ConfigParser import has_section [as 别名]
class Config:
    """
    The Config class contains the configuration information for eXe.
    """

    optionNames = {
        "system": ("webDir", "xulDir", "port", "dataDir", "configDir", "localeDir", "browserPath"),
        "user": ("locale",),
    }

    def __init__(self):
        """
        Initialise
        """
        self.configPath = None
        self.configParser = ConfigParser()
        self.exePath = Path(sys.argv[0]).abspath()
        self.webDir = self.exePath.dirname()
        self.xulDir = self.exePath.dirname()
        self.localeDir = self.exePath.dirname() / "locale"
        self.port = 51235
        self.dataDir = Path(".")
        self.configDir = Path(".")
        self.browserPath = Path("firefox")
        self.locale = chooseDefaultLocale(self.localeDir)
        self.styles = []
        self._overrideDefaultVals()
        self.webDir = Path(self.webDir)
        if not (self.webDir / "scripts").isdir() and (self.webDir / "webui").isdir():
            self.webDir /= "webui"
        self.xulDir = Path(self.xulDir)
        if not (self.xulDir / "scripts").isdir() and (self.xulDir / "xului").isdir():
            self.xulDir /= "xului"
        self.__setConfigPath()
        self._writeDefaultConfigFile()
        self.loadSettings()
        self.setupLogging()
        self.loadStyles()
        self.loadLocales()

    def _overrideDefaultVals(self):
        """
        Override this to override the
        default config values
        """

    def _getConfigPathOptions(self):
        """
        Override this to give a list of
        possible config filenames
        in order of preference
        """
        return ["exe.conf"]

    def _writeDefaultConfigFile(self):
        """
        [Over]writes 'self.configPath' with a default config file 
        (auto write is on so we don't need to write the file at the end)
        """
        for sectionName, optionNames in self.optionNames.items():
            for optionName in optionNames:
                defaultVal = getattr(self, optionName)
                self.configParser.setdefault(sectionName, optionName, defaultVal)
        self.configParser.setdefault("logging", "root", "INFO")

    def __setConfigPath(self):
        """
        sets self.configPath to the filename of the config file that we'll
        use.
        In descendant classes set self.configFileOptions to a list
        of directories where the configDir should be in order of preference.
        If no config files can be found in these dirs, it will
        force creation of the config file in the top dir
        """
        self.configPath = None
        configFileOptions = map(Path, self._getConfigPathOptions())
        if "EXECONF" in os.environ:
            envconf = Path(os.environ["EXECONF"])
            if envconf.isfile():
                self.configPath = os.environ["EXECONF"]
        if self.configPath is None:
            for confPath in configFileOptions:
                if confPath.isfile():
                    self.configPath = confPath
                    break
            else:
                self.configPath = configFileOptions[0]
                folder = self.configPath.abspath().dirname()
                if not folder.exists():
                    folder.makedirs()
                self.configPath.touch()
        self.configParser.read(self.configPath)
        self.configParser.autoWrite = True

    def upgradeFile(self):
        """
        Called before loading the config file,
        removes or upgrades any old settings.
        """
        if self.configParser.has_section("system"):
#.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:103,代码来源:

示例3: __init__

# 需要导入模块: from exe.engine.configparser import ConfigParser [as 别名]
# 或者: from exe.engine.configparser.ConfigParser import has_section [as 别名]

#.........这里部分代码省略.........
        force creation of the config file in the top dir
        """
        # If there's an EXECONF environment variable, use it
        self.configPath = None
        configFileOptions = map(Path, self._getConfigPathOptions())
        if "EXECONF" in os.environ:
            envconf = Path(os.environ["EXECONF"])
            if envconf.isfile():
                self.configPath = os.environ["EXECONF"]
        # Otherwise find the most appropriate existing file
        if self.configPath is None:
            for confPath in configFileOptions:
                if confPath.isfile():
                    self.configPath = confPath
                    break
            else:
                # If no config files exist, create and use the
                # first one on the list
                self.configPath = configFileOptions[0]
                folder = self.configPath.abspath().dirname()
                if not folder.exists():
                    folder.makedirs()
                self.configPath.touch()
        # Now make our configParser
        self.configParser.read(self.configPath)
        self.configParser.autoWrite = True


    def upgradeFile(self):
        """
        Called before loading the config file,
        removes or upgrades any old settings.
        """
        if self.configParser.has_section('system'):
            system = self.configParser.system
            if system.has_option('appDataDir'):
                # Older config files had configDir stored as appDataDir
                self.configDir = Path(system.appDataDir)
                # We'll just upgrade their config file for them for now...
                system.configDir = self.configDir
                del system.appDataDir
            if system.has_option('greDir'):
                # No longer used, system should automatically support
                del system.greDir


    def loadSettings(self):
        """
        Loads the settings from the exe.conf file.
        Overrides the defaults set in __init__
        """
        # Set up the parser so that if a certain value is not in the config
        # file, it will use the value from our default values
        def defVal(dummy, option):
            """If something is not in the config file, just use the default in
            'self'"""
            return getattr(self, option)
        self.configParser.defaultValue = defVal
        self.upgradeFile()
        # System Section
        if self.configParser.has_section('system'):
            system = self.configParser.system
            self.webDir         = Path(system.webDir)
            self.xulDir         = Path(system.xulDir)
            self.localeDir      = Path(system.localeDir)
            self.port           = int(system.port)
开发者ID:KatiaBorges,项目名称:exeLearning,代码行数:70,代码来源:config.py

示例4: TestSections

# 需要导入模块: from exe.engine.configparser import ConfigParser [as 别名]
# 或者: from exe.engine.configparser.ConfigParser import has_section [as 别名]
class TestSections(unittest.TestCase):
    """
    Tests Section objects.
    Section objects allow an easy way to read and write
    """

    def setUp(self):
        """
        Creates a ConfigParser to play with and
        reads in the test text
        """
        self.c = ConfigParser()
        file_ = testFile()
        self.c.read(file_)

    def testSectionCreation(self):
        """
        Tests that the configuration object
        creates the section objects ok
        """
        assert isinstance(self.c.main, Section)
        assert isinstance(self.c.second, Section)
        self.failUnlessRaises(AttributeError, lambda: self.c.notexist)

    def testFromScratch(self):
        """
        Tests adding stuff from nothing
        """
        x = ConfigParser()
        testing = x.addSection('testing')
        assert x.testing is testing
        testing.myval = 4
        self.assertEquals(x.get('testing', 'myval'), '4')

    def testAttributeRead(self):
        """
        Tests that we can read attributes from sections
        """
        assert self.c.main.level == '5'
        assert self.c.main.power == 'on'
        self.failUnlessRaises(AttributeError, lambda: self.c.main.notexist)

    def testAttributeWrite(self):
        """
        Tests that we can set attributes with sections
        """
        self.c.main.level = 7
        # Should be automatically converted to string also :)
        self.assertEquals(self.c.get('main', 'level'), '7')
        self.c.main.new = 'hello'
        assert self.c.get('main', 'new') == 'hello'

    def testAttributeDel(self):
        """
        Tests that we can remove attributes from the config file
        by removing the sections
        """
        del self.c.main.level
        assert not self.c.has_option('main', 'level')

    def testSectionDel(self):
        """
        Tests that we can remove whole sections
        by deleting them
        """
        del self.c.main
        assert not self.c.has_section('main')
        def delete(): del self.c.notexist
        self.failUnlessRaises(AttributeError, delete)

    def testParserIn(self):
        """
        To test if a section exists, use the in operator
        """
        assert 'main' in self.c
        assert 'notexist' not in self.c
        # Dotted format
        assert 'main.level' in self.c
        assert 'main.notexist' not in self.c
        # And the sections can do it too
        assert 'notexist' not in self.c.main
        # Also the hasattr should give the same result
        assert hasattr(self.c, 'main')
        assert not hasattr(self.c, 'notexist')
        assert hasattr(self.c, 'main.level')
        assert hasattr(self.c.main, 'level')
        assert not hasattr(self.c.main, 'notexist')

    def testGet(self):
        """
        System should have a get section for those funny
        names that can't be attributes
        """
        assert self.c.second.get('funny-name_mate') == 'crusty the clown'

    def testCreation(self):
        """
        When one creates a section with the same name
        as an existing section the existing section should
        just be returned
#.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:103,代码来源:


注:本文中的exe.engine.configparser.ConfigParser.has_section方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。