Move filechecks to their own file

This commit is contained in:
Ian Jauslin 2021-10-20 00:55:20 -04:00
parent 89a3974626
commit c50c6152db
2 changed files with 59 additions and 46 deletions

View File

@ -4,6 +4,8 @@ from kivy.graphics import Color,Rectangle
import glob import glob
import os.path import os.path
import filecheck
class Command_prompt(Label): class Command_prompt(Label):
def __init__(self,app,**kwargs): def __init__(self,app,**kwargs):
@ -309,35 +311,13 @@ class Command_prompt(Label):
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 return
# check that the file is not a directory (ret,self.message)=filecheck.check_write(self.argv[1],self.argv[0]=="w!")
if os.path.isdir(self.argv[1]): # add comment if no overwrite
self.message="error: '"+self.argv[1]+"' is a directory" if ret==-2:
self.message+=" (use ':w!' to overwrite)"
if ret<0:
return 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.openfile=self.argv[1]
self.app.readonly=False self.app.readonly=False
self.app.painter.write(self.argv[1]) self.app.painter.write(self.argv[1])
@ -353,36 +333,23 @@ class Command_prompt(Label):
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
# check that the file is not a directory # check that the file can be edited
if os.path.isdir(self.argv[1]): (ret,self.message)=filecheck.check_edit(self.argv[1])
self.message="error: '"+self.argv[1]+"' is a directory" # error
if ret<0:
return return
if os.path.isfile(self.argv[1]): if os.path.isfile(self.argv[1]):
# check permissions
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 # read the file
self.app.painter.read(self.argv[1]) self.app.painter.read(self.argv[1])
# set readonly mode # set readonly mode
self.app.readonly=not os.access(self.argv[1],os.W_OK) self.app.readonly=not os.access(self.argv[1],os.W_OK)
else: 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
# new file, reset painter # new file, reset painter
self.app.painter.reset() self.app.painter.reset()
self.app.readonly=False self.app.readonly=False
# if we haven't returned yet # select openfile in app
# select openfile
self.app.openfile=self.argv[1] self.app.openfile=self.argv[1]
# update status bar # update status bar
self.app.status_bar.draw() self.app.status_bar.draw()

46
filecheck.py Normal file
View File

@ -0,0 +1,46 @@
import os.path
# check that a file can be edited
def check_edit(file):
# check that the file is not a directory
if os.path.isdir(file):
return(-1,"error: '"+file+"' is a directory")
if os.path.isfile(file):
# check permissions
if not os.access(file,os.R_OK):
return(-2,"error: permission denied: cannot read file '"+file+"'")
else:
# check write to directory
if len(os.path.dirname(file))>0:
if not os.access(os.path.dirname(file),os.W_OK):
return(-3,"error: permission denied: cannot write to parent directory '"+os.path.dirname(file)+"'")
else:
if not os.access(os.getcwd(),os.W_OK):
return(-4,"error: permission denied: cannot write to parent directory '"+os.getcwd()+"'")
return(0,"")
# check that a file can be written
def check_write(file,overwrite):
# check that the file is not a directory
if os.path.isdir(file):
return(-1,"error: '"+file+"' is a directory")
# check that file does not already exist
if not overwrite and os.path.isfile(file):
return(-2,"error: '"+file+"' already exists")
# check that the containing directory exists
if len(os.path.dirname(file))>0 and not os.path.isdir(os.path.dirname(file)):
return(-3,"error: could not find directory '"+os.path.dirname(file)+"'")
# check permissions
if os.path.isfile(file):
if not os.access(file,os.W_OK):
return(-4,"error: permission denied: cannot write to file '"+file+"'")
else:
# check write to directory
if len(os.path.dirname(file))>0:
if not os.access(os.path.dirname(file),os.W_OK):
return(-5,"error: permission denied: cannot write to parent directory '"+os.path.dirname(file)+"'")
else:
if not os.access(os.getcwd(),os.W_OK):
return(-6,"error: permission denied: cannot write to parent directory '"+os.getcwd()+"'")
return(0,"")