import ConfigParser
import os
import logging
import logging.handlers

from lala import config
from lala.factory import LalaFactory
from os.path import join
from sys import version_info
from twisted.internet import reactor

if version_info >= (2, 7):
    import argparse
else:
    import optparse

CONFIG_DEFAULTS = {
        "channels": "",
        "plugins": "",
        "nickserv_password": None,
        "log_folder": os.path.expanduser("~/.lala/logs"),
        "encoding": "utf-8",
        "fallback_encoding": "utf-8",
        "max_log_days": 2
        }


def main():
    """Main method"""
    if version_info >= (2, 7):
        parser = argparse.ArgumentParser()
        parser.add_argument("-c", "--config", help="Configuration file location")
        parser.add_argument("-d", "--debug", help="Enable debugging",
                            action="store_true", default=False)
        parser.add_argument("-n", "--no-daemon", help="Do not daemonize",
                            action="store_true", default=False)
        args = parser.parse_args()
    else:
        parser = optparse.OptionParser()
        parser.add_option("-c", "--config", help="Configuration file location")
        parser.add_option("-d", "--debug", help="Enable debugging",
                            action="store_true", default=False)
        parser.add_option("-n", "--no-daemon", help="Do not daemonize",
                            action="store_true", default=False)
        (args, options) = parser.parse_args()

    if args.debug:
        args.no_daemon = True

    cfg = ConfigParser.SafeConfigParser()
    if args.config is None:
        try:
            configfile = os.path.join(os.getenv("XDG_CONFIG_HOME"), "lala", "config")
        except AttributeError:
            configfile = os.path.join(os.getenv("HOME"), ".lala", "config")
        files = cfg.read([configfile, "/etc/lala.config"])
    else:
        files = cfg.read(args.config)

    config._CFG = cfg
    config._FILENAME = files[0]

    log_folder = get_conf_key(cfg, "log_folder")
    config._CFG.set("base", "log_folder", log_folder)
    logfile = join(log_folder, "lala.log")
    if not os.path.exists(log_folder):
        os.makedirs(log_folder)
    logger = logging.getLogger("MessageLog")
    handler = logging.handlers.TimedRotatingFileHandler(
            encoding="utf-8",
            filename=logfile,
            when="midnight",
            backupCount=int(get_conf_key(cfg, "max_log_days")))
    logger.setLevel(logging.INFO)
    handler.setFormatter(
            logging.Formatter("%(asctime)s %(message)s",
                              "%Y-%m-%d %H:%m"))
    logger.addHandler(handler)

    debugformat=\
        "%(levelname)s %(filename)s: %(funcName)s: %(lineno)d %(message)s"

    if args.debug:
        logging.basicConfig(format=debugformat, level=logging.DEBUG)

    if not args.no_daemon:
        import daemon
        with daemon.DaemonContext(files_preserve=[handler.stream.fileno()]):
            f = LalaFactory(get_conf_key(cfg, "channels"),
                    get_conf_key(cfg, "nick"),
                    get_conf_key(cfg, "plugins").split(","),
                    logger)
            reactor.connectTCP(get_conf_key(cfg, "server"),
                    int(get_conf_key(cfg, "port")),
                    f)
            reactor.run()
    else:
            f = LalaFactory(get_conf_key(cfg, "channels"),
                    get_conf_key(cfg, "nick"),
                    get_conf_key(cfg, "plugins").split(","),
                    logger)
            reactor.connectTCP(get_conf_key(cfg, "server"),
                    int(get_conf_key(cfg, "port")),
                    f)
            reactor.run()


def get_conf_key(conf, key):
    try:
        return conf.get("base", key)
    except ConfigParser.NoOptionError:
        return CONFIG_DEFAULTS[key]

@@ -220,65 +220,8 @@
 or%0A%0A
-if version_info %3E= (2, 7):%0A    import argparse%0Aelse:%0A    
 impo
@@ -232,16 +232,16 @@
 ptparse%0A
+
 %0ACONFIG_
@@ -536,485 +536,8 @@
 %22%22%22%0A
-    if version_info %3E= (2, 7):%0A        parser = argparse.ArgumentParser()%0A        parser.add_argument(%22-c%22, %22--config%22, help=%22Configuration file location%22)%0A        parser.add_argument(%22-d%22, %22--debug%22, help=%22Enable debugging%22,%0A                            action=%22store_true%22, default=False)%0A        parser.add_argument(%22-n%22, %22--no-daemon%22, help=%22Do not daemonize%22,%0A                            action=%22store_true%22, default=False)%0A        args = parser.parse_args()%0A    else:%0A    
     
@@ -569,28 +569,24 @@
 arser()%0A    
- 
-   
 parser.add_o
@@ -645,20 +645,16 @@
 ation%22)%0A
-    
     pars
@@ -725,36 +725,32 @@
                 
- 
-   
 action=%22store_tr
@@ -765,28 +765,24 @@
 ault=False)%0A
-    
     parser.a
@@ -853,36 +853,32 @@
                 
-    
 action=%22store_tr
@@ -897,20 +897,16 @@
 =False)%0A
-    
     (arg

Use optparse for all python versions