Example #1
0
def table_xml_files_to_django_model_file(input_files, output_file, xy_to_db_name_func):
    """
    Convert table XML files into a Django model file.
    @param IN input_files        Table XML files
    @param IN output_file        Django model file
    @param IN xy_to_db_name_func Function taking a Xylinq name and returning its DB-compatible name
    """

    table_info_list = []
    for input_file in input_files:
        table_elt_tree = ET.parse(input_file)
        table_elt = table_elt_tree.getroot()
        table_info = get_table_info(table_elt)
        table_info_list.append(table_info)
    output_text = table_info_list_to_django_model_text(table_info_list, xy_to_db_name_func)
    fh = open(output_file, mode="w")
    try:
        fh.write(output_text)
    finally:
        fh.close()
Example #2
0
def table_xml_files_to_django_model_file(input_files, output_file,
                                         xy_to_db_name_func):
    """
    Convert table XML files into a Django model file.
    @param IN input_files        Table XML files
    @param IN output_file        Django model file
    @param IN xy_to_db_name_func Function taking a Xylinq name and returning its DB-compatible name
    """

    table_info_list = []
    for input_file in input_files:
        table_elt_tree = ET.parse(input_file)
        table_elt = table_elt_tree.getroot()
        table_info = get_table_info(table_elt)
        table_info_list.append(table_info)
    output_text = table_info_list_to_django_model_text(table_info_list,
                                                       xy_to_db_name_func)
    fh = open(output_file, mode="w")
    try:
        fh.write(output_text)
    finally:
        fh.close()
Example #3
0
def main():
    import glob
    from optparse import OptionParser
    import os

    parser = OptionParser()
    parser.add_option("--mode",
                      dest="mode",
                      help="'data_files' or 'insert_file'")
    parser.add_option("--meta_data_dir",
                      dest="meta_data_dir",
                      help="Input directory for meta-data "
                      "files")
    parser.add_option("--xml_dirs",
                      dest="xml_dirs",
                      help="Input directories for XML table files; "
                      "'insert_file' mode only")
    parser.add_option("--out_dir",
                      dest="output_dir",
                      help="Output dir for data files; 'data_files'"
                      " mode only")
    parser.add_option("--out",
                      dest="output_file",
                      help="Output file for insert statements; "
                      "'insert_file' mode only")
    parser.add_option("--db",
                      dest="db_type",
                      help="DB type: MySQL, Oracle, ...")
    parser.add_option("--sep",
                      dest="db_statement_sep",
                      help="Separator for the insert statements; "
                      "'insert_file' mode only")
    options, dummy = parser.parse_args()

    mode = options.mode
    if mode is None:
        raise Exception("Missing mandatory argument '--mode'")
    meta_data_dir = options.meta_data_dir
    if meta_data_dir is None:
        raise Exception("Missing mandatory argument '--meta_data_dir'")
    db_type = options.db_type
    if db_type is None:
        raise Exception("Missing mandatory argument '--db'")

    if mode == "data_files":
        output_dir = options.output_dir
        if output_dir is None:
            raise Exception("Missing mandatory argument '--out_dir'")

        meta_data_files = glob.glob(os.path.join(meta_data_dir, "*.dat"))
        for meta_data_file in meta_data_files:
            print "Processing meta-data file %s" % meta_data_file

            # Read the data file
            fh = open(meta_data_file)
            try:
                meta_data_text = fh.read()
            finally:
                fh.close()

            # Convert the meta-data into DB-specific data
            db_data_text = meta_data_text_to_db_data_text(
                meta_data_text, db_type)

            # Build the DB-specific data file
            db_data_file = os.path.join(output_dir,
                                        os.path.basename(meta_data_file))
            fh = open(db_data_file, "w")
            try:
                fh.write(db_data_text)
            finally:
                fh.close()

    elif mode == "insert_file":
        xml_dir_list = options.xml_dirs
        if xml_dir_list is None:
            raise Exception("Missing mandatory argument '--xml_dirs'")
        xml_dir_list = [item.strip() for item in xml_dir_list.split(",")]
        output_file = options.output_file
        if output_file is None:
            raise Exception("Missing mandatory argument '--out'")
        db_statement_sep = options.db_statement_sep
        if db_statement_sep:
            db_statement_sep = db_statement_sep.replace("\\n", "\n")

        info_and_data_list = []
        meta_data_files = glob.glob(os.path.join(meta_data_dir, "*.dat"))
        for meta_data_file in meta_data_files:
            # Read the corresponding XML table file
            for xml_dir in xml_dir_list:
                xml_file = os.path.join(
                    xml_dir, "%s.xml" %
                    os.path.splitext(os.path.basename(meta_data_file))[0])
                if os.path.exists(xml_file):
                    break
            else:
                raise Exception(
                    "No XML table file found for meta-data file %s" %
                    meta_data_file)
            table_elt_tree = ET.parse(xml_file)
            table_elt = table_elt_tree.getroot()
            table_info = get_table_info(table_elt)
            # Read the data file
            fh = open(meta_data_file)
            try:
                meta_data_text = fh.read()
            finally:
                fh.close()
            info_and_data_list.append((table_info, meta_data_text))

        output_text = meta_data_to_db_insert_text(info_and_data_list, db_type,
                                                  db_statement_sep)
        fh = open(output_file, mode="w")
        try:
            fh.write(output_text)
        finally:
            fh.close()

    else:
        raise Exception("Unknown mode: '%s'" % mode)
