[odrs-web] More black fixes



commit 37a5efe60f44daaf467c0b25b18eed639832ab8e
Author: Richard Hughes <richard hughsie com>
Date:   Thu Mar 17 14:03:35 2022 +0000

    More black fixes

 app_data/Makefile                          |   2 +-
 app_data/cron.py                           | 135 ++++++++++++++++++-----------
 app_data/flaskapp.py                       |   2 +-
 app_data/pylint_test.py                    |  16 ++--
 app_data/wsgi-scripts/odrs-apache-setup.py |  17 ++--
 5 files changed, 102 insertions(+), 70 deletions(-)
---
diff --git a/app_data/Makefile b/app_data/Makefile
index 1f6461f..bcd13e5 100644
--- a/app_data/Makefile
+++ b/app_data/Makefile
@@ -44,7 +44,7 @@ check: $(PYTEST)
        $(PYTHON) ./pylint_test.py
 
 blacken:
-       find migrations odrs -name '*.py' -exec ./env/bin/black {} \;
+       find . -path ./env -prune -o -name '*.py' -exec ./env/bin/black --quiet {} \;
 
 codespell:
        codespell --write-changes --builtin en-GB_to_en-US --skip \
diff --git a/app_data/cron.py b/app_data/cron.py
index efebeaf..5e53275 100755
--- a/app_data/cron.py
+++ b/app_data/cron.py
@@ -20,59 +20,73 @@ from odrs import db
 from odrs.models import Review, Taboo, Component
 from odrs.util import _get_rating_for_component, _get_taboos_for_locale
 
+
 def _fsck_components():
 
     # get all existing components
     components = {}
-    for component in db.session.query(Component).\
-                        filter(Component.app_id != '').\
-                        order_by(Component.app_id.asc()).all():
+    for component in (
+        db.session.query(Component)
+        .filter(Component.app_id != "")
+        .order_by(Component.app_id.asc())
+        .all()
+    ):
         components[component.app_id] = component
 
     # guessed, thanks Canonical :/
     for app_id in components:
-        if not app_id.startswith('io.snapcraft.'):
+        if not app_id.startswith("io.snapcraft."):
             continue
         if components[app_id].component_id_parent:
             continue
-        name, _ = app_id[13:].rsplit('-', maxsplit=1)
-        parent = components.get(name + '.desktop')
+        name, _ = app_id[13:].rsplit("-", maxsplit=1)
+        parent = components.get(name + ".desktop")
         if not parent:
             continue
-        print('adding snapcraft parent for {} -> {}'.format(components[app_id].app_id,
-                                                            parent.app_id))
+        print(
+            "adding snapcraft parent for {} -> {}".format(
+                components[app_id].app_id, parent.app_id
+            )
+        )
         parent.adopt(components[app_id])
 
     # upstream drops the .desktop sometimes
     for app_id in components:
         if components[app_id].component_id_parent:
             continue
-        app_id_new = app_id.replace('.desktop', '')
+        app_id_new = app_id.replace(".desktop", "")
         if app_id == app_id_new:
             continue
         parent = components.get(app_id_new)
         if not parent:
             continue
-        print('adding parent for {} -> {}'.format(components[app_id].app_id,
-                                                  parent.app_id))
+        print(
+            "adding parent for {} -> {}".format(
+                components[app_id].app_id, parent.app_id
+            )
+        )
         parent.adopt(components[app_id])
 
     # API change :/
     for app_id in components:
-        if not app_id.endswith('.shell-extension'):
+        if not app_id.endswith(".shell-extension"):
             continue
         if components[app_id].component_id_parent:
             continue
-        app_id_new = app_id.replace('.shell-extension', '')
-        app_id_new = app_id_new.replace('@', '_')
+        app_id_new = app_id.replace(".shell-extension", "")
+        app_id_new = app_id_new.replace("@", "_")
         parent = components.get(app_id_new)
         if not parent:
             continue
-        print('adding shell parent for {} -> {}'.format(components[app_id].app_id,
-                                                        parent.app_id))
+        print(
+            "adding shell parent for {} -> {}".format(
+                components[app_id].app_id, parent.app_id
+            )
+        )
         parent.adopt(components[app_id])
     db.session.commit()
 
+
 def _auto_delete(days=31):
 
     # delete all reviews with taboo, otherwise the moderatorators get overwhelmed
@@ -97,29 +111,30 @@ def _auto_delete(days=31):
         db.session.delete(review)
     db.session.commit()
 
+
 def _fsck():
     _auto_delete()
     _fsck_components()
 
+
 def _regenerate_ratings(fn):
     item = {}
-    for component in db.session.query(Component).\
-                                order_by(Component.app_id.asc()).all():
+    for component in db.session.query(Component).order_by(Component.app_id.asc()).all():
         ratings = _get_rating_for_component(component, 2)
         if len(ratings) == 0:
             continue
         item[component.app_id] = ratings
 
     # dump to file
