Created
August 12, 2013 18:25
-
-
Save repustate/6213556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_rule(rule, ignore_case=False): | |
""" | |
Use Python's AST parser to generate a tree which we'll traverse to create a | |
regular expression. | |
""" | |
results = parser.tokens(rule.strip()) | |
ast_string = '' | |
unicode_strings = [] | |
for token in results: | |
if token.type == 'OR': | |
ast_string += '+' | |
elif token.type == 'AND': | |
ast_string += '*' | |
else: | |
try: | |
ast_string += token.value.decode('ascii') | |
except: | |
ast_string += 'PLACEHOLDER%s' % len(unicode_strings) | |
unicode_strings.append(token.value) | |
tree = ast.parse(ast_string) | |
root = tree.body[0] | |
regexp = process(root) | |
# Check if we have to replace any strings. | |
for idx, u in enumerate(unicode_strings): | |
regexp = regexp.replace('PLACEHOLDER%s' % idx, u) | |
if ignore_case: | |
return re.compile(regexp, re.IGNORECASE) | |
else: | |
return re.compile(regexp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment