From 9c282aa69a4d25f186e591a075b0f2830b5ec45a Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 29 Aug 2019 19:02:31 +0200 Subject: make pretty --- setup.py | 51 +++++++++-------- talerdonations/donations/donations.py | 100 ++++++++++++++++++++-------------- talerdonations/talerconfig.py | 95 ++++++++++++++++++-------------- 3 files changed, 136 insertions(+), 110 deletions(-) diff --git a/setup.py b/setup.py index 71526a9..906567c 100755 --- a/setup.py +++ b/setup.py @@ -1,28 +1,27 @@ from setuptools import setup, find_packages -setup(name='talerdonations', - version='0.6.0pre1', - description='Example donations site for GNU Taler', - url='git://taler.net/donations', - author=['Marcello Stanisci', 'Florian Dold'], - author_email=['stanisci.m@gmail.com', 'dold@taler.net'], - license='GPL', - packages=find_packages(), - install_requires=["Flask>=0.10", - "requests", - "uwsgi", - "jsmin"], - tests_require=["mock", "nose"], - test_suite="nose.collector", - package_data={ - '':[ - "donations/templates/*.html", - "donations/static/*.svg", - "donations/static/*.css", - "donations/static/web-common/*.png", - "donations/static/web-common/*.css", - "donations/static/web-common/*.html", - ] - }, - scripts=['./bin/taler-merchant-donations'], - zip_safe=False) +setup( + name='talerdonations', + version='0.6.0pre1', + description='Example donations site for GNU Taler', + url='git://taler.net/donations', + author=['Marcello Stanisci', 'Florian Dold'], + author_email=['stanisci.m@gmail.com', 'dold@taler.net'], + license='GPL', + packages=find_packages(), + install_requires=["Flask>=0.10", "requests", "uwsgi", "jsmin"], + tests_require=["mock", "nose"], + test_suite="nose.collector", + package_data={ + '': [ + "donations/templates/*.html", + "donations/static/*.svg", + "donations/static/*.css", + "donations/static/web-common/*.png", + "donations/static/web-common/*.css", + "donations/static/web-common/*.html", + ] + }, + scripts=['./bin/taler-merchant-donations'], + zip_safe=False +) diff --git a/talerdonations/donations/donations.py b/talerdonations/donations/donations.py index 5655734..220fbb7 100644 --- a/talerdonations/donations/donations.py +++ b/talerdonations/donations/donations.py @@ -77,9 +77,9 @@ def err_abort(abort_status_code, **params): def backend_get(endpoint, params): headers = {"Authorization": "ApiKey " + APIKEY} try: - resp = requests.get(urljoin(BACKEND_URL, endpoint), - params=params, - headers=headers) + resp = requests.get( + urljoin(BACKEND_URL, endpoint), params=params, headers=headers + ) except requests.ConnectionError: err_abort(500, message="Could not establish connection to backend") try: @@ -87,10 +87,12 @@ def backend_get(endpoint, params): except ValueError: err_abort(500, message="Could not parse response from backend") if resp.status_code != 200: - err_abort(500, - message="Backend returned error status", - json=response_json, - status_code=resp.status_code) + err_abort( + 500, + message="Backend returned error status", + json=response_json, + status_code=resp.status_code + ) return response_json @@ -105,22 +107,26 @@ def backend_get(endpoint, params): def backend_post(endpoint, json): headers = {"Authorization": "ApiKey " + APIKEY} try: - resp = requests.post(urljoin(BACKEND_URL, endpoint), - json=json, - headers=headers) + resp = requests.post( + urljoin(BACKEND_URL, endpoint), json=json, headers=headers + ) except requests.ConnectionError: err_abort(500, message="Could not establish connection to backend") try: response_json = resp.json() except ValueError: - err_abort(500, - message="Could not parse response from backend", - status_code=resp.status_code) + err_abort( + 500, + message="Could not parse response from backend", + status_code=resp.status_code + ) if resp.status_code != 200: - err_abort(500, - message="Backend returned error status", - json=response_json, - status_code=resp.status_code) + err_abort( + 500, + message="Backend returned error status", + json=response_json, + status_code=resp.status_code + ) return response_json @@ -144,9 +150,11 @@ def expect_parameter(name): # (and execution stack!). @app.errorhandler(Exception) def internal_error(e): - return flask.render_template("templates/error.html", - message="Internal error", - stack=traceback.format_exc()) + return flask.render_template( + "templates/error.html", + message="Internal error", + stack=traceback.format_exc() + ) ## @@ -155,8 +163,9 @@ def internal_error(e): # @return response object of the index page. @app.route("/") def index(): - return flask.render_template("templates/index.html", - merchant_currency=CURRENCY) + return flask.render_template( + "templates/index.html", merchant_currency=CURRENCY + ) ## @@ -179,11 +188,13 @@ def checkout(): amount = expect_parameter("donation_amount") donation_receiver = expect_parameter("donation_receiver") donation_donor = expect_parameter("donation_donor") - return flask.render_template("templates/checkout.html", - donation_amount=amount, - donation_receiver=donation_receiver, - donation_donor=donation_donor, - merchant_currency=CURRENCY) + return flask.render_template( + "templates/checkout.html", + donation_amount=amount, + donation_receiver=donation_receiver, + donation_donor=donation_donor, + merchant_currency=CURRENCY + ) ## @@ -211,14 +222,16 @@ def donate(): payment_system = expect_parameter("payment_system") if payment_system != "taler": return flask.redirect(flask.url_for("provider_not_supported")) - fulfillment_url = flask.url_for("fulfillment", - receiver=donation_receiver, - _external=True) + fulfillment_url = flask.url_for( + "fulfillment", receiver=donation_receiver, _external=True + ) order = dict( amount=donation_amount, - extra=dict(donor=donation_donor, - receiver=donation_receiver, - amount=donation_amount), + extra=dict( + donor=donation_donor, + receiver=donation_receiver, + amount=donation_amount + ), fulfillment_url=fulfillment_url, instance=donation_receiver, summary="Donation to {}".format(donation_receiver), @@ -226,9 +239,10 @@ def donate(): order_resp = backend_post("order", dict(order=order)) order_id = order_resp["order_id"] return flask.redirect( - flask.url_for("fulfillment", - receiver=donation_receiver, - order_id=order_id)) + flask.url_for( + "fulfillment", receiver=donation_receiver, order_id=order_id + ) + ) ## @@ -250,12 +264,14 @@ def fulfillment(receiver): if pay_status.get("paid"): extra = pay_status["contract_terms"]["extra"] - return flask.render_template("templates/fulfillment.html", - donation_receiver=extra["receiver"], - donation_amount=extra["amount"], - donation_donor=extra["donor"], - order_id=order_id, - currency=CURRENCY) + return flask.render_template( + "templates/fulfillment.html", + donation_receiver=extra["receiver"], + donation_amount=extra["amount"], + donation_donor=extra["donor"], + order_id=order_id, + currency=CURRENCY + ) # no pay_redirect but article not paid, this should never happen! err_abort(500, message="Internal error, invariant failed", json=pay_status) diff --git a/talerdonations/talerconfig.py b/talerdonations/talerconfig.py index 7959ec2..1a33294 100644 --- a/talerdonations/talerconfig.py +++ b/talerdonations/talerconfig.py @@ -171,11 +171,13 @@ class Entry: LOGGER.warning( "Configuration is missing option '%s' in section '%s',\ falling back to '%s'", self.option, - self.section, default) + self.section, default + ) else: LOGGER.warning( "Configuration ** is missing option '%s' in section '%s'", - self.option.upper(), self.section.upper()) + self.option.upper(), self.section.upper() + ) return default return self.value @@ -373,7 +375,8 @@ class TalerConfig: # a error occurs). def value_string(self, section, option, **kwargs) -> str: return self.sections[section][option].value_string( - kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")) + kwargs.get("default"), kwargs.get("required"), kwargs.get("warn") + ) ## # Get a value from the config that should be a filename. @@ -388,7 +391,8 @@ class TalerConfig: # a error occurs). def value_filename(self, section, option, **kwargs) -> str: return self.sections[section][option].value_filename( - kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")) + kwargs.get("default"), kwargs.get("required"), kwargs.get("warn") + ) ## # Get a integer value from the config. @@ -402,7 +406,8 @@ class TalerConfig: # a error occurs). def value_int(self, section, option, **kwargs) -> int: return self.sections[section][option].value_int( - kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")) + kwargs.get("default"), kwargs.get("required"), kwargs.get("warn") + ) ## # Load default values from canonical locations. @@ -477,48 +482,58 @@ class TalerConfig: pair = line.split() if 2 != len(pair): LOGGER.error( - "invalid inlined config filename given ('%s')" - % line) + "invalid inlined config filename given ('%s')" % + line + ) continue if pair[1].startswith("/"): self.load_file(pair[1]) else: self.load_file( - os.path.join(os.path.dirname(filename), - pair[1])) + os.path.join( + os.path.dirname(filename), pair[1] + ) + ) continue if line.startswith("["): if not line.endswith("]"): LOGGER.error( - "invalid section header in line %s: %s", - lineno, repr(line)) + "invalid section header in line %s: %s", lineno, + repr(line) + ) section_name = line.strip("[]").strip().strip('"') current_section = section_name continue if current_section is None: LOGGER.error( "option outside of section in line %s: %s", lineno, - repr(line)) + repr(line) + ) continue pair = line.split("=", 1) if len(pair) != 2: - LOGGER.error("invalid option in line %s: %s", lineno, - repr(line)) + LOGGER.error( + "invalid option in line %s: %s", lineno, repr(line) + ) key = pair[0].strip() value = pair[1].strip() if value.startswith('"'): value = value[1:] if not value.endswith('"'): - LOGGER.error("mismatched quotes in line %s: %s", - lineno, repr(line)) + LOGGER.error( + "mismatched quotes in line %s: %s", lineno, + repr(line) + ) else: value = value[:-1] - entry = Entry(self.sections, - current_section, - key, - value=value, - filename=filename, - lineno=lineno) + entry = Entry( + self.sections, + current_section, + key, + value=value, + filename=filename, + lineno=lineno + ) sections[current_section][key] = entry except FileNotFoundError: # not logging here, as this interests the final user mostly. @@ -561,26 +576,22 @@ if __name__ == "__main__": import argparse PARSER = argparse.ArgumentParser() - PARSER.add_argument("--section", - "-s", - dest="section", - default=None, - metavar="SECTION") - PARSER.add_argument("--option", - "-o", - dest="option", - default=None, - metavar="OPTION") - PARSER.add_argument("--config", - "-c", - dest="config", - default=None, - metavar="FILE") - PARSER.add_argument("--filename", - "-f", - dest="expand_filename", - default=False, - action='store_true') + PARSER.add_argument( + "--section", "-s", dest="section", default=None, metavar="SECTION" + ) + PARSER.add_argument( + "--option", "-o", dest="option", default=None, metavar="OPTION" + ) + PARSER.add_argument( + "--config", "-c", dest="config", default=None, metavar="FILE" + ) + PARSER.add_argument( + "--filename", + "-f", + dest="expand_filename", + default=False, + action='store_true' + ) ARGS = PARSER.parse_args() TC = TalerConfig.from_file(ARGS.config) -- cgit v1.2.3