Skip to content

Commit

Permalink
Fixed the cli so that curves are properly parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdz10001 committed Oct 12, 2017
1 parent 6a7e9d2 commit 9a64531
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ target/

#IntelliJ Files
*.idea

#Subdivision Log Generator log file
logs/
Empty file added __init__.py
Empty file.
25 changes: 15 additions & 10 deletions user_interface/cli_application.py → cli_application.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@
import logging.config
import os
import sys
from parser import parse_curve_file
from user_interface.curve_parser import parse_curve_file


class CliApplication(cmd.Cmd):

def __init__(self, *args):
def __init__(self, application_path=None, application_name="generator", args=None):
self.logger = logging.getLogger(__name__)
self.curve_paths = os.path.join(application_path, "curves")
self.arguments = args
self.parse_cmd_args(self.arguments)
sys.exit(1)

def parse_cmd_args(*args):
if args.generate_m_value:
self.generate_m_value(args.generate_m_value)
def parse_cmd_args(self, args):
if args is not None:
if args.generate_m_value:
if os.path.exists(args.generate_m_value):
self.generate_m_value(args.generate_m_value)
else:
self.generate_m_value(os.path.join(self.curve_paths, args.generate_m_value))

else:
raise Exception("There were no arguments when running the application")

def generate_m_value(self, curve_file="test.curve"):
knot_curve = parse_curve_file(curve_file)
Expand All @@ -39,11 +48,7 @@ def main():
stick knot's Bezier cure to match the stick knot", default=None, action="store")
arguments = argument_parser.parse_args()

logging.basicConfig(filename="generator_log.txt", level="DEBUG")
logger = logging.getLogger(application_name)
logger.info("Parsing arguments")

application = CliApplication(arguments)
application = CliApplication(application_path, application_name, arguments)


if __name__ == "__main__":
Expand Down
17 changes: 17 additions & 0 deletions data_objects/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"""


def distance_between_points(start_point, end_point):
x = end_point.get_x() - start_point.get_x()
y = end_point.get_y() - start_point.get_y()
z = end_point.get_z() - start_point.get_z()

return float((x * x + y * y + z * z) ** (1 / 2))


class Point:

def __init__(self, x_val, y_val, z_val):
Expand All @@ -17,3 +25,12 @@ def __eq__(self, other):
return self.x_val == other.x_val and self.y_val == other.y_val and self.z_val == other.z_val
return False

def get_x(self):
return self.x_val

def get_y(self):
return self.y_val

def get_z(self):
return self.z_val

File renamed without changes.
21 changes: 21 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
A simplistic Logger class which wraps the Python logging class
@author Peter Zaffetti
@date 10/10/2017
"""
import logging
import os


def get_logger(classname=None):
log_dir = "logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)

logging.basicConfig(filename="logs/generator_log.txt", level="DEBUG")

if classname is None:
classname = "Subdivision Generator"

return logging.getLogger(classname)
4 changes: 1 addition & 3 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)"

$SCRIPT_DIR/user_interface/cli_application.py $*
"$SCRIPT_DIR/user_interface/cli_application.py" $*
2 changes: 1 addition & 1 deletion unit_tests/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@author Peter Zaffetti 2017
"""
import unittest
from user_interface.parser import parse_curve_file
from user_interface.curve_parser import parse_curve_file
from data_objects.point import Point


Expand Down
25 changes: 25 additions & 0 deletions unit_tests/point_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python
"""
@author Peter Zaffetti 2017
"""
import unittest
from data_objects.point import Point, distance_between_points


class PointTest(unittest.TestCase):
origin_test_point = Point(0, 0, 0)
test_point = Point(12, 8, 4)

def test_getters(self):
self.assertTrue(self.test_point.get_x() == 12)
self.assertTrue(self.test_point.get_y() == 8)
self.assertTrue(self.test_point.get_z() == 4)

def test_distance_between_points(self):
"""Test the distance from the origin to 12, 8 , 4 -> it should be about 14.96662954"""
distance = distance_between_points(self.origin_test_point, self.test_point)
self.assertTrue( distance == 14.96662954)


if __name__ == "__main__":
unittest.main()
8 changes: 7 additions & 1 deletion user_interface/parser.py → user_interface/curve_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
"""
from data_objects.point import Point
from data_objects.stick_knot import StickKnot
from logger import get_logger


def parse_curve_file(filename="test.curve"):
logger = get_logger(parse_curve_file.__name__)

curve_file = open(filename, "r")
stick_knot_points = list()

for line in curve_file:
if line.startswith("%"):
line = ''.join(line.split()) # PDZ- Remove all whitespace in the row

if line.startswith("%") or line == "":
continue
comment_split_line = line.split("%")
point_values = comment_split_line[0]
comma_split_point_values = point_values.split(",")
logger.debug(comma_split_point_values)

if len(comma_split_point_values) != 3:
raise Exception("The curve file is not formatted properly")
Expand Down
1 change: 0 additions & 1 deletion user_interface/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
TODO: This is stubbed out for now. Eventually, the GUI will sit on top of the CLI functionality in the cli.py file
@author Peter Zaffetti 2017
"""
from cli_application import GenerateMValue

0 comments on commit 9a64531

Please sign in to comment.