#65 Make python scripts flake8-consistent.
Opened a year ago by fbaumanis. Modified a year ago
fedora-web/ fbaumanis/websites flake8_fixes  into  master

file modified
+117 -82
@@ -1,6 +1,7 @@ 

  # -*- coding: utf-8 -*-

  

- from flask import Blueprint, Flask, abort, g, render_template, redirect, request, Response, send_from_directory, url_for

+ from flask import Blueprint, Flask, abort, g, render_template,\

+                   redirect, request, send_from_directory, url_for

  from flask_babel import Babel

  from flask_assets import Environment, Bundle

  from flask_frozen import Freezer
@@ -12,76 +13,79 @@ 

  # TODO: Is there a nicer way to represent the data globalvar has?

  import globalvar

  

- #FEDORA_LANGUAGES = { 'en' : 'English' , 'de': 'Deutsch'}

+ # FEDORA_LANGUAGES = { 'en' : 'English' , 'de': 'Deutsch'}

  

  # List of langs from old getfedora.org

- FEDORA_LANGUAGES_FULL = { 'en' : u'English',

-                           'af': u'Afrikaans',

-                           'ar': u'عربي',

-                           'as': u'অসমীয়া',

-                           'ast': u'Asturianu',

-                           'bal': u'بلوچی',

-                           'bg': u'български език',

-                           'bn': u'বাংলা',

-                           'bn_IN': u'বাংলা (ভারত)',

-                           'br': u'Brezhoneg',

-                           'ca': u'Català',

-                           'cs': u'česky',

-                           'da': u'dansk',

-                           'de': u'Deutsch',

-                           'el': u'Ελληνικά',

-                           'en': u'English',

-                           'en_GB': u'English (UK)',

-                           'eo': u'Esperanto',

-                           'es': u'Español',

-                           'eu': u'euskera',

-                           'fa': u'پارسی',

-                           'fi': u'suomi',

-                           'fr': u'Français',

-                           'fur': u'Friulian',

-                           'gl': u'galego',

-                           'gu': u'ગુજરાતી',

-                           'he': u'עברית',

-                           'hi': u'हिन्दी',

-                           'hu': u'Magyar',

-                           'ia': u'Interlingua',

-                           'id': u'Indonesia',

-                           'is': u'Íslenska',

-                           'it': u'Italiano',

-                           'ja': u'日本語',

-                           'ka': u'ქართული',

-                           'kn': u'ಕನ್ನಡ',

-                           'ko': u'한국어',

-                           'lv': u'latviešu',

-                           'ml': u'മലയാളം',

-                           'mr': u'मराठी',

-                           'nb': u'Norsk bokmål',

-                           'nl': u'Nederlands',

-                           'or': u'ଓଡ଼ିଆ',

-                           'pa': u'ਪੰਜਾਬੀ',

-                           'pl': u'polski',

-                           'pt': u'Português',

-                           'pt_BR': u'Português brasileiro',

-                           'ro': u'română',

-                           'ru': u'Pусский',

-                           'sk': u'slovenčina',

-                           'sq': u'Shqip',

-                           'sr': u'српски',

-                           'sv': u'svenska',

-                           'ta': u'தமிழ்',

-                           'te': u'తెలుగు',

-                           'tg': u'тоҷикӣ',

-                           'th': u'ไทย',

-                           'tr': u'Tϋrkçe',

-                           'uk': u'українська',

-                           'vi': u'Tiếng Việt',

-                           'zh_CN': u'简体中文',

-                           'zh_TW': u'正體中文'}

