From 36f5226107f2a10c3dfb62347bafc473bf9f68a3 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 23 Sep 2022 14:45:20 -0400 Subject: [PATCH] optional arguments in lattice spec --- src/jam | 13 ++++++++----- src/lattice.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/jam b/src/jam index 72c340f..810ddca 100755 --- a/src/jam +++ b/src/jam @@ -3,6 +3,7 @@ import sys import os.path,os import filecheck +from lattice import Lattice ## read cli before loading kivy, in case there are errors @@ -29,8 +30,10 @@ def read_cli(): else: # read lattice argument if flag=="lattice": - if arg!="square": - print("error: unrecognized lattice: '"+arg+"'",file=sys.stderr) + # test the specification + (obj,message)=Lattice.new(arg) + if obj==None: + print(message,file=sys.stderr) exit(-1) lattice=arg # reset flag @@ -61,7 +64,6 @@ from kivy.config import Config from painter import Painter from status_bar import Status_bar from command_prompt import Command_prompt -from lattice import Square_lattice # App class class Jam_app(App): @@ -103,8 +105,9 @@ class Jam_app(App): self.readonly=not os.access(self.openfile,os.W_OK) # load lattice - if self.lattice=="square": - self.painter.set_lattice(Square_lattice()) + if self.lattice!="": + (obj,message)=Lattice.new(self.lattice) + self.painter.set_lattice(obj) return layout diff --git a/src/lattice.py b/src/lattice.py index 06b08d8..4a15892 100644 --- a/src/lattice.py +++ b/src/lattice.py @@ -19,6 +19,15 @@ class Lattice(): def draw(self,painter): return + # return the lattice according to a specification + def new(spec): + specs=spec.split(":") + # check type of lattice + if specs[0]=="square": + return Square_lattice.new_square(specs[1:],spec) + else: + return(None,"error: unrecognized lattice type: '"+specs[0]+"'") + # square lattice class Square_lattice(Lattice): @@ -35,4 +44,17 @@ class Square_lattice(Lattice): # draw def draw(self,painter): painter.draw_grid(Point(self.spacing/2,self.spacing/2),self.spacing) + + # return the lattice according to a specification + def new_square(specs,spec): + # no optional args + if len(specs)==0: + return (Square_lattice(),"") + if len(specs)>1: + return (None,"error: '"+spec+"' is not a valid specification for the square lattice: should be 'square[:spacing]'") + try: + spacing=float(specs[0]) + return (Square_lattice(spacing=spacing),"") + except: + return (None,"error: '"+spec+"' is not a valid specification for the square lattice: should be 'square[:spacing]'")