flatcam/tclCommands/TclCommandSetPath.py
Marius Stanciu eed7e3d620 - some minor changes in the Python version detection
- added a new Tcl Command named SetPath which will set a path to be used by the Tcl commands. Once set will serve as a fallback path in case that the files fail to be opened first time. It will be persistent, saved in preferences.
- added the GUI for the new Open Example in the FIle -> Scripting menu.
- I am modifying all the open ... handlers to add a parameter that will flag if the method was launched from Tcl Shell. This way if the method will fail to open the filename (which include the path) it will try to open from a set fallback path.
- fixed issue #406, bug introduced recently (leftover changes).
- modified the ImportSVG Tcl command name to OpenSVG (open_svg alias)
- added a new Tcl command named OpenDXF (open_dxf alias)
- fixed some errors in Scripting features
- added a new Tcl command named GetPath as a convenient way to get the current default path stored in App.defaults['global_tcl_path']
2020-04-28 14:37:34 +03:00

94 lines
3.3 KiB
Python

# ##########################################################
# FlatCAM: 2D Post-processing for Manufacturing #
# File Author: Marius Adrian Stanciu (c) #
# Date: 4/28/2020 #
# MIT Licence #
# ##########################################################
from tclCommands.TclCommand import TclCommand
import collections
import os
import logging
import gettext
import FlatCAMTranslation as fcTranslate
import builtins
fcTranslate.apply_language('strings')
if '_' not in builtins.__dict__:
_ = gettext.gettext
log = logging.getLogger('base')
class TclCommandSetPath(TclCommand):
"""
Tcl shell command to set the default path for Tcl Shell for opening files.
example:
"""
# List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['set_path']
description = '%s %s' % ("--", "Set the folder path to the specified path.")
# Dictionary of types from Tcl command, needs to be ordered
arg_names = collections.OrderedDict([
('path', str)
])
# Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = collections.OrderedDict([
])
# array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['path']
# structured help for current command, args needs to be ordered
help = {
'main': "Will set the folder path to the specified path.\n"
"By using this command there is no need for usage of the absolute path to the files.",
'args': collections.OrderedDict([
('path', 'A folder path to where the user is supposed to have the file that he will work with.\n'
'WARNING: No spaces allowed. Use quotes around the path if it contains spaces.'),
]),
'examples': ['set_path D:\\Project_storage_path']
}
def execute(self, args, unnamed_args):
"""
:param args:
:param unnamed_args:
:return:
"""
if 'path' not in args:
return "Failed. The Tcl command set_path was used but no path argument was provided."
else:
path = str(args['path']).replace('\\', '/')
# check if path exists
path_isdir = os.path.isdir(path)
if path_isdir is False:
path_isfile = os.path.isfile(path)
if path_isfile:
self.app.inform.emit('[ERROR] %s: %s, %s' % (
"The provided path",
str(path),
"is a path to file and not a directory as expected."))
return "Failed. The Tcl command set_path was used but it was not a directory."
else:
self.app.inform.emit('[ERROR] %s: %s, %s' % (
"The provided path",
str(path),
"do not exist. Check for typos."))
return "Failed. The Tcl command set_path was used but it does not exist."
cd_command = 'cd %s' % path
self.app.shell.exec_command(cd_command, no_echo=False)
self.app.defaults["global_tcl_path"] = str(path)
self.app.inform.emit('[success] %s: %s' % ("Relative path set to", str(path)))