Code factorisation
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 28 May 2012 20:46:45 +0000 (20:46 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 28 May 2012 20:46:45 +0000 (20:46 +0000)
members.py

index 438413ad94c6d12fbf79009fb03f8e4a7a6c714b..ea8fef029bd9277b5e2e6435532502727b452d93 100755 (executable)
@@ -216,13 +216,18 @@ def members_to_field(members):
     return fields
 
 
+def publish_line(csvfile, text, **args):
+    csvline = text.format(**args)
+    logging.info(csvline.split(',')[-1])
+    if not options.dryrun:
+        csvfile.write(time.strftime(DATETIME_FORMAT)+','+csvline+'\n')
+
 def field_compare(oldmembers, newmembers, csvfile):
     
     oldfield = members_to_field(oldmembers)
     newfield = members_to_field(newmembers)
 
     changes = {}
-    txtchanges2 = []
 
     for nick in newfield.keys():
         told = oldfield.get(nick, 0)
@@ -241,7 +246,7 @@ def field_compare(oldmembers, newmembers, csvfile):
     #print
 
     if not len(changes):
-        txtchanges2.append('NOOP,,,,,,,,,,,No changes')
+        publish_line(csvfile, 'NOOP,,,,,,,,,,,No changes')
  
     for nick in changes.keys():
         delta = changes[nick]['delta']
@@ -253,7 +258,8 @@ def field_compare(oldmembers, newmembers, csvfile):
             if changes[nick2]['delta'] != -delta:
                 continue # not the good one
             percent = float(oldfield[nick2] - newfield[nick2]) / oldfield[nick2] * 100.
-            txtchanges2.append('FLOOD,{nick1},{grade1},{oldfield1},{newfield1},{nick2},{grade2},{oldfield2},{newfield2},{deltafield},{percent},{nick1} a pris {nicedeltafield} cm² à {nick2} ({percent:.2f}%)'.format(
+            publish_line(csvfile, 
+                'FLOOD,{nick1},{grade1},{oldfield1},{newfield1},{nick2},{grade2},{oldfield2},{newfield2},{deltafield},{percent},{nick1} a pris {nicedeltafield} cm² à {nick2} ({percent:.2f}%)',
                 nick1=nick,
                 grade1=newmembers[nick].get('grade', ''),
                 oldfield1=oldfield[nick],
@@ -265,7 +271,7 @@ def field_compare(oldmembers, newmembers, csvfile):
                 deltafield=delta,
                 nicedeltafield= number_format(delta),
                 percent=percent,
-                ))
+                )
             changes[nick]['delta'] = 0
             changes[nick2]['delta'] = 0
 
@@ -274,7 +280,8 @@ def field_compare(oldmembers, newmembers, csvfile):
         if delta < 0:
             if nick in newfield:
                 percent = float(-delta) / oldfield[nick] * 100.
-                txtchanges2.append('LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{percent},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)'.format(
+                publish_line(csvfile, 
+                    'LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{percent},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)',
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
                     oldfield1=oldfield[nick],
@@ -282,79 +289,65 @@ def field_compare(oldmembers, newmembers, csvfile):
                     deltafield=-delta,
                     nicedeltafield=number_format(-delta),
                     percent=percent,
-                    ))
+                    )
             else:
-                txtchanges2.append('QUIT,{nick1},{grade1},{oldfield1},0,,,,,{deltafield},,{nick1} a quité l\'alliance avec {nicedeltafield} cm²'.format(
+                publish_line(csvfile, 
+                    'QUIT,{nick1},{grade1},{oldfield1},0,,,,,{deltafield},,{nick1} a quité l\'alliance avec {nicedeltafield} cm²',
                     nick1=nick,
                     grade1=oldmembers[nick].get('grade', ''),
                     oldfield1=oldfield[nick],
                     deltafield=-delta,
                     nicedeltafield=number_format(-delta),
-                    ))
+                    )
         elif delta > 0:
             if nick in oldfield:
-                txtchanges2.append('WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},,{nick1} a gagné {nicedeltafield} cm²'.format(
+                publish_line(csvfile, 
+                    'WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},,{nick1} a gagné {nicedeltafield} cm²',
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
                     oldfield1=oldfield[nick],
                     newfield1=newfield[nick],
                     deltafield=delta,
                     nicedeltafield=number_format(delta),
-                    ))
+                    )
             else:
-                txtchanges2.append('JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},,{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²'.format(
+                publish_line(csvfile, 
+                    'JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},,{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²',
                     nick1=nick,
                     grade1=newmembers[nick].get('grade', ''),
                     newfield1=newfield[nick],
                     deltafield=delta,
                     nicedeltafield=number_format(delta),
-                    ))
-
-    for txtchange in txtchanges2:
-        logging.info(txtchange.split(',')[-1])
-        if not options.dryrun:
-            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+                    )
 
 def activity_compare(oldmembers, newmembers, csvfile):
-    txtchanges2 = []
     for nick, data in newmembers.items():
         newa = data.get('active', None)
         olda = oldmembers.get(nick, {}).get('active', None)
         if newa and olda and newa != olda:
-            txtchanges2.append('ACT,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est maintenant {lowernewdata1}'.format(
+            publish_line(csvfile, 
+                'ACT,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est maintenant {lowernewdata1}',
                 nick1=nick,
                 grade1=newmembers[nick]['grade'],
                 olddata1=olda,
                 newdata1=newa,
                 lowernewdata1=newa.lower(),
-                ))
-
-    for txtchange in txtchanges2:
-        logging.info(txtchange.split(',')[-1])
-        if not options.dryrun:
-            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+                )
 
 def grade_compare(oldmembers, newmembers, csvfile):
-    txtchanges2 = []
     for nick, data in newmembers.items():
         newdata = data.get('grade', None)
         olddata = oldmembers.get(nick, {}).get('grade', None)
         if newdata is not None and olddata is not None and newdata != olddata:
-            txtchanges2.append('GRADE,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est désormais {grade1}'.format(
+            publish_line(csvfile, 
+                'GRADE,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est désormais {grade1}',
                 nick1=nick,
                 grade1=newmembers[nick]['grade'],
                 olddata1=olddata,
                 newdata1=newdata,
-                ))
-
-    for txtchange in txtchanges2:
-        logging.info(txtchange.split(',')[-1])
-        if not options.dryrun:
-            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
-
+                )
 
 def colonized_compare(oldmembers, newmembers, csvfile):
-    txtchanges2 = []
     for nick, data in newmembers.items():
         newc = data.get('colonized', None)
         oldc = oldmembers.get(nick, {}).get('colonized', None)
@@ -363,46 +356,37 @@ def colonized_compare(oldmembers, newmembers, csvfile):
                 txt = "s'est fait colonisé"
             else:
                 txt = "n'est plus colonisé"
-            txtchanges2.append('COLON,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} {txt}'.format(
+            publish_line(csvfile, 
+                'COLON,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} {txt}',
                 nick1=nick,
                 grade1=newmembers[nick]['grade'],
                 olddata1=str(oldc),
                 newdata1=str(newc),
                 txt=txt,
-                ))
-
-    for txtchange in txtchanges2:
-        logging.info(txtchange.split(',')[-1])
-        if not options.dryrun:
-            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
-
+                )
 
 def evolution_compare(oldmembers, newmembers, csvfile):
-    txtchanges2 = []
     for nick, data in newmembers.items():
         newdata = data['anthill']
         olddata = oldmembers.get(nick, {}).get('anthill', None)
         if newdata is not None and olddata is not None and newdata != olddata:
-            txtchanges2.append('ANTHILL,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La fourmilière de {nick1} a atteint le niveau {newdata1}'.format(
+            publish_line(csvfile, 
+                'ANTHILL,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La fourmilière de {nick1} a atteint le niveau {newdata1}',
                 nick1=nick,
                 grade1=newmembers[nick]['grade'],
                 olddata1=olddata,
                 newdata1=newdata,
-                ))
+                )
         newdata = data['techno']
         olddata = oldmembers.get(nick, {}).get('techno', None)
         if newdata is not None and olddata is not None and newdata != olddata:
-            txtchanges2.append('TECHNO,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La technologie de {nick1} a atteint le niveau {newdata1}'.format(
+            publish_line(csvfile, 
+                'TECHNO,{nick1},{grade1},{olddata1},{newdata1},,,,,,,La technologie de {nick1} a atteint le niveau {newdata1}',
                 nick1=nick,
                 grade1=newmembers[nick]['grade'],
                 olddata1=olddata,
                 newdata1=newdata,
-                ))
-
-    for txtchange in txtchanges2:
-        logging.info(txtchange.split(',')[-1])
-        if not options.dryrun:
-            csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+                )
 
 if __name__ == '__main__':
     from optparse import OptionParser