wikibots / botconfig.py

The default branch has multiple heads

"""
    BotConfig.py
    
    Useful code to help with creating & loading configuration files for my Bots
    
"""

__VERSION__ = "r1"

# the default cfg file path to use (under ~)
DEFAULT_PATH = ".bot-configs"

import os, ConfigParser, logging

class BotConfig(object):
    """
        Provides a useful class for loading, creating and managing bot configs
    """
    
    def __init__(self,botname,cfg_path=None):
        if cfg_path != None:
            self.path = get_user_path(cfg_path)
        else:
            self.path = get_user_path(DEFAULT_PATH)
        self.name = botname
        # check that the path exists
        if not os.path.isdir(self.path):
            response = raw_input("%s does not exist, create? [Y/n]" % self.path)
            if response.lower() == "n":
                # uh, we do need that path!
                raise IOError, "Unable to load config files, path does not exist"
            else:
                os.mkdir(self.path)
        
    def cfgpath(self):
        return self.path
        
    def default(self):
        # load and return a handle on the default config
        return self.load("%s.cfg" % self.name)
        
    def load(self,filename):
        """
            Loads a config file from disk and returns a configparser object
            
            If the file does not exist it checks for a default file to copy in... or errors if none exists
        """
        filepath = os.path.join(self.path,filename)
        print filepath,os.path.isfile(filepath)
        if not os.path.isfile(filepath):
            defaultcfg =  os.path.join(os.getcwd(),filename)
            if os.path.isfile(defaultcfg):
                response = raw_input("Default file (%s) exists, would you like to copy it to the config directory? [Y/n]" % filename)
                if response.lower() == "n":
                    # uh, we do need that path!
                    raise IOError, "Unable to load config files, file does not exist and default was not copied"
                else:
                    config = ConfigParser.ConfigParser()
                    config.read(defaultcfg)
                    with open(filepath, 'wb') as configfile:
                        config.write(configfile)
                    logging.info("Default configuration file created (%s)" % filepath)
                    return config
            else:
                raise IOError, "Unable to load config files, file does not exist and no default exists to copy"
        else:
            config = ConfigParser.ConfigParser()
            config.read(filepath)
            return config
            
    def save(self,config,filename):
        filepath = os.path.join(self.path,filename)
        with open(filepath, 'wb') as configfile:
            config.write(configfile)

def get_user_path(name):
    # helper function to get a path within the users home dir
    return os.path.join(os.path.expanduser("~"),name)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.