diff options
author | Paul King <devnull@localhost> | 2011-05-21 09:08:52 +0200 |
---|---|---|
committer | Paul King <devnull@localhost> | 2011-05-21 09:08:52 +0200 |
commit | 6eb4fda710a8c96fcdc00cad56c3cf15d8babd15 (patch) | |
tree | e4351207e375e4b4304f0b5599c778032fbd1d08 /pyLoadCli.py | |
parent | Fixes commit 1478 (diff) | |
download | pyload-6eb4fda710a8c96fcdc00cad56c3cf15d8babd15.tar.xz |
store cli config per user
Diffstat (limited to 'pyLoadCli.py')
-rw-r--r-- | pyLoadCli.py | 86 |
1 files changed, 57 insertions, 29 deletions
diff --git a/pyLoadCli.py b/pyLoadCli.py index 7fbfd3632..f95e0de6e 100644 --- a/pyLoadCli.py +++ b/pyLoadCli.py @@ -22,7 +22,7 @@ from getopt import GetoptError, getopt import gettext import os from os import _exit -from os.path import join +from os.path import join, exists import sys from sys import exit from threading import Thread, Lock @@ -38,7 +38,7 @@ from module.cli.printer import * from module.cli import AddPackage, ManageFiles from module.utils import formatSize, decode -from module.ConfigParser import ConfigParser +import ConfigParser from module.remote.thriftbackend.ThriftClient import ThriftClient, NoConnection, NoSSL, WrongLogin, ConnectionClosed from module.lib.Getch import Getch @@ -334,7 +334,7 @@ class RefreshThread(Thread): print_exc() -def print_help(): +def print_help(config): print print "pyLoadCli Copyright (c) 2008-2011 the pyLoad Team" print @@ -348,9 +348,10 @@ def print_help(): print print " -u, --username=", " " * 2, "Specify Username" print " --pw=<password>", " " * 2, "Password" - print " -a, --address=", " " * 3, "Specify address (default=127.0.0.1)" - print " -p, --port", " " * 7, "Specify port (default=7227)" + print " -a, --address=", " " * 3, "Specify address (current=%s)" % config["addr"] + print " -p, --port", " " * 7, "Specify port (current=%s)" % config["port"] print + print " -l, --language", " " * 3, "Set user interface language (current=%s)" % config["language"] print " -h, --help", " " * 7, "Display this help screen" print " -c, --commands", " " * 3, "List all available commands" print @@ -401,23 +402,44 @@ def print_commands(): for c in commands: print "%-35s %s" % c +def writeConfig(opts): + try: + with open(join(homedir,".pyloadcli"),"w") as cfgfile: + print >>cfgfile, "[cli]" + for opt in opts: + print >>cfgfile, "%s=%s" % (opt, opts[opt]) + except: + print _("Couldn't write user config file") + if __name__ == "__main__": - config = ConfigParser() + + config = {"addr":"127.0.0.1","port":"7227", "language":"en"} + try: + config["language"]=os.environ["LANG"][0:2] + except: + pass + + if (not exists(join(pypath, "locale", config["language"]))) or config["language"] == "": + config["language"]="en" + + configFile = ConfigParser.ConfigParser() + configFile.read(join(homedir,".pyloadcli")) + + if configFile.has_section("cli"): + for opt in configFile.items("cli"): + config[opt[0]]=opt[1] translation = gettext.translation("pyLoadCli", join(pypath, "locale"), - languages=["en", config['general']['language']]) + languages=["en",config["language"]]) translation.install(unicode=True) - username = "" - password = "" - addr = "127.0.0.1" - port = 7227 - interactive = False command = None + username = "" + password = "" - shortOptions = 'iu:p:a:hc' - longOptions = ['interactive', "username=", "pw=", "address=", "port=", "help", "commands"] + shortOptions = 'iu:p:a:hcl:' + longOptions = ['interactive', "username=", "pw=", "address=", "port=", "help", "commands","language="] try: opts, extraparams = getopt(sys.argv[1:], shortOptions, longOptions) @@ -427,11 +449,16 @@ if __name__ == "__main__": elif option in ("-u", "--username"): username = params elif option in ("-a", "--address"): - addr = params + config["addr"] = params elif option in ("-p", "--port"): - port = int(params) + config["port"] = params + elif option in ("-l", "--language"): + config["language"] = params + translation = gettext.translation("pyLoadCli", join(pypath, "locale"), + languages=["en",config["language"]]) + translation.install(unicode=True) elif option in ("-h", "--help"): - print_help() + print_help(config) exit() elif option in ("--pw"): password = params @@ -441,7 +468,7 @@ if __name__ == "__main__": except GetoptError: print 'Unknown Argument(s) "%s"' % " ".join(sys.argv[1:]) - print_help() + print_help(config) exit() if len(extraparams) >= 1: @@ -451,19 +478,19 @@ if __name__ == "__main__": if interactive: try: - client = ThriftClient(addr, port, username, password) + client = ThriftClient(config["addr"], int(config["port"]), username, password) except WrongLogin: - pass + pass except NoSSL: print _("You need py-openssl to connect to this pyLoad Core.") exit() except NoConnection: - addr = False - port = False + config["addr"] = False + config["port"] = False if not client: - if not addr: addr = raw_input(_("Address: ")) - if not port: port = int(raw_input(_("Port: "))) + if not config["addr"]: config["addr"] = raw_input(_("Address: ")) + if not config["port"]: config["port"] = raw_input(_("Port: ")) if not username: username = raw_input(_("Username: ")) if not password: from getpass import getpass @@ -471,23 +498,24 @@ if __name__ == "__main__": password = getpass(_("Password: ")) try: - client = ThriftClient(addr, port, username, password) + client = ThriftClient(config["addr"], int(config["port"]), username, password) except WrongLogin: print _("Login data is wrong.") except NoConnection: - print _("Could not establish connection to %(addr)s:%(port)s." % {"addr": addr, "port": port}) + print _("Could not establish connection to %(addr)s:%(port)s." % {"addr": config["addr"], "port": config["port"]}) else: try: - client = ThriftClient(addr, port, username, password) + client = ThriftClient(config["addr"], int(config["port"]), username, password) except WrongLogin: print _("Login data is wrong.") except NoConnection: - print _("Could not establish connection to %(addr)s:%(port)s." % {"addr": addr, "port": port}) + print _("Could not establish connection to %(addr)s:%(port)s." % {"addr": config["addr"], "port": config["port"]}) except NoSSL: print _("You need py-openssl to connect to this pyLoad core.") - + if interactive and command: print _("Interactive mode ignored since you passed some commands.") if client: + writeConfig(config) cli = Cli(client, command) |