+ FEDORA_LANGUAGES_FULL = {'en': u'English',

+                          'af': u'Afrikaans',

+                          'ar': u'عربي',

+                          'as': u'অসমীয়া',

+                          'ast': u'Asturianu',

+                          'bal': u'بلوچی',

+                          'bg': u'български език',

+                          'bn': u'বাংলা',

+                          'bn_IN': u'বাংলা (ভারত)',

+                          'br': u'Brezhoneg',

+                          'ca': u'Català',

+                          'cs': u'česky',

+                          'da': u'dansk',

+                          'de': u'Deutsch',

+                          'el': u'Ελληνικά',

+                          'en': u'English',

+                          'en_GB': u'English (UK)',

+                          'eo': u'Esperanto',

+                          'es': u'Español',

+                          'eu': u'euskera',

+                          'fa': u'پارسی',

+                          'fi': u'suomi',

+                          'fr': u'Français',

+                          'fur': u'Friulian',

+                          'gl': u'galego',

+                          'gu': u'ગુજરાતી',

+                          'he': u'עברית',

+                          'hi': u'हिन्दी',

+                          'hu': u'Magyar',

+                          'ia': u'Interlingua',

+                          'id': u'Indonesia',

+                          'is': u'Íslenska',

+                          'it': u'Italiano',

+                          'ja': u'日本語',

+                          'ka': u'ქართული',

+                          'kn': u'ಕನ್ನಡ',

+                          'ko': u'한국어',

+                          'lv': u'latviešu',

+                          'ml': u'മലയാളം',

+                          'mr': u'मराठी',

+                          'nb': u'Norsk bokmål',

+                          'nl': u'Nederlands',

+                          'or': u'ଓଡ଼ିଆ',

+                          'pa': u'ਪੰਜਾਬੀ',

+                          'pl': u'polski',

+                          'pt': u'Português',

+                          'pt_BR': u'Português brasileiro',

+                          'ro': u'română',

+                          'ru': u'Pусский',

+                          'sk': u'slovenčina',

+                          'sq': u'Shqip',

+                          'sr': u'српски',

+                          'sv': u'svenska',

+                          'ta': u'தமிழ்',

+                          'te': u'తెలుగు',

+                          'tg': u'тоҷикӣ',

+                          'th': u'ไทย',

+                          'tr': u'Tϋrkçe',

+                          'uk': u'українська',

+                          'vi': u'Tiếng Việt',

+                          'zh_CN': u'简体中文',

+                          'zh_TW': u'正體中文'}

  

  FEDORA_LANGUAGE_DEFAULT = 'en'

  

  # Only include translations which actually have a translations file

- FEDORA_LANGUAGES = {k: v for k, v in FEDORA_LANGUAGES_FULL.items() if k in os.listdir('translations') or k == FEDORA_LANGUAGE_DEFAULT}

+ FEDORA_LANGUAGES = {}

+ for k, v in FEDORA_LANGUAGES_FULL.items():

+     if k in os.listdir('translations') or k == FEDORA_LANGUAGE_DEFAULT:

+         FEDORA_LANGUAGES[k] = v

  

  # Set this early so we can base routing logic on it.

  if __name__ == '__main__':
@@ -119,39 +123,52 @@ 

  assets.register('css_all', css)

  

  loader = jinja2.FileSystemLoader(

-     [ './site/'

-     , './partials/'      

-     , '../partials/templates/'

-     , '../templates'

-     ])

+     ['./site/',

+      './partials/',

+      '../partials/templates/',

+      '../templates']

+ )

  app.jinja_loader = loader

  

  dl_links = set()

  checksum_links = set()

  

+ 

  @app.context_processor

  def inject_globalvars():

      r = {}

+ 

      with open('release.yaml') as data:

          r = yaml.safe_load(data)

+ 

      def download_link(override, link):

          global dl_links

          if override != 'default':

              link = override

          dl_links.add(link)

          return link

+ 

      def checksum_link(link):

          global checksum_links

          checksum_links.add(link)

          return url_for('checksums.static', filename=link)

-     return dict(

+ 

+     result_dict = dict(

          dl=download_link,

          checksum=checksum_link,

          globalvar=globalvar,

          releaseinfo=r,

-         lang_code=g.current_lang if hasattr(g, 'current_lang') else app.config['BABEL_DEFAULT_LOCALE'],

          languages=FEDORA_LANGUAGES,

-         endpoint=request.endpoint.replace('_i18n', ''))

+         endpoint=request.endpoint.replace('_i18n', '')

+     )

+ 

+     if hasattr(g, 'current_lang'):

+         result_dict["lang_code"] = g.current_lang

+     else:

+         result_dict["lang_code"] = app.config['BABEL_DEFAULT_LOCALE']

+ 

+     return result_dict

+ 

  

  @app.before_request

  def handle_language_code():
@@ -162,32 +179,44 @@ 

          else:

              request.view_args.pop('lang_code')

  

