home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / xdg / RecentFiles.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.9 KB  |  192 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  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.         not (self.filename)
  81.         if not filename:
  82.             filename = self.filename
  83.         
  84.         f = open(filename, 'w')
  85.         fcntl.lockf(f, fcntl.LOCK_EX)
  86.         f.write('<?xml version="1.0"?>\n')
  87.         f.write('<RecentFiles>\n')
  88.         for r in self.RecentFiles:
  89.             f.write('  <RecentItem>\n')
  90.             f.write('    <URI>%s</URI>\n' % xml.sax.saxutils.escape(r.URI))
  91.             f.write('    <Mime-Type>%s</Mime-Type>\n' % r.MimeType)
  92.             f.write('    <Timestamp>%s</Timestamp>\n' % r.Timestamp)
  93.             if r.Private == True:
  94.                 f.write('    <Private/>\n')
  95.             
  96.             if len(r.Groups) > 0:
  97.                 f.write('    <Groups>\n')
  98.                 for group in r.Groups:
  99.                     f.write('      <Group>%s</Group>\n' % group)
  100.                 
  101.                 f.write('    </Groups>\n')
  102.             
  103.             f.write('  </RecentItem>\n')
  104.         
  105.         f.write('</RecentFiles>\n')
  106.         fcntl.lockf(f, fcntl.LOCK_UN)
  107.         f.close()
  108.  
  109.     
  110.     def getFiles(self, mimetypes = None, groups = None, limit = 0):
  111.         tmp = []
  112.         i = 0
  113.         for item in self.RecentFiles:
  114.             if groups:
  115.                 for group in groups:
  116.                     if group in item.Groups:
  117.                         tmp.append(item)
  118.                         i += 1
  119.                         continue
  120.                 
  121.             elif mimetypes:
  122.                 for mimetype in mimetypes:
  123.                     if mimetype == item.MimeType:
  124.                         tmp.append(item)
  125.                         i += 1
  126.                         continue
  127.                 
  128.             elif item.Private == False:
  129.                 tmp.append(item)
  130.                 i += 1
  131.             
  132.             if limit != 0 and i == limit:
  133.                 break
  134.                 continue
  135.         
  136.         return tmp
  137.  
  138.     
  139.     def addFile(self, item, mimetype, groups = None, private = False):
  140.         if item in self.RecentFiles:
  141.             index = self.RecentFiles.index(item)
  142.             recent = self.RecentFiles[index]
  143.         elif len(self.RecentFiles) == 500:
  144.             self.RecentFiles.pop()
  145.         
  146.         recent = RecentFile()
  147.         self.RecentFiles.append(recent)
  148.         recent.URI = item
  149.         recent.MimeType = mimetype
  150.         recent.Timestamp = int(time.time())
  151.         recent.Private = private
  152.         recent.Groups = groups
  153.         self.sort()
  154.  
  155.     
  156.     def deleteFile(self, item):
  157.         if item in self.RecentFiles:
  158.             self.RecentFiles.remove(item)
  159.         
  160.  
  161.     
  162.     def sort(self):
  163.         self.RecentFiles.sort()
  164.         self.RecentFiles.reverse()
  165.  
  166.  
  167.  
  168. class RecentFile:
  169.     
  170.     def __init__(self):
  171.         self.URI = ''
  172.         self.MimeType = ''
  173.         self.Timestamp = ''
  174.         self.Private = False
  175.         self.Groups = []
  176.  
  177.     
  178.     def __cmp__(self, other):
  179.         return cmp(self.Timestamp, other.Timestamp)
  180.  
  181.     
  182.     def __eq__(self, other):
  183.         if self.URI == str(other):
  184.             return True
  185.         return False
  186.  
  187.     
  188.     def __str__(self):
  189.         return self.URI
  190.  
  191.  
  192.