# coding=utf-8 from xml.etree.cElementTree import Element, SubElement class Category(object): """ represents an eSales category """ def __init__(self, key, display_name=None, **kwargs): self.key = key self.display_name = display_name self.attributes = {} for k, v in kwargs.items(): self.attributes[k] = v @property def etree(self): root = Element("category", key=self.key) if self.display_name: SubElement(root, "display_name").text = self.display_name if self.attributes: attributes = SubElement(root, "attributes") for k, v in self.attributes.items(): SubElement(attributes, k).text = str(v) return root class CategoryTree(object): """represents an eSales CategoryTree""" def __init__(self, product_attribute): self.categories = [] self.product_attribute = product_attribute def add_category(self, category): self.categories.append(category) @property def etree(self): root = Element("category_tree", product_attribute=self.product_attribute) for category in self.categories: root.append(category.etree) return root