Skip to content

Commit

Permalink
Fix get code in Ansible and Puppet. Fix long resource and improper al…
Browse files Browse the repository at this point in the history
…ignment for Puppet
  • Loading branch information
Nfsaavedra committed Oct 1, 2022
1 parent 9e20659 commit 8469c13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
18 changes: 14 additions & 4 deletions glitch/analysis/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ def check(self, element, file: str):
return []

class PuppetImproperAlignmentSmell(SmellChecker):
cached_file = ""
lines = []

def check(self, element, file: str) -> list[Error]:
if DesignVisitor.PuppetImproperAlignmentSmell.cached_file != file:
with open(file, "r") as f:
DesignVisitor.PuppetImproperAlignmentSmell.lines = f.readlines()
DesignVisitor.PuppetImproperAlignmentSmell.cached_file = file
lines = DesignVisitor.PuppetImproperAlignmentSmell.lines

longest = 0
longest_ident = 0
longest_split = ""
for a in element.attributes:
if len(a.name) > longest and '=>' in a.code:
longest = len(a.name)
split = a.code.split('=>')[0]
split = lines[a.line - 1].split('=>')[0]
longest_ident = len(split)
longest_split = split
if longest_split == "": return []
Expand All @@ -42,7 +51,7 @@ def check(self, element, file: str) -> list[Error]:
element, file, repr(element))]

for a in element.attributes:
first_line = a.code.split("\n")[0]
first_line = lines[a.line - 1]
cur_arrow_column = len(first_line.split('=>')[0])
if cur_arrow_column != longest_ident:
return [Error('implementation_improper_alignment',
Expand Down Expand Up @@ -298,8 +307,9 @@ def check_atomicunit(self, au: AtomicUnit, file: str) -> list[Error]:

if au.type in DesignVisitor.__EXEC:
lines = 0
for line in au.code.split('\n'):
if line.strip() != "": lines += 1
for attr in au.attributes:
for line in attr.code.split('\n'):
if line.strip() != "": lines += 1

if lines > 7:
errors.append(Error("design_long_resource", au, file, repr(au)))
Expand Down
36 changes: 34 additions & 2 deletions glitch/parsers/cmof.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,34 @@ def yaml_comments(d):

return set(comments)

@staticmethod
def __get_element_code(start_token, end_token, code):
if isinstance(end_token, list) and len(end_token) > 0:
end_token = end_token[-1]
elif isinstance(end_token, list) or isinstance(end_token, str):
end_token = start_token

if start_token.start_mark.line == end_token.end_mark.line:
res = code[start_token.start_mark.line][start_token.start_mark.column : end_token.end_mark.column]
else:
res = code[start_token.start_mark.line]

for line in range(start_token.start_mark.line + 1, end_token.end_mark.line):
res += code[line]

if start_token.start_mark.line != end_token.end_mark.line:
res += code[end_token.end_mark.line][:end_token.end_mark.column]

return res

@staticmethod
def __parse_vars(unit_block, cur_name, token, code):
def create_variable(name, value):
has_variable = ("{{" in value) and ("}}" in value)
if (value in ["null", "~"]): value = ""
v = Variable(name, value, has_variable)
v.line = token.start_mark.line + 1
v.code = AnsibleParser.__get_element_code(token, value, code)
v.code = ''.join(code[token.start_mark.line : token.end_mark.line + 1])
unit_block.add_variable(v)

Expand Down Expand Up @@ -111,7 +132,7 @@ def create_attribute(token, name, value):
if (value in ["null", "~"]): value = ""
a = Attribute(name, value, has_variable)
a.line = token.start_mark.line + 1
a.code = ''.join(code[token.start_mark.line : token.end_mark.line + 1])
a.code = AnsibleParser.__get_element_code(token, val, code)
attributes.append(a)

attributes = []
Expand Down Expand Up @@ -966,7 +987,18 @@ def __process_unitblock_component(ce, unit_block: UnitBlock):
@staticmethod
def __process_codeelement(codeelement, path, code):
def get_code(ce):
return ''.join(code[ce.line - 1 : ce.end_line])
if ce.line == ce.end_line:
res = code[ce.line - 1][ce.col - 1 : ce.end_col - 1]
else:
res = code[ce.line - 1]

for line in range(ce.line, ce.end_line - 1):
res += code[line]

if ce.line != ce.end_line:
res += code[ce.end_line - 1][:ce.end_col - 1]

return res

if (isinstance(codeelement, puppetmodel.Value)):
if isinstance(codeelement, puppetmodel.Hash):
Expand Down

0 comments on commit 8469c13

Please sign in to comment.