
class DockerArg(object):
    def __init__(self, name, val):
        self.name = name
        self.value = val

    def is_null(self):
        if self.value:
            return False
        return True

    def __str__(self):
        return self.value

class DockerOpt(object):
    def __init__(self, opt, val):
        self.opt = opt
        self.value = val

    def is_null(self):
        if self.value:
            return False
        return True

    def __str__(self):
        return '%s %s' % (self.opt, self.value)

class BoolOpt(DockerOpt):
    def __init__(self, *args):
        DockerOpt.__init__(self, *args)

    def __str__(self):
        return self.opt

class ByteValueOpt(DockerOpt):
    """ Option with one or more user-defined values """
    def __init__(self, *args):
        DockerOpt.__init__(self, *args)

    @staticmethod
    def format_bytes(x):
        KB = 1024
        MB = KB*1024
        GB = MB*1024
        x = float(x)
        if x < 1024:
            return '%sb' % round(x)
        elif 1024 <= x < MB:
            return '%sk' % round(x/KB)
        elif KB <= x < GB:
            return '%sm' % round(x/MB)
        elif GB <= x:
            return '%sg' % round(x/GB)

    def __str__(self):
        try:
            return '%s %s' % (self.opt, self.format_bytes(self.value))
        except ValueError:
            raise TypeError('unsupported value type for option "%s": %s' % \
                    (self.opt, self.value))

class ValueOpt(DockerOpt):
    """ Option with one or more user-defined values """
    def __init__(self, *args):
        DockerOpt.__init__(self, *args)

    def __str__(self):
        if type(self.value) in (str, unicode, int, float):
            return '%s %s' % (self.opt, self.value)
        elif type(self.value) == list:
            return ' '.join([ '%s %s' % (self.opt, v) for v in self.value ])
        else:
            raise TypeError('unsupported value type for option "%s": %s' % \
                    (self.opt, self.value))

class MapOpt(DockerOpt):
    """ Option with one or more user-defined mappings """
    def __init__(self, *args):
        DockerOpt.__init__(self, *args)

    def __str__(self):
        if not isinstance(self.value, dict):
            raise TypeError('unsupported value type for option "%s": %s' % \
                    (self.opt, self.value))
        kvlist = [ '%s=%s' % (k,v) for k,v in self.value.items() ]
        return ' '.join([ '%s %s' % (self.opt, i) for i in kvlist ])

@@ -1532,32 +1532,33 @@
 ined values %22%22%22%0A
+%0A
     def __init__
@@ -1668,37 +1668,15 @@
 ue) 
-in (str, unicode, int, float)
+== list
 :%0A  
@@ -1683,32 +1683,43 @@
           return
+ ' '.join(%5B
  '%25s %25s' %25 (self
@@ -1715,32 +1715,44 @@
 %25s' %25 (self.opt,
+ v) for v in
  self.value)%0A   
@@ -1746,16 +1746,18 @@
 lf.value
+ %5D
 )%0A      
@@ -1762,37 +1762,11 @@
     
-elif type(self.value) == list
+try
 :%0A  
@@ -1777,35 +1777,24 @@
       return
- ' '.join(%5B
  '%25s %25s' %25 (
@@ -1798,36 +1798,24 @@
 %25 (self.opt,
- v) for v in
  self.value 
@@ -1805,34 +1805,32 @@
 .opt, self.value
- %5D
 )%0A        else:%0A
@@ -1824,19 +1824,32 @@
        e
-lse
+xcept ValueError
 :%0A      

remove type checking from value opt