Skip to content

Commit 17ff453

Browse files
authored
CI: Test on Python 3.8, drop 3.5, format with Black, lint with F… (#176)
CI: Test on Python 3.8, drop 3.5, format with Black, lint with Flake8
2 parents 984272e + e68b767 commit 17ff453

6 files changed

+68
-42
lines changed

.flake8

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 88

.travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ cache: pip
44
matrix:
55
fast_finish: true
66
include:
7+
- python: 3.8
78
- python: 3.7
89
- python: 3.6
9-
- python: 3.5
1010

1111
# whitelist
1212
# gh-pages is otherwise ignored by Travis CI
@@ -18,12 +18,13 @@ branches:
1818

1919
install:
2020
- pip install -r requirements.txt
21-
- pip install coveralls pyflakes
21+
- pip install black coveralls flake8
2222

2323
script:
2424
- coverage run --append --source scripts test/test_gpo_member_photos.py
2525
- coverage run --append --source scripts scripts/missing.py
26-
- pyflakes scripts/*.py
26+
- black --check --diff .
27+
- flake8 scripts
2728

2829
after_success:
2930
- coverage report

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
target_version = ['py36']

scripts/gpo_member_photos.py

+44-25
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
# pip install -r requirements.txt
1818
import mechanicalsoup
1919

20-
USER_AGENT = ('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 '
21-
'(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
20+
USER_AGENT = (
21+
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 "
22+
"(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
23+
)
2224

2325
regex1 = re.compile(
2426
r'<a href="https://www.congress.gov/member/[^/]+/(\w+)[^<]+</a></span>'
25-
'[^<]*<div[^<]+<div class="member-image"><img src="/img/member/([^\"]+)"')
27+
'[^<]*<div[^<]+<div class="member-image"><img src="/img/member/([^"]+)"'
28+
)
2629

2730
regex2 = re.compile('<a class="next" href="([^"]+)">')
2831

@@ -49,18 +52,20 @@ def get_photo_list(br, congress_number, delay):
4952
# Fetch a page of results from Congress.gov.
5053
print("Page %d of Congress.gov Member listing..." % page)
5154
response = br.get(
52-
"https://www.congress.gov/search?" +
53-
urlencode({
54-
"q": json.dumps(
55-
{"source": "members",
56-
"congress": str(congress_number)}),
57-
"pageSize": 250,
58-
"page": page,
59-
})).text
55+
"https://www.congress.gov/search?"
56+
+ urlencode(
57+
{
58+
"q": json.dumps(
59+
{"source": "members", "congress": str(congress_number)}
60+
),
61+
"pageSize": 250,
62+
"page": page,
63+
}
64+
)
65+
).text
6066

6167
if len(response) == 0:
62-
sys.exit("Page is blank. Try again later, you may have hit a "
63-
"limit.")
68+
sys.exit("Page is blank. Try again later, you may have hit a limit.")
6469

6570
# Scan for links to Member pages and img tags. The link to the
6671
# Congress.gov page uses the Member's Bioguide ID as the key, and the
@@ -116,8 +121,7 @@ def download_photos(br, photo_list, outdir, delay):
116121
ok = 0
117122

118123
for bioguide_id, photo_filename in photo_list:
119-
photo_url = ("https://memberguide.gpo.gov/PictorialImages/" +
120-
photo_filename)
124+
photo_url = "https://memberguide.gpo.gov/PictorialImages/" + photo_filename
121125
print(bioguide_id, photo_url)
122126

123127
filename = os.path.join(outdir, bioguide_id + ".jpg")
@@ -145,20 +149,35 @@ def resize_photos():
145149
if __name__ == "__main__":
146150
parser = argparse.ArgumentParser(
147151
description="Scrape https://memberguide.gpo.gov and save "
148-
"members' photos named after their Bioguide IDs",
149-
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
152+
"members' photos named after their Bioguide IDs",
153+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
154+
)
150155
parser.add_argument(
151-
'-n', '--congress', default='114',
152-
help="Congress session number, for example: 110, 111, 112, 113")
156+
"-n",
157+
"--congress",
158+
default="114",
159+
help="Congress session number, for example: 110, 111, 112, 113",
160+
)
153161
parser.add_argument(
154-
'-o', '--outdir', default="congress/original",
155-
help="Directory to save photos in")
162+
"-o",
163+
"--outdir",
164+
default="congress/original",
165+
help="Directory to save photos in",
166+
)
156167
parser.add_argument(
157-
'-d', '--delay', type=int, default=5, metavar='seconds',
158-
help="Rate-limiting delay between scrape requests")
168+
"-d",
169+
"--delay",
170+
type=int,
171+
default=5,
172+
metavar="seconds",
173+
help="Rate-limiting delay between scrape requests",
174+
)
159175
parser.add_argument(
160-
'-t', '--test', action='store_true',
161-
help="Test mode: don't actually save images")
176+
"-t",
177+
"--test",
178+
action="store_true",
179+
help="Test mode: don't actually save images",
180+
)
162181
args = parser.parse_args()
163182

164183
br = mechanicalsoup.Browser()

scripts/missing.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ def download_legislator_data():
1313
# clone it if it's not out
1414
if not os.path.exists("congress-legislators"):
1515
print("Cloning the congress-legislators repo...")
16-
os.system("git clone -q --depth 1 "
17-
"https://github.com/unitedstates/congress-legislators "
18-
"congress-legislators")
16+
os.system(
17+
"git clone -q --depth 1 "
18+
"https://github.com/unitedstates/congress-legislators "
19+
"congress-legislators"
20+
)
1921

2022
# Update the repo so we have the latest.
2123
print("Updating the congress-legislators repo...")
2224
# these two == git pull, but git pull ignores -q on the merge part
2325
# so is less quiet
24-
os.system("cd congress-legislators; git fetch -pq; "
25-
"git merge --ff-only -q origin/master")
26+
os.system(
27+
"cd congress-legislators; git fetch -pq; "
28+
"git merge --ff-only -q origin/master"
29+
)
2630

2731

2832
def load_yaml(filename):
@@ -36,7 +40,7 @@ def file_exists(filename):
3640
if not os.path.exists(filename):
3741
print("---")
3842
print("Not found:", filename)
39-
print(l['name'])
43+
print(l["name"])
4044
return False
4145
return True
4246

@@ -45,10 +49,9 @@ def file_exists(filename):
4549
# clone or update legislator YAML
4650
download_legislator_data()
4751

48-
legislators = load_yaml(
49-
"congress-legislators/legislators-current.yaml")
52+
legislators = load_yaml("congress-legislators/legislators-current.yaml")
5053
for l in legislators:
51-
bioguide = l['id']['bioguide']
54+
bioguide = l["id"]["bioguide"]
5255
filename = os.path.join("congress", "original", bioguide + ".jpg")
5356
if file_exists(filename):
5457
# Only check for yaml if jpg exists

test/test_gpo_member_photos.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
import sys
1010
import unittest
1111

12-
sys.path.insert(0, 'scripts')
12+
sys.path.insert(0, "scripts")
1313
import gpo_member_photos
1414

1515

1616
class TestSequenceFunctions(unittest.TestCase):
17-
1817
def test_save_metadata(self):
1918
""" Test file is saved """
2019
bioguide_id = "A000000"
@@ -40,11 +39,11 @@ def test_pause(self):
4039
time2 = datetime.datetime.now()
4140

4241
# Assert
43-
self.assertLess(time1-time0, delta)
44-
self.assertGreaterEqual(time2-time1, delta)
42+
self.assertLess(time1 - time0, delta)
43+
self.assertGreaterEqual(time2 - time1, delta)
4544

4645

47-
if __name__ == '__main__':
46+
if __name__ == "__main__":
4847
unittest.main()
4948

5049
# End of file

0 commit comments

Comments
 (0)