From cd52efdf1c7d797f49d43f883fee5179a48a404e Mon Sep 17 00:00:00 2001
From: Seblu <sebastien.luttringer@smartjog.com>
Date: Wed, 9 Feb 2011 13:35:24 +0100
Subject: [PATCH] tagdisplay command

---
 bin/cc-cli                  |  2 +-
 cccli/cli.py                |  2 +-
 cccli/command/__init__.py   |  1 +
 cccli/command/command.py    |  6 +++
 cccli/command/server.py     |  1 -
 cccli/command/tagdisplay.py | 99 +++++++++++++++++++++++++++++++++++++
 6 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 cccli/command/tagdisplay.py

diff --git a/bin/cc-cli b/bin/cc-cli
index 4d09a81..04766a5 100755
--- a/bin/cc-cli
+++ b/bin/cc-cli
@@ -24,7 +24,7 @@ settings = {
     "timeout": "30",
     "hsize": "100",
     "alias": "%s/alias"%BaseDirectory.save_config_path(cccli.canonical_name),
-    "tag": "%s/tag"%BaseDirectory.save_config_path(cccli.canonical_name),
+    "tagdisplay": "%s/tagdisplay"%BaseDirectory.save_config_path(cccli.canonical_name),
     "history": "%s/history"%BaseDirectory.save_data_path(cccli.canonical_name),
     "expert": "%s/expert"%BaseDirectory.save_data_path(cccli.canonical_name),
     }
diff --git a/cccli/cli.py b/cccli/cli.py
index 8afa740..9123996 100644
--- a/cccli/cli.py
+++ b/cccli/cli.py
@@ -62,7 +62,7 @@ class Cli(object):
         self.alias.load(self.settings.get("alias", ""))
         self.printer.debug("Loaded aliases: %d"%len(self.alias))
         # load tagdisplay
-        self.tagdisplay.load(self.settings.get("tag", ""))
+        self.tagdisplay.load(self.settings.get("tagdisplay", ""))
         # connecting
         self._connect()
         # auth
diff --git a/cccli/command/__init__.py b/cccli/command/__init__.py
index a2f447f..58782b2 100644
--- a/cccli/command/__init__.py
+++ b/cccli/command/__init__.py
@@ -19,3 +19,4 @@ from cccli.command.vm import *
 from cccli.command.list import Command_list
 from cccli.command.expert import Command_expert
 from cccli.command.server import Command_server
+from cccli.command.tagdisplay import Command_tagdisplay
diff --git a/cccli/command/command.py b/cccli/command/command.py
index 5b4aa9d..92549d8 100644
--- a/cccli/command/command.py
+++ b/cccli/command/command.py
@@ -14,6 +14,9 @@ class Command(object):
         self.printer = self.cli.printer
         self.name = argv0
 
+    def __call__(self, argv):
+        raise NotImplementedError
+
     def usage(self):
         return "Usage: %s"%self.name
 
@@ -27,6 +30,9 @@ class OptionCommand(Command):
         Command.__init__(self, cli, argv0)
         self.option = OptionParser(prog=argv0)
 
+    def __call__(self, argv):
+        raise NotImplementedError
+
     def usage(self):
         return self.option.format_help().strip()
 
diff --git a/cccli/command/server.py b/cccli/command/server.py
index 1a0eedb..3ce28ad 100644
--- a/cccli/command/server.py
+++ b/cccli/command/server.py
@@ -25,7 +25,6 @@ class Command_server(OptionCommand):
             (options, args) = self.option.parse_args(argv[1:])
         except SystemExit:
             return
-        print dir(options)
         if len(args) > 0 or (not options.cache and not options.commands):
             self.printer.out(self.usage())
             return
diff --git a/cccli/command/tagdisplay.py b/cccli/command/tagdisplay.py
new file mode 100644
index 0000000..e0608e0
--- /dev/null
+++ b/cccli/command/tagdisplay.py
@@ -0,0 +1,99 @@
+#!/usr/bin/env python
+#coding=utf8
+
+'''
+CloudControl tagdisplay command
+'''
+from cccli.exception import *
+from sjrpc.core.exceptions import *
+from cccli.printer import Printer, color
+from cccli.command.command import OptionCommand
+
+class Command_tagdisplay(OptionCommand):
+    '''Tagdisplay tool'''
+
+    def __init__(self, cli, argv0):
+        OptionCommand.__init__(self, cli, argv0)
+        self.option.set_usage("%prog [options] [tag] ...")
+        self.option.add_option("-c", action="store", dest="setcolor",
+                               help="Set color SETCOLOR to tags")
+        self.option.add_option("-t", action="store", dest="settype",
+                               help="Set type SETTYPE to tags")
+        self.option.add_option("-C", action="store_true", dest="delcolor",
+                               help="Remove custom tags color")
+        self.option.add_option("-T", action="store_true", dest="deltype",
+                               help="Remove custom tags type")
+        self.option.add_option("--list-colors", action="store_true", dest="listcolor",
+                               help="List allowed color")
+        self.option.add_option("--list-types", action="store_true", dest="listtype",
+                               help="List allowed types")
+
+    def __call__(self, argv):
+        try:
+            (options, args) = self.option.parse_args(argv[1:])
+        except SystemExit:
+            return
+        if options.listcolor:
+            self.list("color")
+        elif options.listtype:
+            self.list("type")
+        elif len(args) > 0 and options.setcolor:
+            self.set("color", args, options.setcolor)
+        elif len(args) > 0 and options.delcolor:
+            self.set("color", args, None)
+        elif len(args) > 0 and options.settype:
+            self.set("type", args, options.settype)
+        elif len(args) > 0 and options.deltype:
+            self.set("type", args, None)
+        elif len(args) == 0:
+            self.list("my")
+        else:
+            self.printer.out(self.usage())
+
+    def list(self, what):
+        '''List displaytag information'''
+        if what == "color":
+            self.printer.out("%sTag allowed colors:%s"%(color["lblue"],  color["reset"]))
+            for c in color.keys():
+                if c.isalpha():
+                    self.printer.out(c)
+        elif what == "type":
+            self.printer.out("%sTag allowed types:%s"%(color["lblue"], color["reset"]))
+            for t in self.cli.tagdisplay.types:
+                self.printer.out(t)
+        elif what == "my":
+            self.printer.out("%sMy tagdisplay colors:%s"%(color["lblue"], color["reset"]))
+            for (u,v) in self.cli.tagdisplay.tagcolor.items():
+                self.printer.out("%s: %s"%(u,v))
+            self.printer.out("%sMy tagdisplay types:%s"%(color["lblue"], color["reset"]))
+            for (u,v) in self.cli.tagdisplay.tagtype.items():
+                self.printer.out("%s: %s"%(u,v))
+        else:
+            raise AttributeError
+
+    def set(self, what, tags, value):
+        '''Set displaytag info'''
+        # crazy gard
+        if not len(tags) > 0:
+            return
+        # set appropriate database
+        if what == "color":
+            db = self.cli.tagdisplay.tagcolor
+            vdb = [ c for c in color.keys() if c.isalpha() ]
+        elif what == "type":
+            db = self.cli.tagdisplay.tagtype
+            vdb = self.cli.tagdisplay.types
+        else:
+            raise AttributeError
+        # check attribute
+        if value is not None and value not in vdb:
+            self.printer.error("Invalid value: %s"%value)
+            return
+        # update db
+        for tagname in tags:
+            if value is None:
+                if tagname in db:
+                    del db[tagname]
+            else:
+                db[tagname] = value
+        self.cli.tagdisplay.save(self.cli.settings.get("tagdisplay", ""))
-- 
GitLab