chiark / gitweb /
create addElementIfInApk() function for clean up common operation
authorHans-Christoph Steiner <hans@eds.org>
Thu, 13 Oct 2016 16:02:44 +0000 (18:02 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Wed, 2 Nov 2016 17:10:13 +0000 (18:10 +0100)
There are currently a couple different ways this is done in the code, this
commit changes all of them to be like addElementNonEmpty().

fdroidserver/update.py

index a93d982ebffcdbd8b6924393107b0cc1e3b8105f..0935325617c5ab7289e1f9ec130003078134350c 100644 (file)
@@ -883,6 +883,12 @@ def make_index(apps, sortedids, apks, repodir, archive, categories):
             return
         addElement(name, value, doc, parent)
 
+    def addElementIfInApk(name, apk, key, doc, parent):
+        if key not in apk:
+            return
+        value = str(apk[key])
+        addElement(name, value, doc, parent)
+
     def addElementCDATA(name, value, doc, parent):
         el = doc.createElement(name)
         el.appendChild(doc.createCDATASection(value))
@@ -1069,8 +1075,7 @@ def make_index(apps, sortedids, apks, repodir, archive, categories):
             addElement('version', apk['version'], doc, apkel)
             addElement('versioncode', str(apk['versioncode']), doc, apkel)
             addElement('apkname', apk['apkname'], doc, apkel)
-            if 'srcname' in apk:
-                addElement('srcname', apk['srcname'], doc, apkel)
+            addElementIfInApk('srcname', apk, 'srcname', doc, apkel)
             for hash_type in ['sha256']:
                 if hash_type not in apk:
                     continue
@@ -1081,14 +1086,18 @@ def make_index(apps, sortedids, apks, repodir, archive, categories):
             addElement('sig', apk['sig'], doc, apkel)
             addElement('size', str(apk['size']), doc, apkel)
             addElement('sdkver', str(apk['minSdkVersion']), doc, apkel)
-            if 'targetSdkVersion' in apk:
-                addElement('targetSdkVersion', str(apk['targetSdkVersion']), doc, apkel)
-            if 'maxSdkVersion' in apk:
-                addElement('maxsdkver', str(apk['maxSdkVersion']), doc, apkel)
-            addElementNonEmpty('obbMainFile', apk.get('obbMainFile'), doc, apkel)
-            addElementNonEmpty('obbMainFileSha256', apk.get('obbMainFileSha256'), doc, apkel)
-            addElementNonEmpty('obbPatchFile', apk.get('obbPatchFile'), doc, apkel)
-            addElementNonEmpty('obbPatchFileSha256', apk.get('obbPatchFileSha256'), doc, apkel)
+            addElementIfInApk('targetSdkVersion', apk,
+                              'targetSdkVersion', doc, apkel)
+            addElementIfInApk('maxsdkver', apk,
+                              'maxSdkVersion', doc, apkel)
+            addElementIfInApk('obbMainFile', apk,
+                              'obbMainFile', doc, apkel)
+            addElementIfInApk('obbMainFileSha256', apk,
+                              'obbMainFileSha256', doc, apkel)
+            addElementIfInApk('obbPatchFile', apk,
+                              'obbPatchFile', doc, apkel)
+            addElementIfInApk('obbPatchFileSha256', apk,
+                              'obbPatchFileSha256', doc, apkel)
             if 'added' in apk:
                 addElement('added', time.strftime('%Y-%m-%d', apk['added']), doc, apkel)