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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: GDAL/OGR Utilities
# Purpose: Bash completion script generation
# Author: Guillaume Pasero <guillaume dot pasero at c dash s dot fr>
#
###############################################################################
# Copyright (c) 2016, Guillaume Pasero <guillaume dot pasero at c dash s dot fr>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import sys
from subprocess import PIPE, STDOUT, Popen
def showHelp():
print("Usage : completionFinder.py output_script")
def processLine(line):
outList = []
lvl1 = line.split(" ")
lvl2 = []
for item in lvl1:
lvl2 += item.split("[")
lvl3 = []
for item in lvl2:
lvl3 += item.split("]")
lvl4 = []
for item in lvl3:
lvl4 += item.split("|")
for item in lvl4:
if len(item) >= 2:
if item[0] == "-":
key = item
# Handle special cases -key={A/B/C}
if key.count("={") == 1 and key[-1] == "}":
pair = key.split("=")
choices = ((pair[1])[1:-1]).split("/")
for c in choices:
outList.append(pair[0] + "=" + c)
else:
outList.append(key)
return outList
def processTool(toolName):
command = [toolName, "-"]
process = Popen(command, stdout=PIPE, stderr=STDOUT)
result = process.communicate()[0]
lines = result.decode("utf-8").split("\n")
index = 0
while index < len(lines):
if "usage:" in lines[index].lower():
break
index += 1
options = []
while index < len(lines):
cleanLine = lines[index].strip("\t\r ")
if not cleanLine:
break
options += processLine(cleanLine)
index += 1
return options
def parseGDALGeneralOptions():
command = ["gdalinfo", "--help-general"]
process = Popen(command, stdout=PIPE, stderr=STDOUT)
result = process.communicate()[0]
lines = result.decode("utf-8").split("\n")
index = 0
options = []
while index < len(lines):
cleanLine = lines[index].strip("\t\r ")
parts = cleanLine.split(":")
if parts[0].find("--") == 0:
words = parts[0].split(" ")
options.append(words[0])
index += 1
return options
def parseOGRGeneralOptions():
command = ["ogr2ogr", "--help-general"]
process = Popen(command, stdout=PIPE, stderr=STDOUT)
result = process.communicate()[0]
lines = result.decode("utf-8").split("\n")
index = 0
options = []
while index < len(lines):
cleanLine = lines[index].strip("\t\r ")
parts = cleanLine.split(":")
if parts[0].find("--") == 0:
words = parts[0].split(" ")
options.append(words[0])
index += 1
return options
# generate completion script section for :
# - name : the given gdal/ogr tool
# - optList : option list
def getCompletionScript(name, optList):
output = []
output.append("_" + name + "()\n")
output.append("{\n")
output.append(" local cur prev\n")
output.append(" COMPREPLY=()\n")
output.append(" _get_comp_words_by_ref cur prev\n")
output.append(' case "$cur" in\n')
output.append(" -*)\n")
optLine = ' key_list="'
for item in optList:
optLine += item + " "
optLine += '"\n'
output.append(optLine)
output.append(' mapfile -t COMPREPLY < <(compgen -W "$key_list" -- "$cur")\n')
output.append(" return 0\n")
output.append(" ;;\n")
output.append(" esac\n")
# adapt format parsing command : GDAL type of OGR type
isGdal = True
if name.startswith("ogr"):
isGdal = False
# gdal type
if isGdal:
formatParsingCmd = "$tool --formats | tail -n +2 | cut -f 3 -d ' '"
if ("-ot" in optList) or ("-of" in optList) or ("--format" in optList):
output.append(" tool=${COMP_WORDS[0]}\n")
output.append(' case "$prev" in\n')
if "-ot" in optList:
output.append(" -ot)\n")
output.append(
' key_list="Byte Int16 UInt16 UInt32 Int32 Float32 Float64 CInt16 CInt32 CFloat32 CFloat64"\n'
)
output.append(
' mapfile -t COMPREPLY < <(compgen -W "$key_list" -- "$cur")\n'
)
output.append(" ;;\n")
for arg in ("-f", "-of"):
if (arg in optList) and isGdal:
output.append(" %s)\n" % arg)
output.append(' key_list="$( ' + formatParsingCmd + ')"\n')
output.append(
' mapfile -t COMPREPLY < <(compgen -W "$key_list" -- "$cur")\n'
)
output.append(" ;;\n")
if ("--format" in optList) and isGdal:
output.append(" --format)\n")
output.append(' key_list="$( ' + formatParsingCmd + ')"\n')
output.append(
' mapfile -t COMPREPLY < <(compgen -W "$key_list" -- "$cur")\n'
)
output.append(" ;;\n")
output.append(" esac\n")
else:
# ogr type
formatParsingCmd = "$tool --formats | tail -n +2 | grep -o -E '\"[^\"]+\"' | sed 's/\ /__/'" # noqa: W605
if "-f" in optList:
# replace ogrtindex by ogr2ogr to check --formats
output.append(" tool=${COMP_WORDS[0]/ogrtindex/ogr2ogr}\n")
output.append(' case "$prev" in\n')
for arg in ("-f", "-of"):
if (arg in optList) and not isGdal:
# completion is more tricky here because of spaces
output.append(" %s)\n" % arg)
output.append(' key_list="$( ' + formatParsingCmd + ')"\n')
output.append(" for iter in $key_list; do\n")
output.append(" if [[ $iter =~ ^$cur ]]; then\n")
output.append(' COMPREPLY+=( "${iter//__/ }" )\n')
output.append(" fi\n")
output.append(" done\n")
output.append(" ;;\n")
output.append(" esac\n")
output.append(" return 0\n")
output.append("}\n")
output.append("complete -o default -F _" + name + " " + name + "\n")
return output
def main(argv):
if len(argv) < 2:
showHelp()
return 1
gdaltools = [
"gdal2tiles.py",
"gdal2xyz.py",
"gdaladdo",
"gdalbuildvrt",
"gdal_calc.py",
"gdalchksum.py",
"gdalcompare.py",
"gdal-config",
"gdal_contour",
"gdaldem",
"gdal_edit.py",
"gdalenhance",
"gdal_fillnodata.py",
"gdal_grid",
"gdalident.py",
"gdalimport.py",
"gdalinfo",
"gdallocationinfo",
"gdalmanage",
"gdal_merge.py",
"gdalmove.py",
"gdal_polygonize.py",
"gdal_proximity.py",
"gdal_rasterize",
"gdal_retile.py",
"gdal_sieve.py",
"gdalsrsinfo",
"gdaltindex",
"gdaltransform",
"gdal_translate",
"gdalwarp",
"gdal_viewshed",
"gdal_create",
]
ogrtools = [
"ogr2ogr",
"ogrinfo",
"ogrlineref",
"ogrtindex",
"ogrmerge.py",
"ogr_layer_algebra.py",
]
# parse general options
generalOptions = parseGDALGeneralOptions()
generalOGROptions = parseOGRGeneralOptions()
outFile = argv[1]
of = open(outFile, "w")
of.write("# shellcheck shell=bash disable=SC2148\n")
of.write("# File auto-generated by completionFinder.py, do not modify manually\n")
of.write(
"""
function_exists() {
declare -f -F \"$1\" > /dev/null
return $?
}
# Checks that bash-completion is recent enough
function_exists _get_comp_words_by_ref || return 0
"""
)
for name in gdaltools:
# verbose print
print(name)
optList = processTool(name)
if "--help-general" in optList:
for item in generalOptions:
if item not in optList:
optList.append(item)
of.writelines(getCompletionScript(name, optList))
for name in ogrtools:
# verbose print
print(name)
optList = processTool(name)
if "--help-general" in optList:
for item in generalOGROptions:
if item not in optList:
optList.append(item)
of.writelines(getCompletionScript(name, optList))
of.close()
return 0
if __name__ == "__main__":
main(sys.argv)
|