convert to python3, qt5

This commit is contained in:
2022-05-19 11:41:10 -04:00
parent be65c2d5bb
commit 8bee71db38
+82 -54
View File
@@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
""" a simple gui or command line app """ a simple gui or command line app
to view and create/edit file comments to view and create/edit file comments
@@ -14,6 +14,9 @@ where possible, comments are duplicated in
these comments stick to the symlink, not the deref these comments stick to the symlink, not the deref
nav tools are enabled, so you can double-click to go into a dir, and
there's an UP arrow to navigate back
""" """
@@ -42,8 +45,9 @@ comment box gets a light yellow background.
import sys,os,argparse,stat import sys,os,argparse,stat
#~ from dirWidget import DirWidget #~ from dirWidget import DirWidget
from PyQt4.QtGui import * from PyQt5.QtGui import *
from PyQt4 import QtGui, QtCore from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import xattr, sqlite3, time import xattr, sqlite3, time
VERSION = "0.2" VERSION = "0.2"
@@ -52,7 +56,7 @@ DATABASE_NAME = "~/.dirnotes.db"
# convert the ~/ form to a fully qualified path # convert the ~/ form to a fully qualified path
DATABASE_NAME = os.path.expanduser(DATABASE_NAME) DATABASE_NAME = os.path.expanduser(DATABASE_NAME)
class DataBase: class dnDataBase:
''' the database is flat ''' the database is flat
fileName: fully qualified name fileName: fully qualified name
st_mtime: a float st_mtime: a float
@@ -81,22 +85,22 @@ class DataBase:
c = self.db.cursor() c = self.db.cursor()
c.execute("select * from dirnotes where name=? and comment<>'' order by comment_date desc",(os.path.abspath(fileName),)) c.execute("select * from dirnotes where name=? and comment<>'' order by comment_date desc",(os.path.abspath(fileName),))
return c.fetchone() return c.fetchone()
def setData(self, fileName, _date, _size, comment): def setData(self, fileName, comment):
c = self.db.cursor()
c.execute("insert into dirnotes values (?,?,?,?,?)",
(fileName, _date, _size, comment, time.time()))
self.db.commit()
return True
def log(self, fileName, comment):
''' TODO: convert filename to canonical ''' ''' TODO: convert filename to canonical '''
c = self.db.cursor() c = self.db.cursor()
s = os.lstat(fileName) s = os.lstat(fileName)
print "params: %s %s %d %s %s" % ((os.path.abspath(fileName), print ("params: %s %s %d %s %s" % ((os.path.abspath(fileName),
DataBase.epochToDb(s.st_mtime), s.st_size, comment, dnDataBase.epochToDb(s.st_mtime), s.st_size, comment,
DataBase.epochToDb(time.time()))) dnDataBase.epochToDb(time.time()))))
try:
c.execute("insert into dirnotes values (?,datetime(?,'unixepoch','localtime'),?,?,datetime(?,'unixepoch','localtime'))", c.execute("insert into dirnotes values (?,datetime(?,'unixepoch','localtime'),?,?,datetime(?,'unixepoch','localtime'))",
(os.path.abspath(fileName), s.st_mtime, s.st_size, str(comment), time.time())) (os.path.abspath(fileName), s.st_mtime, s.st_size, str(comment), time.time()))
self.db.commit() self.db.commit()
except sqlite3.OperationalError:
print("database is locked or unwriteable")
#TODO: put up a message box for locked database
@staticmethod @staticmethod
def epochToDb(epoch): def epochToDb(epoch):
return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(epoch)) return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(epoch))
@@ -105,7 +109,7 @@ class DataBase:
return time.mktime(time.strptime(dbTime,"%Y-%m-%d %H:%M:%S")) return time.mktime(time.strptime(dbTime,"%Y-%m-%d %H:%M:%S"))
@staticmethod @staticmethod
def getShortDate(longDate): def getShortDate(longDate):
e = DataBase.DbToEpoch(longDate) e = dnDataBase.DbToEpoch(longDate)
sd = time.strptime(longDate,"%Y-%m-%d %H:%M:%S") sd = time.strptime(longDate,"%Y-%m-%d %H:%M:%S")
# check for this year, or today # check for this year, or today
ty = time.mktime((time.localtime()[0],1,1,0,0,0,0,1,-1)) ty = time.mktime((time.localtime()[0],1,1,0,0,0,0,1,-1))
@@ -120,8 +124,8 @@ class DataBase:
#~ for i in range(int(time.time() - 370*24*3600), #~ for i in range(int(time.time() - 370*24*3600),
#~ int(time.time() + 48*3600), #~ int(time.time() + 48*3600),
#~ 3599): #~ 3599):
#~ ds = DataBase.epochToDb(i) #~ ds = dnDataBase.epochToDb(i)
#~ print("%d\t%s\t%s" % (i,ds,DataBase.getShortDate(ds))) #~ print("%d\t%s\t%s" % (i,ds,dnDataBase.getShortDate(ds)))
@@ -146,29 +150,31 @@ class FileObj():
def __init__(self, fileName): def __init__(self, fileName):
self.fileName = fileName self.fileName = fileName
s = os.lstat(fileName) s = os.lstat(fileName)
self.date = DataBase.epochToDb(s.st_mtime) self.date = dnDataBase.epochToDb(s.st_mtime)
if stat.S_ISDIR(s.st_mode): if stat.S_ISDIR(s.st_mode):
self.size = FileObj.FILE_IS_DIR self.size = FileObj.FILE_IS_DIR
elif stat.S_ISLNK(s.st_mode): elif stat.S_ISLNK(s.st_mode):
self.size = FileObj.FILE_IS_LINK self.size = FileObj.FILE_IS_LINK
else: else:
self.size = s.st_size self.size = s.st_size
self.comment = '' self.xattrComment = ''
try: try:
self.comment = xattr.get(fileName,COMMENT_KEY,nofollow=True) self.xattrComment = xattr.get(fileName,COMMENT_KEY,nofollow=True).decode()
except Exception as e: except Exception as e:
print("comment read on %s failed, execption %s" % (self.fileName,e)) #print("comment read on %s failed, execption %s" % (self.fileName,e))
pass pass
def getName(self): def getName(self):
return self.fileName return self.fileName
def getFileName(self): def getFileName(self):
return os.path.split(self.fileName)[1] return os.path.split(self.fileName)[1]
def getComment(self):
return self.comment def getXattrComment(self):
def setComment(self,newComment): return self.xattrComment
self.comment = newComment def setXattrComment(self,newComment):
self.xattrComment = newComment
try: try:
xattr.set(self.fileName,COMMENT_KEY,self.comment,nofollow=True) xattr.set(self.fileName,COMMENT_KEY,self.xattrComment,nofollow=True)
return True return True
# we need to move these cases out to a handler # we need to move these cases out to a handler
except Exception as e: except Exception as e:
@@ -184,6 +190,15 @@ class FileObj():
elif "Errno 95" in str(e): elif "Errno 95" in str(e):
print("is this a VFAT or EXFAT volume? these don't allow comments") print("is this a VFAT or EXFAT volume? these don't allow comments")
return False return False
def getDbComment(self,db):
self.dbComment = ""
row = db.getData(self.fileName)
if row and len(row)>1:
self.dbComment = row[3]
def setDbComment(self,db,comment):
db.setData(self.fileName,comment)
def getDate(self): def getDate(self):
return self.date return self.date
def getSize(self): def getSize(self):
@@ -207,10 +222,12 @@ class HelpWidget(QDialog):
# sortable TableWidgetItem, based on idea by Aledsandar # sortable TableWidgetItem, based on idea by Aledsandar
# http://stackoverflow.com/questions/12673598/python-numerical-sorting-in-qtablewidget # http://stackoverflow.com/questions/12673598/python-numerical-sorting-in-qtablewidget
# NOTE: the QTableWidgetItem has setData() and data() which may allow data bonding # NOTE: the QTableWidgetItem has setData() and data() which may allow data bonding
# in Qt5, data() binding is more awkward, so do it here
class SortableTableWidgetItem(QTableWidgetItem): class SortableTableWidgetItem(QTableWidgetItem):
def __init__(self, text, sortValue): def __init__(self, text, sortValue, file_object):
QTableWidgetItem.__init__(self, text, QTableWidgetItem.UserType) QTableWidgetItem.__init__(self, text, QTableWidgetItem.UserType)
self.sortValue = sortValue self.sortValue = sortValue
self.file_object = file_object
def __lt__(self, other): def __lt__(self, other):
return self.sortValue < other.sortValue return self.sortValue < other.sortValue
@@ -231,7 +248,7 @@ class DirNotes(QMainWindow):
lb = QTableWidget() lb = QTableWidget()
self.lb = lb self.lb = lb
lb.setColumnCount(4) lb.setColumnCount(4)
lb.horizontalHeader().setResizeMode( 3, QHeaderView.Stretch ); lb.horizontalHeader().setSectionResizeMode( 3, QHeaderView.Stretch );
lb.verticalHeader().setDefaultSectionSize(20); # thinner rows lb.verticalHeader().setDefaultSectionSize(20); # thinner rows
lb.verticalHeader().setVisible(False) lb.verticalHeader().setVisible(False)
@@ -306,7 +323,7 @@ class DirNotes(QMainWindow):
print("closing") print("closing")
def sbd(self): def sbd(self):
print("sort by date") print("sort by date")
self.lb.sortItems(1,QtCore.Qt.DescendingOrder) self.lb.sortItems(1,Qt.DescendingOrder)
def sbs(self): def sbs(self):
print("sort by size") print("sort by size")
self.lb.sortItems(2) self.lb.sortItems(2)
@@ -317,12 +334,12 @@ class DirNotes(QMainWindow):
HelpWidget(self) HelpWidget(self)
def sbc(self): def sbc(self):
print("sort by comment") print("sort by comment")
self.lb.sortItems(3,QtCore.Qt.DescendingOrder) self.lb.sortItems(3,Qt.DescendingOrder)
def newDir(self): def newDir(self):
print("change dir to "+self.dirLeft.currentPath()) print("change dir to "+self.dirLeft.currentPath())
def double(self,row,col): def double(self,row,col):
print("double click {} {}".format(row, col)) print("double click {} {}".format(row, col))
fo = self.lb.item(row,0).data(32).toPyObject() fo = self.lb.item(row,0).file_object
if col==0 and fo.getSize() == FileObj.FILE_IS_DIR: if col==0 and fo.getSize() == FileObj.FILE_IS_DIR:
print("double click on {}".format(fo.getName())) print("double click on {}".format(fo.getName()))
self.curPath = fo.getName() self.curPath = fo.getName()
@@ -339,8 +356,8 @@ class DirNotes(QMainWindow):
dirIcon = QIcon.fromTheme('folder') dirIcon = QIcon.fromTheme('folder')
fileIcon = QIcon.fromTheme('text-x-generic') fileIcon = QIcon.fromTheme('text-x-generic')
linkIcon = QIcon.fromTheme('emblem-symbolic-link') linkIcon = QIcon.fromTheme('emblem-symbolic-link')
current, dirs, files = os.walk(self.curPath,followlinks=True).next() current, dirs, files = next(os.walk(self.curPath,followlinks=True))
dirs = map(lambda x:x+'/', dirs) dirs = list(map(lambda x:x+'/', dirs))
dirs.sort() dirs.sort()
files.sort() files.sort()
@@ -353,40 +370,52 @@ class DirNotes(QMainWindow):
#~ print("insert {} items into cleared table {}".format(len(d),current)) #~ print("insert {} items into cleared table {}".format(len(d),current))
for i in range(len(d)): for i in range(len(d)):
this_file = FileObj(current+'/'+d[i]) this_file = FileObj(current+'/'+d[i])
this_file.getDbComment(self.db)
print("FileObj created as {} and the db-comment is <{}>".format(this_file.fileName, this_file.dbComment))
#~ print("insert order check: {} {} {} {}".format(d[i],i,this_file.getName(),this_file.getDate())) #~ print("insert order check: {} {} {} {}".format(d[i],i,this_file.getName(),this_file.getDate()))
#~ self.files.update({this_file.getName(),this_file}) #~ self.files.update({this_file.getName(),this_file})
self.files = self.files + [this_file] self.files = self.files + [this_file]
if this_file.getSize() == FileObj.FILE_IS_DIR: if this_file.getSize() == FileObj.FILE_IS_DIR:
item = SortableTableWidgetItem(d[i],' '+d[i]) # directories sort first item = SortableTableWidgetItem(d[i],' '+d[i], this_file) # directories sort first
else: else:
item = SortableTableWidgetItem(d[i],d[i]) item = SortableTableWidgetItem(d[i],d[i], this_file)
item.setFlags(QtCore.Qt.ItemIsEnabled) item.setFlags(Qt.ItemIsEnabled)
item.setData(32,this_file) # keep a hidden copy of the file object #item.setData(32,this_file) # keep a hidden copy of the file object
item.setToolTip(this_file.getName()) item.setToolTip(this_file.getName())
self.lb.setItem(i,0,item) self.lb.setItem(i,0,item)
#lb.itemAt(i,0).setFlags(Qt.ItemIsEnabled) #NoItemFlags) #lb.itemAt(i,0).setFlags(Qt.ItemIsEnabled) #NoItemFlags)
comment = this_file.getComment()
# get the comment from database & xattrs, either can fail
comment = ""
row = self.db.getData(this_file.getName())
if row and len(row)>0:
comment = row[3]
xattrComment = this_file.getXattrComment()
self.lb.setItem(i,3,QTableWidgetItem(comment)) self.lb.setItem(i,3,QTableWidgetItem(comment))
if xattrComment != comment:
self.lb.item(i,3).setBackground(QBrush(Qt.yellow))
print("got two comments <{}> and <{}>".format(comment, xattrComment))
dt = this_file.getDate() dt = this_file.getDate()
da = SortableTableWidgetItem(DataBase.getShortDate(dt),dt) da = SortableTableWidgetItem(dnDataBase.getShortDate(dt),dt,this_file)
#da.setFont(small_font) #da.setFont(small_font)
da.setToolTip(dt) da.setToolTip(dt)
da.setFlags(QtCore.Qt.ItemIsEnabled) da.setFlags(Qt.ItemIsEnabled)
self.lb.setItem(i,1,da) self.lb.setItem(i,1,da)
si = this_file.getSize() si = this_file.getSize()
if si==FileObj.FILE_IS_DIR: if si==FileObj.FILE_IS_DIR:
sa = SortableTableWidgetItem('',0) sa = SortableTableWidgetItem('',0,this_file)
item.setIcon(dirIcon) item.setIcon(dirIcon)
elif si==FileObj.FILE_IS_LINK: elif si==FileObj.FILE_IS_LINK:
sa = SortableTableWidgetItem('symlink',-1) sa = SortableTableWidgetItem('symlink',-1,this_file)
item.setIcon(linkIcon) item.setIcon(linkIcon)
dst = os.path.realpath(this_file.getName()) dst = os.path.realpath(this_file.getName())
sa.setToolTip("symlink: " + dst) sa.setToolTip("symlink: " + dst)
else: else:
sa = SortableTableWidgetItem(str(si),si) sa = SortableTableWidgetItem(str(si),si,this_file)
item.setIcon(fileIcon) item.setIcon(fileIcon)
sa.setTextAlignment(QtCore.Qt.AlignRight) sa.setTextAlignment(Qt.AlignRight)
sa.setFlags(QtCore.Qt.ItemIsEnabled) sa.setFlags(Qt.ItemIsEnabled)
self.lb.setItem(i,2,sa) self.lb.setItem(i,2,sa)
self.refilling = False self.refilling = False
self.lb.sortingEnabled = True self.lb.sortingEnabled = True
@@ -396,16 +425,17 @@ class DirNotes(QMainWindow):
if self.refilling: if self.refilling:
return return
print("debugging " + x.text() + " r:" + str(x.row()) + " c:" + str(x.column())) print("debugging " + x.text() + " r:" + str(x.row()) + " c:" + str(x.column()))
print(" selected file: "+self.lb.item(x.row(),0).data(32).toPyObject().getName()) print(" selected file: "+self.lb.item(x.row(),0).file_object.getName())
the_file = self.lb.item(x.row(),0).data(32).toPyObject() the_file = self.lb.item(x.row(),0).file_object
print(" and the row file is "+the_file.getName()) print(" and the row file is "+the_file.getName())
r = the_file.setComment(str(x.text())) # TODO: change priority r = the_file.setXattrComment(str(x.text())) # TODO: change priority
if r: if r:
self.db.log(the_file.getName(),x.text()) self.db.setData(the_file.getName(),x.text())
def restore_from_database(self): def restore_from_database(self):
print("restore from database") print("restore from database")
# retrieve the full path name # retrieve the full path name
fileName = str(self.lb.item(self.lb.currentRow(),0).data(32).toPyObject().getName()) fileName = str(self.lb.item(self.lb.currentRow(),0).file_object.getName())
print("using filename: "+fileName) print("using filename: "+fileName)
existing_comment = str(self.lb.item(self.lb.currentRow(),3).text()) existing_comment = str(self.lb.item(self.lb.currentRow(),3).text())
print("restore....existing="+existing_comment+"=") print("restore....existing="+existing_comment+"=")
@@ -431,8 +461,9 @@ if __name__=="__main__":
p.dirname = p.dirname + '/' p.dirname = p.dirname + '/'
print(p.dirname) print(p.dirname)
db = DataBase() db = dnDataBase()
# TODO: if there's a file specified, jump to it
a = QApplication([]) a = QApplication([])
dn = DirNotes(p.dirname,db) dn = DirNotes(p.dirname,db)
dn.show() dn.show()
@@ -471,7 +502,4 @@ user.xdg.publisher
''' commandline xattr ''' commandline xattr
getfattr -h (don't follow symlink) -d (dump all properties) getfattr -h (don't follow symlink) -d (dump all properties)
''' '''
''' qt set color
newitem.setData(Qt.BackgroundRole,QBrush(QColor("yellow")))
'''
''' if the args line contains a file, jump to it ''' ''' if the args line contains a file, jump to it '''