Skip to content

Commit

Permalink
Merge pull request #62 from hivesolutions/joamag/black
Browse files Browse the repository at this point in the history
Refactor of code according to black
  • Loading branch information
joamag authored Jan 2, 2024
2 parents 7c2a7dc + e3daca6 commit f24d1d6
Show file tree
Hide file tree
Showing 90 changed files with 8,941 additions and 7,704 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
pip install -r requirements.py2.txt
pip install -r extra.py2.txt
if: matrix.python-version == '2.7'
- run: |
pip install black
black . --check
if: matrix.python-version == '3.12'
- run: ADAPTER=tiny HTTPBIN=httpbin.bemisc.com python setup.py test
- run: pip install twine wheel
- run: python setup.py sdist bdist_wheel
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
pip install -r requirements.py2.txt
pip install -r extra.py2.txt
if: matrix.python-version == '2.7'
- run: |
pip install black
black . --check
if: matrix.python-version == '3.12'
- run: ADAPTER=tiny HTTPBIN=httpbin.bemisc.com python setup.py test
build-pypy:
name: Build PyPy
Expand All @@ -55,4 +59,8 @@ jobs:
pip install -r requirements.py2.txt
pip install -r extra.py2.txt
if: matrix.python-version == '2.7'
- run: |
pip install black
black . --check
if: matrix.python-version == '3.12'
- run: ADAPTER=tiny HTTPBIN=httpbin.bemisc.com pypy setup.py test
2 changes: 1 addition & 1 deletion doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ The following are reserved configuration variables that modify Appier's behavior
| **LOGO_RASTER_URL** | `str` | If provided ensures an alternative to `LOGO_URL` with a raster image (eg: `PNG`, `JPEG`, etc.) to be used in contexts where a vector image is not suitable .(default: `None`). |
| **FAVICON_URL** | `str` | The URL of the preferred `favicon` to be used by the application (default: `None`). |
| **COPYRIGHT** | `str` | Name of the company to which the copyrights of the application should be attributed (default: `Hive Solutions`). |
| **COPYRIGHT_YEAR** | `str` | The year or range of year to be used in the copyright labels (default: `2008-2022`). |
| **COPYRIGHT_YEAR** | `str` | The year or range of year to be used in the copyright labels (default: `2008-2024`). |
| **COPYRIGHT_URL** | `str` | The target URL for the copyright label (default: `http://hive.pt`). |
| **LOCALE** | `str` | The default locale value to be used for language, region, and any special variant preferences. |
| **APPIER_BASE_PATH** | `str` | Override the default base path for the app (calculated as a relative directory to the main app file) (default: `None`). |
Expand Down
49 changes: 18 additions & 31 deletions examples/async/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

# Hive Appier Framework
# Copyright (c) 2008-2022 Hive Solutions Lda.
# Copyright (c) 2008-2024 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
Expand All @@ -22,16 +22,7 @@
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2022 Hive Solutions Lda."
__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
Expand All @@ -42,19 +33,15 @@

import appier

class AsyncApp(appier.App):

class AsyncApp(appier.App):
def __init__(self, *args, **kwargs):
appier.App.__init__(
self,
name = "async",
*args, **kwargs
)
appier.App.__init__(self, name="async", *args, **kwargs)

@appier.route("/async", "GET")
@appier.route("/async/hello", "GET")
def hello(self):
partial = self.field("partial", True, cast = bool)
partial = self.field("partial", True, cast=bool)
handler = self.handler_partial if partial else self.handler
yield from appier.header_a()
yield "before\n"
Expand All @@ -63,32 +50,29 @@ def hello(self):

@appier.route("/async/callable", "GET")
def callable(self):
sleep = self.field("sleep", 3.0, cast = float)
sleep = self.field("sleep", 3.0, cast=float)
yield from appier.header_a()
yield "before\n"
yield from appier.ensure_a(lambda: time.sleep(sleep))
yield "after\n"

