Skip to content

Commit

Permalink
Merge branch 'modular'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucretiel committed Feb 17, 2016
2 parents 8e30378 + ee253b6 commit ed0af3e
Show file tree
Hide file tree
Showing 26 changed files with 1,176 additions and 550 deletions.
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ def getfile(filename):

setup(
name='autocommand',
version='2.0.1',
py_modules=['autocommand'],
version='2.1.0',
packages=[
'autocommand'
],
package_dir={'': 'src'},
platforms='any',
license='LGPLv3',
Expand Down
25 changes: 25 additions & 0 deletions src/autocommand/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2014-2016 Nathan West
#
# This file is part of autocommand.
#
# autocommand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autocommand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autocommand. If not, see <http://www.gnu.org/licenses/>.

from .automain import automain
from .autoparse import autoparse, smart_open
from .autocommand import autocommand

try:
from .autoasync import autoasync
except ImportError: # pragma: no cover
pass
98 changes: 98 additions & 0 deletions src/autocommand/autoasync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2014-2015 Nathan West
#
# This file is part of autocommand.
#
# autocommand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autocommand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autocommand. If not, see <http://www.gnu.org/licenses/>.

from asyncio import get_event_loop
from functools import wraps
from inspect import signature


def autoasync(coro=None, *, loop=None, forever=False, pass_loop=False):
'''
Convert an asyncio coroutine into a function which, when called, is
evaluted in an event loop, and the return value returned. This is intented
to make it easy to write entry points into asyncio coroutines, which
otherwise need to be explictly evaluted with an event loop's
run_until_complete.
If `loop` is given, it is used as the event loop to run the coro in. If it
is None (the default), the loop is retreived using asyncio.get_event_loop.
This call is defered until the decorated function is called, so that
callers can install custom event loops or event loop policies after
@autoasync is applied.
If `forever` is True, the loop is run forever after the decorated coroutine
is finished. Use this for servers created with asyncio.start_server and the
like.
If `pass_loop` is True, the event loop object is passed into the coroutine
as the `loop` kwarg when the wrapper function is called. In this case, the
wrapper function's __signature__ is updated to remove this parameter, so
that autoparse can still be used on it without generating a parameter for
`loop`.
This coroutine can be called with ( @autoasync(...) ) or without
( @autoasync ) arguments.
Examples:
@autoasync
def get_file(host, port):
reader, writer = yield from asyncio.open_connection(host, port)
data = reader.read()
sys.stdout.write(data.decode())
get_file(host, port)
@autoasync(forever=True, pass_loop=True)
def server(host, port, loop):
yield_from loop.create_server(Proto, host, port)
server('localhost', 8899)
'''
if coro is None:
return lambda c: autoasync(
c, loop=loop,
forever=forever,
pass_loop=pass_loop)

@wraps(coro)
def autoasync_wrapper(*args, **kwargs):
# Defer the call to get_event_loop so that, if a custom policy is
# installed after the autoasync decorator, it is respected at call time
local_loop = get_event_loop() if loop is None else loop

if pass_loop:
kwargs['loop'] = local_loop

if forever:
# Explicitly don't create a reference to the created task. This
# ensures that if an exception is raised, it is shown as soon as
# possible, when the created task is garbage collected.
local_loop.create_task(coro(*args, **kwargs))
local_loop.run_forever()
else:
return local_loop.run_until_complete(coro(*args, **kwargs))

# Attach an updated signature, with the "loop" parameter filted out. This
# allows 'pass_loop' to be used with autoparse
if pass_loop:
sig = signature(coro)
autoasync_wrapper.__signature__ = sig.replace(parameters=(
param for name, param in sig.parameters.items() if name != "loop"))

return autoasync_wrapper
70 changes: 70 additions & 0 deletions src/autocommand/autocommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2014-2015 Nathan West
#
# This file is part of autocommand.
#
# autocommand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autocommand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autocommand. If not, see <http://www.gnu.org/licenses/>.

from .autoparse import autoparse
from .automain import automain
try:
from .autoasync import autoasync
except ImportError: # pragma: no cover
pass


def autocommand(
module, *,
description=None,
epilog=None,
add_nos=False,
parser=None,
loop=None,
forever=False,
pass_loop=False):

if callable(module):
raise TypeError('autocommand requires a module name argument')

def autocommand_decorator(func):
# Step 1: if requested, run it all in an asyncio event loop. autoasync
# patches the __signature__ of the decorated function, so that in the
# event that pass_loop is True, the `loop` parameter of the original
# function will *not* be interpreted as a command-line argument by
# autoparse
if loop is not None or forever or pass_loop:
func = autoasync(
func,
loop=None if loop is True else loop,
pass_loop=pass_loop,
forever=forever)

# Step 2: create parser. We do this second so that the arguments are
# parsed and passed *before* entering the asyncio event loop, if it
# exists. This simplifies the stack trace and ensures errors are
# reported earlier. It also ensures that errors raised during parsing &
# passing are still raised if `forever` is True.
func = autoparse(
func,
description=description,
epilog=epilog,
add_nos=add_nos,
parser=parser)

# Step 3: call the function automatically if __name__ == '__main__' (or
# if True was provided)
func = automain(module)(func)

return func

return autocommand_decorator
59 changes: 59 additions & 0 deletions src/autocommand/automain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2014-2015 Nathan West
#
# This file is part of autocommand.
#
# autocommand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autocommand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autocommand. If not, see <http://www.gnu.org/licenses/>.

import sys
from .errors import AutocommandError


class AutomainRequiresModuleError(AutocommandError, TypeError):
pass


def automain(module, *, args=(), kwargs=None):
'''
This decorator automatically invokes a function if the module is being run
as the "__main__" module. Optionally, provide args or kwargs with which to
call the function. If `module` is "__main__", the function is called, and
the program is `sys.exit`ed with the return value. You can also pass `True`
to cause the function to be called unconditionally. If the function is not
called, it is returned unchanged by the decorator.
Usage:
@automain(__name__) # Pass __name__ to check __name__=="__main__"
def main():
...
If __name__ is "__main__" here, the main function is called, and then
sys.exit called with the return value.
'''

# Check that @automain(...) was called, rather than @automain
if callable(module):
raise AutomainRequiresModuleError(module)

if module == '__main__' or module is True:
if kwargs is None:
kwargs = {}

# Use a function definition instead of a lambda for a neater traceback
def automain_decorator(main):
sys.exit(main(*args, **kwargs))

return automain_decorator
else:
return lambda main: main

0 comments on commit ed0af3e

Please sign in to comment.