MCPcopy Create free account
hub / github.com/ActiveState/code / FindCommand

Function FindCommand

recipes/Python/577454_Extract_expexecute/recipe-577454.py:77–155  ·  view source on GitHub ↗

Find command keyword and extract containing line pKey -- String identifying line to use as command template pFilePath -- File to use in macro expansion pSearchPath -- File to scan for command ' pCount -- Number of lines at the start of the file to scan pDef

(pKey, pFilePath, pSearchPath, pCount=10, pDefault='#!', pList=False, pWidth=10, pVerbose=False)

Source from the content-addressed store, hash-verified

75import subprocess
76
77def FindCommand(pKey, pFilePath, pSearchPath, pCount=10, pDefault='#!', pList=False, pWidth=10, pVerbose=False):
78 '''
79 Find command keyword and extract containing line
80 pKey -- String identifying line to use as command template
81 pFilePath -- File to use in macro expansion
82 pSearchPath -- File to scan for command '
83 pCount -- Number of lines at the start of the file to scan
84 pDefault -- Prefix for keys, from UNIX #!
85 pList -- True to display available commands
86 pWidth -- Number of columns at start of line in which key must appear
87 '''
88 lShortest = 6 # A line shorter than this will abort scan
89
90 if not os.path.exists(pSearchPath):
91 lCommand = ''
92 print 'pb.py Could not find search file', pSearchPath
93
94 else:
95# ---- Check allowed key values
96 if pKey == '' or pKey == pDefault: # #!
97 lKey = pDefault
98 elif pKey.find('{') >= 0: # #!{e}
99 lKey = pDefault + Do.Expand(pKey, pFilePath)
100 if not lKey.startswith(pDefault):
101 lKey = pDefault + lKey
102 elif pKey.startswith(pDefault): # #!text
103 lKey = pKey
104 else:
105 lKey = pDefault + pKey # text
106 lKey = lKey.lower()
107# lKey += ' ' # Full key only
108
109# ---- read maximum scan lines
110 lFile = open(pSearchPath, 'r')
111 lText = lFile.readlines(pCount)
112 lText = lText[0:pCount] #???
113 lFile.close()
114
115 lCommand = ''
116
117# ---- list available commands
118 if pList:
119 lText = [x for x in lText if x.find(pDefault) >= 0 ]
120 lText = [x for x in lText if x.find(pDefault) < pWidth ]
121 print 'Available Commands in', pSearchPath
122 for (lCount, lLine) in enumerate(lText):
123 lLine = lLine.strip()
124 print "%5d: %s" % (lCount, lLine)
125
126# ---- Scan for selected command
127# Since comments in some languages do not start with first character of key,
128# the key need not appear at start of line but must appear within first pWidth
129# columns to allow for skipping lines with key that are not to be matched
130 else:
131 lCount = 0
132 for lLine in lText:
133 if len(lLine.strip()) < lShortest: # blank or short line will stop scan
134 break

Callers 1

ExpandFunction · 0.70

Calls 9

startswithMethod · 0.80
openFunction · 0.50
enumerateFunction · 0.50
existsMethod · 0.45
findMethod · 0.45
lowerMethod · 0.45
readlinesMethod · 0.45
closeMethod · 0.45
stripMethod · 0.45

Tested by

no test coverage detected