Ajout des colones x et y
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 28 May 2012 21:57:11 +0000 (21:57 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 28 May 2012 21:57:11 +0000 (21:57 +0000)
Préservations des données existantes x, y, colonized and active si pas dispo dans newmembers

members.py

index ea8fef029bd9277b5e2e6435532502727b452d93..759b614b550f7136d1a7c1833c5363051f6e7f38 100755 (executable)
@@ -205,6 +205,20 @@ def members_fetch(alliance_tag=None):
     return members
 
 
+def copy_existing_data(oldmembers, newmembers):
+    '''
+    Check if newmembers contains some keys and if missing, copy them from
+    oldmembers:
+    colonized, active: if alliance is forced, these keys are not fetched
+    x, y: only fetch them when neded.
+    '''
+    for nick, data in newmembers.items():
+        for key in ['colonized', 'active', 'x', 'y' ]:
+            if data.get(key, None) is None:
+                # missing or None
+                value = oldmembers.get(nick, {}).get(key, None)
+                newmembers[nick][key] = value
+
 def members_to_field(members):
     """
     Takes a full information dict "members"
@@ -465,6 +479,7 @@ if __name__ == '__main__':
         newmembers = members_fetch(options.alliance)
 
     if oldmembers is not None:
+        copy_existing_data(oldmembers, newmembers)
         csvfilename = os.path.join(BASE_DIRECTORY, 'results.csv')
         csvfile = open(csvfilename, mode='a', encoding='utf-8')
         field_compare(oldmembers, newmembers, csvfile)
@@ -486,7 +501,7 @@ if __name__ == '__main__':
         members_csvfilename = os.path.join(BASE_DIRECTORY, "members.csv")
         members_csvfilename_new = members_csvfilename + '.new'
         f = open(members_csvfilename_new, mode='w', encoding='utf-8')
-        f.write('grade,nick,field,techno,anthill,active,colonized\n')
+        f.write('grade,nick,field,techno,anthill,active,colonized,x,y\n')
         for nick, data in newmembers.items():
             data = [
                 data['grade'],
@@ -496,6 +511,8 @@ if __name__ == '__main__':
                 data['anthill'],
                 data['active'],
                 data['colonized'],
+                data['x'],
+                data['y'],
                 ]
             data = [ str(x) for x in data ]
             f.write(','.join(data) + '\n')