Skip to content

Commit

Permalink
main.py updated (#52)
Browse files Browse the repository at this point in the history
* main.py updated.

check_jobfile modified: with this update, it can now accept a string, folder or file path as input

* name bug solved

* job_file_generator is implemented

* Refactoring test files

Completed until w3_error_3()

* Update tests/test_checks.py

* Update tests/test_checks.py

Co-authored-by: Çağtay Fabry <cagtay.fabry@bam.de>

* reformat completed, also #54 solved

---------

Co-authored-by: Çağtay Fabry <cagtay.fabry@bam.de>
  • Loading branch information
barisccolak and CagtayFabry authored Nov 28, 2023
1 parent 128a9c5 commit 14d9cc6
Show file tree
Hide file tree
Showing 57 changed files with 339 additions and 571 deletions.
6 changes: 4 additions & 2 deletions testmodule/jobfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ def __repr__(self):
+ str(len(self.headlines))
+ "\n"
+ "Number of program lines:\n\t"
+ str(len(self.programlines))
+ str(number_program_lines()) #except NOP, END and " "
+ "\n"
+ "Number of LVARS:\n\t"
+ str(len(self.LVARS))
)
return rep

def number_program_lines(self):
return len([line for line in self.program_lines if not line in ["NOP", "END", ""]])

def read_LVARS(self):
"""Create a dictionary with the local variables."""
start_parsing = False # Flag to indicate when to start parsing LVARS section
Expand Down Expand Up @@ -113,7 +116,6 @@ def save_name(self):
"""Filter the characters in the name line until ' ,' and save as name."""
until = " "
self.name = self.headlines[1]
self.name = self.name[self.name.index(until) :]
self.name = self.name.strip() # delete the empty space

def save_foldername(self):
Expand Down
80 changes: 41 additions & 39 deletions testmodule/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Main.py script."""
import argparse
from pathlib import Path

from testmodule.jobfile import JobFile
from testmodule.rule import (
Rule,
Expand All @@ -12,7 +14,6 @@
check_w7,
check_w8,
)
import argparse

rules = [
Rule("JBI-W", 1, logic=check_w1),
Expand All @@ -26,60 +27,61 @@
]


def check_jobfile(file_path: str):
"""Run the rules in a folder or in a file."""
p = Path(file_path)

files = []
def check_jobfile(input_data: str):
"""Run the rules on a folder on a file or on a string."""
p = Path(input_data)

# input_data as a file
if p.is_file():
files = [p]
job_file = JobFile(p)
refactor(p)

# input_data as a directory
elif p.is_dir():
files = sorted(p.glob("*.JBI"))

for file in files:
refactor(file)

# input_data as a string
else:
raise ValueError(
f"Invalid input: '{file_path}' is neither a file nor a directory."
)

for file in files:
job_file = JobFile(file)
for rule in rules:
results = rule.apply_rule(job_file)

if results is None:
continue

if isinstance(results, tuple):
results = [results]

for result in results:
warning = (
result[0]
+ str(result[1])
+ " ["
+ str(result[2])
+ "] : "
+ result[3]
)
print(warning)


def main(file_path):
refactor(input_data)


def refactor(input_data):
"""Refactor the output message."""
job_file = JobFile(input_data)
for rule in rules:
results = rule.apply_rule(job_file)

if results is None:
continue

if isinstance(results, tuple):
results = [results]

for result in results:
warning = (
result[0] + str(result[1]) + " [" + str(result[2]) + "] : " + result[3]
)
print(warning)


def main(input_data):
"""Parser arguments."""
check_jobfile(file_path)
check_jobfile(input_data)


if __name__ == "__main__":
# setup the argument parser
parser = argparse.ArgumentParser(
prog="YASKAWA fileparser",
description="Check one or multiple YASKAWA JOB files for logic errors.",
)
parser.add_argument(
"filename",
"input_data",
type=str,
help="path to a single file or folder containing JOB files",
)
args = parser.parse_args()

main(args.filename)
main(args.input_data)
Loading

0 comments on commit 14d9cc6

Please # to comment.