Ejemplo n.º 1
0
def urlrequestpost(*args):
    """
    .. function:: urlrequestpost(data_jdict, [null], url) -> response

    This functions connects to the *url* (via POST HTTP method), submits the *data_jdict*, and returns the request's result. If second
    parameter is *null*, then in case of errors *null* will be returned.

    Examples:

    >>> sql('''select urlrequestpost('{"POST_param_name":"data"}', 'http://www.google.com/not_existing')''')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> sql('''select urlrequestpost('["POST_param_name","data"]', null, 'http://www.google.com/not_existing') as result''')
    result
    ------
    None

    >>> sql("select urlrequestpost(jdict('param1','value1'), null, 'http://www.google.com/not_existing') as result")
    result
    ------
    None

    >>> sql("select urlrequestpost(jpack('param1','value1'), null, 'http://www.google.com/not_existing') as result")
    result
    ------
    None

    """
    try:
        req = urllib2.Request(''.join((x for x in args[1:] if x != None)), None, domainExtraHeaders)

        datain = jopts.fromjsingle(args[0])

        dataout = []
        if type(datain) == list:
            for i in xrange(0, len(datain), 2):
                dataout.append((datain[i].encode('utf_8'), datain[i + 1].encode('utf_8')))
        else:
            dataout = [(x.encode('utf_8'), y.encode('utf_8')) for x, y in datain.items()]

        if dataout == []:
            raise functions.OperatorError('urlrequestpost', "A list or dict should be provided")

        hreq = urllib2.urlopen(req, urllib.urlencode(dataout))

        if [1 for x, y in hreq.headers.items() if
            x.lower() in ('content-encoding', 'content-type') and y.lower().find('gzip') != -1]:
            hreq = gzip.GzipFile(fileobj=hreq)

        return unicode(hreq.read(), 'utf-8', errors='replace')

    except urllib2.HTTPError, e:
        if args[1] == None:
            return None
        else:
            raise e
Ejemplo n.º 2
0
def urlrequestpost(*args):

    """
    .. function:: urlrequestpost(data_jdict, [null], url) -> response

    This functions connects to the *url* (via POST HTTP method), submits the *data_jdict*, and returns the request's result. If second
    parameter is *null*, then in case of errors *null* will be returned.

    Examples:

    >>> sql('''select urlrequestpost('{"POST_param_name":"data"}', 'http://www.google.com/not_existing')''')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> sql('''select urlrequestpost('["POST_param_name","data"]', null, 'http://www.google.com/not_existing') as result''')
    result
    ------
    None

    >>> sql("select urlrequestpost(jdict('param1','value1'), null, 'http://www.google.com/not_existing') as result")
    result
    ------
    None

    >>> sql("select urlrequestpost(jpack('param1','value1'), null, 'http://www.google.com/not_existing') as result")
    result
    ------
    None

    """
    try:
        req = urllib2.Request(''.join((x for x in args[1:] if x != None)), None, domainExtraHeaders)

        datain = jopts.fromjsingle(args[0])

        dataout = []
        if type(datain) == list:
            for i in xrange(0, len(datain), 2):
                dataout.append((datain[i].encode('utf_8'), datain[i+1].encode('utf_8')))
        else:
            dataout = [( x.encode('utf_8'), y.encode('utf_8') ) for x,y in datain.items()]

        if dataout == []:
            raise functions.OperatorError('urlrequestpost',"A list or dict should be provided")

        hreq = urllib2.urlopen(req, urllib.urlencode(dataout))

        if [1 for x,y in hreq.headers.items() if x.lower() in ('content-encoding', 'content-type') and y.lower().find('gzip')!=-1]:
            hreq = gzip.GzipFile(fileobj=hreq)

        return unicode(hreq.read(), 'utf-8', errors = 'replace')

    except urllib2.HTTPError,e:
        if args[1] == None:
            return None
        else:
            raise e
Ejemplo n.º 3
0
def jdict(*args):
    """
    .. function:: jdict(key, value, key1, value1) -> jdict

    Returns a jdict of the keys and value pairs.

    Examples:

    >>> sql(''' select jdict('key1', 'val1', 'key2', 'val2') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key1', 'val1', 'key2', 'val2')
    -------------------------------------
    {"key1":"val1","key2":"val2"}

    >>> sql(''' select jdict('key', '{"k1":1,"k2":2}') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key', '{"k1":1,"k2":2}')
    -------------------------------
    {"key":{"k1":1,"k2":2}}

    >>> sql(''' select jdict('key', '["val1", "val2"]') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key', '["val1", "val2"]')
    --------------------------------
    {"key":["val1","val2"]}

    >>> sql(''' select jdict('1') ''') # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator JDICT: At least two arguments required

    """

    if len(args) == 1:
        raise functions.OperatorError('jdict',
                                      "At least two arguments required")

    result = OrderedDict()

    for i in xrange(0, len(args), 2):
        result[args[i]] = jopts.fromjsingle(args[i + 1])

    return jopts.toj(result)
Ejemplo n.º 4
0
def jdict(*args):

    """
    .. function:: jdict(key, value, key1, value1) -> jdict

    Returns a jdict of the keys and value pairs.

    Examples:

    >>> sql(''' select jdict('key1', 'val1', 'key2', 'val2') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key1', 'val1', 'key2', 'val2')
    -------------------------------------
    {"key1":"val1","key2":"val2"}

    >>> sql(''' select jdict('key', '{"k1":1,"k2":2}') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key', '{"k1":1,"k2":2}')
    -------------------------------
    {"key":{"k1":1,"k2":2}}

    >>> sql(''' select jdict('key', '["val1", "val2"]') ''') # doctest: +NORMALIZE_WHITESPACE
    jdict('key', '["val1", "val2"]')
    --------------------------------
    {"key":["val1","val2"]}

    >>> sql(''' select jdict('1') ''') # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
    ...
    OperatorError: Madis SQLError:
    Operator JDICT: At least two arguments required

    """

    if len(args)==1:
        raise functions.OperatorError('jdict',"At least two arguments required")

    result = OrderedDict()
    
    for i in xrange(0, len(args), 2):
        result[args[i]] = jopts.fromjsingle(args[i+1])

    return jopts.toj( result )
Ejemplo n.º 5
0
 def step(self, *args):
     if len(args) == 1:
         self.outgroup[args[0]] = None
     else:
         self.outgroup[args[0]] = jopts.fromjsingle(*args[1:])
Ejemplo n.º 6
0
 def step(self, *args):
     if len(args) == 1:
         self.outgroup[args[0]] = None
     else:
         self.outgroup[args[0]] = jopts.fromjsingle(*args[1:])