@appier.route("/async/file", "GET")
def file(self):
file_path = self.field("path", None, mandatory = True)
delay = self.field("delay", 0.0, cast = float)
thread = self.field("thread", False, cast = bool)
type, _encoding = mimetypes.guess_type(file_path, strict = True)
file_path = self.field("path", None, mandatory=True)
delay = self.field("delay", 0.0, cast=float)
thread = self.field("thread", False, cast=bool)
type, _encoding = mimetypes.guess_type(file_path, strict=True)
type = type or "application/octet-stream"
self.request.content_type = type
yield from appier.header_a()
yield from appier.ensure_a(
self.read_file,
args = [file_path],
kwargs = dict(delay = delay),
thread = thread
self.read_file, args=[file_path], kwargs=dict(delay=delay), thread=thread
)

@appier.route("/async/http", "GET")
def http(self):
url = self.field("url", "https://www.flickr.com/")
delay = self.field("delay", 0.0, cast = float)
delay = self.field("delay", 0.0, cast=float)
self.request.content_type = "text/html"
yield from appier.header_a()
yield from appier.sleep(delay)
Expand Down Expand Up @@ -119,19 +103,22 @@ def calculator(self, *args, **kwargs):
return sum(args)

@appier.coroutine
def read_file(self, file_path, chunk = 65536, delay = 0.0):
def read_file(self, file_path, chunk=65536, delay=0.0):
count = 0
file = open(file_path, "rb")
try:
while True:
data = file.read(chunk)
if not data: break
if not data:
break
count += len(data)
if delay: yield from appier.sleep(delay)
if delay:
yield from appier.sleep(delay)
yield data
finally:
file.close()
return count


app = AsyncApp()
app.serve()
61 changes: 20 additions & 41 deletions examples/async/async_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

# Hive Appier Framework
# Copyright (c) 2008-2022 Hive Solutions Lda.
# Copyright (c) 2008-2024 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
Expand All @@ -22,16 +22,7 @@
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2022 Hive Solutions Lda."
__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
Expand All @@ -44,77 +35,65 @@

import aiohttp

class Person(appier.Model):

identifier = appier.field(
type = int,
index = True,
increment = True,
default = True
)
class Person(appier.Model):
identifier = appier.field(type=int, index=True, increment=True, default=True)

name = appier.field()

class AsyncHTTPApp(appier.App):

class AsyncHTTPApp(appier.App):
def __init__(self, *args, **kwargs):
appier.App.__init__(
self,
name = "async_neo",
*args, **kwargs
)
appier.App.__init__(self, name="async_neo", *args, **kwargs)
self._register_model(Person)

@appier.route("/async", "GET")
@appier.route("/async/request", "GET")
async def request_(self):
url = self.field("url", "https://httpbin.bemisc.com/ip")
size = self.field("size", 4096, cast = int)
size = self.field("size", 4096, cast=int)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
while True:
data = await response.content.read(size)
if not data: break
await self.send(data, content_type = response.content_type)
if not data:
break
await self.send(data, content_type=response.content_type)

@appier.route("/async/sleep", "GET")
async def sleep_(self):
sleep = self.field("sleep", 5.0, cast = float)
sleep = self.field("sleep", 5.0, cast=float)
await asyncio.sleep(sleep)
return json.dumps(dict(sleep = sleep))
return json.dumps(dict(sleep=sleep))

@appier.route("/async/list", "GET")
async def list_(self):
name = self.field("name", "John Doe")
persons = await Person.find_a(name = name, map = True)
persons = await Person.find_a(name=name, map=True)
return json.dumps(persons)

@appier.route("/async/create", ("GET", "POST"))
async def create_(self):
name = self.field("name", "John Doe")
person = Person(name = name)
person = Person(name=name)
await person.save_a()
person = await person.reload_a(map = True)
person = await person.reload_a(map=True)
return json.dumps(person)

@appier.route("/async/read", "GET")
async def read_(self):
name = self.field("name", "John Doe")
person = await Person.get_a(name = name, map = True)
person = await Person.get_a(name=name, map=True)
return json.dumps(person)

@appier.route("/async/delete", "GET")
async def delete_(self):
name = self.field("name", "John Doe")
persons = await Person.find_a(name = name)
persons = await Person.find_a(name=name)
await asyncio.gather(*[person.delete_a() for person in persons])
persons_a = await Person.find_a(name = name)
return json.dumps(
dict(
before = len(persons),
after = len(persons_a)
)
)
persons_a = await Person.find_a(name=name)
return json.dumps(dict(before=len(persons), after=len(persons_a)))


app = AsyncHTTPApp()
app.serve()
Loading

0 comments on commit f24d1d6

Please sign in to comment.