From c50c6152db179d09cac654e4e803bd4fb2fd7713 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 20 Oct 2021 00:55:20 -0400 Subject: [PATCH] Move filechecks to their own file --- command_prompt.py | 59 +++++++++++------------------------------------ filecheck.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 filecheck.py diff --git a/command_prompt.py b/command_prompt.py index 0801d62..99f28f0 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -4,6 +4,8 @@ from kivy.graphics import Color,Rectangle import glob import os.path +import filecheck + class Command_prompt(Label): 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" 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" + (ret,self.message)=filecheck.check_write(self.argv[1],self.argv[0]=="w!") + # add comment if no overwrite + if ret==-2: + self.message+=" (use ':w!' to overwrite)" + if ret<0: 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]) @@ -353,36 +333,23 @@ class Command_prompt(Label): self.message="error: could not open file: no argument found -- usage: ':e '" 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" + # check that the file can be edited + (ret,self.message)=filecheck.check_edit(self.argv[1]) + # error + if ret<0: return + 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 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: - 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 + # select openfile in app self.app.openfile=self.argv[1] # update status bar self.app.status_bar.draw() diff --git a/filecheck.py b/filecheck.py new file mode 100644 index 0000000..36631b1 --- /dev/null +++ b/filecheck.py @@ -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,"")