def parse_acl(acl_iter): """Parse a string, or list of ACE definitions, into usable ACEs.""" if isinstance(acl_iter, string_types): acl_iter = [acl_iter] for chunk in acl_iter: if isinstance(chunk, string_types): chunk = chunk.splitlines() chunk = [re.sub(r'#.+', '', line).strip() for line in chunk] chunk = filter(None, chunk) else: chunk = [chunk] for ace in chunk: # If this was provided as a string, then parse the permission set. # Otherwise, use it as-is, which will result in an equality test. if isinstance(ace, string_types): ace = ace.split(None, 2) state, predicate, permission_set = ace yield (parse_state(state), parse_predicate(predicate), parse_permission_set(permission_set)) else: state, predicate, permission_set = ace yield parse_state(state), parse_predicate(predicate), permission_set
def parse_acl(acl_iter): """Parse a string, or list of ACE definitions, into usable ACEs.""" if isinstance(acl_iter, basestring): acl_iter = [acl_iter] for chunk in acl_iter: if isinstance(chunk, basestring): chunk = chunk.splitlines() chunk = [re.sub(r'#.+', '', line).strip() for line in chunk] chunk = filter(None, chunk) else: chunk = [chunk] for ace in chunk: # If this was provided as a string, then parse the permission set. # Otherwise, use it as-is, which will result in an equality test. if isinstance(ace, basestring): ace = ace.split(None, 2) state, predicate, permission_set = ace yield parse_state(state), parse_predicate(predicate), parse_permission_set(permission_set) else: state, predicate, permission_set = ace yield parse_state(state), parse_predicate(predicate), permission_set
def test_parsing_strings(self): for state, res in [ ('Allow', True), ('Deny', False), ('Grant', True), ('Reject', False), ]: self.assertIs(res, parse_state(state)) self.assertIs(res, parse_state(state.upper())) self.assertIs(res, parse_state(state.lower()))