From 74f80f56393a0b27e784ea0faf20b49d1008eb88 Mon Sep 17 00:00:00 2001 From: Seblu <sebastien.luttringer@smartjog.com> Date: Fri, 31 Dec 2010 12:50:49 +0100 Subject: [PATCH] add option --force-yes to handle asking when not interactive --- bin/cc-cli | 9 ++++++++- cccli/cli.py | 19 +++++++++---------- cccli/command.py | 5 ++--- cccli/printer.py | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/bin/cc-cli b/bin/cc-cli index f2fc917..a47e380 100755 --- a/bin/cc-cli +++ b/bin/cc-cli @@ -87,10 +87,15 @@ try: dest="hsize", default="", help="History max entry count") + oparser.add_option("--force-yes", + action="store_true", + dest="forceyes", + default="", + help="Answer yes to yes/no question") (options, args) = oparser.parse_args() # options handling - for i in ("debug", "login", "server", "port", "timeout", "history", "hsize"): + for i in ("debug", "login", "server", "port", "timeout", "history", "hsize", "forceyes"): if hasattr(options, i): o = getattr(options, i) if o: @@ -116,10 +121,12 @@ try: raise BadArgument("Invalid %s number"%i) # check login if "login" not in settings: + printer.setinteractive() settings["login"] = printer.ask("Login: ") # check password if "pass" not in settings: + printer.setinteractive() settings["pass"] = printer.getpass("Password: ") # start cli diff --git a/cccli/cli.py b/cccli/cli.py index 9b040d2..4f382e9 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -21,14 +21,11 @@ from sjrpc.client import SimpleRpcClient from sjrpc.utils import RpcHandler, ConnectionProxy, pure from sjrpc.core.exceptions import * - - class Cli(object): def __init__(self, settings): self.settings = settings - self.prompt = "" + self.prompt = "> " self.rpc = None - self.printer = None self.alias = Alias() def start(self, line=""): @@ -39,25 +36,27 @@ class Cli(object): # set interactive mode self.interactive = sys.stderr.isatty() and sys.stdin.isatty() # start printer and load history - self.printer = Printer(self.interactive) - self.printer.history.load(self.settings.get("history", "")) - self.printer.history.maxsize(self.settings.get("hsize", None)) + self.printer = Printer(self.interactive, + self.settings.get("forceyes", False), + historyfile=self.settings.get("history", ""), + historysize=self.settings.get("hsize", None)) # load alias self.alias.load(self.settings.get("alias", "")) # print debug self.printer.debug("Interactive: %s"%self.interactive) self.printer.debug("Alias: %s"%self.alias) self.printer.debug("Loaded history: %s"%len(self.printer.history)) - # Connecting + # connecting self._connect() - # authentifications + # auth self._auth() - # Parsing + # parsing self._parse() # save history self.printer.history.save(self.settings.get("history", "")) def _connect(self): + '''Connect to a cloud control server''' self.printer.debug("Connecting...") try: rpcc = SimpleRpcClient.from_addr(self.settings["server"], diff --git a/cccli/command.py b/cccli/command.py index 37a429b..bea4c9d 100644 --- a/cccli/command.py +++ b/cccli/command.py @@ -193,14 +193,13 @@ class Command(object): for item in items: self.printer.out("%s"%item["id"]) self.printer.out("Count: %s"%len(items)) - if self.printer.ask("Are you sure to %s? (yes/NO) "%argv[0]) != "yes": + if self.printer.ask("Are you sure to %s? (yes/NO) "%argv[0], "yes") != "yes": raise cmdWarning("Aborted") if len(items) > 5: - if self.printer.ask("You request is on more than 5 objets. Are you really sure to %s its? (Yes, I am) "%argv[0]) != "Yes, I am": + if self.printer.ask("You request is on more than 5 objets. Are you really sure to %s its? (Yes, I am) "%argv[0], "Yes, I am") != "Yes, I am": raise cmdWarning("Aborted") self.cli.rpc[argv[0]](tql) - def cmd_start(self, argv): '''Start objects''' self._startstopsdestroypauseresume(argv) diff --git a/cccli/printer.py b/cccli/printer.py index 9774d9e..d447bbf 100644 --- a/cccli/printer.py +++ b/cccli/printer.py @@ -10,6 +10,7 @@ import os import getpass import cccli +from cccli.exception import * color = { # regular @@ -42,17 +43,31 @@ color = { class Printer(object): '''Print relative class''' - def __init__(self, interactive=False): + def __init__(self, interactive=False, forceyes=False, historyfile=None, historysize=None): self.readline = None + self.history = History() + self.historyfile = historyfile + self.historysize = historysize + self.forceyes = forceyes if interactive: - import readline - self.readline = readline - self.history = History(self.readline) + self.setinteractive() def isinteractive(self): '''Return if printer is in interactive mode''' return self.readline is not None + def setinteractive(self): + if self.readline is not None: + return + import readline + self.readline = readline + if self.history is None: + self.history = History(self.readline) + else: + self.history.readline = readline + self.history.load(self.historyfile) + self.history.maxsize(self.historysize) + def out(self, message, fd=sys.stdout, nl=os.linesep, flush=True): '''Print a message in fd ended by nl''' fd.write("%s%s"%(message, nl)) @@ -98,20 +113,26 @@ class Printer(object): def getpass(self, prompt): '''Ask for a password. No echo. Not in history''' + if self.readline is None: + raise cliError("Unable to ask a password in non-interactive mode") return getpass.getpass(prompt) - def ask(self, prompt): + def ask(self, prompt, goodans=None): '''Used to ask a question. Default answer not saved to history''' + if self.forceyes and goodans is not None: + return goodans + if self.readline is None: + raise cliError("Unable to ask question in non-interactive mode") return self.getline(prompt, history=False) class History(object): '''History class''' - def __init__(self, readline): - self.readline = readline + def __init__(self): + self.readline = None def __nonzero__(self): - return not self.readline is None + return self.readline is not None def __getattribute__(self, name): r = object.__getattribute__(self, "readline") -- GitLab