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]'"
return
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)
return
else:
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
# 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 len(self.argv)>2:
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:
self.message="error: could not open file: no argument found -- usage: ':e <path to file>'"
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
# check that the file exists
elif not os.path.isfile(self.argv[1]):
self.message="error: could not find file '"+self.argv[1]+"'"
return
# 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
if os.path.isfile(self.argv[1]):
# check permissions
elif not os.access(self.argv[1],os.R_OK):
self.message="error: permission denied"
if not os.access(self.argv[1],os.R_OK):
self.message="error: permission denied: cannot read file '"+self.argv[1]+"'"
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:
# select openfile
self.app.openfile=self.argv[1]
# read the file
self.app.painter.read(self.argv[1])
# update status bar
self.app.status_bar.draw()
if not os.access(os.getcwd(),os.W_OK):
self.message="error: permission denied: cannot write to parent directory '"+os.getcwd()+"'"
return
# new file, reset painter
self.app.painter.reset()
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
if self.argv[0]=="q" or self.argv=="wq":

View File

@ -222,6 +222,13 @@ class Cross_painter(Widget):
# init Widget
super(Cross_painter,self).__init__(**kwargs)
def reset(self):
self.crosses=[]
self.selected=None
self.canvas.clear()
self.draw()
# draw all crosses
def draw(self):
@ -317,13 +324,23 @@ class Cross_painter(Widget):
# read configuration from file
def read(self,file):
self.crosses=[]
ff=open(file,"r")
self.reset()
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
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
# remove newline

5
jam
View File

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

View File

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