Skip to content

adds support for other paramstyles, disables logging by default when Flask not running #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 20 additions & 31 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
language: python
python:
- '2.7'
- '3.6'
python: '3.6'
branches:
except: "/^v\\d/"
services:
- mysql
- postgresql
- mysql
- postgresql
install:
- python setup.py install
- pip install mysqlclient
- pip install psycopg2-binary
- python setup.py install
- pip install mysqlclient
- pip install psycopg2-binary
before_script:
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
- psql -c 'create database test;' -U postgres
- touch test.db test1.db
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
- psql -c 'create database test;' -U postgres
- touch test.db test1.db
script: python tests/sql.py
after_script: rm -f test.db
jobs:
include:
- stage: deploy
python: '3.6'
install: skip
before_script: skip
script: skip
deploy:
- provider: script
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",
\"target_commitish\": \"$TRAVIS_COMMIT\", \"name\": \"v$(python setup.py --version)\"
}" --user bot50:$GITHUB_TOKEN https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases'
on:
branch: master
- provider: pypi
user: "$PYPI_USERNAME"
password: "$PYPI_PASSWORD"
on:
branch: master
deploy:
- provider: script
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",
\"target_commitish\": \"$TRAVIS_COMMIT\", \"name\": \"v$(python setup.py --version)\"
}" --user bot50:$GITHUB_TOKEN https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases'
on:
branch: master
- provider: pypi
user: "$PYPI_USERNAME"
password: "$PYPI_PASSWORD"
on: master
notifications:
slack:
secure: lJklhcBVjDT6KzUNa3RFHXdXSeH7ytuuGrkZ5ZcR72CXMoTf2pMJTzPwRLWOp6lCSdDC9Y8MWLrcg/e33dJga4Jlp9alOmWqeqesaFjfee4st8vAsgNbv8/RajPH1gD2bnkt8oIwUzdHItdb5AucKFYjbH2g0d8ndoqYqUeBLrnsT1AP5G/Vi9OHC9OWNpR0FKaZIJE0Wt52vkPMH3sV2mFeIskByPB+56U5y547mualKxn61IVR/dhYBEtZQJuSvnwKHPOn9Pkk7cCa+SSSeTJ4w5LboY8T17otaYNauXo46i1bKIoGiBcCcrJyQHHiPQmcq/YU540MC5Wzt9YXUycmJzRi347oyQeDee27wV3XJlWMXuuhbtJiKCFny7BTQ160VATlj/dbwIzN99Ra6/BtTumv/6LyTdKIuVjdAkcN8dtdDW1nlrQ29zuPNCcXXzJ7zX7kQaOCUV1c2OrsbiH/0fE9nknUORn97txqhlYVi0QMS7764wFo6kg0vpmFQRkkQySsJl+TmgcZ01AlsJc2EMMWVuaj9Af9JU4/4yalqDiXIh1fOYYUZnLfOfWS+MsnI+/oLfqJFyMbrsQQTIjs+kTzbiEdhd2R4EZgusU/xRFWokS2NAvahexrRhRQ6tpAI+LezPrkNOR3aHiykBf+P9BkUa0wPp6V2Ayc6q0=
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="3.1.0"
version="3.2.0"
)
4 changes: 2 additions & 2 deletions src/cs50/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:

# Save student's sys.path
path = sys.path[:]
_path = sys.path[:]

# In case student has files that shadow packages
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
Expand All @@ -25,4 +25,4 @@
finally:

# Restore student's sys.path (just in case library raised an exception that caller caught)
sys.path = path
sys.path = _path
38 changes: 30 additions & 8 deletions src/cs50/flask.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from distutils.version import StrictVersion
from os import getenv
from pkg_resources import get_distribution
Expand All @@ -8,31 +10,51 @@
try:

# Only patch >= 1.0
version = StrictVersion(get_distribution("flask").version)
assert version >= StrictVersion("1.0")
_version = StrictVersion(get_distribution("flask").version)
assert _version >= StrictVersion("1.0")

# Reformat logger's exceptions
# http://flask.pocoo.org/docs/1.0/logging/
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
try:
import flask.logging
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
except:
except Exception:
pass

# Enable logging when Flask is in use,
# monkey-patching own SQL module, which shouldn't need to know about Flask
logging.getLogger("cs50").disabled = True
try:
import flask
from .sql import SQL
except ImportError:
pass
else:
_before = SQL.execute
def _after(*args, **kwargs):
disabled = logging.getLogger("cs50").disabled
if flask.current_app:
logging.getLogger("cs50").disabled = False
try:
return _before(*args, **kwargs)
finally:
logging.getLogger("cs50").disabled = disabled
SQL.execute = _after

# Add support for Cloud9 proxy so that flask.redirect doesn't redirect from HTTPS to HTTP
# http://stackoverflow.com/a/23504684/5156190
if getenv("C9_HOSTNAME") and not getenv("IDE_OFFLINE"):
try:
import flask
from werkzeug.contrib.fixers import ProxyFix
before = flask.Flask.__init__
def after(self, *args, **kwargs):
before(self, *args, **kwargs)
_before = flask.Flask.__init__
def _after(*args, **kwargs):
_before(*args, **kwargs)
self.wsgi_app = ProxyFix(self.wsgi_app)
flask.Flask.__init__ = after
flask.Flask.__init__ = _after
except:
pass

except:
except Exception:
pass
Loading