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 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

View File

@ -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):
@ -36,3 +45,16 @@ class Square_lattice(Lattice):
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]'")