home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / var / lib / python-support / python2.4 / xdg / RecentFiles.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.4 KB  |  192 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''
  5. Implementation of the XDG Recent File Storage Specification Version 0.2
  6. http://standards.freedesktop.org/recent-file-spec
  7. '''
  8. import xml.dom.minidom as xml
  9. import xml.sax.saxutils as xml
  10. import os
  11. import time
  12. import fcntl
  13. from xdg.Exceptions import *
  14.  
  15. class RecentFiles:
  16.     
  17.     def __init__(self):
  18.         self.RecentFiles = []
  19.         self.filename = ''
  20.  
  21.     
  22.     def parse(self, filename = None):
  23.         if not filename:
  24.             filename = os.path.join(os.getenv('HOME'), '.recently-used')
  25.         
  26.         
  27.         try:
  28.             doc = xml.dom.minidom.parse(filename)
  29.         except IOError:
  30.             raise ParsingError('File not found', filename)
  31.         except xml.parsers.expat.ExpatError:
  32.             raise ParsingError('Not a valid .menu file', filename)
  33.  
  34.         self.filename = filename
  35.         for child in doc.childNodes:
  36.             if child.nodeType == xml.dom.Node.ELEMENT_NODE:
  37.                 if child.tagName == 'RecentFiles':
  38.                     for recent in child.childNodes:
  39.                         if recent.nodeType == xml.dom.Node.ELEMENT_NODE:
  40.                             if recent.tagName == 'RecentItem':
  41.                                 self._RecentFiles__parseRecentItem(recent)
  42.                             
  43.                         recent.tagName == 'RecentItem'
  44.                     
  45.                 
  46.             child.tagName == 'RecentFiles'
  47.         
  48.         self.sort()
  49.  
  50.     
  51.     def __parseRecentItem(self, item):
  52.         recent = RecentFile()
  53.         self.RecentFiles.append(recent)
  54.         for attribute in item.childNodes:
  55.             if attribute.nodeType == xml.dom.Node.ELEMENT_NODE:
  56.                 if attribute.tagName == 'URI':
  57.                     recent.URI = attribute.childNodes[0].nodeValue
  58.                 elif attribute.tagName == 'Mime-Type':
  59.                     recent.MimeType = attribute.childNodes[0].nodeValue
  60.                 elif attribute.tagName == 'Timestamp':
  61.                     recent.Timestamp = attribute.childNodes[0].nodeValue
  62.                 elif attribute.tagName == 'Private':
  63.                     recent.Prviate = True
  64.                 elif attribute.tagName == 'Groups':
  65.                     for group in attribute.childNodes:
  66.                         if group.nodeType == xml.dom.Node.ELEMENT_NODE:
  67.                             if group.tagName == 'Group':
  68.                                 recent.Groups.append(group.childNodes[0].nodeValue)
  69.                             
  70.                         group.tagName == 'Group'
  71.                     
  72.                 
  73.             attribute.tagName == 'URI'
  74.         
  75.  
  76.     
  77.     def write(self, filename = None):
  78.         if not filename and not (self.filename):
  79.             raise ParsingError('File not found', filename)
  80.         elif not filename:
  81.             filename = self.filename
  82.         
  83.         f = open(filename, 'w')
  84.         fcntl.lockf(f, fcntl.LOCK_EX)
  85.         f.write('<?xml version="1.0"?>\n')
  86.         f.write('<RecentFiles>\n')
  87.         for r in self.RecentFiles:
  88.             f.write('  <RecentItem>\n')
  89.             f.write('    <URI>%s</URI>\n' % xml.sax.saxutils.escape(r.URI))
  90.             f.write('    <Mime-Type>%s</Mime-Type>\n' % r.MimeType)
  91.             f.write('    <Timestamp>%s</Timestamp>\n' % r.Timestamp)
  92.             if r.Private == True:
  93.                 f.write('    <Private/>\n')
  94.             
  95.             if len(r.Groups) > 0:
  96.                 f.write('    <Groups>\n')
  97.                 for group in r.Groups:
  98.                     f.write('      <Group>%s</Group>\n' % group)
  99.                 
  100.                 f.write('    </Groups>\n')
  101.             
  102.             f.write('  </RecentItem>\n')
  103.         
  104.         f.write('</RecentFiles>\n')
  105.         fcntl.lockf(f, fcntl.LOCK_UN)
  106.         f.close()
  107.  
  108.     
  109.     def getFiles(self, mimetypes = None, groups = None, limit = 0):
  110.         tmp = []
  111.         i = 0
  112.         for item in self.RecentFiles:
  113.             if groups:
  114.                 for group in groups:
  115.                     if group in item.Groups:
  116.                         tmp.append(item)
  117.                         i += 1
  118.                         continue
  119.                 
  120.             elif mimetypes:
  121.                 for mimetype in mimetypes:
  122.                     if mimetype == item.MimeType:
  123.                         tmp.append(item)
  124.                         i += 1
  125.                         continue
  126.                 
  127.             elif item.Private == False:
  128.                 tmp.append(item)
  129.                 i += 1
  130.             
  131.             if limit != 0 and i == limit:
  132.                 break
  133.                 continue
  134.         
  135.         return tmp
  136.  
  137.     
  138.     def addFile(self, item, mimetype, groups = None, private = False):
  139.         if item in self.RecentFiles:
  140.             index = self.RecentFiles.index(item)
  141.             recent = self.RecentFiles[index]
  142.         elif len(self.RecentFiles) == 500:
  143.             self.RecentFiles.pop()
  144.         
  145.         recent = RecentFile()
  146.         self.RecentFiles.append(recent)
  147.         recent.URI = item
  148.         recent.MimeType = mimetype
  149.         recent.Timestamp = int(time.time())
  150.         recent.Private = private
  151.         recent.Groups = groups
  152.         self.sort()
  153.  
  154.     
  155.     def deleteFile(self, item):
  156.         if item in self.RecentFiles:
  157.             self.RecentFiles.remove(item)
  158.         
  159.  
  160.     
  161.     def sort(self):
  162.         self.RecentFiles.sort()
  163.         self.RecentFiles.reverse()
  164.  
  165.  
  166.  
  167. class RecentFile:
  168.     
  169.     def __init__(self):
  170.         self.URI = ''
  171.         self.MimeType = ''
  172.         self.Timestamp = ''
  173.         self.Private = False
  174.         self.Groups = []
  175.  
  176.     
  177.     def __cmp__(self, other):
  178.         return cmp(self.Timestamp, other.Timestamp)
  179.  
  180.     
  181.     def __eq__(self, other):
  182.         if self.URI == str(other):
  183.             return True
  184.         else:
  185.             return False
  186.  
  187.     
  188.     def __str__(self):
  189.         return self.URI
  190.  
  191.  
  192.