def __get_picklist_value_node(self, picklist_value, default_value):
     value_node = Xml("value")
     value_node.set_field("fullName", picklist_value)
     is_default = picklist_value == default_value
     value_node.set_field("default", str(is_default).lower())
     value_node.set_field("label", picklist_value)
     return value_node
    def __append_picklist_values(self, field_element, field):
        value_set_definition = Xml("valueSetDefinition")
        value_set_definition.set_field("sorted", "true")
        added_values = set()
        for picklist_value in field.values:
            if picklist_value and picklist_value.lower() not in added_values:
                value_set_definition.append(
                    self.__get_picklist_value_node(picklist_value,
                                                   field.default_value))
                added_values.add(picklist_value.lower())

        value_set = Xml("valueSet")
        value_set.append(value_set_definition)
        field_element.append(value_set)
class MetadataBuilder(object):
    def __init__(self, extension, folder, metadata_label, filename,
                 config_manager):
        self.extension = extension
        self.folder = folder
        self.filename = filename
        self.config = config_manager
        self.__initialise_metadata_xml(metadata_label)

    def __initialise_metadata_xml(self, metadata_label):
        self.metadata_xml = Xml(metadata_label)
        self.metadata_xml.set_attribute("xmlns",
                                        self.config.sfdc_metadata_xmlns)

    def write_metadata(self, package_path):
        full_filename = self.filename + "." + self.extension
        metadata_directory = package_path + "/" + self.folder
        self.__create_directory(metadata_directory)
        file_path = metadata_directory + "/" + full_filename
        self.metadata_xml.write(file_path)

    def __create_directory(self, path):
        if not os.path.exists(path):
            os.makedirs(path)
class PackageCreator(object):
    def __init__(self, enviroment, config_manager):
        self.enviroment = enviroment
        self.object_model = read_spreadsheet(enviroment.spreadsheet_path)
        self.config = config_manager
        self.__initialise_package_xml()

    def __initialise_package_xml(self):
        self.package_xml = Xml("Package")
        self.package_xml.set_attribute("xmlns",
                                       self.config.sfdc_metadata_xmlns)

    def write_package(self, package_path):
        object_types = Xml("types")
        for sheet_name in self.object_model.get_sheet_names():

            if sheet_name.lower() in self.config.sheets_to_ignore:
                continue

            object_metadata = ObjectMetadataBuilder(sheet_name, self.config)
            object_types.set_field("members", object_metadata.object_api_name)

            self.__write_object_metadata(object_metadata, package_path)

        object_types.set_field("name", "CustomObject")
        self.package_xml.append(object_types)
        self.package_xml.set_field("version", self.enviroment.api_version)
        self.package_xml.write(package_path + "/package.xml")

    def __write_object_metadata(self, object_metadata, package_path):

        object_sheet = self.object_model.get_sheet(
            object_metadata.object_label)

        while object_sheet.has_more_rows():
            row = object_sheet.get_next_row()
            field = SalesforceField(row)
            object_metadata.add_field(field)

        object_metadata.append_sharing()

        object_metadata.write_metadata(package_path)
    def write_package(self, package_path):
        object_types = Xml("types")
        for sheet_name in self.object_model.get_sheet_names():

            if sheet_name.lower() in self.config.sheets_to_ignore:
                continue

            object_metadata = ObjectMetadataBuilder(sheet_name, self.config)
            object_types.set_field("members", object_metadata.object_api_name)

            self.__write_object_metadata(object_metadata, package_path)

        object_types.set_field("name", "CustomObject")
        self.package_xml.append(object_types)
        self.package_xml.set_field("version", self.enviroment.api_version)
        self.package_xml.write(package_path + "/package.xml")
 def __append_name_field(self, field, supported_types):
     name_field = Xml("nameField")
     self.__append_autonumber_format(name_field, field)
     name_field.set_field("label", field.label)
     name_field.set_field("type", supported_types[field.type])
     self.metadata_xml.append(name_field)
 def __start_field_element(self, field, supported_types):
     field_element = Xml("fields")
     field_element.set_field("label", field.label)
     field_element.set_field("fullName",
                             self.__get_custom_api_name(field.label))
     field_element.set_field("description", field.description)
     field_element.set_field("inlineHelpText", field.help_text)
     field_element.set_field("type", supported_types[field.type])
     return field_element
 def __initialise_metadata_xml(self, metadata_label):
     self.metadata_xml = Xml(metadata_label)
     self.metadata_xml.set_attribute("xmlns",
                                     self.config.sfdc_metadata_xmlns)
 def __initialise_package_xml(self):
     self.package_xml = Xml("Package")
     self.package_xml.set_attribute("xmlns",
                                    self.config.sfdc_metadata_xmlns)