First implementation of machine groups priorities

portnov [2009-02-24 19:57:22]
First implementation of machine groups priorities
Filename
machines.py
diff --git a/machines.py b/machines.py
index 54e1bc7..5378f0b 100644
--- a/machines.py
+++ b/machines.py
@@ -1,4 +1,7 @@

+lastpriority = 0
+groups = []
+
 class Host(object):
     def __init__(self):
         self.ip = ''
@@ -7,10 +10,50 @@ class Host(object):
     def __repr__(self):
         return "<Host %s>" % self.hostname

+class PriorityList(list):
+    def __setitem__(self,idx,val):
+        if idx >= len(self):
+            self.extend([None]*(idx-len(self)))
+            self.append(val)
+        else:
+            list.__setitem__(self,idx,val)
+
+    def swap_up(self,idx):
+        if idx==len(self)-1:
+            return
+        self[idx+1],self[idx] = self[idx],self[idx+1]
+
+    def swap_down(self,idx):
+        if not idx:
+            return
+        self[idx-1],self[idx] = self[idx],self[idx-1]
+
+bypriority = PriorityList()
+
 class MachinesGroup(object):
-    def __init__(self,name,*args):
+    def __init__(self,name,load=False,*args):
+        global lastpriority
+        global groups
+
         self.name = name
         self.machines = args
+        groups.append(self)
+        if not load:
+            lastpriority += 1
+            self.set_priority(lastpriority)
+
+    def set_priority(self,priority):
+        global bypriority
+        self.priority = priority
+        bypriority[priority] = self
+
+    def inc_priority(self):
+        global bypriority
+        bypriority.swap_up(self.priority)
+
+    def dec_priority(self):
+        global bypriority
+        bypriority.swap_down(self.priority)

     def __repr__(self):
         return "<Group %s (%d machines)>" % (self.name, len(self.machines))
ViewGit