Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perlre python3 #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cookbooks/pet/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#installing dependency that has no package on debian
execute 'pip install pyramid_chameleon'
execute 'pip install nose'
execute 'pip install coveralls'

#creating user pet
Expand Down
45 changes: 32 additions & 13 deletions pet/perlre.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@
REPLACEMENT_MARKER = 1
REPLACEMENT = 2
FLAGS = 3
RE_IGNORECASE = 2


def compile(pattern):
"""Compile a regular expression pattern into a regular expression object,
which can be used for matching using its match() and search() methods."""
for regex, sub in _pattern_rules:
pattern = regex.sub(sub, pattern)
pattern = regex.subn(sub, pattern)[0]
return re.compile(pattern)


Expand Down Expand Up @@ -154,13 +155,13 @@ def flag_decoding(regexp, string):
py_flags = 0
for flag in flags:
if flag == 'i':
py_flags |= re.I
py_flags |= RE_IGNORECASE
elif flag == 'g':
count = 0
else:
raise RegexpError(
"Unknown flag '{0}' used in regular expression.".format(flags))
return count
return count, py_flags


def regex_in_rules(regexp, string):
Expand All @@ -169,19 +170,38 @@ def regex_in_rules(regexp, string):
pattern = initial_decoding(regexp, string)[0]
replacement = initial_decoding(regexp, string)[1]
for regex, sub in _pattern_rules:
pattern = regex.sub(sub, pattern)
pattern = regex.subn(sub, pattern)[0]
for regex, sub in _replacement_rules:
replacement = regex.sub(sub, replacement)
replacement = regex.subn(sub, replacement)[0]

return pattern, replacement


def apply_substitute_regex(pattern, replacement, string, count):
def get_python_version():
import sys
tuple_python_version = sys.version_info
major_version = tuple_python_version[0]
return major_version


def apply_substitute_regex(regex_rules, string, flags_decode):
"""Apply pattern and replacement on the string and return."""
count = flags_decode[0]
flags = flags_decode[1]

pattern = regex_rules[0]
replacement = regex_rules[1]
try:
string_regex_applied = re.sub(
pattern, replacement, string, count=count
)
python_version = get_python_version()
if python_version >= 3:
regex_applied = re.subn(
pattern, replacement, string, count=count, flags=flags
)
else:
regex_applied = re.subn(
pattern, replacement, string, count=count
)
string_regex_applied = regex_applied[0]
except:
string_regex_applied = string

Expand All @@ -193,11 +213,10 @@ def apply_perlre(regexp, string):
empty_regex = not_null_regex(regexp, string)
if empty_regex:
operator_matching(regexp, string)
pattern = regex_in_rules(regexp, string)[0]
replacement = regex_in_rules(regexp, string)[1]
count = flag_decoding(regexp, string)
regex_rules = regex_in_rules(regexp, string)
flags = flag_decoding(regexp, string)
string_regex_applied = apply_substitute_regex(
pattern, replacement, string, count
regex_rules, string, flags
)
else:
string_regex_applied = string
Expand Down