utils: Clean up str_to_mode()

The first conditional around string being set is not needed. If string
is not set, the while loop will be skipped and mode will be returned.

The variable tmp was being overloaded by being the regex search result
and then being reassigned to be the first match group in the regex
search result. This patch keeps tmp as the regex search result and then
uses mode_char to represent the first match group of the search.

Group the search and replace actions together at the beginning of the
loop and group the mode character processing at the end of the loop.

Finally, remove the unnecessary check of tmp (now mode_char) before
calling MODE_HASH.get(tmp, False). If tmp is None or '', get() will
do the right thing and return False.

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Christian Boltz <apparmor@cboltz.de>
This commit is contained in:
Tyler Hicks 2014-04-23 15:46:42 -05:00
parent ddf977faf5
commit 6b85a158e9

View file

@ -86,16 +86,16 @@ def str_to_mode(string):
def sub_str_to_mode(string):
mode = set()
if not string:
return mode
while string:
tmp = MODE_MAP_RE.search(string)
if not tmp:
break
tmp = tmp.groups()[0]
string = MODE_MAP_RE.sub('', string, 1)
if tmp and MODE_HASH.get(tmp, False):
mode |= MODE_HASH[tmp]
mode_char = tmp.groups()[0]
if MODE_HASH.get(mode_char, False):
mode |= MODE_HASH[mode_char]
else:
pass