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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import tempfile
  5. import shutil
  6. import os
  7. from threading import Lock
  8. from urlparse import urlparse
  9.  
  10. try:
  11.     from cStringIO import StringIO
  12. except ImportError:
  13.     from StringIO import StringIO
  14.  
  15.  
  16. class Serializer(object):
  17.     
  18.     def __init__(self, serializer):
  19.         self.serializer = serializer
  20.         self._Serializer__save_lock = Lock()
  21.  
  22.     
  23.     def _get_store(self):
  24.         return self.serializer.store
  25.  
  26.     
  27.     def _set_store(self, store):
  28.         self.serializer.store = store
  29.  
  30.     store = property(_get_store, _set_store)
  31.     
  32.     def serialize(self, destination = None, format = 'xml', base = None, encoding = None, **args):
  33.         if destination is None:
  34.             stream = StringIO()
  35.             self.serializer.serialize(stream, base = base, encoding = encoding)
  36.             return stream.getvalue()
  37.         if hasattr(destination, 'write'):
  38.             stream = destination
  39.             self.serializer.serialize(stream, base = base, encoding = encoding)
  40.         else:
  41.             location = destination
  42.             
  43.             try:
  44.                 self._Serializer__save_lock.acquire()
  45.                 (scheme, netloc, path, params, query, fragment) = urlparse(location)
  46.                 if netloc != '':
  47.                     print 'WARNING: not saving as location is not a local file reference'
  48.                     return None
  49.                 name = tempfile.mktemp()
  50.                 stream = open(name, 'wb')
  51.                 self.serializer.serialize(stream, base = base, encoding = encoding, **args)
  52.                 stream.close()
  53.                 if hasattr(shutil, 'move'):
  54.                     shutil.move(name, path)
  55.                 else:
  56.                     shutil.copy(name, path)
  57.                     os.remove(name)
  58.             finally:
  59.                 self._Serializer__save_lock.release()
  60.  
  61.  
  62.  
  63.