schema_ini_convert.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from pathlib import Path
  2. def schema_convert(base_dir):
  3. base_dir = Path(base_dir).absolute()
  4. schema_file = base_dir / "schema.ini"
  5. with open(schema_file, "r", encoding="latin-1") as frh:
  6. schema = frh.read()
  7. # schema = schema.replace('\n\n', '\n').replace('\n\n', '\n')
  8. tables = dict([convert_table_info(t) for t in schema[1:].split("\n[")])
  9. for table, details in tables.items():
  10. table_file = base_dir / table
  11. if "schema.ini" in table or not table_file.exists():
  12. continue
  13. format_file = base_dir / (table + ".xml")
  14. record = []
  15. row = []
  16. for i, c in details["columns"].items():
  17. col_name = c.split(" ")[0]
  18. record.append(
  19. f' <FIELD ID="{i}" xsi:type="CharTerm" TERMINATOR=";" MAX_LENGTH="255" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>'
  20. )
  21. row.append(f' <COLUMN SOURCE="{i}" NAME="{col_name}" xsi:type="SQLVARYCHAR"/>')
  22. record[-1] = record[-1].replace(";", "\\r\\n")
  23. with open(format_file, "w") as fwh:
  24. fwh.write(
  25. '<?xml version="1.0"?>\n<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" '
  26. )
  27. fwh.write('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n<RECORD>\n')
  28. fwh.write("\n".join(record))
  29. fwh.write("\n</RECORD>\n<ROW>\n")
  30. fwh.write("\n".join(row))
  31. fwh.write("\n</ROW>\n</BCPFORMAT>")
  32. # print(tables)
  33. def convert_table_info(table_info):
  34. info = table_info.split("]\n")
  35. if len(info) < 2:
  36. return ("", "")
  37. details = {}
  38. details["columns"] = {}
  39. for key, value in [row.split("=") for row in info[1].split("\n") if "=" in row]:
  40. if key.lower() != "colnameheader" and key.lower()[:3] == "col":
  41. details["columns"][key[3:]] = value
  42. return (info[0], details)
  43. if __name__ == "__main__":
  44. schema_convert("C:\\GlobalCube_LOCOSOFT\\GCStruct_SKR51\\Kontenrahmen")
  45. # plac.Interpreter.call(IqdConverter)