Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make more ways to configure [proutes] section
  • Loading branch information
sontek committed Jan 22, 2015
1 parent 8724ab8 commit b8ba0f1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
11 changes: 11 additions & 0 deletions docs/narr/commandline.rst
Expand Up @@ -350,6 +350,17 @@ For example you may remove request method and place the view first:
name
pattern
You can also separate the formats with commas or spaces:

.. code-block:: text
:linenos:
[proutes]
format = view name pattern
[proutes]
format = view, name, pattern
If you want to temporarily configure the columns and order there is the
``--format`` which is a comma separated list of columns you want to include. The
current available formats are ``name``, ``pattern``, ``view``, and ``method``.
Expand Down
5 changes: 4 additions & 1 deletion pyramid/scripts/proutes.py
Expand Up @@ -2,6 +2,7 @@
import optparse
import sys
import textwrap
import re

from pyramid.paster import bootstrap
from pyramid.compat import (string_types, configparser)
Expand Down Expand Up @@ -291,7 +292,8 @@ def proutes_file_config(self, filename):
items = config.items('proutes')
for k, v in items:
if 'format' == k:
self.column_format = [x.strip() for x in v.split('\n')]
cols = re.split(r'[,|\s|\n]*', v)
self.column_format = [x.strip() for x in cols]

except configparser.NoSectionError:
return
Expand All @@ -314,6 +316,7 @@ def run(self, quiet=False):
env = self.bootstrap[0](config_uri, options=parse_vars(self.args[1:]))
registry = env['registry']
mapper = self._get_mapper(registry)

self.proutes_file_config(config_uri)

if self.options.format:
Expand Down
73 changes: 69 additions & 4 deletions pyramid/tests/test_scripts/test_proutes.py
Expand Up @@ -632,7 +632,7 @@ def view1(context, request): return 'view1'
self.assertEqual(result, 2)
self.assertEqual(L[0], expected)

def test_config_format_ini(self):
def test_config_format_ini_newlines(self):
from pyramid.renderers import null_renderer as nr
from pyramid.config import not_

Expand All @@ -648,14 +648,79 @@ def view1(context, request): return 'view1'
)

command = self._makeOne()
command.options.glob = '*foo*'
command.options.format = 'method,name'

L = []
command.out = L.append
command.bootstrap = (dummy.DummyBootstrap(registry=config.registry),)
config_factory = dummy.DummyConfigParserFactory()
command.ConfigParser = config_factory
config_factory.items = [('format', 'method\nname')]

result = command.run()
self.assertEqual(result, 0)
self.assertEqual(len(L), 3)
compare_to = L[-1].split()
expected = ['!POST,*', 'foo']

self.assertEqual(compare_to, expected)
self.assertEqual(L[0].split(), ['Method', 'Name'])

def test_config_format_ini_spaces(self):
from pyramid.renderers import null_renderer as nr
from pyramid.config import not_

def view1(context, request): return 'view1'

config = self._makeConfig(autocommit=True)
config.add_route('foo', '/a/b')
config.add_view(
route_name='foo',
view=view1,
renderer=nr,
request_method=not_('POST')
)

command = self._makeOne()

L = []
command.out = L.append
command.bootstrap = (dummy.DummyBootstrap(registry=config.registry),)
config_factory = dummy.DummyConfigParserFactory()
command.ConfigParser = config_factory
config_factory.items = [('format', 'method name')]

result = command.run()
self.assertEqual(result, 0)
self.assertEqual(len(L), 3)
compare_to = L[-1].split()
expected = ['!POST,*', 'foo']

self.assertEqual(compare_to, expected)
self.assertEqual(L[0].split(), ['Method', 'Name'])

def test_config_format_ini_commas(self):
from pyramid.renderers import null_renderer as nr
from pyramid.config import not_

def view1(context, request): return 'view1'

config = self._makeConfig(autocommit=True)
config.add_route('foo', '/a/b')
config.add_view(
route_name='foo',
view=view1,
renderer=nr,
request_method=not_('POST')
)

command = self._makeOne()

L = []
command.out = L.append
command.bootstrap = (dummy.DummyBootstrap(registry=config.registry),)
config_factory = dummy.DummyConfigParserFactory()
command.ConfigParser = config_factory
config_factory.items = [('format', 'method\name')]
config_factory.items = [('format', 'method,name')]

result = command.run()
self.assertEqual(result, 0)
Expand Down

0 comments on commit b8ba0f1

Please sign in to comment.