Skip to content

Commit

Permalink
Merge pull request #252 from freezingsaddles/lint-refactoring
Browse files Browse the repository at this point in the history
Lint refactoring, add GitHub Actions linting
  • Loading branch information
obscurerichard authored Feb 5, 2024
2 parents 16ecdd3 + c87bdf2 commit 701f9c1
Show file tree
Hide file tree
Showing 32 changed files with 325 additions and 299 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
# Thanks https://www.reddit.com/r/learnpython/comments/rr6y69/comment/hqeqt68/?utm_source=share&utm_medium=web2x&context=3
ignore = E203, W503, E501

max-line-length = 88
max-complexity = 39
extend-ignore = E203
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: Lint

on: [push, pull_request]

jobs:

# Thanks https://black.readthedocs.io/en/stable/integrations/github_actions.html
lint:
runs-on: ubuntu-latest
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: black Lint
uses: psf/[email protected]
- name: isort Lint
uses: isort/isort-action@v1
with:
requirements-files: "requirements.txt requirements-test.txt"
- name: flake8 Lint
uses: py-actions/flake8@v2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*.cfg
*.egg-info
*.pyc
.*
.env*
.venv*
/*.iml
/build
/data/cache/*
Expand Down
20 changes: 8 additions & 12 deletions freezing/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import os.path

from flask import Flask, session, g

from flask import Flask, g, session
from freezing.model import init_model, meta

from .config import config

# Thanks https://stackoverflow.com/a/17073583
Expand All @@ -13,19 +10,18 @@
init_model(config.SQLALCHEMY_URL)

# This needs to be after the app is created, unfortunately.
from freezing.web.views import (
from freezing.web.views import ( # noqa
alt_scoring,
api,
chartdata,
general,
leaderboard,
chartdata,
people,
user,
pointless,
photos,
api,
alt_scoring,
pointless,
tribes,
user,
)
from freezing.web.utils import auth

# Register our blueprints

Expand Down
18 changes: 6 additions & 12 deletions freezing/web/autolog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import
import sys
import logging

import inspect
import logging


class EagerFormattingAdapter(logging.LoggerAdapter):
Expand Down Expand Up @@ -64,16 +64,10 @@ def _getUnterpolatedMessage(self, msg, args):
# by casting the left side (the "msg" variable) in this context
# to unicode. So we'll do that here

if sys.version_info >= (
3,
0,
):
# this is most likely unnecessary on python 3, but it's here
# for completeness, in the case of someone manually creating
# a bytestring
unicode_type = str
else:
unicode_type = unicode
# this is most likely unnecessary on python 3, but it's here
# for completeness, in the case of someone manually creating
# a bytestring
unicode_type = str

# handle the attempt to print utf-8 encoded data, similar to
# %-interpolation's handling of unicode formatting non-ascii
Expand Down
18 changes: 8 additions & 10 deletions freezing/web/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import logging
import os
from typing import List
from datetime import datetime, tzinfo

from colorlog import ColoredFormatter
from envparse import env
from typing import List

import arrow
import pytz

from colorlog import ColoredFormatter
from envparse import env

envfile = os.environ.get("APP_SETTINGS", os.path.join(os.getcwd(), ".env"))

Expand Down Expand Up @@ -90,9 +88,9 @@ def init_logging(loglevel: int = logging.INFO, color: bool = False):
logging.root,
]

for l in loggers:
if l is logging.root:
l.setLevel(logging.DEBUG)
for logger in loggers:
if logger is logging.root:
logger.setLevel(logging.DEBUG)
else:
l.setLevel(logging.INFO)
l.addHandler(ch)
logger.setLevel(logging.INFO)
logger.addHandler(ch)
40 changes: 18 additions & 22 deletions freezing/web/data.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
"""
Functions for interacting with the datastore and the strava apis.
"""
from __future__ import division, unicode_literals
import re
import logging

from instagram import InstagramAPIError, InstagramClientError

from polyline.codec import PolylineCodec

from sqlalchemy import and_
from geoalchemy import WKTSpatialElement

from requests.exceptions import HTTPError
from __future__ import division, unicode_literals

from stravalib import Client
from stravalib import model as strava_model
from stravalib import unithelper
import logging
import re

from freezing.model import meta, orm
from freezing.model.orm import (
Athlete,
Ride,
RideGeo,
RideEffort,
RideGeo,
RidePhoto,
RideTrack,
Team,
)
from geoalchemy import WKTSpatialElement
from instagram import InstagramAPIError, InstagramClientError
from requests.exceptions import HTTPError
from sqlalchemy import and_
from stravalib import Client
from stravalib import model as strava_model
from stravalib import unithelper

from freezing.web import app, config
from freezing.web import config
from freezing.web.autolog import log
from freezing.web.exc import (
DataEntryError,
InvalidAuthorizationToken,
NoTeamsError,
MultipleTeamsError,
DataEntryError,
NoTeamsError,
)
from freezing.web.utils import insta, wktutils

Expand Down Expand Up @@ -118,7 +114,7 @@ def update_athlete_auth(strava_athlete, token_dict):

def disambiguate_athlete_display_names():
q = meta.scoped_session().query(orm.Athlete)
q = q.filter(orm.Athlete.access_token != None)
q = q.filter(orm.Athlete.access_token is not None)
athletes = q.all()

# Ok, here is the plan; bin these things together based on firstname and last initial.
Expand Down Expand Up @@ -529,7 +525,7 @@ def write_ride_efforts(strava_activity, ride):

ride.efforts_fetched = True

except:
except Exception:
log.exception("Error adding effort for ride: {0}".format(ride))
raise

Expand Down Expand Up @@ -731,7 +727,7 @@ def write_ride_photo_primary(strava_activity, ride):
# Start by removing any priamry photos for this ride.
meta.engine.execute(
RidePhoto.__table__.delete().where(
and_(RidePhoto.ride_id == strava_activity.id, RidePhoto.primary == True)
and_(RidePhoto.ride_id == strava_activity.id, RidePhoto.primary is True)
)
)

Expand Down Expand Up @@ -772,7 +768,7 @@ def write_ride_photos_nonprimary(activity_photos, ride):

meta.engine.execute(
RidePhoto.__table__.delete().where(
and_(RidePhoto.ride_id == ride.id, RidePhoto.primary == False)
and_(RidePhoto.ride_id == ride.id, RidePhoto.primary is False)
)
)

Expand Down
2 changes: 1 addition & 1 deletion freezing/web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<li><a href="/explore/distance_by_lowtemp">Distance vs. Avg Low Temp</a></li>
<li><a href="/explore/indiv_elev_dist">Individual Dist/Elev/Speed</a></li>
<li class="disabled"><a class="disabled" {# href="/explore/team_cumul" #} data-toggle="tooltip" data-placement="top" title="This leaderboard is returning soon!">Team Cumulative</a></li>
<li><a href="/explore/team_cumul">Team Cumulative</a></li>
<li><a href="/explore/team_weekly">Team Weekly Points</a></li>
</ul>
</li>
Expand Down
6 changes: 3 additions & 3 deletions freezing/web/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
Some authentication-related utility functions/classes.
"""

import logging
from functools import wraps, update_wrapper
from datetime import timedelta
from functools import update_wrapper, wraps

from flask import session, g
from flask import make_response, request, current_app
from flask import current_app, g, make_response, request, session
from werkzeug.exceptions import Forbidden


Expand Down
5 changes: 1 addition & 4 deletions freezing/web/utils/dates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import pytz

from datetime import datetime
from dateutil import parser as date_parser

from freezing.web import app, config
from freezing.web import config


def parse_competition_timestamp(ts):
Expand Down
10 changes: 3 additions & 7 deletions freezing/web/utils/genericboard.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import decimal
import os
import enum
from datetime import datetime
from typing import List, Dict, Any, Tuple
from typing import Any, Dict, List, Tuple

import yaml

from marshmallow import fields
from marshmallow_enum import EnumField

from freezing.model import meta
from freezing.model.msg import BaseSchema, BaseMessage
from freezing.model.msg import BaseMessage, BaseSchema
from marshmallow import fields

from freezing.web.config import config
from freezing.web.exc import ObjectNotFound
Expand Down
7 changes: 6 additions & 1 deletion freezing/web/utils/gviz_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
__author__ = "Amit Weinstein, Misha Seltzer, Jacob Baskin"

import cgi
from io import StringIO
import csv
import datetime
from io import StringIO

try:
import json
except ImportError:
import simplejson as json

import types
from decimal import Decimal

Expand Down Expand Up @@ -725,6 +726,10 @@ def _PreparedData(self, order_by=()):
"Expected tuple with second value: " "'asc' or 'desc'"
)

# Thanks https://stackoverflow.com/a/22490617/424301
def cmp(a, b):
return (a > b) - (a < b)

def SortCmpFunc(row1, row2):
"""cmp function for sorted. Compares by keys and 'asc'/'desc' keywords."""
for key, asc_mult in proper_sort_keys:
Expand Down
5 changes: 1 addition & 4 deletions freezing/web/utils/hashboard.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import decimal
import os
from typing import List

import yaml

from freezing.model.msg import BaseMessage, BaseSchema
from marshmallow import fields

from freezing.model.msg import BaseSchema, BaseMessage

from freezing.web.config import config
from freezing.web.exc import ObjectNotFound

Expand Down
20 changes: 5 additions & 15 deletions freezing/web/utils/insta.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Utility functions for working with images.
"""

import os
import urllib
import shutil
import urllib

from instagram.client import InstagramAPI
from freezing.web import app, exc

from freezing.web import exc
from freezing.web.config import config

THUMBNAIL_DIMS = (150, 150)
Expand Down Expand Up @@ -57,18 +59,6 @@ def cache_photos(uid, base_dir):
)


def cache_photos(uid, base_dir):
api = configured_instagram_client()

mediaobj = api.media(uid)

for res in ("standard_resolution", "low_resolution", "thumbnail"):
photo = mediaobj.images[res]
_write_instagram_photo(
uid=uid, photo=photo, dest_dir=os.path.join(base_dir, res)
)


def _write_instagram_photo(uid, photo, dest_dir):
"""
Writes out photo for specified uid and Image object to destination directory.
Expand All @@ -79,6 +69,6 @@ def _write_instagram_photo(uid, photo, dest_dir):
"""
photo_fname = IMG_FNAME_TPL.format(uid=uid)
(filename, headers) = urllib.urlretrieve(photo.url)
if not os.path.exists(dest_dir):
if os.path.exists(dest_dir) is False:
os.makedirs(dest_dir)
shutil.move(filename, os.path.join(dest_dir, photo_fname))
4 changes: 1 addition & 3 deletions freezing/web/utils/tribes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
from typing import List

import yaml

from freezing.model.msg import BaseMessage, BaseSchema
from marshmallow import fields

from freezing.model.msg import BaseSchema, BaseMessage

from freezing.web.config import config
from freezing.web.exc import ObjectNotFound

Expand Down
Loading

0 comments on commit 701f9c1

Please sign in to comment.