Revision 256eb94b lib/utils.py
b/lib/utils.py | ||
---|---|---|
42 | 42 |
import resource |
43 | 43 |
import logging |
44 | 44 |
import signal |
45 |
import string |
|
45 | 46 |
|
46 | 47 |
from cStringIO import StringIO |
47 | 48 |
|
... | ... | |
489 | 490 |
return pid |
490 | 491 |
|
491 | 492 |
|
492 |
def MatchNameComponent(key, name_list): |
|
493 |
def MatchNameComponent(key, name_list, case_sensitive=True):
|
|
493 | 494 |
"""Try to match a name against a list. |
494 | 495 |
|
495 | 496 |
This function will try to match a name like test1 against a list |
... | ... | |
504 | 505 |
@param key: the name to be searched |
505 | 506 |
@type name_list: list |
506 | 507 |
@param name_list: the list of strings against which to search the key |
508 |
@type case_sensitive: boolean |
|
509 |
@param case_sensitive: whether to provide a case-sensitive match |
|
507 | 510 |
|
508 | 511 |
@rtype: None or str |
509 | 512 |
@return: None if there is no match I{or} if there are multiple matches, |
... | ... | |
512 | 515 |
""" |
513 | 516 |
if key in name_list: |
514 | 517 |
return key |
515 |
mo = re.compile("^%s(\..*)?$" % re.escape(key)) |
|
516 |
names_filtered = [name for name in name_list if mo.match(name) is not None] |
|
517 |
if len(names_filtered) != 1: |
|
518 |
return None |
|
519 |
return names_filtered[0] |
|
518 |
|
|
519 |
re_flags = 0 |
|
520 |
if not case_sensitive: |
|
521 |
re_flags |= re.IGNORECASE |
|
522 |
key = string.upper(key) |
|
523 |
mo = re.compile("^%s(\..*)?$" % re.escape(key), re_flags) |
|
524 |
names_filtered = [] |
|
525 |
string_matches = [] |
|
526 |
for name in name_list: |
|
527 |
if mo.match(name) is not None: |
|
528 |
names_filtered.append(name) |
|
529 |
if not case_sensitive and key == string.upper(name): |
|
530 |
string_matches.append(name) |
|
531 |
|
|
532 |
if len(string_matches) == 1: |
|
533 |
return string_matches[0] |
|
534 |
if len(names_filtered) == 1: |
|
535 |
return names_filtered[0] |
|
536 |
return None |
|
520 | 537 |
|
521 | 538 |
|
522 | 539 |
class HostInfo: |
Also available in: Unified diff