commit:     5e7347647516660603dddeedcf570d0cfef27b1a
Author:     Gilles Dartiguelongue <eva <AT> gentoo <DOT> org>
AuthorDate: Sun Jan 22 12:18:00 2017 +0000
Commit:     Gilles Dartiguelongue <eva <AT> gentoo <DOT> org>
CommitDate: Sun Jan 22 12:23:56 2017 +0000
URL:        https://gitweb.gentoo.org/proj/grumpy.git/commit/?id=5e734764

sync: define project keys default values

Costs less than checking for it in each loop iteration and does no
harm later to loop on empty lists.

 backend/lib/sync.py | 64 +++++++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/backend/lib/sync.py b/backend/lib/sync.py
index 5e8240d..0aab3bc 100644
--- a/backend/lib/sync.py
+++ b/backend/lib/sync.py
@@ -28,7 +28,10 @@ def get_project_data():
         if proj_elem.tag.lower() != 'project':
             print("Skipping unknown <projects> subtag <%s>" % proj_elem.tag)
             continue
-        proj = {}
+        proj = {
+            'members': [],
+            'subprojects': [],
+        }
         for elem in proj_elem:
             tag = elem.tag.lower()
             if tag in ['email']:
@@ -46,14 +49,11 @@ def get_project_data():
                     if member_tag in ['name', 'role']:
                         member[member_tag] = member_elem.text
                 if 'email' in member:
-                    if 'members' not in proj:
-                        proj['members'] = []
                     proj['members'].append(member)
             elif tag == 'subproject':
                 if 'ref' in elem.attrib:
-                    if 'subprojects' not in proj:
-                        proj['subprojects'] = []
-                    # subprojects will be a list of (subproject_email, 
inherit-members) tuples where inherit-members is True or False. TODO: Might 
change if sync code will want it differently
+                    # subprojects will be a list of (subproject_email, 
inherit-members) tuples where inherit-members is True or False.
+                    # TODO: Might change if sync code will want it differently
                     proj['subprojects'].append((elem.attrib['ref'].lower(), 
True if ('inherit-members' in elem.attrib and elem.attrib['inherit-members'] == 
'1') else False))
                 else:
                     print("Invalid <subproject> tag inside project %s - 
required 'ref' attribute missing" % proj['email'] if 'email' in proj else 
"<unknown>")
@@ -86,32 +86,34 @@ def sync_projects():
             new_maintainer = Maintainer(email=email, is_project=True, 
description=data['description'], name=data['name'], url=data['url'])
             db.session.add(new_maintainer)
             existing_maintainers[email] = new_maintainer
+
         members = []
-        if 'subprojects' in data:
-            for subproject_email, inherit_members in data['subprojects']:
-                # TODO: How should we handle inherit_members?
-                if subproject_email in existing_maintainers:
-                    members.append(existing_maintainers[subproject_email])
-                else:
-                    print("Creating new project entry for subproject: %s" % 
subproject_email)
-                    new_subproject = Maintainer(email=subproject_email, 
is_project=True)
-                    db.session.add(new_subproject)
-                    existing_maintainers[subproject_email] = new_subproject
-                    members.append(new_subproject)
-        if 'members' in data:
-            for member in data['members']:
-                if member['email'] in existing_maintainers:
-                    # TODO: Stop overwriting the name from master data, 
if/once we have a proper sync source for individual maintainers (Gentoo LDAP?)
-                    if 'name' in member:
-                        existing_maintainers[member['email']].name = 
member['name']
-                    members.append(existing_maintainers[member['email']])
-                else:
-                    print("Adding individual maintainer %s" % member['email'])
-                    new_maintainer = Maintainer(email=member['email'], 
is_project=False, name=member['name'] if 'name' in member else None)
-                    db.session.add(new_maintainer)
-                    existing_maintainers[member['email']] = new_maintainer
-                    members.append(new_maintainer)
-            # TODO: Include role information in the association?
+
+        for subproject_email, inherit_members in data['subprojects']:
+            # TODO: How should we handle inherit_members?
+            if subproject_email in existing_maintainers:
+                members.append(existing_maintainers[subproject_email])
+            else:
+                print("Creating new project entry for subproject: %s" % 
subproject_email)
+                new_subproject = Maintainer(email=subproject_email, 
is_project=True)
+                db.session.add(new_subproject)
+                existing_maintainers[subproject_email] = new_subproject
+                members.append(new_subproject)
+
+        for member in data['members']:
+            if member['email'] in existing_maintainers:
+                # TODO: Stop overwriting the name from master data, if/once we 
have a proper sync source for individual maintainers (Gentoo LDAP?)
+                if 'name' in member:
+                    existing_maintainers[member['email']].name = member['name']
+                members.append(existing_maintainers[member['email']])
+            else:
+                print("Adding individual maintainer %s" % member['email'])
+                new_maintainer = Maintainer(email=member['email'], 
is_project=False, name=member['name'] if 'name' in member else None)
+                db.session.add(new_maintainer)
+                existing_maintainers[member['email']] = new_maintainer
+                members.append(new_maintainer)
+
+        # TODO: Include role information in the association?
         existing_maintainers[email].members = members
     db.session.commit()
 

Reply via email to