Revise edit and write restrictions

This commit is contained in:
Ian Jauslin 2021-10-18 18:57:35 -04:00
parent fa1821d6a6
commit e650923bc7
4 changed files with 103 additions and 45 deletions

View File

@ -298,35 +298,52 @@ class Command_prompt(Label):
self.message="error: could not write to file: too many arguments -- usage: ':w [path to file]'" self.message="error: could not write to file: too many arguments -- usage: ':w [path to file]'"
return return
elif len(self.argv)==1: elif len(self.argv)==1:
if os.path.isfile(self.app.openfile): if self.app.openfile!="":
if self.app.readonly:
self.message="error: open file is readonly"
return
self.app.painter.write(self.app.openfile) self.app.painter.write(self.app.openfile)
return
else: else:
self.message="error: no file is open for editing, specify a path" self.message="error: no file is open for editing, specify a path"
return
else:
# check that the file is not a directory
if os.path.isdir(self.argv[1]):
self.message="error: '"+self.argv[1]+"' is a directory"
return return
# check that file does not already exist
elif self.argv[0]!="w!" and self.argv[1]!= self.app.openfile and os.path.isfile(self.argv[1]):
self.message="error: '"+self.argv[1]+"' already exists (use ':w!' to overwrite)"
return
# check that the containing directory exists
elif len(os.path.dirname(self.argv[1]))>0 and not os.path.isdir(os.path.dirname(self.argv[1])):
self.message="error: could not find directory '"+os.path.dirname(self.argv[1])+"'"
return
# check permissions
elif (len(os.path.dirname(self.argv[1]))>0 and not os.access(os.path.dirname(self.argv[1]),os.W_OK)) or (len(os.path.dirname(self.argv[1]))==0 and not os.access(os.getcwd(),os.W_OK)):
self.message="error: permission denied"
return
else:
self.app.openfile=self.argv[1]
self.app.painter.write(self.argv[1])
# update status bar
self.app.status_bar.draw()
# open file # check that the file is not a directory
if os.path.isdir(self.argv[1]):
self.message="error: '"+self.argv[1]+"' is a directory"
return
# check that file does not already exist
if self.argv[0]!="w!" and self.argv[1]!= self.app.openfile and os.path.isfile(self.argv[1]):
self.message="error: '"+self.argv[1]+"' already exists (use ':w!' to overwrite)"
return
# check that the containing directory exists
if len(os.path.dirname(self.argv[1]))>0 and not os.path.isdir(os.path.dirname(self.argv[1])):
self.message="error: could not find directory '"+os.path.dirname(self.argv[1])+"'"
return
# check permissions
if os.path.isfile(self.argv[1]):
if not os.access(self.argv[1],os.W_OK):
self.message="error: permission denied: cannot write to file '"+self.argv[1]+"'"
return
else:
# check write to directory
if len(os.path.dirname(self.argv[1]))>0:
if not os.access(os.path.dirname(self.argv[1]),os.W_OK):
self.message="error: permission denied: cannot write to parent directory '"+os.path.dirname(self.argv[1])+"'"
return
else:
if not os.access(os.getcwd(),os.W_OK):
self.message="error: permission denied: cannot write to parent directory '"+os.getcwd()+"'"
return
# if we haven't returned yet
self.app.openfile=self.argv[1]
self.app.readonly=False
self.app.painter.write(self.argv[1])
# update status bar
self.app.status_bar.draw()
# edit file
if self.argv[0]=="e": if self.argv[0]=="e":
if len(self.argv)>2: if len(self.argv)>2:
self.message="error: could not open file: too many arguments -- usage: ':e <path to file>'" self.message="error: could not open file: too many arguments -- usage: ':e <path to file>'"
@ -334,26 +351,40 @@ class Command_prompt(Label):
elif len(self.argv)==1: elif len(self.argv)==1:
self.message="error: could not open file: no argument found -- usage: ':e <path to file>'" self.message="error: could not open file: no argument found -- usage: ':e <path to file>'"
return return
else:
# check that the file is not a directory # check that the file is not a directory
if os.path.isdir(self.argv[1]): if os.path.isdir(self.argv[1]):
self.message="error: '"+self.argv[1]+"' is a directory" self.message="error: '"+self.argv[1]+"' is a directory"
return return
# check that the file exists if os.path.isfile(self.argv[1]):
elif not os.path.isfile(self.argv[1]):
self.message="error: could not find file '"+self.argv[1]+"'"
return
# check permissions # check permissions
elif not os.access(self.argv[1],os.R_OK): if not os.access(self.argv[1],os.R_OK):
self.message="error: permission denied" self.message="error: permission denied: cannot read file '"+self.argv[1]+"'"
return return
# read the file
self.app.painter.read(self.argv[1])
# set readonly mode
self.app.readonly=not os.access(self.argv[1],os.W_OK)
else:
# check write to directory
if len(os.path.dirname(self.argv[1]))>0:
if not os.access(os.path.dirname(self.argv[1]),os.W_OK):
self.message="error: permission denied: cannot write to parent directory '"+os.path.dirname(self.argv[1])+"'"
return
else: else:
# select openfile if not os.access(os.getcwd(),os.W_OK):
self.app.openfile=self.argv[1] self.message="error: permission denied: cannot write to parent directory '"+os.getcwd()+"'"
# read the file return
self.app.painter.read(self.argv[1]) # new file, reset painter
# update status bar self.app.painter.reset()
self.app.status_bar.draw() self.app.readonly=False
# if we haven't returned yet
# select openfile
self.app.openfile=self.argv[1]
# update status bar
self.app.status_bar.draw()
# quit # quit
if self.argv[0]=="q" or self.argv=="wq": if self.argv[0]=="q" or self.argv=="wq":

