Skip to content

Commit

Permalink
Uplift code to python3
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ngie-eign committed May 10, 2023
1 parent c9e395e commit 06155b1
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 86 deletions.
2 changes: 1 addition & 1 deletion programming/python/flask-example/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
__date__ = "$Date$"
__license__ = ""
__version_info__ = (0, 0, 1)
__version__ = '.'.join(map(str, __version_info__))
__version__ = ".".join(str(vi) for vi in __version_info__)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

app = Flask(__name__)


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

model_callee = form_to_model(api_version, stack=2)

return jsonify(model_callee(**dict(request.args)))
Expand Down
3 changes: 2 additions & 1 deletion programming/python/flask-example/frontend/forms/req1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from frontend.forms import app, get_json_from_api

@app.route('/<int:api_version>/req1')

@app.route("/<int:api_version>/req1")
def req1(api_version):
# 1. Grab the request.
# 2. Punt it over to the model logic
Expand Down
17 changes: 8 additions & 9 deletions programming/python/flask-example/frontend/forms/req2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@

from frontend.forms import app

@app.route('/<int:api_version>/req2')

@app.route("/<int:api_version>/req2")
def req2(api_version):
d = {
'time': time.clock(),
'version': api_version,
"time": time.monotonic(),
"version": api_version,
}
if request.args.get('action', None) == 'gets_me_a_string':
d['foo'] = 'foo'
d['a'] = list('abcd')
if request.args.get("action", None) == "gets_me_a_string":
d["foo"] = "foo"
d["a"] = list("abcd")
else:
d['error'] = 'INVALID REQUEST!'
d["error"] = "INVALID REQUEST!"
return jsonify(d)


27 changes: 14 additions & 13 deletions programming/python/flask-example/frontend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@
from frontend.forms import app


if __name__ == '__main__':
if __name__ == "__main__":
app.debug = settings.SITE_DEBUG


# Log via email if exceptions occur; based on:
# http://flask.pocoo.org/docs/errorhandling/
if not app.debug:
from logging.handlers import SMTPHandler
subject_line = 'Exception encountered on %s' % (settings.SITE_HOSTNAME)
mail_handler = SMTPHandler(settings.SITE_MAIL_HOST,
settings.SITE_ADMINS[0],
settings.SITE_ADMINS,
subject_line,
credentials=(settings.SITE_MAIL_USER,
settings.SITE_MAIL_PASSWORD),
secure=(),
)

subject_line = "Exception encountered on %s" % (settings.SITE_HOSTNAME)
mail_handler = SMTPHandler(
settings.SITE_MAIL_HOST,
settings.SITE_ADMINS[0],
settings.SITE_ADMINS,
subject_line,
credentials=(settings.SITE_MAIL_USER, settings.SITE_MAIL_PASSWORD),
secure=(),
)
mail_handler.setLevel(settings.SITE_MAIL_LOG_LEVEL)
app.logger.addHandler(mail_handler)


if __name__ == '__main__':
if __name__ == "__main__":
run_dict = {
'host': settings.SITE_HTTP_LISTEN_ADDRESS,
'port': settings.SITE_HTTP_LISTEN_PORT,
"host": settings.SITE_HTTP_LISTEN_ADDRESS,
"port": settings.SITE_HTTP_LISTEN_PORT,
}
app.run(**run_dict)
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import inspect


VALID_API_VERSIONS = (1, )
VALID_API_VERSIONS = (1,)


def valid_api_version(version):
return version in VALID_API_VERSIONS


4 changes: 3 additions & 1 deletion programming/python/flask-example/frontend/models/v1/req1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
Enji Cooper, October 2013
"""
from __future__ import print_function


def req1(**kwargs):
print('kwargs is', kwargs)
print("kwargs is", kwargs)
return kwargs
2 changes: 0 additions & 2 deletions programming/python/flask-example/frontend/models/v1/req2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
Enji Cooper, October 2013
"""


