def _encode_class(self, data_type): """Encode a class to an XML string. Args: data_type (type): class to encode. Returns: ElementTree. XML element represent the class. """ class_element = builder.E(self._CLASS_TYPE) type_element = builder.E(TYPE_NAME, self._encode(extract_type_path(data_type))) class_element.append(type_element) return class_element
def _encode_list(self, list_data): """Encode a list to an XML string. Args: list_data (list): list to encode. Returns: ElementTree. XML element represent a list. """ list_element = builder.E(self._LIST_TYPE) for item in list_data: element = builder.E(self._LIST_ITEM_TYPE, self._encode(item)) list_element.append(element) return list_element
def to_tag(self): return B.E.file( B.E.path(unicode(self.path)), *(([B.E.size(str(self.size))] if self.size >= 0 else []) + [ B.E.hash(B.E(hash, hval)) for (hash, hval) in self.hashes.items() ]))
def create_activity(self, body='', locale='en_US', content_type='linkedin-html'): log.info("creating activity: %r", body) tree = builder.E( "activity", builder.E("content-type", content_type), builder.E("body", body), locale=locale, ) doc = etree.tostring(tree, standalone=True, encoding='UTF-8') def success(resp): log.info("create_activity success.") self.api.call("people/~/person-activities").POST(data=doc, success=success)
def _encode_message(self, message): """Encode a message to XML string. Args: message (AbstractMessage): a message to encode. Returns: str. XML string that represent the encoded message. """ header = builder.E(message.__class__.__name__) for slot in message.__slots__: slot_value = getattr(message, slot) # BasicStruct handle None slots. slot_element = builder.E(slot, self._encode(slot_value)) header.append(slot_element) return etree.tostring(header)
def post_comment(self, post_id, comment, callback=None): post = self.get_post_by_id(post_id) if post is None: return callback.error({'error_msg': "Network update not found"}) update_key = post.update_key post_data = etree.tostring(builder.E("update-comment", builder.E("comment", comment)), standalone=True, encoding='UTF-8') self.api.call('people/~/network/updates/key=%s/update-comments' % update_key.encode('url')).POST( post_data, success=callback.success, error=lambda err: self._post_comment_error( err, callback=callback))
def _encode_resource(self, resource): """Encode a resource to an XML string. Args: resource (BaseResource): resource to encode. Returns: ElementTree. XML element represent a resource. """ resource_element = builder.E(self._RESOURCE_TYPE) type_name = extract_type_path(type(resource)) type_element = builder.E(TYPE_NAME, self._encode(type_name)) resource_element.append(type_element) data_element = builder.E(DATA_NAME, self._encode(resource.data)) resource_element.append(data_element) return resource_element
def E(*args, **kwargs): fixed_kwargs = {} for k, v in kwargs.items(): if k == "href": k = "{http://www.w3.org/1999/xlink}href" if k.endswith("_"): k = k.strip("_") if isinstance(v, (int, float)): v = str(v) fixed_kwargs[k] = v return builder.E(*args, **fixed_kwargs)
def _encode_resource_data(self, resource_data): """Encode resource data to an XML string. Args: resource_data (ResourceData): resource to encode. Returns: ElementTree. XML element represent a resource. """ resource_element = builder.E(self._RESOURCE_DATA_TYPE) type_name = extract_type_path(type(resource_data)) type_element = builder.E(TYPE_NAME, self._encode(type_name)) resource_element.append(type_element) properties_element = \ builder.E(PROPERTIES, self._encode(resource_data.get_fields())) resource_element.append(properties_element) return resource_element
def _encode_dict(self, dict_data): """Encode a dictionary to an XML string. Args: dict_data (dict): dictionary to encode. Returns: ElementTree. XML element represent a dictionary. Raises: ParsingError: one of the keys is not of type str. """ dict_element = builder.E(self._DICT_TYPE) for key, value in dict_data.iteritems(): if not isinstance(key, basestring): raise ParsingError("Failed to encode dictionary, " "key %r is not a string" % key) element = builder.E(key, self._encode(value)) dict_element.append(element) return dict_element
def generate_manifest(root, hashes=('md5', ), ignore=()): root = path.path(root) hashes = tuple(hashes) file_tags = [] for file in root.walkfiles(): if any(file.startswith(d) for d in ignore): continue file = path.path(file) short = root.relpathto(file) f = FileMeta(short, file.size, (zip(hashes, hashfile(file, hashes)))) file_tags.append(f.to_tag()) manifest = B.E('manifest', *file_tags) return etree.tostring(manifest)
def set_status(self, msg, callback=None): self.api.call("people/~/current-status").PUT(data=etree.tostring( builder.E("current-status", msg), standalone=True, encoding='UTF-8'), callback=callback)