Professional Documents
Culture Documents
ImportKeywordHighlightINI Py
ImportKeywordHighlightINI Py
# $interface = "1.0"
import os
import shutil
import time, datetime
global g_strConfigPath
g_strConfigPath = ""
def IsSessionReadOnly(strSessionPath):
objConfig = crt.OpenSessionConfiguration(strSessionPath)
strTimeStamp = time.time()
strDateTime = datetime.datetime.fromtimestamp(strTimeStamp).strftime('%Y%m%d_%H
%M%S.%f')[:-3]
objConfig.SetOption(strOptionName, strDateTime)
objConfig.Save()
objConfig = crt.OpenSessionConfiguration(strSessionPath)
strNewValue = objConfig.GetOption(strOptionName)
# Now, let's restore the setting to its original value
objConfig.SetOption(strOptionName, strOrigValue)
objConfig.Save()
def GetConfigPath():
objConfig = crt.OpenSessionConfiguration("Default")
# Try and get at where the configuration folder is located. To achieve
# this goal, we'll use one of SecureCRT's cross-platform path
# directives that means "THE path this instance of SecureCRT
# is using to load/save its configuration": ${VDS_CONFIG_PATH}.
# Now, load a fresh copy of the config, and pull the option... so
# that SecureCRT will convert from the template path value to the
# actual path value:
objConfig = crt.OpenSessionConfiguration("Default")
strConfigPath = objConfig.GetOption(strOptionName)
def main():
if IsSessionReadOnly("Default"):
crt.Dialog.MessageBox(
"Your configuration folder/files seem to be read only.\r\n\r\n" +
"Please ensure your user is able to create directories and " +
"modify files in SecureCRT's configuration folder. \r\n\r\n")
return
global g_strConfigPath
g_strConfigPath = GetConfigPath()
strKeywordsDestFileName = os.path.join(
strKeywordsFolder,
os.path.basename(strKeywordIniFilePath)
)
elif nButtonClicked == 6:
# YES
# This is a no-op since the copy function we'll be calling
# will overwrite any existing file for us.
strKeywordsDestFileName = strKeywordsDestFileName
elif nButtonClicked == 7:
# NO
strKeywordsDestFileName = os.path.join(
strKeywordsFolder,
strNewUniqueFileName)
try:
shutil.copy2(strKeywordIniFilePath, strKeywordsDestFileName)
except Exception as objInst:
crt.Dialog.MessageBox(
"Failed to copy file...\r\n" +
"from: {0}\r\n\r\nto: {1}\r\n\r\nError:\r\n{2}".format(
strKeywordIniFilePath,
strKeywordsDestFileName,
str(objInst)))
return
strKeywordsDestFileBasename = os.path.basename(strKeywordsDestFileName)
strKeywordSetName = os.path.splitext(strKeywordsDestFileBasename)[0]
main()