Skip to content

Commit

Permalink
Merge pull request #417 from Cornices/fix-coverage
Browse files Browse the repository at this point in the history
Fix coverage
  • Loading branch information
leplatrem committed Oct 28, 2016
2 parents c12b0dc + d7c10e1 commit 8b2a8d7
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 192 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[report]
show_missing = True
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps
*.swp
html
docs/build
\.coverage
.coverage
*~
._build.etag
_build.py.bak*
Expand Down
2 changes: 1 addition & 1 deletion cornice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def setup_localization(config):
available_languages=available_languages,
default_locale_name=default_locale_name)
config.add_subscriber(set_localizer, NewRequest)
except ImportError:
except ImportError: # pragma: no cover
# add_translation_dirs raises an ImportError if colander is not
# installed
pass
Expand Down
5 changes: 1 addition & 4 deletions cornice/errors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
try:
import simplejson as json
except ImportError:
import json
import simplejson as json


class Errors(list):
Expand Down
13 changes: 4 additions & 9 deletions cornice/pyramidhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,10 @@ def _mungle_view_args(args, predicate_list):
# we need to build a custom predicate if argument value is a callable
predicates = args.get('custom_predicates', [])
if callable(value):
func = callable_map.get(kind)
if func:
predicate_checker = functools.partial(func, value)
predicates.append(predicate_checker)
args['custom_predicates'] = predicates
else:
raise ValueError(
'No function defined for ' +
'handling callables for field "{0}"'.format(kind))
func = callable_map[kind]
predicate_checker = functools.partial(func, value)
predicates.append(predicate_checker)
args['custom_predicates'] = predicates
else:
# otherwise argument value is just a scalar
args[kind] = value
Expand Down
34 changes: 14 additions & 20 deletions cornice/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import warnings

import venusian

from cornice import Service
try:
import venusian
VENUSIAN = True
except ImportError:
VENUSIAN = False


def resource(depth=2, **kw):
Expand Down Expand Up @@ -84,9 +82,6 @@ class User(object):
elif k not in service_args:
service_args[k] = kw[k]

if prefix == 'collection_' and service_args.get('collection_acl'):
service_args['acl'] = service_args['collection_acl']

# create service
service_name = (service_args.pop('name', None) or
klass.__name__.lower())
Expand Down Expand Up @@ -114,17 +109,16 @@ class User(object):

setattr(klass, '_services', services)

if VENUSIAN:
def callback(context, name, ob):
# get the callbacks registred by the inner services
# and call them from here when the @resource classes are being
# scanned by venusian.
for service in services.values():
config = context.config.with_package(info.module)
config.add_cornice_service(service)

info = venusian.attach(klass, callback, category='pyramid',
depth=depth)
def callback(context, name, ob):
# get the callbacks registred by the inner services
# and call them from here when the @resource classes are being
# scanned by venusian.
for service in services.values():
config = context.config.with_package(info.module)
config.add_cornice_service(service)

info = venusian.attach(klass, callback, category='pyramid', depth=depth)

return klass


Expand Down Expand Up @@ -169,7 +163,7 @@ def get(self):
add_resource(User, collection_path='/users', path='/users/{id}')
"""
# XXX needed in py2 to set on instancemethod
if hasattr(func, '__func__'):
if hasattr(func, '__func__'): # pragma: no cover
func = func.__func__
# store view argument to use them later in @resource
views = getattr(func, '__views__', None)
Expand Down
24 changes: 10 additions & 14 deletions cornice/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import functools
from pyramid.exceptions import ConfigurationError
from pyramid.response import Response
from cornice.validators import (
DEFAULT_VALIDATORS,
DEFAULT_FILTERS,
)
import venusian

from cornice.util import is_string, to_list, json_error, func_name

try:
import venusian
VENUSIAN = True
except ImportError:
VENUSIAN = False

SERVICES = []

Expand Down Expand Up @@ -190,7 +188,7 @@ def __init__(self, name, path, description=None, cors_policy=None, depth=1,
setattr(self, key, value)

if hasattr(self, 'factory') and hasattr(self, 'acl'):
raise KeyError("Cannot specify both 'acl' and 'factory'")
raise ConfigurationError("Cannot specify both 'acl' and 'factory'")

# instantiate some variables we use to keep track of what's defined for
# this service.
Expand All @@ -205,15 +203,13 @@ def __init__(self, name, path, description=None, cors_policy=None, depth=1,
setattr(self, verb.lower(),
functools.partial(self.decorator, verb))

if VENUSIAN:
# this callback will be called when config.scan (from pyramid) will
# be triggered.
def callback(context, name, ob):
config = context.config.with_package(info.module)
config.add_cornice_service(self)
# this callback will be called when config.scan (from pyramid) will
# be triggered.
def callback(context, name, ob):
config = context.config.with_package(info.module)
config.add_cornice_service(self)

info = venusian.attach(self, callback, category='pyramid',
depth=depth)
info = venusian.attach(self, callback, category='pyramid', depth=depth)

def get_arguments(self, conf=None):
"""Return a dictionary of arguments. Takes arguments from the :param
Expand Down
22 changes: 7 additions & 15 deletions cornice/util.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import sys
import warnings

import json
import simplejson

from pyramid import httpexceptions as exc
from pyramid.compat import string_types
from pyramid.renderers import IRendererFactory
from pyramid.response import Response


__all__ = ['json_renderer', 'to_list', 'json_error', 'match_accept_header']


PY3 = sys.version_info[0] == 3

if PY3:
string_types = str, # noqa
else:
string_types = basestring, # noqa


def is_string(s):
return isinstance(s, string_types)

Expand Down Expand Up @@ -190,9 +182,9 @@ def func_name(f):
"""Return the name of a function or class method."""
if isinstance(f, string_types):
return f
elif hasattr(f, '__qualname__'): # Python 3
return f.__qualname__
elif hasattr(f, 'im_class'): # Python 2
return '{0}.{1}'.format(f.im_class.__name__, f.__name__)
else:
return f.__name__
elif hasattr(f, '__qualname__'): # pragma: no cover
return f.__qualname__ # Python 3
elif hasattr(f, 'im_class'): # pragma: no cover
return '{0}.{1}'.format(f.im_class.__name__, f.__name__) # Python 2
else: # pragma: no cover
return f.__name__ # Python 2
6 changes: 4 additions & 2 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from pyramid.httpexceptions import HTTPException
from pyramid import testing

from cornice.errors import Errors


logger = logging.getLogger('cornice')

Expand All @@ -27,11 +29,11 @@ def __repr__(self):


class DummyRequest(testing.DummyRequest):
errors = []

def __init__(self, *args, **kwargs):
super(DummyRequest, self).__init__(*args, **kwargs)
self.context = DummyContext()
self.errors = Errors()
self.content_type = None


def dummy_factory(request):
Expand Down
109 changes: 0 additions & 109 deletions tests/test_colander.py

This file was deleted.

0 comments on commit 8b2a8d7

Please sign in to comment.