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

Improving single line commit replacement & regex for function replacement. #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 3 additions & 15 deletions chameleon.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,23 +460,11 @@ def replace_comments(self):
_content = self.eol.join(rows)

# Single Line Comments
slc_pattern = re.compile(r"#.+")
slc_pattern = re.compile(r"#[^(\"|')]+?\n")
s1_pattern = re.compile(r"\"([^\"]*)\"")
s2_pattern = re.compile(r"'([^']*)'")
for line in rows:
match = slc_pattern.search(line)
if match:
# Single string, we don't do anything = won't break the script
res = [s1_pattern.search(line), s2_pattern.search(line)]
if any(res):
res = [r.group() for r in res if r and r.group().find("#") > -1]
else:
res = []
if len(res) > 0:
continue

_content = _content.replace(match.group(), self.placeholder)
self.content = _content
self.content = slc_pattern.sub(f"{self.placeholder}\n", _content)

def remove_comment_placeholders(self):
while self.content.find(self.placeholder) >= 0:
Expand Down Expand Up @@ -682,7 +670,7 @@ def replace_variables(self):
match.group() + "\n" + "\n".join([f"{v} = {k}" for k, v in special_vars.items()])
)

var_pattern = re.compile(r'\$[\w|_]+')
var_pattern = re.compile(r'\$\w+')
matches = [match.group() for match in var_pattern.finditer(self.content)]
matches = list(set(matches))
matches.sort(key=len)
Expand Down