added basic functionality to import designers into esales cluster
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# coding=utf-8
|
||||
import glob
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger("esales")
|
||||
|
||||
|
||||
class CommandException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Cluster(object):
|
||||
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
|
||||
def run_command(self, command):
|
||||
""" execute a command against the esales cluster """
|
||||
try:
|
||||
# find the location of the jar file (provided by apptus) that is
|
||||
# used to execute the commands
|
||||
jar = os.path.abspath(os.path.dirname(
|
||||
__file__) + "/command/command.jar")
|
||||
subprocess.check_output(
|
||||
"java -jar {0} {1}".format(jar, command).split(), stderr=subprocess.STDOUT)
|
||||
except subprocess.CalledProcessError as e:
|
||||
message = "Could not execute command {0}. Error: {1}".format(
|
||||
command, e.output)
|
||||
logger.error(message)
|
||||
raise CommandException(message)
|
||||
except OSError:
|
||||
sys.exit("Java not found on this system")
|
||||
|
||||
def import_products_and_categories(self, pattern):
|
||||
""" imports products and categories into the eSales cluster """
|
||||
i = 0
|
||||
for f in sorted(glob.iglob(pattern)):
|
||||
if os.path.isfile(f):
|
||||
logger.info("Importing {}".format(f))
|
||||
try:
|
||||
self.run_command("import -p {0} {1}".format(self.url, f))
|
||||
i += 1
|
||||
except CommandException as e:
|
||||
logger.error("Failed to import {}".format(f))
|
||||
finally:
|
||||
os.remove(f)
|
||||
|
||||
logger.info("Imported {} file(s)".format(i))
|
||||
return i
|
||||
Executable
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,47 @@
|
||||
# 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():
|
||||
s = str(v)
|
||||
if s:
|
||||
SubElement(attributes, k).text = s
|
||||
|
||||
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
|
||||
@@ -0,0 +1,35 @@
|
||||
# coding=utf-8
|
||||
import os
|
||||
import uuid
|
||||
import time
|
||||
from xml.etree.cElementTree import ElementTree, Element, SubElement, dump
|
||||
|
||||
|
||||
class ImportFile(object):
|
||||
|
||||
def __init__(self):
|
||||
self.operations = []
|
||||
|
||||
def add_operation(self, operation, model):
|
||||
assert operation in ["create", "update", "delete"]
|
||||
self.operations.append((operation, model))
|
||||
|
||||
@property
|
||||
def etree(self):
|
||||
root = Element("operations")
|
||||
for name, model in self.operations:
|
||||
operation = SubElement(root, name)
|
||||
operation.append(model.etree)
|
||||
return root
|
||||
|
||||
def dump_xml(self):
|
||||
return dump(self.etree)
|
||||
|
||||
def write(self, directory, filename=None):
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
if not filename:
|
||||
filename = "{}.xml".format(time.time())
|
||||
xml = ElementTree(self.etree)
|
||||
xml.write("{}/{}".format(directory, filename),
|
||||
encoding="UTF-8", xml_declaration=True)
|
||||
Reference in New Issue
Block a user