This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from plone/commands
create API for making command line utilities
- Loading branch information
Showing
11 changed files
with
201 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# COMMAND | ||
|
||
You can provide your own CLI commands for plone.server through a simple interface. | ||
|
||
|
||
## CREATING THE COMMAND | ||
|
||
plone.server provides a simple API to write your own CLI commands. | ||
|
||
|
||
Here is a minimalistic example: | ||
|
||
```python | ||
|
||
from plone.server.commands import Command | ||
class MyCommand(Command): | ||
|
||
def get_parser(self): | ||
parser = super(MyCommand, self).get_parser() | ||
# add command arguments here... | ||
return parser | ||
|
||
def run(self, arguments, settings, app): | ||
pass | ||
|
||
``` | ||
|
||
Then, in your setup.py file, include an entry point like this for your command: | ||
|
||
```python | ||
setup( | ||
entry_points={ | ||
'console_scripts': [ | ||
'mycommand = my.package.commands:MyCommand' | ||
] | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone.server.factory import make_app | ||
|
||
import argparse | ||
import json | ||
import logging | ||
|
||
|
||
logger = logging.getLogger('plone.server') | ||
|
||
|
||
class Command(object): | ||
|
||
def __init__(self): | ||
parser = self.get_parser() | ||
arguments = parser.parse_args() | ||
app = make_app(config_file=arguments.configuration) | ||
|
||
with open(arguments.configuration, 'r') as config: | ||
settings = json.load(config) | ||
|
||
if arguments.debug: | ||
logging.basicConfig( | ||
level=logging.DEBUG) | ||
|
||
self.run(arguments, settings, app) | ||
|
||
def get_parser(self): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-c', '--configuration', | ||
default='config.json', help='Configuration file') | ||
parser.add_argument('--debug', dest='debug', action='store_true', | ||
help='Log verbose') | ||
parser.set_defaults(debug=False) | ||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from plone.server import app_settings | ||
from plone.server.commands import Command | ||
from plone.server.testing import PloneRequester | ||
from plone.server.testing import TESTING_SETTINGS | ||
from pprint import pformat | ||
|
||
import asyncio | ||
import logging | ||
import threading | ||
import time | ||
|
||
|
||
logger = logging.getLogger('plone.server') | ||
|
||
|
||
def format_headers(headers): | ||
return '\n'.join(['\t{}: {}'.format(n, v) | ||
for n, v in headers.items()]) | ||
|
||
|
||
class CliCommand(Command): | ||
|
||
def get_parser(self): | ||
parser = super(CliCommand, self).get_parser() | ||
parser.add_argument('-m', '--method', nargs='?', | ||
default='get', help='HTTP method') | ||
parser.add_argument('-p', '--path', nargs='?', | ||
default='/', help='Path to endpoint') | ||
parser.add_argument('-b', '--body', default=u'', | ||
help='Request body') | ||
return parser | ||
|
||
def run(self, arguments, settings, app): | ||
app_settings['root_user']['password'] = TESTING_SETTINGS['root_user']['password'] | ||
|
||
loop = app.loop | ||
handler = app.make_handler(keep_alive_on=False) | ||
loop.run_until_complete(loop.create_server( | ||
handler, | ||
'127.0.0.1', | ||
5777)) | ||
|
||
def loop_in_thread(loop): | ||
asyncio.set_event_loop(loop) | ||
loop.run_forever() | ||
|
||
t = threading.Thread(target=loop_in_thread, args=(loop,)) | ||
t.start() | ||
|
||
req_body = arguments.body or '' | ||
|
||
requester = PloneRequester('http://localhost:5777') | ||
resp = requester( | ||
arguments.method, arguments.path, | ||
data=req_body or None) | ||
|
||
print(''' | ||
Path: {path} | ||
Method: {method} | ||
Status code: {code} | ||
Request Headers: | ||
{request_headers} | ||
Response Headers: | ||
{response_headers} | ||
Request body: | ||
{request_body} | ||
Response body: | ||
{body} | ||
'''.format( | ||
path=arguments.path, | ||
method=arguments.method, | ||
code=resp.status_code, | ||
request_headers=format_headers(resp.request.headers), | ||
response_headers=format_headers(resp.headers), | ||
body=pformat(resp.json()), | ||
request_body=req_body | ||
)) | ||
|
||
loop.call_soon_threadsafe(loop.stop) | ||
while(loop.is_running()): | ||
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from aiohttp import web | ||
from plone.server.commands import Command | ||
|
||
|
||
class ServerCommand(Command): | ||
|
||
def run(self, arguments, settings, app): | ||
web.run_app(app, port=settings['address']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from code import interact | ||
from plone.server import app_settings | ||
from plone.server.commands import Command | ||
from plone.server.interfaces import IApplication | ||
from plone.server.testing import TESTING_SETTINGS | ||
from zope.component import getUtility | ||
|
||
|
||
class ShellCommand(Command): | ||
|
||
def run(self, arguments, settings, app): | ||
app_settings['root_user']['password'] = TESTING_SETTINGS['root_user']['password'] | ||
root = getUtility(IApplication, name='root') | ||
interact(''' | ||
plone.server interactive shell | ||
============================== | ||
Available local variables: | ||
- app | ||
- root | ||
- app_settings | ||
''', local={ | ||
'app': app, | ||
'root': root, | ||
'app_settings': app_settings | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.