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
|
'''This module contains utility functions.'''
import sys
from collections import namedtuple
from .type_utils import is_list_type
def warn(*a):
'''Print a warning.'''
print('WARNING:', *a, file=sys.stderr)
def print_err(*a):
'''Print to STDERR.'''
print(*a, file=sys.stderr)
class GeneralAbbreviationGenerator:
'''A class for generating abbreviations from a list of words.
Attributes:
min_abbreviated_length (int): The minimum length of an abbreviation.
abbreviations (dict): A dictionary mapping each word to its list of abbreviations.
min_lengths (dict): A dictionary mapping each word to its minimum abbreviation length.
'''
def __init__(self, min_abbreviated_length, words):
'''Initialize the GeneralAbbreviationGenerator instance.
Args:
min_abbreviated_length (int): The minimum length of an abbreviation.
words (iterable of str): The list of words from which to generate abbreviations.
'''
assert isinstance(min_abbreviated_length, int), \
"GeneralAbbreviationGenerator: min_abbreviated_length: expected int, got %r" % min_abbreviated_length
assert is_list_type(words), \
"GeneralAbbreviationGenerator: words: expected iterable, got %r" % words
self.min_abbreviated_length = min_abbreviated_length
self.abbreviations = {}
self.min_lengths = {}
for word in words:
self.abbreviations[word] = []
self.min_lengths[word] = len(word)
for length in range(len(word), self.min_abbreviated_length - 1, -1):
abbrev = word[0:length]
can_abbreviate = True
for other_word in filter(lambda w: w != word, words):
if other_word.startswith(abbrev):
can_abbreviate = False
break
if can_abbreviate or abbrev == word:
self.abbreviations[word].append(abbrev)
self.min_lengths[word] = length
def get_abbreviations(self, word):
'''Get the list of abbreviations for a given word.
Args:
word (str): The word for which to retrieve abbreviations.
Returns:
list: A list of abbreviations for the given word.
'''
assert isinstance(word, str), \
"GeneralAbbreviationGenerator.get_abbreviations: word: expected str, got %r" % word
return self.abbreviations[word]
def get_many_abbreviations(self, words):
'''Get the list of abbreviations for multiple words.
Args:
words (iterable of str): The words for which to retrieve abbreviations.
Returns:
list: A list of abbreviations for the given words.
'''
assert is_list_type(words), \
"GeneralAbbreviationGenerator.get_many_abbreviations: words: expected iterable, got %r" % words
r = []
for word in words:
r.extend(self.get_abbreviations(word))
return r
class OptionAbbreviationGenerator(GeneralAbbreviationGenerator):
'''AbbreviationGenerator for abbreviating long and old-style options.'''
def __init__(self, words):
assert is_list_type(words), \
"OptionAbbreviationGenerator.get_many_abbreviations: words: expected iterable, got %r" % words
words = list(words)
for word in words:
if word.startswith('--') and len(word) > 3:
pass
elif word.startswith('-') and len(word) > 2:
pass
else:
raise ValueError('Not a long or old-style option: %r' % word)
super().__init__(3, words)
class CommandAbbreviationGenerator(GeneralAbbreviationGenerator):
'''AbbreviationGenerator for abbreviating commands.'''
def __init__(self, words):
super().__init__(1, words)
class DummyAbbreviationGenerator:
'''A dummy abbreviation generator that returns the original word as the abbreviation.
This class is used as a placeholder when abbreviation generation is not required.
'''
def __init__(self):
pass
def get_abbreviations(self, word):
'''Don't abbreviate, just return the input.'''
assert isinstance(word, str), \
"DummyAbbreviationGenerator.get_abbreviations: word: expected str, got %r" % word
return [word]
def get_many_abbreviations(self, words):
'''Don't abbreviate, just return the input.'''
assert is_list_type(words), \
"DummyAbbreviationGenerator.get_many_abbreviations: words: expected iterable, got %r" % words
return words
def get_option_abbreviator(commandline):
'''Return an OptionAbbreviationGenerator for options in `commandline`.'''
if not commandline.abbreviate_options:
return DummyAbbreviationGenerator()
options = commandline.get_options(with_parent_options=commandline.inherit_options)
option_strings = []
for option in options:
option_strings.extend(option.get_long_option_strings())
option_strings.extend(option.get_old_option_strings())
return OptionAbbreviationGenerator(option_strings)
def get_all_command_variations(commandline):
'''Return all possible names for this command.
If `commandline.abbreviate_commands` is True, also return abbreviated forms.
'''
if commandline.parent is None:
return [commandline.prog] + commandline.aliases
if commandline.abbreviate_commands:
all_commands = []
for subcommand in commandline.parent.subcommands.subcommands:
all_commands.append(subcommand.prog)
abbrevs = CommandAbbreviationGenerator(all_commands)
else:
abbrevs = DummyAbbreviationGenerator()
return abbrevs.get_abbreviations(commandline.prog) + commandline.aliases
def get_defined_option_types(commandline):
'''Return a tuple of defined option types.'''
short_required = False
short_optional = False
short_flag = False
long_required = False
long_optional = False
long_flag = False
old_required = False
old_optional = False
old_flag = False
positionals = False
for cmdline in commandline.get_all_commandlines():
if len(cmdline.get_positionals()) > 0:
positionals = True
if cmdline.get_subcommands() and len(cmdline.get_subcommands().subcommands) > 0:
positionals = True
for option in cmdline.options:
if option.get_long_option_strings():
if option.complete and option.optional_arg is True:
long_optional = True
elif option.complete:
long_required = True
else:
long_flag = True
if option.get_old_option_strings():
if option.complete and option.optional_arg is True:
old_optional = True
elif option.complete:
old_required = True
else:
old_flag = True
if option.get_short_option_strings():
if option.complete and option.optional_arg is True:
short_optional = True
elif option.complete:
short_required = True
else:
short_flag = True
return namedtuple('Types', (
'positionals',
'short_required', 'short_optional', 'short_flag', 'short',
'long_required', 'long_optional', 'long_flag', 'long',
'old_required', 'old_optional', 'old_flag', 'old'))(
positionals,
short_required, short_optional, short_flag,
short_required or short_optional or short_flag,
long_required, long_optional, long_flag,
long_required or long_optional or long_flag,
old_required, old_optional, old_flag,
old_required or old_optional or old_flag)
def get_query_option_strings(commandline, with_parent_options=True):
'''Return a string that can be used by {fish,zsh}_query functions.
Returns something like:
"-f,--flag,-a=,--argument=,-o=?,--optional=?"
'''
r = []
for option in commandline.get_options(with_parent_options=with_parent_options):
if option.complete and option.optional_arg is True:
r.extend('%s=?' % s for s in option.option_strings)
elif option.complete:
r.extend('%s=' % s for s in option.option_strings)
else:
r.extend(option.option_strings)
return ','.join(r)
def is_worth_a_function(commandline):
'''Check if a commandline "is worth a function".
This means that a commandline has on of:
- Subcommands
- Positionals
- Options that aren't --help or --version
'''
if len(commandline.get_positionals()) > 0:
return True
if commandline.get_subcommands():
return True
options = commandline.get_options()
options = list(filter(lambda o: '--help' not in o.option_strings and
'--version' not in o.option_strings, options))
if len(options) > 0:
return True
return False
|