from __future__ import division, absolute_import, with_statement, print_function, unicode_literals
try:
    import magic
except:
    print("python-magic module not installed...")
    magic = False

__author__ = "Drew Bonasera"
__license__ = "MPL 2.0"

TYPE = "Metadata"
NAME = "libmagic"
DEFAULTCONF = {
    'magicfile':None,
    'ENABLED': True
    }

def check(conf=DEFAULTCONF):
    if not conf['ENABLED']:
        return False
    if magic:
        return True
    else:
        return False

def scan(filelist, conf=DEFAULTCONF):
    if conf['magicfile']:
        try:
            maaagic = magic.Magic(magic_file=conf['magicfile'])
        except:
            print("ERROR: Failed to use magic file", conf['magicfile'])
            maaagic = magic.Magic()
    else:
        maaagic = magic.Magic()
    results = []
    for fname in filelist:
        results.append((fname, maaagic.from_file(fname).decode(encoding='UTF-8', errors='replace')))

    metadata = {}
    metadata["Name"] = NAME
    metadata["Type"] = TYPE
    metadata["Include"] = False
    return (results, metadata)


from __future__ import division, absolute_import, with_statement, print_function, unicode_literals
try:
    import magic
except:
    print("python-magic module not installed...")
    magic = False

__author__ = "Drew Bonasera"
__license__ = "MPL 2.0"

TYPE = "Metadata"
NAME = "libmagic"
DEFAULTCONF = {
    'magicfile':None,
    'ENABLED': True
    }

def check(conf=DEFAULTCONF):
    if not conf['ENABLED']:
        return False
    if magic:
        return True
    else:
        return False

def scan(filelist, conf=DEFAULTCONF):
    if conf['magicfile']:
        try:
            maaagic = magic.Magic(magic_file=conf['magicfile'])
        except:
            print("ERROR: Failed to use magic file", conf['magicfile'])
            maaagic = magic.Magic()
    else:
        maaagic = magic.Magic()
    results = []
    for fname in filelist:
        results.append((fname, maaagic.from_file(fname).decode('UTF-8', 'replace')))

    metadata = {}
    metadata["Name"] = NAME
    metadata["Type"] = TYPE
    metadata["Include"] = False
    return (results, metadata)


Fix for python 2.6 support
Fix for python 2.6 support

  from __future__ import division, absolute_import, with_statement, print_function, unicode_literals
  try:
      import magic
  except:
      print("python-magic module not installed...")
      magic = False
  
  __author__ = "Drew Bonasera"
  __license__ = "MPL 2.0"
  
  TYPE = "Metadata"
  NAME = "libmagic"
  DEFAULTCONF = {
      'magicfile':None,
      'ENABLED': True
      }
  
  def check(conf=DEFAULTCONF):
      if not conf['ENABLED']:
          return False
      if magic:
          return True
      else:
          return False
  
  def scan(filelist, conf=DEFAULTCONF):
      if conf['magicfile']:
          try:
              maaagic = magic.Magic(magic_file=conf['magicfile'])
          except:
              print("ERROR: Failed to use magic file", conf['magicfile'])
              maaagic = magic.Magic()
      else:
          maaagic = magic.Magic()
      results = []
      for fname in filelist:
-         results.append((fname, maaagic.from_file(fname).decode(encoding='UTF-8', errors='replace')))
+         results.append((fname, maaagic.from_file(fname).decode('UTF-8', 'replace')))
  
      metadata = {}
      metadata["Name"] = NAME
      metadata["Type"] = TYPE
      metadata["Include"] = False
      return (results, metadata)
  
  
Fix for python 2.6 support
## Code Before:
from __future__ import division, absolute_import, with_statement, print_function, unicode_literals
try:
    import magic
except:
    print("python-magic module not installed...")
    magic = False

__author__ = "Drew Bonasera"
__license__ = "MPL 2.0"

TYPE = "Metadata"
NAME = "libmagic"
DEFAULTCONF = {
    'magicfile':None,
    'ENABLED': True
    }

def check(conf=DEFAULTCONF):
    if not conf['ENABLED']:
        return False
    if magic:
        return True
    else:
        return False

def scan(filelist, conf=DEFAULTCONF):
    if conf['magicfile']:
        try:
            maaagic = magic.Magic(magic_file=conf['magicfile'])
        except:
            print("ERROR: Failed to use magic file", conf['magicfile'])
            maaagic = magic.Magic()
    else:
        maaagic = magic.Magic()
    results = []
    for fname in filelist:
        results.append((fname, maaagic.from_file(fname).decode(encoding='UTF-8', errors='replace')))

    metadata = {}
    metadata["Name"] = NAME
    metadata["Type"] = TYPE
    metadata["Include"] = False
    return (results, metadata)


## Instruction:
Fix for python 2.6 support
## Code After:
from __future__ import division, absolute_import, with_statement, print_function, unicode_literals
try:
    import magic
except:
    print("python-magic module not installed...")
    magic = False

__author__ = "Drew Bonasera"
__license__ = "MPL 2.0"

TYPE = "Metadata"
NAME = "libmagic"
DEFAULTCONF = {
    'magicfile':None,
    'ENABLED': True
    }

def check(conf=DEFAULTCONF):
    if not conf['ENABLED']:
        return False
    if magic:
        return True
    else:
        return False

def scan(filelist, conf=DEFAULTCONF):
    if conf['magicfile']:
        try:
            maaagic = magic.Magic(magic_file=conf['magicfile'])
        except:
            print("ERROR: Failed to use magic file", conf['magicfile'])
            maaagic = magic.Magic()
    else:
        maaagic = magic.Magic()
    results = []
    for fname in filelist:
        results.append((fname, maaagic.from_file(fname).decode('UTF-8', 'replace')))

    metadata = {}
    metadata["Name"] = NAME
    metadata["Type"] = TYPE
    metadata["Include"] = False
    return (results, metadata)

