Exemple #1
0
def PackForSoappy(obj, xmlns, type_name, soappy_service, wrap_lists,
                  prefix_function):
  """Packs a given object into SOAPpy.Type objects for transport.

  Args:
    obj: mixed The python object to pack for SOAPpy transport. May be a string,
         list, or dictionary depending on what it represents.
    xmlns: string The namespace that the given object's type belongs to.
    type_name: string The name of the SOAP type this object represents.
    soappy_service: SOAPpy.WSDL.Proxy The SOAPpy service object encapsulating
                    this service/WSDL.
    wrap_lists: boolean Whether or not to wrap lists in an additional layer.
    prefix_function: callable Takes in an xml namespace and returns the prefix
                     to use to represent it.

  Returns:
    mixed The given object ready for SOAPpy transport. Depending on the input,
    this will be either a list, a SOAPpy.Types.structType, or a
    SOAPpy.Types.untypedType.
  """
  if isinstance(obj, dict):
    return _PackDictForSoappy(obj, xmlns, type_name, soappy_service, wrap_lists,
                              prefix_function)
  elif isinstance(obj, (list, tuple)):
    return _PackListForSoappy(obj, xmlns, type_name, soappy_service, wrap_lists,
                              prefix_function)
  elif isinstance(obj, str):
    return SOAPpy.Types.untypedType(Utils.HtmlEscape(obj).decode('utf-8'))
  elif isinstance(obj, unicode):
    return SOAPpy.Types.untypedType(Utils.HtmlEscape(obj))
  elif isinstance(obj, SOAPpy.Types.anyType) or obj is None:
    return obj
  else:
    return SOAPpy.Types.untypedType(obj)
def _PackStringAsXml(obj, xml_tag_name):
    """Pack a Python string into an XML string.

  Args:
    obj: str Python string to pack.
    xml_tag_name: str The name of the XML tag that will house this string.

  Returns:
    str An XML element representing the given string.
  """
    if obj is None:
        return '<%s xsi3:nil="true" />' % (xml_tag_name)
    else:
        obj = Utils.HtmlEscape(obj)
        if xml_tag_name:
            return '<%s>%s</%s>' % (xml_tag_name, obj, xml_tag_name)
        else:
            return obj
Exemple #3
0
def ValidateCreative(creative):
    """Validate Creative object.

  Args:
    creative: dict Creative object.

  Returns:
    dict Creative instance.
  """
    SanityCheck.ValidateTypes(((creative, dict), ))
    if ('type' not in creative and 'creativeType' not in creative
            and 'Creative_Type' not in creative):
        msg = ('The \'creativeType\' or \'Creative_Type\' or \'type\' of the '
               'creative is missing.')
        raise ValidationError(msg)
    for key in creative:
        if creative[key] == 'None': continue
        if key in ('size', 'flashAssetSize', 'fallbackAssetSize', 'assetSize'):
            ValidateSize(creative[key])
        elif key in ('previewUrl', ):
            creative[key] = Utils.HtmlEscape(creative[key])
        else:
            SanityCheck.ValidateTypes(((creative[key], (str, unicode)), ))
    return creative