20 changes: 11 additions & 9 deletions programming/python/flask-example/frontend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

SITE_DEBUG = 0


def em_obfuscate(username, domain):
# Screw you spambots
return '@'.join([username, domain])
return "@".join([username, domain])


# Email logging settings.
SITE_ADMINS = [em_obfuscate('yaneurabeya', 'gmail.com')]
SITE_HTTP_LISTEN_ADDRESS = '0.0.0.0'
SITE_ADMINS = [em_obfuscate("yaneurabeya", "gmail.com")]
SITE_HTTP_LISTEN_ADDRESS = "0.0.0.0"
SITE_HTTP_LISTEN_PORT = 80
SITE_HOSTNAME = socket.getfqdn()
SITE_MAIL_LOG_LEVEL = logging.ERROR
SITE_MAIL_HOST = os.getenv('SITE_MAIL_HOST') or 'localhost'
if ':' in SITE_MAIL_HOST:
SITE_MAIL_HOST = SITE_MAIL_HOST.split(':')
SITE_MAIL_HOST = os.getenv("SITE_MAIL_HOST") or "localhost"
if ":" in SITE_MAIL_HOST:
SITE_MAIL_HOST = SITE_MAIL_HOST.split(":")
SITE_MAIL_HOST[-1] = int(SITE_MAIL_HOST[-1])
SITE_MAIL_HOST = tuple(SITE_MAIL_HOST)
SITE_MAIL_USER = os.getenv('SITE_MAIL_USER') or getpass.getuser()
SITE_MAIL_PASSWORD = os.getenv('SITE_MAIL_PASSWORD') or \
(sys.stdin.isatty() and getpass.getpass())
SITE_MAIL_USER = os.getenv("SITE_MAIL_USER") or getpass.getuser()
SITE_MAIL_PASSWORD = os.getenv("SITE_MAIL_PASSWORD") or (
sys.stdin.isatty() and getpass.getpass()
)
65 changes: 19 additions & 46 deletions programming/python/flask-example/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,28 @@
#
# @author Enji Cooper

#import glob
from __future__ import absolute_import

import os
#from stat import ST_MODE
from setuptools import setup, find_packages
#from distutils import log
#from distutils.command.install import install
import sys
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'frontend'))
import frontend as frontend
from setuptools import find_packages
from setuptools import setup

sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), "frontend"))

"""
class Install(install):
def run(self):
install.run(self)
data_files = []
for prefix, paths in self.distribution.data_files:
data_files.extend(map(lambda path: \
os.path.join(self.root + prefix,
os.path.basename(path)),
paths))
for file in self.get_outputs():
if file not in data_files:
continue
if self.dry_run:
log.info("changing mode of %s", file)
else:
mode = ((os.stat(file)[ST_MODE]) | 0555) & 07777
log.info("changing mode of %s to %o", file, mode)
os.chmod(file, mode)
"""
import frontend

setup(
#cmdclass={
# 'install': Install,
#},
#data_files=[
# ('/etc/init.d', glob.glob('init.d/*')),
# ('/usr/local/bin/${package_name}', glob.glob('daemon/*')),
#],
name="Frontend server",
version=frontend.__version__,
description="Frontend server",
author="Enji Cooper",
author_email="dev@null",
maintainer="Enji Cooper",
maintainer_email="dev@null",
packages=find_packages(exclude=['*test*']),
license=frontend.__license__,
platforms="Posix; Mac OS X",
url="http://dev.null/",
install_requires=['flask==1.0.2'],
name="Frontend server",
version=frontend.__version__,
description="Frontend server",
author="Enji Cooper",
author_email="dev@null",
maintainer="Enji Cooper",
maintainer_email="dev@null",
packages=find_packages(exclude=["*test*"]),
license=frontend.__license__,
platforms="Posix; Mac OS X",
url="http://dev.null/",
install_requires=["flask~=2.0"],
)

0 comments on commit 06155b1

Please sign in to comment.