optional arguments in lattice spec

This commit is contained in:
Ian Jauslin 2022-09-23 14:45:20 -04:00
parent 5d40050580
commit 36f5226107
2 changed files with 30 additions and 5 deletions

13
src/jam
View File

@ -3,6 +3,7 @@
import sys import sys
import os.path,os import os.path,os
import filecheck import filecheck
from lattice import Lattice
## read cli before loading kivy, in case there are errors ## read cli before loading kivy, in case there are errors
@ -29,8 +30,10 @@ def read_cli():
else: else:
# read lattice argument # read lattice argument
if flag=="lattice": if flag=="lattice":
if arg!="square": # test the specification
print("error: unrecognized lattice: '"+arg+"'",file=sys.stderr) (obj,message)=Lattice.new(arg)
if obj==None:
print(message,file=sys.stderr)
exit(-1) exit(-1)
lattice=arg lattice=arg
# reset flag # reset flag
@ -61,7 +64,6 @@ from kivy.config import Config
from painter import Painter from painter import Painter
from status_bar import Status_bar from status_bar import Status_bar
from command_prompt import Command_prompt from command_prompt import Command_prompt
from lattice import Square_lattice
# App class # App class
class Jam_app(App): class Jam_app(App):
@ -103,8 +105,9 @@ class Jam_app(App):
self.readonly=not os.access(self.openfile,os.W_OK) self.readonly=not os.access(self.openfile,os.W_OK)
# load lattice # load lattice
if self.lattice=="square": if self.lattice!="":
self.painter.set_lattice(Square_lattice()) (obj,message)=Lattice.new(self.lattice)
self.painter.set_lattice(obj)
return layout return layout

View File

@ -19,6 +19,15 @@ class Lattice():
def draw(self,painter): def draw(self,painter):
return 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 # square lattice
class Square_lattice(Lattice): class Square_lattice(Lattice):
@ -35,4 +44,17 @@ class Square_lattice(Lattice):
# draw # draw
def draw(self,painter): def draw(self,painter):
painter.draw_grid(Point(self.spacing/2,self.spacing/2),self.spacing) 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]'")