Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ckanapi/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import json

from ckanapi.errors import (CKANAPIError, NotAuthorized, NotFound,
ValidationError, SearchQueryError, SearchError, SearchIndexError,
ServerIncompatibleError)
from ckanapi.errors import (CKANAPIError, NotAuthorized, NotFound, SearchError,
SearchIndexError, SearchQueryError,
ServerIncompatibleError, ValidationError)


class ActionShortcut(object):
"""
Expand All @@ -33,9 +34,19 @@ class ActionShortcut(object):
{'package_id': 'foo'}, files={'upload': open(..)})

"""

def __init__(self, ckan):
self._ckan = ckan

# __getstate__ and __setstate__ provide pickle support. If they didn't
# exist, then __getattr__ would result in a runtime pickle exception because
# it returns a local.
def __getstate__(self):
return {'_ckan': self._ckan}

def __setstate__(self, state):
self.__dict__.update(state)

def __getattr__(self, name):
def action(**kwargs):
files = {}
Expand All @@ -44,10 +55,10 @@ def action(**kwargs):
files[k] = v
if files:
nonfiles = dict((k, v) for k, v in kwargs.items()
if k not in files)
if k not in files)
return self._ckan.call_action(name,
data_dict=nonfiles,
files=files)
data_dict=nonfiles,
files=files)
return self._ckan.call_action(name, data_dict=kwargs)
return action

Expand Down
20 changes: 20 additions & 0 deletions ckanapi/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pickle
import unittest

from ckanapi import RemoteCKAN
from ckanapi.common import ActionShortcut


class TestCommon(unittest.TestCase):
def test_pickling(self):
with RemoteCKAN('http://localhost:8901') as ckan:
action_shortcut = ActionShortcut(ckan)
# Verifies that pickling seems to work. Previously, this could
# result in errors like:
# TypeError: ActionShortcut.__getattr__.<locals>.action() takes 0
# positional arguments but 1 was given
pickled = pickle.dumps(action_shortcut)
unpickled = pickle.loads(pickled)
# Partially check that the pickling+unpickling worked
self.assertEqual(action_shortcut._ckan.address,
unpickled._ckan.address)