Skip to content

Commit 06155b1

Browse files
committed
Uplift code to python3
This change paints the code black and deletes a bunch of old code from setup.py . Fix a dependabot issue in the interim re: flask.
1 parent c9e395e commit 06155b1

File tree

10 files changed

+60
-86
lines changed

10 files changed

+60
-86
lines changed

programming/python/flask-example/frontend/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
__date__ = "$Date$"
55
__license__ = ""
66
__version_info__ = (0, 0, 1)
7-
__version__ = '.'.join(map(str, __version_info__))
7+
__version__ = ".".join(str(vi) for vi in __version_info__)

programming/python/flask-example/frontend/forms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
app = Flask(__name__)
1919

20+
2021
# The gist of any call from the forms to the models should be something like:
2122
# 1. Grab the request (flask)
2223
# 2. Punt it over to the model logic (us)
@@ -43,7 +44,6 @@ def form_to_model(api_version, stack=1):
4344
# need to write another decorator to make versioned APIs transparent
4445
# first.
4546
def get_json_from_api(api_version):
46-
4747
model_callee = form_to_model(api_version, stack=2)
4848

4949
return jsonify(model_callee(**dict(request.args)))

programming/python/flask-example/frontend/forms/req1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from frontend.forms import app, get_json_from_api
1111

12-
@app.route('/<int:api_version>/req1')
12+
13+
@app.route("/<int:api_version>/req1")
1314
def req1(api_version):
1415
# 1. Grab the request.
1516
# 2. Punt it over to the model logic

programming/python/flask-example/frontend/forms/req2.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111

1212
from frontend.forms import app
1313

14-
@app.route('/<int:api_version>/req2')
14+
15+
@app.route("/<int:api_version>/req2")
1516
def req2(api_version):
1617
d = {
17-
'time': time.clock(),
18-
'version': api_version,
18+
"time": time.monotonic(),
19+
"version": api_version,
1920
}
20-
if request.args.get('action', None) == 'gets_me_a_string':
21-
d['foo'] = 'foo'
22-
d['a'] = list('abcd')
21+
if request.args.get("action", None) == "gets_me_a_string":
22+
d["foo"] = "foo"
23+
d["a"] = list("abcd")
2324
else:
24-
d['error'] = 'INVALID REQUEST!'
25+
d["error"] = "INVALID REQUEST!"
2526
return jsonify(d)
26-
27-

programming/python/flask-example/frontend/main.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@
77
from frontend.forms import app
88

99

10-
if __name__ == '__main__':
10+
if __name__ == "__main__":
1111
app.debug = settings.SITE_DEBUG
1212

1313

1414
# Log via email if exceptions occur; based on:
1515
# http://flask.pocoo.org/docs/errorhandling/
1616
if not app.debug:
1717
from logging.handlers import SMTPHandler
18-
subject_line = 'Exception encountered on %s' % (settings.SITE_HOSTNAME)
19-
mail_handler = SMTPHandler(settings.SITE_MAIL_HOST,
20-
settings.SITE_ADMINS[0],
21-
settings.SITE_ADMINS,
22-
subject_line,
23-
credentials=(settings.SITE_MAIL_USER,
24-
settings.SITE_MAIL_PASSWORD),
25-
secure=(),
26-
)
18+
19+
subject_line = "Exception encountered on %s" % (settings.SITE_HOSTNAME)
20+
mail_handler = SMTPHandler(
21+
settings.SITE_MAIL_HOST,
22+
settings.SITE_ADMINS[0],
23+
settings.SITE_ADMINS,
24+
subject_line,
25+
credentials=(settings.SITE_MAIL_USER, settings.SITE_MAIL_PASSWORD),
26+
secure=(),
27+
)
2728
mail_handler.setLevel(settings.SITE_MAIL_LOG_LEVEL)
2829
app.logger.addHandler(mail_handler)
2930

3031

31-
if __name__ == '__main__':
32+
if __name__ == "__main__":
3233
run_dict = {
33-
'host': settings.SITE_HTTP_LISTEN_ADDRESS,
34-
'port': settings.SITE_HTTP_LISTEN_PORT,
34+
"host": settings.SITE_HTTP_LISTEN_ADDRESS,
35+
"port": settings.SITE_HTTP_LISTEN_PORT,
3536
}
3637
app.run(**run_dict)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import inspect
22

33

4-
VALID_API_VERSIONS = (1, )
4+
VALID_API_VERSIONS = (1,)
55

66

77
def valid_api_version(version):
88
return version in VALID_API_VERSIONS
9-
10-

programming/python/flask-example/frontend/models/v1/req1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
Enji Cooper, October 2013
66
"""
7+
from __future__ import print_function
8+
79

810
def req1(**kwargs):
9-
print('kwargs is', kwargs)
11+
print("kwargs is", kwargs)
1012
return kwargs

programming/python/flask-example/frontend/models/v1/req2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
55
Enji Cooper, October 2013
66
"""
7-
8-

