This program is designed to be executed from a console window on a Win32 platform. It expands user entered commands. Three styles of commands are accepted 'x comp myfile.c', 'x #!test myfile.exe test.dat' and 'x {print} myfile.c' to extract, expand and excute commands saved in a menu file, an arbitrary file or associated with a file type in the Win32 registry.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | mHelpText = '''
# ----------------------------------------------
# Name: x
## D20H-53 Wrapper for DoM.py, DoCommand.py and pb.py
#
# Author: Philip S. Rist
# Date: 10/31/2010
# Copyright 2010 by St. Thomas Software
# ----------------------------------------------
# This program is freeware. It may be used
# for any moral purpose. You use it at your
# own risk. St. Thomas Software makes no
# guarantees to its fitness to do anything.
#
# If you feel you must pay for it. Please
# send one million dollars to
#
# The International Rescue Committee
# 122 East 42nd Street
# New York, N.Y. 10168-1289
#
# Ok, we are in a recession. So, make it a half
# million.
# Extract command from menu, Windows registry or primary file using DoM,py,
# pb.py or DoCommand.py. Expand the command and execute it. Use within
# console window to expand commands as a DOS expander macro. All matches
# are case insensitive.
#
# Examples:
# x {DoEdit} do.py - Execute DoEdit command associated with .py extension
# x open do.py - Execute open command in the current section in the current menu file
# x #!Open do.py - Execute #!Open command at the head of the do.py file
#
# Syntax:
#
# x <key> <file path> [ <arguments> ... ]
#
# <key> -
# <menu path>'<section>'<key> - Extract command <key> from section <section> of menu <menu path>
# <menu path>'<section>'? - List commands in section <section> of menu <menu path>
# <menu path>'*'<key> - Extract first matching command in menu <menu path> ignoring sections
# <menu path>'*'? - List all commands in menu <menu path>
# <section>'<key> - Extract command <command key> from section <section> of default menu
# <section>'? - List commands in section <section> in default menu
# *'<key> - Extract first matching command in default menu file ignoring sections
# *'? - List commands in all sections of default menu
# <key> - Extract command <key> from default section of default menu
# ? - List commands in default section of default menu
#
# <search path>'#!<key> - Extract command <key> from file <search path>
# <search path>'#!? - List available commands in <search path>
# #!? - List available commands in primary file <file path>
# #!<command key> - Extract command <key> from the primary file <file path>
#
# <ext>'{<key>} - Extract command <key> from registry for extension <ext>
# {<key>} - Extract command <command key> from registry tree for
# file type of <file path>
#
# <menu path> = See MENUPATH below. Use '_' instead of spaces
# <search path> = See SEARCHPATH below. Use '_' instead of spaces
# <section> = '*' to scan all sections, first acceptable command
# will be used. Incomplete sections willmatch first section
# name beginning with <section> Use '_' instead of spaces
# <command key> = '?' will list all available commands in selected section or
# sections. Incomplete keys will match first beginning with <key>
# Use '_' instead of spaces
#
# Environment variables:
#
# SEARCHPATH - path to file containing command templates. When not defined the
# file <file path> is used. May contain macros as in
# {o}\menus.ini;c:\bin\menus.ini
# MENUPATH - path to menu file containing command template. When not defined
# the file <file path> is used. May contain macros as in
# {o}\menus.ini;c:\bin\menus.ini
# SECTION - default section in menu file. When not defined 'dos' is used.
# 'i' is reserved for internal commands, only 'help' currently
# 'his' is reserved for command history, not implemented yet.
# May contain macros such ae '{e} tools'
# INTERNAL - path to menu file to be used for internal commands. Only
# section 'int' is used. When not defined only commands defined
# in x.py are available. Only 'help' currently. May contain macros as in
# {o}\menus.ini;c:\bin\menus.ini
# FILEPATH - path to primary file
#
'''
import sys, os, getopt
import DoM # Recipe: 577453
import pb # Recipe: 577454
import Do # Recipe: 577439
import Do2 # Recipe: 577440
import DoCommand # Recipe: 577441
if __name__ == '__main__':
(mOptions, mArgs) = getopt.getopt(sys.argv[1:], 'd:e:m:s:v')
mDefaultSection = 'tools' # Change as needed
mExtension = mDefaultExtension = '.txt'
# ---- Check verbosity
mVerbose = False
for (mKey, mValue) in mOptions:
if mKey == '-v':
mVerbose = True
# ---- Scan options list
mSep = "'" # anything except :./\"
# ---- Get defaults from environment
try:
mSearchPath = os.environ['SEARCHPATH']
if mVerbose:
print 'x.py Default search path', mSearchPath
except:
mSearchPath = ''
try:
mMenuPath = os.environ['MENUPATH']
if mVerbose:
print 'x.py Default menu path', mMenuPath
except:
mMenuPath = ''
try:
mInternalPath = os.environ['INTERNAL']
if mVerbose:
print 'x.py Default internal path', mInternalPath
except:
mInternalPath = ''
try:
mSection = os.environ['SECTION']
if mVerbose:
print 'x.py Default section', mSection
except:
mSection = mDefaultSection
if mSection == '':
mSection = mDefaultSection
# ---- Primary file path
if len(mArgs) > 1:
mFilePath = os.path.abspath(mArgs[1])
mArgs[1] = ''
else:
try:
mFilePath = os.environ['FILEPATH']
if mVerbose:
print 'x.py File path', mFilePath
except:
mFilePath = '' # <-- something else should go here
mVerbose = False
mHelp = False
# ---- Scan options
for (mKey, mValue) in mOptions:
if mKey == '-d': # Set current directory
if mValue.find('{') >= 0:
mValue = Do2.ExpandArg(mValue, mFilePath, '')
os.chdir(mValue)
elif mKey == '-e': # Set environment variable
DoM.setenviron(mValue, mFilePath)
elif mKey == '-m':
mMenuPath = os.path.abspath(mValue)
elif mKey == '-s':
mSection = mValue
elif mKey == '-v':
mVerbose = True
if len(mArgs) <= 0:
mKey = '?'
else:
# ---- Command key
mArgs[0] = mArgs[0].replace('_', ' ')
mKey = mArgs[0]
if mKey.find(mSep) >= 0:
mFields = mKey.split(mSep)
mLen = len(mFields)
if mLen > 2:
mMenuPath = mFields[-3]
if mLen > 1:
if mFields[-1].startswith('#!'):
mSearchPath = mFields[-2]
mSection = ''
elif mFields[-1][0] == '{':
mExtension = mFields[-2]
mSection = ''
elif mFields[-2] != '':
mSection = mFields[-2]
else:
mPos = mFilePath.rfind('.')
if mPos >= 0:
mSection = mFilePath[lPos+1:]
else:
mSection = 'txt'
mKey = mFields[-1]
mArgs[0] = mKey
if mSection.find('{'):
mSection = Do.Expand(mSection, mFilePath)
# ---- Extract command from registry
# Remove this and import if this is going to far
# Can not list commands from registry
if mKey[0] == '{':
if mExtension == mDefaultExtension: # No extension specified
lPos = mFilePath.rfind('.')
if lPos > 0:
mExtension = mFilePath[lPos:] # Contains extension with '.'
else:
mExtension = mDefaultExtension
if len(mArgs) > 2:
mArgs = mArgs[2:]
else:
mArgs = []
print mFilePath, mKey[1:-1], mExtension, mArgs
DoCommand.DoCommand(mFilePath, mKey[1:-1], mExtension, mArgs, mVerbose)
# ---- Extract command from file
# Remove this and import if this is going to far
elif mKey.startswith('#!'):
mTemp = [ mFilePath ]
for lArg in mArgs:
mTemp.append(lArg)
mArgs = mTemp
if mSearchPath == '':
mSearchPath = mFilePath
if mSearchPath.find('{') >= 0:
mSearchPath = Do2.ExpandArg(mSearchPath, mFilePath, '')
if mSearchPath[0] == '"':
mSearchPath = mSearchPath[1:-1]
mSearchPath = os.path.abspath(mSearchPath)
if mFilePath == '':
mFilePath = mSearchPath
if mFilePath == '':
print 'x.py No primary file available'
else:
pb.submitnow(mArgs, mFilePath, mSearchPath, 20, mKey == '#!?', pVerbose=mVerbose, pWidth=15)
# ---- Internal commands
elif mSection == 'i':
if mVerbose:
print 'x.py File: ', mFilePath
print 'x.py Menu: ', mMenuPath
print 'x.py Section:', mSection
print 'x.py Command:', mKey
if 'help'.startswith(mKey.lower()):
print mHelpText
elif mInternalPath != '':
if mInternalPath.find('{') >= 0:
mInternalPath = Do2.ExpandArg(mInternalPath, mFilePath, '')
if mInternalPath[0] == '"':
mInternalPath = mInternalPath[1:-1]
mInternalPath = os.path.abspath(mInternalPath)
DoM.submitnow(mArgs, mFilePath, mInternalPath, 'int', mVerbose)
else:
print 'x.py Currently no internal command', mKey
# ---- Extract command from menu
else:
if mMenuPath == '':
mMenuPath = mFilePath
else:
if mMenuPath.find('{') >= 0:
mMenuPath = Do2.ExpandArg(mMenuPath, mFilePath, '')
if mMenuPath[0] == '"':
mMenuPath = mMenuPath[1:-1]
mMenuPath = os.path.abspath(mMenuPath)
if mVerbose:
print 'x.py File: ', mFilePath
print 'x.py Menu: ', mMenuPath
print 'x.py Section:', mSection
print 'x.py Command:', mKey
if mKey.lower() == '?':
DoM.FindCommand('?', mFilePath, mMenuPath, mSection, True)
else:
DoM.submitnow(mArgs, mFilePath, mMenuPath, mSection, mVerbose)
|
First lie
This upload may be a little premature. I may be loosing my home soon. I wanted to get this uploaded before then.
Second lie
On my machine my default association for Python files is to my editor. Any time I run completed Python programs, I use a shortcut file, file association, or menu file where I can include appropriate command line arguments. I am actually using a batch file x.bat which does nothing but execute this program. With the correct file associations you should be able to execute this program simply by typing 'x'.
Third lie
So far my primary use of this program has been to test DoM (recipe 577453), pb (recipe 577454) and DoCommand (recipe 577441). It may need more testing.
This program is meant to be a user friendly front end to DoM, pb, and DoCommand used within a console window. It does not replace the console window. It uses environment variables to set default menu and file paths and section names which can be used by succeeding executions of this program.
SET MENUPATH={o}\menus.ini
SET SECTION=Project
X open myfile.py
X tools'save myfile.py
The third command will execute the open command in the Project section of the menus.ini file in the Python source directory using DoM. The fourth line will execute the save command in the tools section of the same menu file.
X #!comp myfile.c
This command will execute the #!comp command saved in myfile.c using DoCommand.
X {print} myfile.c
This command will execute the print command associated with the file type associated to th c file extension.
This program as with all the programs of this series should be not thought of as a finished program but as a starting point for developing your own tool.