Refactoring
This commit is contained in:
parent
a009b661d1
commit
449aaf7790
24
obja.py
24
obja.py
|
@ -8,10 +8,12 @@ import random
|
||||||
obja model for python.
|
obja model for python.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Face:
|
class Face:
|
||||||
"""
|
"""
|
||||||
The class that holds a, b, and c, the indices of the vertices of the face.
|
The class that holds a, b, and c, the indices of the vertices of the face.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, a, b, c, visible=True):
|
def __init__(self, a, b, c, visible=True):
|
||||||
self.a = a
|
self.a = a
|
||||||
self.b = b
|
self.b = b
|
||||||
|
@ -69,10 +71,12 @@ class Face:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
|
||||||
class VertexError(Exception):
|
class VertexError(Exception):
|
||||||
"""
|
"""
|
||||||
An operation references a vertex that does not exist.
|
An operation references a vertex that does not exist.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, index, line):
|
def __init__(self, index, line):
|
||||||
"""
|
"""
|
||||||
Creates the error from index of the referenced vertex and the line where the error occured.
|
Creates the error from index of the referenced vertex and the line where the error occured.
|
||||||
|
@ -87,10 +91,12 @@ class VertexError(Exception):
|
||||||
"""
|
"""
|
||||||
return f'There is no vector {self.index} (line {self.line})'
|
return f'There is no vector {self.index} (line {self.line})'
|
||||||
|
|
||||||
|
|
||||||
class FaceError(Exception):
|
class FaceError(Exception):
|
||||||
"""
|
"""
|
||||||
An operation references a face that does not exist.
|
An operation references a face that does not exist.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, index, line):
|
def __init__(self, index, line):
|
||||||
"""
|
"""
|
||||||
Creates the error from index of the referenced face and the line where the error occured.
|
Creates the error from index of the referenced face and the line where the error occured.
|
||||||
|
@ -105,10 +111,12 @@ class FaceError(Exception):
|
||||||
"""
|
"""
|
||||||
return f'There is no face {self.index} (line {self.line})'
|
return f'There is no face {self.index} (line {self.line})'
|
||||||
|
|
||||||
|
|
||||||
class FaceVertexError(Exception):
|
class FaceVertexError(Exception):
|
||||||
"""
|
"""
|
||||||
An operation references a face vector that does not exist.
|
An operation references a face vector that does not exist.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, index, line):
|
def __init__(self, index, line):
|
||||||
"""
|
"""
|
||||||
Creates the error from index of the referenced face vector and the line where the error occured.
|
Creates the error from index of the referenced face vector and the line where the error occured.
|
||||||
|
@ -123,10 +131,12 @@ class FaceVertexError(Exception):
|
||||||
"""
|
"""
|
||||||
return f'Face has no vector {self.index} (line {self.line})'
|
return f'Face has no vector {self.index} (line {self.line})'
|
||||||
|
|
||||||
|
|
||||||
class UnknownInstruction(Exception):
|
class UnknownInstruction(Exception):
|
||||||
"""
|
"""
|
||||||
An instruction is unknown.
|
An instruction is unknown.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, instruction, line):
|
def __init__(self, instruction, line):
|
||||||
"""
|
"""
|
||||||
Creates the error from instruction and the line where the error occured.
|
Creates the error from instruction and the line where the error occured.
|
||||||
|
@ -141,13 +151,15 @@ class UnknownInstruction(Exception):
|
||||||
"""
|
"""
|
||||||
return f'Instruction {self.instruction} unknown (line {self.line})'
|
return f'Instruction {self.instruction} unknown (line {self.line})'
|
||||||
|
|
||||||
|
|
||||||
class Model:
|
class Model:
|
||||||
"""
|
"""
|
||||||
The OBJA model.
|
The OBJA model.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Intializes an empty model.
|
Initializes an empty model.
|
||||||
"""
|
"""
|
||||||
self.vertices = []
|
self.vertices = []
|
||||||
self.faces = []
|
self.faces = []
|
||||||
|
@ -244,6 +256,7 @@ class Model:
|
||||||
return
|
return
|
||||||
# raise UnknownInstruction(split[0], self.line)
|
# raise UnknownInstruction(split[0], self.line)
|
||||||
|
|
||||||
|
|
||||||
def parse_file(path):
|
def parse_file(path):
|
||||||
"""
|
"""
|
||||||
Parses a file and returns the model.
|
Parses a file and returns the model.
|
||||||
|
@ -252,13 +265,15 @@ def parse_file(path):
|
||||||
model.parse_file(path)
|
model.parse_file(path)
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
class Output:
|
class Output:
|
||||||
"""
|
"""
|
||||||
The type for a model that outputs as obja.
|
The type for a model that outputs as obja.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, output, random_color=False):
|
def __init__(self, output, random_color=False):
|
||||||
"""
|
"""
|
||||||
Initializes the index mapping dictionnaries.
|
Initializes the index mapping dictionaries.
|
||||||
"""
|
"""
|
||||||
self.vertex_mapping = dict()
|
self.vertex_mapping = dict()
|
||||||
self.face_mapping = dict()
|
self.face_mapping = dict()
|
||||||
|
@ -279,7 +294,8 @@ class Output:
|
||||||
if len(self.vertex_mapping) == 0:
|
if len(self.vertex_mapping) == 0:
|
||||||
print('ev {} {} {} {}'.format(index, vertex[0], vertex[1], vertex[2]), file=self.output)
|
print('ev {} {} {} {}'.format(index, vertex[0], vertex[1], vertex[2]), file=self.output)
|
||||||
else:
|
else:
|
||||||
print('ev {} {} {} {}'.format(self.vertex_mapping[index] + 1, vertex[0], vertex[1],vertex[2]), file = self.output)
|
print('ev {} {} {} {}'.format(self.vertex_mapping[index] + 1, vertex[0], vertex[1], vertex[2]),
|
||||||
|
file=self.output)
|
||||||
|
|
||||||
def add_face(self, index, face):
|
def add_face(self, index, face):
|
||||||
"""
|
"""
|
||||||
|
@ -316,6 +332,7 @@ class Output:
|
||||||
file=self.output
|
file=self.output
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
print("obja needs a path to an obja file")
|
print("obja needs a path to an obja file")
|
||||||
|
@ -325,5 +342,6 @@ def main():
|
||||||
print(model.vertices)
|
print(model.vertices)
|
||||||
print(model.faces)
|
print(model.faces)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
# Standard library imports.
|
# Standard library imports.
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
from SocketServer import ThreadingMixIn
|
from SocketServer import ThreadingMixIn
|
||||||
import BaseHTTPServer
|
import BaseHTTPServer
|
||||||
|
@ -35,6 +34,7 @@ import errno
|
||||||
|
|
||||||
DATA_DIR = getcwd()
|
DATA_DIR = getcwd()
|
||||||
|
|
||||||
|
|
||||||
class ThreadingHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
class ThreadingHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -240,6 +240,7 @@ def get_server(port=8000, next_attempts=0, serve_path=None):
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def main(args=None):
|
def main(args=None):
|
||||||
if args is None:
|
if args is None:
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
|
@ -256,6 +257,6 @@ def main(args=None):
|
||||||
print("serving at port " + str(PORT))
|
print("serving at port " + str(PORT))
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue