Move filechecks to their own file
This commit is contained in:
parent
89a3974626
commit
c50c6152db
@ -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"
|
||||
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()+"'"
|
||||
(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
|
||||
|
||||
# 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 <path to file>'"
|
||||
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()
|
||||
|
46
filecheck.py
Normal file
46
filecheck.py
Normal 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,"")
|
Loading…
Reference in New Issue
Block a user