import commands
import mimetypes
from utils import debug as _d
METHOD_MIME = 'mime'
METHOD_MIXED = 'mixed'
METHOD_FILE = 'file'
TYPE_TEXT = 1
TYPE_VIDEO = 2
def mimedetermination(filename):
mimetype = mimetypes.guess_type(filename)[0]
try:
mimetype = mimetype.split("/")[0]
except:
return None
if mimetype.lower() == "video":
return TYPE_VIDEO
if mimetype.lower() == "text":
return TYPE_TEXT
return None
def filedetermination(filename):
output = '\n'.join(commands.getoutput("file %s" % filename).split(":")[1:])
if output.find("MPEG") >= 0 or output.find("AVI") >= 0 or\
output.find("ASF") >= 0 or output.find("OMG video"):
return TYPE_VIDEO
if output.find("text") >= 0:
return TYPE_TEXT
return None
def determineFiletype(filename, method):
"""
Determines type of a file.
method can be:
* mime - mimetype is used, filename and extension counts
* mixed - mimetype. if doesn't work, then file
* file - uses unix `file' binary called through commands.getoutput.
File content counts, not the name
Right now, only text and video types are well supported
"""
type = None
if method == METHOD_MIME or method == METHOD_MIXED:
type = mimedetermination(filename)
if method == METHOD_FILE or (method == METHOD_MIXED and \
type == None):
type = filedetermination(filename)
return type
2004-11-22 11:39:16 CET