awlsim-proupgrade 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Project file upgrade utility
  5. #
  6. # Copyright 2017 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import sys
  24. import getopt
  25. from awlsim_loader.common import *
  26. from awlsim.gui.util import *
  27. from awlsim.gui.fup.fupwidget import FupFactory, FupWidget
  28. def usage():
  29. print("awlsim-proupgrade version %s" % VERSION_STRING)
  30. print("")
  31. print("This utility re-writes a project file using the latest file format version.")
  32. print("")
  33. print("Usage: awlsim-proupgrade [OPTIONS] PROJECTFILE.awlpro")
  34. print("")
  35. print("Options:")
  36. print(" -L|--loglevel LVL Set the client log level:")
  37. print(" 0: Log nothing")
  38. print(" 1: Log errors")
  39. print(" 2: Log errors and warnings (default)")
  40. print(" 3: Log errors, warnings and info messages")
  41. print(" 4: Verbose logging")
  42. print(" 5: Extremely verbose logging")
  43. def main():
  44. opt_loglevel = Logging.LOG_WARNING
  45. Logging.setPrefix("awlsim-proupgrade: ")
  46. try:
  47. (opts, args) = getopt.getopt(sys.argv[1:],
  48. "hL:",
  49. [ "help", "loglevel=", ])
  50. except getopt.GetoptError as e:
  51. printError(str(e))
  52. usage()
  53. return ExitCodes.EXIT_ERR_CMDLINE
  54. for (o, v) in opts:
  55. if o in ("-h", "--help"):
  56. usage()
  57. return ExitCodes.EXIT_OK
  58. if o in ("-L", "--loglevel"):
  59. try:
  60. opt_loglevel = int(v)
  61. except ValueError:
  62. printError("-L|--loglevel: Invalid log level")
  63. sys.exit(1)
  64. if len(args) != 1:
  65. usage()
  66. return ExitCodes.EXIT_ERR_CMDLINE
  67. projectFile = args[0]
  68. try:
  69. Logging.setLoglevel(opt_loglevel)
  70. # Parse the project file and write it back immediately.
  71. # The read will parse old formats, too.
  72. # The write will write using the latest format.
  73. printInfo("Upgrading file format of: %s" % projectFile)
  74. project = Project.fromFile(projectFile)
  75. for fupSource in project.getFupSources():
  76. factory = FupFactory(fupWidget=FupWidget(None, None))
  77. factory.parse(fupSource.sourceBytes)
  78. fupSource.sourceBytes = factory.compose()
  79. for kopSource in project.getKopSources():
  80. pass#TODO
  81. project.toFile()
  82. except AwlSimError as e:
  83. printError(e.getReport())
  84. return ExitCodes.EXIT_ERR_SIM
  85. return ExitCodes.EXIT_OK
  86. if __name__ == "__main__":
  87. qapp = QApplication(sys.argv)
  88. sys.exit(main())