def PackDictAsXml(obj, key='', key_map=[], order=[], wrap_list=False):
    """Pack a Python dictionary object into an XML string.

  DEPRECATED. As of version 2.0.0, use PackVarAsXml instead.

  For example, an input in a form of "dct = {'ids': [12345, 67890]}", where
  "dct" is key and "{'ids': ['12345', '67890']}" is obj, the output will be
  "<dct><ids>12345</ids><ids>67890</ids></dct>".

  Args:
    obj: dict Python dictionary to pack.
    [optional]
    key: str Key that maps to this Python dictionary.
    key_map: dict Object key order map.
    order: list Order of sub keys for this Python dictionary.
    wrap_list: bool If True, wraps list into an extra layer (e.g.,
               "'ids': ['12345']"  becomes "<ids><ids>12345</ids></ids>"). If
               False, "'ids': ['12345']" becomes "<ids>12345</ids>".

  Returns:
    str XML snippet.
  """
    buf = xsi_type = ''
    has_native_type = False
    tmp_xsi_type, local_order, tmp_key_type_obj = ('', [], {})
    if isinstance(obj, dict):
        # Determine if the object is typed.
        for item in obj:
            if (item == 'xsi_type' or item == 'type' or item.find('.Type') > -1
                    or item.find('_Type') > -1):
                xsi_type = obj[item]
                if key not in key_map: continue
                for key_map_item in key_map[key]:
                    if key_map_item[
                            'type'] != xsi_type and 'native_type' in key_map_item:
                        xsi_type = key_map_item['type']
                        has_native_type = True
        if key == 'operations' and xsi_type.find('Operation') < 0:
            xsi_type = ''
        # Step through each key/value pair in the dictionary and pack it.
        for sub_key in obj:
            if (not has_native_type and
                (sub_key == 'xsi_type' or sub_key == 'type'
                 or sub_key.find('.Type') > -1 or sub_key.find('_Type') > -1)
                    and not (sub_key == 'type' and 'xsi_type' in obj)):
                continue
            tmp_xsi_type, local_order, tmp_key_type_obj = ('', [], {})
            if key in key_map:
                if 'xsi_type' not in obj and 'type' not in obj:
                    if not has_native_type:
                        for key_type_obj in key_map[key]:
                            if not key_type_obj['type']:
                                tmp_xsi_type = ''
                                local_order = key_type_obj['order']
                else:
                    for key_type_obj in key_map[key]:
                        if key_type_obj['type'] and xsi_type == key_type_obj[
                                'type']:
                            tmp_xsi_type = key_type_obj['type']
                            local_order = key_type_obj['order']
                            break
                        elif not key_type_obj['type']:
                            tmp_key_type_obj = key_type_obj
                if tmp_key_type_obj:
                    if tmp_key_type_obj['type'] is None:
                        tmp_xsi_type = xsi_type
                    else:
                        tmp_xsi_type = tmp_key_type_obj['type']
                    local_order = tmp_key_type_obj['order']
                else:
                    if len(key_map[key]) == 1:
                        tmp_xsi_type = key_map[key][0]['type']
                        local_order = key_map[key][0]['order']
            else:
                local_order = ()
            buf += PackDictAsXml(obj[sub_key], sub_key, key_map, local_order,
                                 wrap_list)
            if local_order and buf:
                pre_tags = re.split('(<.*?>)', buf)[1:-1]
                if not pre_tags: return '<%s/>' % key
                tag_name = pre_tags[0][1:-1]
                post_tags = []
                tag_buf = ''
                counter = 0
                for pre_tag in pre_tags:
                    if not pre_tag: continue
                    tag = ''
                    if pre_tag[0] == '<' and pre_tag[-1] == '>':
                        tag = re.findall('<(?:/|)(\w+).*(?:/|)>', pre_tag)
                        if tag: tag = tag[0]
                    if counter == 0: tag_name = tag
                    tag_buf += pre_tag
                    if tag and tag_name == tag:
                        counter += 1
                        if counter == 2 or pre_tag[-2] == '/':
                            post_tags.append(tag_buf)
                            tag_buf = ''
                            counter = 0
                xml_elems = post_tags
                if (len(xml_elems) > 1 and len(local_order) > 1
                        and xml_elems[0][1:] != xml_elems[-1][1:-1]):
                    tmp_buf = ''
                    pre_tags = []
                    for pre_tag in xml_elems:
                        tags = re.findall('<(?:/|)(\w+).*(?:/|)>', pre_tag)
                        if tags: pre_tags.append(tags[0])
                    sub_local_order = []
                    for tag_name in local_order:
                        if tag_name in pre_tags:
                            sub_local_order.append(tag_name)
                    for tag_name in sub_local_order:
                        for xml_elem in xml_elems:
                            if xml_elem[0] != '<': xml_elem = '<%s' % xml_elem
                            if xml_elem[-1] != '>': xml_elem = '%s>' % xml_elem
                            if (xml_elem.find(tag_name, 1,
                                              len(tag_name) + 1) > -1 and
                                (xml_elem[len(tag_name) + 1] == ' '
                                 or xml_elem[len(tag_name) + 1] == '>')):
                                tmp_buf += xml_elem
                    if tmp_buf and len(tmp_buf) >= len(buf): buf = tmp_buf
        if xsi_type and len(obj.keys()) == 1:
            data = '<%s xsi3:type="%s"/>' % (key, xsi_type)
        elif xsi_type:
            data = '<%s xsi3:type="%s">%s</%s>' % (key, xsi_type, buf, key)
        else:
            if tmp_xsi_type: tmp_xsi_type = ' xsi3:type="%s"' % (tmp_xsi_type)
            if key:
                data = '<%s%s>%s</%s>' % (key, tmp_xsi_type, buf, key)
            else:
                data = buf
    elif isinstance(obj, list):
        for item in obj:
            buf += PackDictAsXml(item, key, key_map, order, wrap_list)
        if wrap_list:
            data = '<%s>%s</%s>' % (key, buf, key)
        else:
            data = buf
    else:
        if obj is None:
            data = '<%s xsi3:nil="true" />' % (key)
        else:
            obj = Utils.HtmlEscape(obj)
            if key:
                data = '<%s>%s</%s>' % (key, obj, key)
            else:
                data = obj
    return data