# Path: gap/utils/imports.py
# def import_class(class_name):
#     """
#     imports and returns class by its path
# 
#     USAGE:
#         myclass = import_class('StringIO.StringIO')
#         # or
#         from StringIO import StringIO
#         myclass = import_class(StringIO)
#     """
#     if isinstance(class_name, StringTypes):
#         from importlib import import_module
#         module_name, class_name = class_name.rsplit('.', 1)
#         module = import_module(module_name)
#         return getattr(module, class_name)
#     else:
#         return class_name


import json
import webapp2
from google.appengine.api import taskqueue
from gap.utils.imports import import_class
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @author Robin Gottfried <google@kebet.cz>
# part of gap project (https://github.com/czervenka/gap)
__author = 'google@kebet.cz'



def defer(callback, *args, **kwargs):
    if '_queue_name' in kwargs:
        queue_name = kwargs['_queue_name']
        del kwargs['_queue_name']
    else:
        queue_name = 'utils-defer'
    callback = "%s.%s" % (callback.__module__, callback.__name__)
    taskqueue.add(
        url='/utils/defer/%s/%s' % (queue_name, callback,),
        params={'args': json.dumps(args), 'kwargs': json.dumps(kwargs)},
        queue_name=queue_name,
    )


class _Defer(webapp2.RequestHandler):

    def post(self, queue_name, callback):
        request = self.request

# Path: gap/utils/imports.py
# def import_class(class_name):
#     """
#     imports and returns class by its path
# 
#     USAGE:
#         myclass = import_class('StringIO.StringIO')
#         # or
#         from StringIO import StringIO
#         myclass = import_class(StringIO)
#     """
#     if isinstance(class_name, StringTypes):
#         from importlib import import_module
#         module_name, class_name = class_name.rsplit('.', 1)
#         module = import_module(module_name)
#         return getattr(module, class_name)
#     else:
#         return class_name


# Path: gap/utils/defer.py
import json
import webapp2
from google.appengine.api import taskqueue
from gap.utils.imports import import_class

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @author Robin Gottfried <google@kebet.cz>
# part of gap project (https://github.com/czervenka/gap)
__author = 'google@kebet.cz'



def defer(callback, *args, **kwargs):
    if '_queue_name' in kwargs:
        queue_name = kwargs['_queue_name']
        del kwargs['_queue_name']
    else:
        queue_name = 'utils-defer'
    callback = "%s.%s" % (callback.__module__, callback.__name__)
    taskqueue.add(
        url='/utils/defer/%s/%s' % (queue_name, callback,),
        params={'args': json.dumps(args), 'kwargs': json.dumps(kwargs)},
        queue_name=queue_name,
    )


class _Defer(webapp2.RequestHandler):

    def post(self, queue_name, callback):
        request = self.request

        callback = import_class(callback)