Commit e4c2e492 authored by Seblu's avatar Seblu
Browse files

new initialization process

new profile loading schema
parent 3ae32cbe
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -12,11 +12,19 @@ You will find examples in examples subdirectory in git repository.

To quickly connect to mulptiple servers, you can define a profile by server.

=======
Loading
=======
Profile is loaded in this order:
1 load [cli] profile in ~/.config/cc-cli/profile
2 load profile specified by option profile in [cli] profile
3 load profile specified by environment
4 load profile specified on argument line
4 load profile specified on command line
5 load options from environment
6 load options from command line

Please note: Environment variable CC_DEBUG is loaded before every work to be
able to activate an early debugging.

===========
Environment
+48 −39
Original line number Diff line number Diff line
@@ -29,41 +29,13 @@ settings = {
    }

try:
    # load a printer
    printer = Printer(False)

    # parse profile file and load default profile
    try:
        propath = "%s/profile"%BaseDirectory.save_config_path(cccli.canonical_name)
        fparser = ConfigParser.RawConfigParser()
        fparser.read(propath)
        if fparser.has_section("cli"):
            settings.update(fparser.items("cli"))
    except Exception:
        fparser = None

    # load "default profile" profile
    if "profile" in settings:
        if fparser is not None and fparser.has_section(settings["profile"]):
            settings.update(fparser.items(settings["profile"]))
        del settings["profile"]

    # parse env
    if "CC_SERVER" in os.environ:
        settings["server"] = os.environ["CC_SERVER"]
    if "CC_PORT" in os.environ:
        settings["port"] = os.environ["CC_PORT"]
    if "CC_LOGIN" in os.environ:
        settings["login"] = os.environ["CC_LOGIN"]
    if "CC_PASS" in os.environ:
        settings["pass"] = os.environ["CC_PASS"]
    # Early debug loading
    if "CC_DEBUG" in os.environ:
        settings["debug"] = "True"
        cccli.debug = True

    # load env profile
    if "CC_PROFILE" in os.environ:
        if fparser is not None and fparser.has_section(os.environ["CC_PROFILE"]):
            settings.update(fparser.items(os.environ["CC_PROFILE"]))
    # load a printer
    printer = Printer(False)

    # Parse line argument
    oparser = optparse.OptionParser(usage="usage: %prog [options] [commands]",
@@ -86,19 +58,56 @@ try:
                       help="History max entry count")
    (options, args) = oparser.parse_args()

    # options handling
    # try loading profiles
    try:
        # parse profile file
        propath = "%s/profile"%BaseDirectory.save_config_path(cccli.canonical_name)
        fparser = ConfigParser.RawConfigParser()
        fparser.read(propath)
        # load default profile
        if fparser.has_section("cli"):
            settings.update(fparser.items("cli"))
        # load next profile from cli profile
        if "profile" in settings:
            if fparser.has_section(settings["profile"]):
                settings.update(fparser.items(settings["profile"]))
                del settings["profile"]
            else:
                printer.warn("Unable to load profile %s"%settings["profile"])
        # load env profile
        if "CC_PROFILE" in os.environ:
            if fparser.has_section(os.environ["CC_PROFILE"]):
                settings.update(fparser.items(os.environ["CC_PROFILE"]))
            else:
                printer.warn("Unable to load profile %s"%os.environ["CC_PROFILE"])
        # load argline profile
        if options.profile:
            if fparser.has_section(options.profile):
                settings.update(fparser.items(options.profile))
            else:
                printer.warn("Unable to load profile %s"%option.profile)
    except Exception as e:
        if cccli.debug:
            raise
        pass

    # Load environment variables
    if "CC_SERVER" in os.environ:
        settings["server"] = os.environ["CC_SERVER"]
    if "CC_PORT" in os.environ:
        settings["port"] = os.environ["CC_PORT"]
    if "CC_LOGIN" in os.environ:
        settings["login"] = os.environ["CC_LOGIN"]
    if "CC_PASS" in os.environ:
        settings["pass"] = os.environ["CC_PASS"]

    # Load command line options
    for i in [ x.dest for x in oparser.option_list if x.dest ]:
        if hasattr(options, i):
            o = getattr(options, i)
            if o:
                settings[i] = o

    # load argline profile
    if "profile" in settings:
        if fparser is not None and fparser.has_section(settings["profile"]):
            settings.update(fparser.items(settings["profile"]))
        del settings["profile"]

    # debug stuff
    if "debug" in settings:
        cccli.debug = bool(settings["debug"])