-    with open(fn, 'w') as outfd:
-        outfd.write(json.dumps(item, sort_keys=True, indent=4, separators=(',', ': ')))
+    with open(fn, "w") as outfd:
+        outfd.write(json.dumps(item, sort_keys=True, indent=4, separators=(",", ": ")))
+
 
 def _taboo_check():
 
     # this is moderately expensive, so cache for each locale
     taboos = {}
-    for review in db.session.query(Review).\
-                    filter(Review.reported < 5).all():
+    for review in db.session.query(Review).filter(Review.reported < 5).all():
         if review.locale not in taboos:
             taboos[review.locale] = _get_taboos_for_locale(review.locale)
         matched_taboos = review.matches_taboos(taboos[review.locale])
@@ -129,6 +144,7 @@ def _taboo_check():
             review.reported = 5
     db.session.commit()
 
+
 def _appstream_import(fn):
 
     # get existing components
@@ -137,13 +153,13 @@ def _appstream_import(fn):
         app_ids[component.app_id] = component
 
     # parse xml
-    with gzip.open(fn, 'rb') as f:
-        for component in ET.fromstring(f.read()).xpath('/components/component'):
-            app_id = component.xpath('id')[0].text
+    with gzip.open(fn, "rb") as f:
+        for component in ET.fromstring(f.read()).xpath("/components/component"):
+            app_id = component.xpath("id")[0].text
             if app_id not in app_ids:
                 continue
             children = []
-            for provide in component.xpath('provides/id'):
+            for provide in component.xpath("provides/id"):
                 child_id = provide.text
                 if child_id == app_id:
                     continue
@@ -156,89 +172,102 @@ def _appstream_import(fn):
                 continue
             parent = app_ids[app_id]
             for child in children:
-                print('adding AppStream parent for {} -> {}'.format(child.app_id,
-                                                                    parent.app_id))
+                print(
+                    "adding AppStream parent for {} -> {}".format(
+                        child.app_id, parent.app_id
+                    )
+                )
                 parent.adopt(child)
     db.session.commit()
 
+
 def _taboo_import_item(taboos, locale, value, description, severity):
-    key = locale + ':' + value
+    key = locale + ":" + value
     if key in taboos:
         taboo = taboos[key]
         is_dirty = False
         if taboo.description != description:
-            print('Modifying {} description from "{}" to "{}"'.format(key,
-                                                                      taboo.description,
-                                                                      description))
+            print(
+                'Modifying {} description from "{}" to "{}"'.format(
+                    key, taboo.description, description
+                )
+            )
             taboo.description = description
             is_dirty = True
         if taboo.severity != severity:
-            print('Modifying {} severity from "{}" to "{}"'.format(key,
-                                                                   taboo.severity,
-                                                                   severity))
+            print(
+                'Modifying {} severity from "{}" to "{}"'.format(
+                    key, taboo.severity, severity
+                )
+            )
             taboo.severity = severity
             is_dirty = True
         return is_dirty
-    if value.find(' ') != -1:
-        print('Ignoring', locale, value)
+    if value.find(" ") != -1:
+        print("Ignoring", locale, value)
         return False
     if value.lower() != value:
-        print('Ignoring', locale, value)
+        print("Ignoring", locale, value)
         return False
     taboo = Taboo(locale, value, description, severity=severity)
     taboos[key] = taboo
-    print('Adding {}'.format(key))
+    print("Adding {}".format(key))
     db.session.add(taboo)
     return True
 
+
 def _taboo_import(fn):
 
     # get all the taboos in one database call
     taboos = {}
     for taboo in db.session.query(Taboo).all():
-        key = taboo.locale + ':' + taboo.value
+        key = taboo.locale + ":" + taboo.value
         taboos[key] = taboo
 
     # add any new ones
     is_dirty = False
-    with open(fn, newline='') as csvfile:
+    with open(fn, newline="") as csvfile:
         for locale, values, description, severity in csv.reader(csvfile):
             locale = locale.strip()
             description = description.strip()
             severity = int(severity)
-            for value in values.split('/'):
+            for value in values.split("/"):
                 value = value.strip()
-                is_dirty = _taboo_import_item(taboos, locale, value, description, severity) or is_dirty
+                is_dirty = (
+                    _taboo_import_item(taboos, locale, value, description, severity)
+                    or is_dirty
+                )
     db.session.commit()
 
     # if dirty, check all the existing reviews
     if is_dirty:
         _taboo_check()
 
-if __name__ == '__main__':
+
+if __name__ == "__main__":
 
     if len(sys.argv) < 2:
-        print('Usage: %s ratings|fsck|taboo-check|taboo-import' % sys.argv[0])
+        print("Usage: %s ratings|fsck|taboo-check|taboo-import" % sys.argv[0])
         sys.exit(1)
 
     # create the ratings data
-    if sys.argv[1] == 'ratings':
+    if sys.argv[1] == "ratings":
         if len(sys.argv) < 3:
-            print('Usage: %s ratings filename' % sys.argv[0])
+            print("Usage: %s ratings filename" % sys.argv[0])
             sys.exit(1)
         _regenerate_ratings(sys.argv[2])
-    elif sys.argv[1] == 'fsck':
+    elif sys.argv[1] == "fsck":
         _fsck()
-    elif sys.argv[1] == 'taboo-check':
+    elif sys.argv[1] == "taboo-check":
         _taboo_check()
-    elif sys.argv[1] == 'taboo-import':
+    elif sys.argv[1] == "taboo-import":
         if len(sys.argv) < 3:
-            print('Usage: %s taboo-import filename' % sys.argv[0])
+            print("Usage: %s taboo-import filename" % sys.argv[0])
             sys.exit(1)
         _taboo_import(sys.argv[2])
-    elif sys.argv[1] == 'appstream-import':
+    elif sys.argv[1] == "appstream-import":
         if len(sys.argv) < 3:
-            print('Usage: %s taboo-import filename' % sys.argv[0])
+            print("Usage: %s taboo-import filename" % sys.argv[0])
             sys.exit(1)
         _appstream_import(sys.argv[2])
     else:
diff --git a/app_data/flaskapp.py b/app_data/flaskapp.py
index d516a1a..84a9e2f 100755
--- a/app_data/flaskapp.py
+++ b/app_data/flaskapp.py
@@ -7,6 +7,6 @@
 
 from odrs import app
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     app.debug = True
     app.run()
diff --git a/app_data/pylint_test.py b/app_data/pylint_test.py
index 43479c9..115397d 100755
--- a/app_data/pylint_test.py
+++ b/app_data/pylint_test.py
@@ -11,24 +11,25 @@ import sys
 from glob import glob
 from pylint import epylint
 
+
 def main():
 
     # find python files
-    filenames = [y for x in os.walk('.') for y in glob(os.path.join(x[0], '*.py'))]
+    filenames = [y for x in os.walk(".") for y in glob(os.path.join(x[0], "*.py"))]
     rc = 0
     argv = []
     for fn in sorted(filenames):
-        if fn.find('migrations/') != -1:
+        if fn.find("migrations/") != -1:
             continue
-        if fn.startswith('./env'):
+        if fn.startswith("./env"):
             continue
-        print('Checking %s' % fn)
+        print("Checking %s" % fn)
         argv.append(fn)
 
     # run with 8 parallel tasks
-    argv.append('-j 8')
-    argv.append('--rcfile contrib/pylintrc')
-    (pylint_stdout, pylint_stderr) = epylint.py_run(' '.join(argv), return_std=True)
+    argv.append("-j 8")
+    argv.append("--rcfile contrib/pylintrc")
+    (pylint_stdout, pylint_stderr) = epylint.py_run(" ".join(argv), return_std=True)
     stderr = pylint_stderr.read()
     stdout = pylint_stdout.read()
     if stderr or stdout:
@@ -36,5 +37,6 @@ def main():
         rc = 1
     return rc
 
+
 if __name__ == "__main__":
     sys.exit(main())
diff --git a/app_data/wsgi-scripts/odrs-apache-setup.py b/app_data/wsgi-scripts/odrs-apache-setup.py
index 2446e13..a93c4f6 100644
--- a/app_data/wsgi-scripts/odrs-apache-setup.py
+++ b/app_data/wsgi-scripts/odrs-apache-setup.py
@@ -8,17 +8,18 @@
 import sys
 import os
 
+
 def subst_inplace(fn):
-    with open(fn, 'r') as f:
+    with open(fn, "r") as f:
         blob = f.read()
-    for key in ['SQLALCHEMY_DATABASE_URI',
-                'ODRS_REVIEWS_SECRET']:
-        search = '%%{ENV:%s}' % format(key)
-        blob = blob.replace(search, os.environ.get(key, 'INVALID'))
-    with open(fn, 'w') as f:
+    for key in ["SQLALCHEMY_DATABASE_URI", "ODRS_REVIEWS_SECRET"]:
+        search = "%%{ENV:%s}" % format(key)
+        blob = blob.replace(search, os.environ.get(key, "INVALID"))
+    with open(fn, "w") as f:
         f.write(blob)
 
-if __name__ == '__main__':
+
+if __name__ == "__main__":
     for argv in sys.argv[1:]:
-        print('Processing', argv)
+        print("Processing", argv)
         subst_inplace(argv)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]