View File

@ -222,6 +222,13 @@ class Cross_painter(Widget):
# init Widget # init Widget
super(Cross_painter,self).__init__(**kwargs) super(Cross_painter,self).__init__(**kwargs)
def reset(self):
self.crosses=[]
self.selected=None
self.canvas.clear()
self.draw()
# draw all crosses # draw all crosses
def draw(self): def draw(self):
@ -317,13 +324,23 @@ class Cross_painter(Widget):
# read configuration from file # read configuration from file
def read(self,file): def read(self,file):
self.crosses=[] self.reset()
ff=open(file,"r") try:
ff=open(file,"r")
except:
self.app.command_prompt.message="error: could not read file '"+file+"' (this should not happen and is probably a bug)"
return
# counter # counter
i=0 i=0
for line in ff.readlines(): try:
lines=ff.readlines()
except:
self.app.command_prompt.message="error: could not read the contents of file '"+file+"'"
return
for line in lines:
i+=1 i+=1
# remove newline # remove newline

5
jam
View File

@ -19,6 +19,9 @@ class Jam_app(App):
# the file open for editing # the file open for editing
self.openfile=kwargs.get("openfile","") self.openfile=kwargs.get("openfile","")
# readonly mode
self.readonly=False
super(Jam_app,self).__init__() super(Jam_app,self).__init__()
@ -35,8 +38,8 @@ class Jam_app(App):
layout.add_widget(self.painter) layout.add_widget(self.painter)
layout.add_widget(self.status_bar) layout.add_widget(self.status_bar)
layout.add_widget(self.command_prompt) layout.add_widget(self.command_prompt)
return layout
return layout
# disable red circles on right click # disable red circles on right click

View File

@ -29,9 +29,16 @@ class Status_bar(Label):
self.message="" self.message=""
return return
# list openfile
if self.app.openfile!="": if self.app.openfile!="":
self.raw_text=self.app.openfile self.raw_text=self.app.openfile
else: else:
self.raw_text="[no file]" self.raw_text="[no file]"
# readonly
if self.app.readonly:
self.raw_text+=" [RO]"
# do not wrap # do not wrap
self.text=self.raw_text[:min(len(self.raw_text),int(self.width/self.char_width))] self.text=self.raw_text[:min(len(self.raw_text),int(self.width/self.char_width))]