+ 

  @babel.localeselector

  def get_locale():

-     translations = [str(translation) for translation in babel.list_translations()]

      return g.get('current_lang', app.config['BABEL_DEFAULT_LOCALE'])

  

+ 

  # We register these as blueprints so that everything in these folders gets

  # frozen. This is slightly hacky, but not too bad.

- keys = Blueprint('keys', __name__, static_folder='static/keys', static_url_path='/static/keys')

+ keys = Blueprint(

+         'keys',

+         __name__,

+         static_folder='static/keys',

+         static_url_path='/static/keys')

  app.register_blueprint(keys)

  

- checksums = Blueprint('checksums', __name__, static_folder='static/checksums', static_url_path='/static/checksums')

+ checksums = Blueprint(

+         'checksums',

+         __name__,

+         static_folder='static/checksums',

+         static_url_path='/static/checksums')

  app.register_blueprint(checksums)

  

  # This is a more manual attempt at still having some automation.

  freeze_indexes = set()

+ 

+ 

  def export_route(name, path, template=None):

      global freeze_indexes

      freeze_indexes.add(name)

+ 

      def r():

          return render_template(template or path.strip('/') + '/index.html')

      r.__name__ = name

      if freezing:

-         # This is a bit hacky, but we want to generate index.html.langcode files

-         # while tricking url_for into generating links that are prettier than

-         # that. So we generate two routes, one for the pretty url, and one for

-         # the i18n-specific index.html.langcode URL.

+         # This is a bit hacky, but we want to generate index.html.langcode

+         # files while tricking url_for into generating links that are

+         # prettier than that. So we generate two routes, one for the pretty

+         # url, and one for the i18n-specific index.html.langcode URL.

          # We tell the freezer to include the _i18n one, but everything else

          # refers to the pretty one.

          app.route('/<lang_code>' + path, endpoint=name)(r)
@@ -196,10 +225,13 @@ 

          app.route('/<lang_code>' + path, endpoint=name)(r)

      return r

  

+ 

  if not freezing:

      @app.route('/')

      def index_redirect():

-         return redirect('/' + app.config['BABEL_DEFAULT_LOCALE'] + '/', code=302)

+         return redirect(

+                 '/' + app.config['BABEL_DEFAULT_LOCALE'] + '/',

+                 code=302)

  

  export_route('index', '/')

  
@@ -228,17 +260,20 @@ 

  def magazine_json():

      return send_from_directory('static', 'magazine.json')

  

+ 

  @app.route('/static/fedora.gpg')

  def gpgkey():

      return send_from_directory('static', 'fedora.gpg')

  

+ 

  @freezer.register_generator

  def index():

      for lang in FEDORA_LANGUAGES:

-         #yield {'lang_code': lang}

+         # yield {'lang_code': lang}

          for name in freeze_indexes:

              yield (name + '_i18n'), {'lang_code': lang}

  

+ 

  if __name__ == '__main__':

      # Minification is good for production, but not for debugging.

      app.config['MINIFY_PAGE'] = True

@@ -5,7 +5,10 @@ 

  headers = {'user-agent': 'getfedora-builder/0.0.1'}

  params = {'per_page': '3'}

  

- r = requests.get('https://fedoramagazine.org/wp-json/wp/v2/posts', params=params, headers=headers)

+ r = requests.get(

+         'https://fedoramagazine.org/wp-json/wp/v2/posts',

+         params=params,

+         headers=headers)

  f = open('static/magazine.json', 'w')

  posts = []

  for i in r.json()[0:3]:
@@ -14,9 +17,9 @@ 

      p['title'] = i['title']['rendered']

      p['date'] = i['date']

  

-     #['wp:featuredmedia'][0].href:

+     # ['wp:featuredmedia'][0].href:

      image_url = i['_links']['wp:featuredmedia'][0]['href']

-     r2 = requests.get(image_url,headers=headers)

+     r2 = requests.get(image_url, headers=headers)

      p['image_url'] = r2.json()['media_details']['sizes']['medium_large']['source_url']

      posts.append(p)

  

Thsi PR contains changeds to make the python scripts in the websites repo consistent to the flake8 styleguides.
I avoided changes to releases-json.py, as this script gets merged into main.py in another PR.