"""Filesystem import utility""" __version__='$Revision:$'[11:-2] import sys, os, string from Globals import MessageDialog from OFS.Image import File def fsimport(self, fsdir, REQUEST=None): """Import a filesystem directory as a new folder object into the current folder. """ if not os.path.isdir(fsdir): raise ValueError, 'Path does not exist' rcimport(fsdir, self) if REQUEST is not None: return MessageDialog(title='Directory imported.', message='Directory imported.', action='%s/manage_main' % REQUEST['URL1'] ) def rcimport(fsdir, obdir): filenames=os.listdir(fsdir) for name in filenames: pathname=os.path.join(fsdir, name) if name[0] in ('.', '#', '_'): continue if name[-1] in ('~',): continue elif os.path.isfile(pathname): if _isdoc(pathname): file=open(pathname, 'r') data=file.read() file.close() obdir.manage_addDocument(name, '', data) elif _isimg(pathname): file=open(pathname, 'rb') data=file.read() file.close() obdir.manage_addImage(name, data) else: file=open(pathname, 'rb') data=file.read() file.close() fobj=File(name, '', data, content_type=_ftype(pathname)) obdir._setObject(name, fobj) elif os.path.isdir(pathname): obdir.manage_addFolder(name) rcimport(pathname, getattr(obdir, name)) else: __traceback_info__=(fsdir, obdir, name) raise ValueError, name typemap={ 'a': 'application/octet-stream', 'ai': 'application/postscript', 'aif': 'audio/x-aiff', 'aifc': 'audio/x-aiff', 'aiff': 'audio/x-aiff', 'au': 'audio/basic', 'avi': 'video/x-msvideo', 'bcpio': 'application/x-bcpio', 'bin': 'application/octet-stream', 'cdf': 'application/x-netcdf', 'cpio': 'application/x-cpio', 'csh': 'application/x-csh', 'dll': 'application/octet-stream', 'dvi': 'application/x-dvi', 'exe': 'application/octet-stream', 'eps': 'application/postscript', 'etx': 'text/x-setext', 'gif': 'image/gif', 'gtar': 'application/x-gtar', 'gz': 'application/x-gzip', 'hdf': 'application/x-hdf', 'htm': 'text/html', 'html': 'text/html', 'ief': 'image/ief', 'jpe': 'image/jpeg', 'jpeg': 'image/jpeg', 'jpg': 'image/jpeg', 'latex': 'application/x-latex', 'man': 'application/x-troff-man', 'me': 'application/x-troff-me', 'mif': 'application/x-mif', 'mov': 'video/quicktime', 'movie': 'video/x-sgi-movie', 'mpe': 'video/mpeg', 'mpeg': 'video/mpeg', 'mpg': 'video/mpeg', 'ms': 'application/x-troff-ms', 'nc': 'application/x-netcdf', 'o': 'application/octet-stream', 'obj': 'application/octet-stream', 'oda': 'application/oda', 'pbm': 'image/x-portable-bitmap', 'pdf': 'application/pdf', 'pgm': 'image/x-portable-graymap', 'pnm': 'image/x-portable-anymap', 'png': 'image/png', 'ppm': 'image/x-portable-pixmap', 'py': 'text/x-python', 'pyc': 'application/x-python-code', 'ps': 'application/postscript', 'qt': 'video/quicktime', 'ras': 'image/x-cmu-raster', 'rgb': 'image/x-rgb', 'roff': 'application/x-troff', 'rtf': 'application/rtf', 'rtx': 'text/richtext', 'sgm': 'text/x-sgml', 'sgml': 'text/x-sgml', 'sh': 'application/x-sh', 'shar': 'application/x-shar', 'snd': 'audio/basic', 'so': 'application/octet-stream', 'src': 'application/x-wais-source', 'sv4cpio': 'application/x-sv4cpio', 'sv4crc': 'application/x-sv4crc', 't': 'application/x-troff', 'tar': 'application/x-tar', 'tcl': 'application/x-tcl', 'tex': 'application/x-tex', 'texi': 'application/x-texinfo', 'texinfo': 'application/x-texinfo', 'tif': 'image/tiff', 'tiff': 'image/tiff', 'tr': 'application/x-troff', 'tsv': 'text/tab-separated-values', 'txt': 'text/plain', 'ustar': 'application/x-ustar', 'wav': 'audio/x-wav', 'xbm': 'image/x-xbitmap', 'xpm': 'image/x-xpixmap', 'xwd': 'image/x-xwindowdump', 'zip': 'application/zip', 'Z': 'application/x-compress', } def _isdoc(path): ext=string.split(path, '.')[-1] return ext in ('html', 'htm', 'shtml', 'dtml', 'txt') def _isimg(path): ext=string.split(path, '.')[-1] return ext in ('gif', 'jpg', 'png', 'bmp', 'jpeg') def _ftype(path): ext=string.split(path, '.')[-1] if typemap.has_key(ext): return typemap[ext] return 'application/octet-stream'