Example #4
0
def main():
    import glob
    from optparse import OptionParser
    import os

    parser = OptionParser()
    parser.add_option("--mode", dest="mode", help="'data_files' or 'insert_file'")
    parser.add_option("--meta_data_dir", dest="meta_data_dir", help="Input directory for meta-data "
        "files")
    parser.add_option("--xml_dirs", dest="xml_dirs", help="Input directories for XML table files; "
        "'insert_file' mode only")
    parser.add_option("--out_dir", dest="output_dir", help="Output dir for data files; 'data_files'"
        " mode only")
    parser.add_option("--out", dest="output_file", help="Output file for insert statements; "
        "'insert_file' mode only")
    parser.add_option("--db", dest="db_type", help="DB type: MySQL, Oracle, ...")
    parser.add_option("--sep", dest="db_statement_sep", help="Separator for the insert statements; "
        "'insert_file' mode only")
    options, dummy = parser.parse_args()

    mode = options.mode
    if mode is None:
        raise Exception("Missing mandatory argument '--mode'")
    meta_data_dir = options.meta_data_dir
    if meta_data_dir is None:
        raise Exception("Missing mandatory argument '--meta_data_dir'")
    db_type = options.db_type
    if db_type is None:
        raise Exception("Missing mandatory argument '--db'")

    if mode == "data_files":
        output_dir = options.output_dir
        if output_dir is None:
            raise Exception("Missing mandatory argument '--out_dir'")

        meta_data_files = glob.glob(os.path.join(meta_data_dir, "*.dat"))
        for meta_data_file in meta_data_files:
            print "Processing meta-data file %s" % meta_data_file

            # Read the data file
            fh = open(meta_data_file)
            try:
                meta_data_text = fh.read()
            finally:
                fh.close()

            # Convert the meta-data into DB-specific data
            db_data_text = meta_data_text_to_db_data_text(meta_data_text, db_type)

            # Build the DB-specific data file
            db_data_file = os.path.join(output_dir, os.path.basename(meta_data_file))
            fh = open(db_data_file, "w")
            try:
                fh.write(db_data_text)
            finally:
                fh.close()

    elif mode == "insert_file":
        xml_dir_list = options.xml_dirs
        if xml_dir_list is None:
            raise Exception("Missing mandatory argument '--xml_dirs'")
        xml_dir_list = [item.strip() for item in xml_dir_list.split(",")]
        output_file = options.output_file
        if output_file is None:
            raise Exception("Missing mandatory argument '--out'")
        db_statement_sep = options.db_statement_sep
        if db_statement_sep:
            db_statement_sep = db_statement_sep.replace("\\n", "\n")

        info_and_data_list = []
        meta_data_files = glob.glob(os.path.join(meta_data_dir, "*.dat"))
        for meta_data_file in meta_data_files:
            # Read the corresponding XML table file
            for xml_dir in xml_dir_list:
                xml_file = os.path.join(xml_dir, "%s.xml" %
                    os.path.splitext(os.path.basename(meta_data_file))[0])
                if os.path.exists(xml_file):
                    break
            else:
                raise Exception("No XML table file found for meta-data file %s" % meta_data_file)
            table_elt_tree = ET.parse(xml_file)
            table_elt = table_elt_tree.getroot()
            table_info = get_table_info(table_elt)
            # Read the data file
            fh = open(meta_data_file)
            try:
                meta_data_text = fh.read()
            finally:
                fh.close()
            info_and_data_list.append((table_info, meta_data_text))

        output_text = meta_data_to_db_insert_text(info_and_data_list, db_type, db_statement_sep)
        fh = open(output_file, mode="w")
        try:
            fh.write(output_text)
        finally:
            fh.close()

    else:
        raise Exception("Unknown mode: '%s'" % mode)