File: PrefixMatcher.py

package info (click to toggle)
nitrotool 0.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 300 kB
  • sloc: python: 689; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,607 bytes parent folder | download
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
#!/usr/bin/python3
#
#	PrefixMatcher - Match the shortest unambiguous prefix
#	Copyright (C) 2011-2012 Johannes Bauer
#
#	This file is part of jpycommon.
#
#	jpycommon is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; this program is ONLY licensed under
#	version 3 of the License, later versions are explicitly excluded.
#
#	jpycommon is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with jpycommon; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#	Johannes Bauer <JohannesBauer@gmx.de>
#
#	File UUID f75ad3fb-12d0-4368-b14b-9353df125800

class PrefixMatcher(object):
	def __init__(self, options):
		self._opts = options

	def matchunique(self, value):
		result = self.match(value)
		if len(result) != 1:
			if len(result) == 0:
				raise Exception("'%s' did not match any options." % (value))
			else:
				raise Exception("'%s' is ambiguous. Please clarify further. Available: %s" % (value, ", ".join(sorted(list(result)))))
		return result[0]

	def match(self, value):
		return [ option for option in self._opts if option.startswith(value) ]

if __name__ == "__main__":
	pm = PrefixMatcher([ "import", "install", "foo" ])

	print(pm.match("i"))
	print(pm.matchunique("i"))