programming/python/flask-example/frontend/settings.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66

77
SITE_DEBUG = 0
88

9+
910
def em_obfuscate(username, domain):
1011
# Screw you spambots
11-
return '@'.join([username, domain])
12+
return "@".join([username, domain])
1213

1314

1415
# Email logging settings.
15-
SITE_ADMINS = [em_obfuscate('yaneurabeya', 'gmail.com')]
16-
SITE_HTTP_LISTEN_ADDRESS = '0.0.0.0'
16+
SITE_ADMINS = [em_obfuscate("yaneurabeya", "gmail.com")]
17+
SITE_HTTP_LISTEN_ADDRESS = "0.0.0.0"
1718
SITE_HTTP_LISTEN_PORT = 80
1819
SITE_HOSTNAME = socket.getfqdn()
1920
SITE_MAIL_LOG_LEVEL = logging.ERROR
20-
SITE_MAIL_HOST = os.getenv('SITE_MAIL_HOST') or 'localhost'
21-
if ':' in SITE_MAIL_HOST:
22-
SITE_MAIL_HOST = SITE_MAIL_HOST.split(':')
21+
SITE_MAIL_HOST = os.getenv("SITE_MAIL_HOST") or "localhost"
22+
if ":" in SITE_MAIL_HOST:
23+
SITE_MAIL_HOST = SITE_MAIL_HOST.split(":")
2324
SITE_MAIL_HOST[-1] = int(SITE_MAIL_HOST[-1])
2425
SITE_MAIL_HOST = tuple(SITE_MAIL_HOST)
25-
SITE_MAIL_USER = os.getenv('SITE_MAIL_USER') or getpass.getuser()
26-
SITE_MAIL_PASSWORD = os.getenv('SITE_MAIL_PASSWORD') or \
27-
(sys.stdin.isatty() and getpass.getpass())
26+
SITE_MAIL_USER = os.getenv("SITE_MAIL_USER") or getpass.getuser()
27+
SITE_MAIL_PASSWORD = os.getenv("SITE_MAIL_PASSWORD") or (
28+
sys.stdin.isatty() and getpass.getpass()
29+
)

programming/python/flask-example/setup.py

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,28 @@
44
#
55
# @author Enji Cooper
66

7-
#import glob
7+
from __future__ import absolute_import
8+
89
import os
9-
#from stat import ST_MODE
10-
from setuptools import setup, find_packages
11-
#from distutils import log
12-
#from distutils.command.install import install
1310
import sys
14-
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'frontend'))
15-
import frontend as frontend
11+
from setuptools import find_packages
12+
from setuptools import setup
13+
14+
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "frontend"))
1615

17-
"""
18-
class Install(install):
19-
def run(self):
20-
install.run(self)
21-
data_files = []
22-
for prefix, paths in self.distribution.data_files:
23-
data_files.extend(map(lambda path: \
24-
os.path.join(self.root + prefix,
25-
os.path.basename(path)),
26-
paths))
27-
for file in self.get_outputs():
28-
if file not in data_files:
29-
continue
30-
if self.dry_run:
31-
log.info("changing mode of %s", file)
32-
else:
33-
mode = ((os.stat(file)[ST_MODE]) | 0555) & 07777
34-
log.info("changing mode of %s to %o", file, mode)
35-
os.chmod(file, mode)
36-
"""
16+
import frontend
3717

3818
setup(
39-
#cmdclass={
40-
# 'install': Install,
41-
#},
42-
#data_files=[
43-
# ('/etc/init.d', glob.glob('init.d/*')),
44-
# ('/usr/local/bin/${package_name}', glob.glob('daemon/*')),
45-
#],
46-
name="Frontend server",
47-
version=frontend.__version__,
48-
description="Frontend server",
49-
author="Enji Cooper",
50-
author_email="dev@null",
51-
maintainer="Enji Cooper",
52-
maintainer_email="dev@null",
53-
packages=find_packages(exclude=['*test*']),
54-
license=frontend.__license__,
55-
platforms="Posix; Mac OS X",
56-
url="http://dev.null/",
57-
install_requires=['flask==1.0.2'],
19+
name="Frontend server",
20+
version=frontend.__version__,
21+
description="Frontend server",
22+
author="Enji Cooper",
23+
author_email="dev@null",
24+
maintainer="Enji Cooper",
25+
maintainer_email="dev@null",
26+
packages=find_packages(exclude=["*test*"]),
27+
license=frontend.__license__,
28+
platforms="Posix; Mac OS X",
29+
url="http://dev.null/",
30+
install_requires=["flask~=2.0"],
5831
)

0 commit comments

Comments
 (0)