dirnotes2 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/python3
  2. """ a simple gui or command line app
  3. to view and create/edit file comments
  4. comments are stored in xattr user.xdg.comment
  5. depends on python-pyxattr
  6. because files are so often over-written, save a copy
  7. of the comments in a database ~/.dirnotes.db
  8. these comments stick to the symlink
  9. """
  10. import sys,os,argparse
  11. from dirWidget2 import DirWidget
  12. from tkinter import *
  13. import xattr, sqlite3, time
  14. VERSION = "0.2"
  15. COMMENT_KEY = "user.xdg.comment"
  16. DATABASE_NAME = "~/.dirnotes.db"
  17. # convert the ~/ form to a fully qualified path
  18. DATABASE_NAME = os.path.expanduser(DATABASE_NAME)
  19. class DataBase:
  20. ''' the database is flat
  21. fileName: fully qualified name
  22. st_mtime: a float
  23. size: a long
  24. comment: a string
  25. comment_time: a float, the time of the comment save
  26. this is effectively a log file, as well as a resource for a restore
  27. (in case a file-move is done without comment)
  28. the database is associated with a user, in the $HOME dir
  29. '''
  30. def __init__(self):
  31. '''try to open the database; if not found, create it'''
  32. try:
  33. self.db = sqlite3.connect(DATABASE_NAME)
  34. except sqlite3.OperationalError:
  35. print("Database %s not found" % DATABASE_NAME)
  36. raise OperationalError
  37. c = self.db.cursor()
  38. try:
  39. c.execute("select * from dirnotes")
  40. except sqlite3.OperationalError:
  41. print("Table %s created" % ("dirnotes"))
  42. c.execute("create table dirnotes (name TEXT, date DATETIME, size INTEGER, comment TEXT, comment_date DATETIME)")
  43. def getData(self, fileName):
  44. c = self.db.cursor()
  45. c.execute("select * from dirnotes where name=? order by comment_date desc",(os.path.abspath(fileName),))
  46. return c.fetchone()
  47. def setData(self, fileName, _date, _size, comment):
  48. c = self.db.cursor()
  49. c.execute("insert into dirnotes values (?,?,?,?,?)",
  50. (fileName, _date, _size, comment, time.time()))
  51. self.db.commit()
  52. return True
  53. def log(self, fileName, comment):
  54. ''' TODO: convert filename to canonical '''
  55. c = self.db.cursor()
  56. s = os.stat(fileName)
  57. print ("params: %s %f %d %s %f" % ((os.path.abspath(fileName), s.st_mtime, s.st_size, comment, time.time())))
  58. c.execute("insert into dirnotes values (?,datetime(?,'unixepoch','localtime'),?,?,datetime(?,'unixepoch','localtime'))",
  59. (os.path.abspath(fileName), s.st_mtime, s.st_size, str(comment), time.time()))
  60. self.db.commit()
  61. def parse():
  62. parser = argparse.ArgumentParser(description='dirnotes application')
  63. parser.add_argument('dirname',metavar='dirname',type=str,
  64. help='directory [default=current dir]',default=".",nargs='?')
  65. parser.add_argument('dirname2',help='comparison directory, shows two-dirs side-by-side',nargs='?')
  66. parser.add_argument('-n','--nogui',action="store_const",const="1",
  67. help='use text base interface')
  68. parser.add_argument('-v','--version',action='version',version='%(prog)s '+VERSION)
  69. group = parser.add_mutually_exclusive_group()
  70. group.add_argument('-s','--sort-by-name',metavar='sort',action="store_const",const='n')
  71. group.add_argument('-m','--sort-by-date',metavar='sort',action='store_const',const='d')
  72. return parser.parse_args()
  73. class FileObj():
  74. def __init__(self, fileName):
  75. self.fileName = fileName
  76. # also get the date, directory or not, etc
  77. self.comment = ''
  78. try:
  79. self.comment = xattr.getxattr(fileName,COMMENT_KEY)
  80. except Exception as e:
  81. #print("comment read on %s failed, execption %s" % (self.fileName,e))
  82. pass
  83. def getName(self):
  84. return self.fileName
  85. def getComment(self):
  86. return self.comment
  87. def setComment(self,newComment):
  88. self.comment = newComment
  89. try:
  90. xattr.setxattr(self.fileName,COMMENT_KEY,self.comment)
  91. return True
  92. # we need to move these cases out to a handler
  93. except Exception as e:
  94. print("problem setting the comment on file %s" % (self.fileName,))
  95. if os.access(self.fileName, os.W_OK)!=True:
  96. print("you don't appear to have write permissions on this file")
  97. # change the listbox background to yellow
  98. self.displayBox.notifyUnchanged()
  99. elif "Errno 95" in str(e):
  100. print("is this a VFAT or EXFAT volume? these don't allow comments")
  101. return False
  102. class DirNotes(Frame):
  103. ''' the main window of the app
  104. has 3 list boxes: dir_left, dir_right (may be invisible) and files
  105. '''
  106. def __init__(self, filename, db, parent=None):
  107. super(DirNotes,self).__init__(parent)
  108. self.db = db
  109. win = QWidget()
  110. self.setCentralWidget(win)
  111. lb = QTableWidget()
  112. self.lb = lb
  113. lb.setColumnCount(2)
  114. lb.horizontalHeader().setResizeMode( 1, QHeaderView.Stretch );
  115. lb.verticalHeader().setDefaultSectionSize(20); # thinner rows
  116. lb.verticalHeader().setVisible(False)
  117. # resize the comments column
  118. # and resize the parent window to match the directory size
  119. # allow multiple entries on the line at this point
  120. #d = os.listdir(p.filename[0])
  121. #d.sort()
  122. current, dirs, files = os.walk(filename,followlinks=True).next()
  123. dirs = map(lambda x:x+'/', dirs)
  124. dirs.sort()
  125. files.sort()
  126. d = dirs + files
  127. lb.setRowCount(len(d))
  128. self.files = []
  129. for i in range(len(d)):
  130. this_file = FileObj(current+'/'+d[i])
  131. self.files = self.files + [this_file]
  132. item = QTableWidgetItem(this_file.getName())
  133. item.setFlags(QtCore.Qt.ItemIsEnabled)
  134. lb.setItem(i,0,item)
  135. #lb.itemAt(i,0).setFlags(Qt.ItemIsEnabled) #NoItemFlags)
  136. comment = this_file.getComment()
  137. lb.setItem(i,1,QTableWidgetItem(comment))
  138. lb.setHorizontalHeaderItem(0,QTableWidgetItem("file"))
  139. lb.setHorizontalHeaderItem(1,QTableWidgetItem("comment"))
  140. lb.resizeColumnsToContents()
  141. e = QLabel("View and edit file comments stored in extended attributes user.xdg.comment",win)
  142. b1 = QPushButton("restore from database",win)
  143. dirLeft = DirWidget(current,win)
  144. dirLeft.setMaximumHeight(140)
  145. dirLeft.setMaximumWidth(200)
  146. dirRight = DirWidget(current,win)
  147. dirRight.setMaximumHeight(140)
  148. dirRight.setMaximumWidth(200)
  149. dirRight.setEnabled(False)
  150. layout = QVBoxLayout()
  151. upperLayout = QHBoxLayout()
  152. layout.addWidget(e)
  153. upperLayout.addWidget(dirLeft)
  154. upperLayout.addWidget(b1)
  155. upperLayout.addWidget(dirRight)
  156. layout.addLayout(upperLayout)
  157. layout.addWidget(lb)
  158. win.setLayout(layout)
  159. lb.itemChanged.connect(self.change)
  160. b1.pressed.connect(self.restore_from_database)
  161. QShortcut(QKeySequence("Ctrl+Q"), self, self.close)
  162. self.setWindowTitle("test")
  163. self.setMinimumSize(600,400)
  164. def closeEvent(self,e):
  165. print("closing")
  166. def change(self,x):
  167. print("debugging " + x.text() + " r:" + str(x.row()) + " c:" + str(x.column()))
  168. the_file = dn.files[x.row()]
  169. r = the_file.setComment(str(x.text()))
  170. if r:
  171. self.db.log(the_file.getName(),x.text())
  172. def restore_from_database(self):
  173. print("restore from database")
  174. fileName = str(self.lb.item(self.lb.currentRow(),0).text())
  175. fo_row = self.db.getData(fileName)
  176. if len(fo_row)>1:
  177. comment = fo_row[3]
  178. print(fileName,fo_row[0],comment)
  179. if __name__=="__main__":
  180. p = parse()
  181. if p.dirname[-1]=='/':
  182. p.dirname = p.dirname[:-1]
  183. print(p.dirname)
  184. db = DataBase()
  185. tk_basis = Tk()
  186. dn = DirNotes(tk_basis,p.dirname,db)
  187. dn.pack()
  188. mainloop()
  189. #xattr.setxattr(filename,COMMENT_KEY,commentText)
  190. ''' files from directories
  191. use os.isfile()
  192. os.isdir()
  193. current, dirs, files = os.walk("path").next()
  194. possible set folllowLinks=True'''
  195. ''' notes from the wdrm project
  196. table showed
  197. filename, size, date size, date, desc
  198. at start, fills the list of all the files
  199. skip the . entry
  200. '''
  201. ''' should we also do user.xdg.tags="TagA,TagB" ?
  202. user.charset
  203. user.creator=application_name or user.xdg.creator
  204. user.xdg.origin.url
  205. user.xdg.language=[RFC3066/ISO639]
  206. user.xdg.publisher
  207. '''
  208. ''' to allow column-sorting, you use the sortByColumn and set the Horiz-header to clickable
  209. '''
  210. ''' TODO: also need a way to display-&-restore comments from the database '''
  211. ''' QFileDialog
  212. -make my own?
  213. -existing one has
  214. -history
  215. -back button
  216. -up button
  217. -but we don't need
  218. -directory date
  219. -icon option
  220. -url browser (unless we go network file system)
  221. -new folder button
  222. -file type chooser
  223. -text entry box
  224. -choose & cancel buttons
  225. '''