Skip to content
This repository has been archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #45 from plone/commands
Browse files Browse the repository at this point in the history
create API for making command line utilities
  • Loading branch information
bloodbare committed Dec 27, 2016
2 parents a4d343f + aad9c99 commit 90db886
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 173 deletions.
37 changes: 37 additions & 0 deletions docs/source/commands.md
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'
]
})
```
1 change: 1 addition & 0 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Contents:
[Addons](addons.md)
[Configuration](configuration.md)
[Design](design.md)
[Command](commands.md)

Indices and tables
==================
Expand Down
5 changes: 5 additions & 0 deletions src/plone.server/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
1.0a9 (unreleased)
------------------

- Provide base plone.server.commands.Command class to provide your own commands.
Commands have been moved in code so you'll need to re-run buildout to get
pserver to work after this update.
[vangheem]

- Automatically give authenticated users new `plone.Authenticated` role
[vangheem]

Expand Down
89 changes: 0 additions & 89 deletions src/plone.server/plone/server/cli.py

This file was deleted.

35 changes: 35 additions & 0 deletions src/plone.server/plone/server/commands/__init__.py
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
84 changes: 84 additions & 0 deletions src/plone.server/plone/server/commands/cli.py
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)
8 changes: 8 additions & 0 deletions src/plone.server/plone/server/commands/server.py
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'])
28 changes: 28 additions & 0 deletions src/plone.server/plone/server/commands/shell.py
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
})
43 changes: 0 additions & 43 deletions src/plone.server/plone/server/pshell.py

This file was deleted.

38 changes: 0 additions & 38 deletions src/plone.server/plone/server/server.py

This file was deleted.

0 comments on commit 90db886

Please sign in to comment.