TclCommandSetPath.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 4/28/2020 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from tclCommands.TclCommand import TclCommand
  8. import collections
  9. import os
  10. import logging
  11. import gettext
  12. import FlatCAMTranslation as fcTranslate
  13. import builtins
  14. fcTranslate.apply_language('strings')
  15. if '_' not in builtins.__dict__:
  16. _ = gettext.gettext
  17. log = logging.getLogger('base')
  18. class TclCommandSetPath(TclCommand):
  19. """
  20. Tcl shell command to set the default path for Tcl Shell for opening files.
  21. example:
  22. """
  23. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  24. aliases = ['set_path']
  25. description = '%s %s' % ("--", "Set the folder path to the specified path.")
  26. # Dictionary of types from Tcl command, needs to be ordered
  27. arg_names = collections.OrderedDict([
  28. ('path', str)
  29. ])
  30. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  31. option_types = collections.OrderedDict([
  32. ])
  33. # array of mandatory options for current Tcl command: required = {'name','outname'}
  34. required = ['path']
  35. # structured help for current command, args needs to be ordered
  36. help = {
  37. 'main': "Will set the folder path to the specified path.\n"
  38. "By using this command there is no need for usage of the absolute path to the files.",
  39. 'args': collections.OrderedDict([
  40. ('path', 'A folder path to where the user is supposed to have the file that he will work with.\n'
  41. 'WARNING: No spaces allowed. Use quotes around the path if it contains spaces.'),
  42. ]),
  43. 'examples': ['set_path D:\\Project_storage_path']
  44. }
  45. def execute(self, args, unnamed_args):
  46. """
  47. :param args:
  48. :param unnamed_args:
  49. :return:
  50. """
  51. if 'path' not in args:
  52. return "Failed. The Tcl command set_path was used but no path argument was provided."
  53. else:
  54. path = str(args['path']).replace('\\', '/')
  55. # check if path exists
  56. path_isdir = os.path.isdir(path)
  57. if path_isdir is False:
  58. path_isfile = os.path.isfile(path)
  59. if path_isfile:
  60. self.app.inform.emit('[ERROR] %s: %s, %s' % (
  61. "The provided path",
  62. str(path),
  63. "is a path to file and not a directory as expected."))
  64. return "Failed. The Tcl command set_path was used but it was not a directory."
  65. else:
  66. self.app.inform.emit('[ERROR] %s: %s, %s' % (
  67. "The provided path",
  68. str(path),
  69. "do not exist. Check for typos."))
  70. return "Failed. The Tcl command set_path was used but it does not exist."
  71. cd_command = 'cd %s' % path
  72. self.app.shell.exec_command(cd_command, no_echo=False)
  73. self.app.defaults["global_tcl_path"] = str(path)
  74. self.app.inform.emit('[success] %s: %s' % ("Relative path set to", str(path)))