Package plugins :: Package fsrules :: Package magic :: Module main |
[hide private]
[frames] | no frames]

# Source Code for Module plugins.fsrules.magic.main

     1  #-*- coding: utf8 -*
     2  #
     3  # Max E. Kuznecov <syhpoon@syhpoon.name> 2009
     4  #
     5  # This file is part of XYZCommander.
     6  # XYZCommander is free software: you can redistribute it and/or modify
     7  # it under the terms of the GNU Lesser Public License as published by
     8  # the Free Software Foundation, either version 3 of the License, or
     9  # (at your option) any later version.
    10  # XYZCommander is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  # GNU Lesser Public License for more details.
    14  # You should have received a copy of the GNU Lesser Public License
    15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
    16
    17  import re
    18  import subprocess
    19
    20  from libxyz.core import FSRule
    21  from libxyz.core.plugins import BasePlugin
    22
    23 -class XYZPlugin(BasePlugin):
    24      "Magic database FSRule"
    25
    26      NAME = u"magic"
    27      AUTHOR = u"Max E. Kuznecov <syhpoon@syhpoon.name>"
    28      VERSION = u"0.1"
    29      BRIEF_DESCRIPTION = u"Magic database FSRule"
    30      FULL_DESCRIPTION = _(u"Plugin extends FSRule functionality to match "\
    31                           u"objects based on magic database description")
    32      NAMESPACE = u"fsrules"
    33      MIN_XYZ_VERSION = None
    34      DOC = None
    35      HOMEPAGE = "http://xyzcmd.syhpoon.name"
    36
    37 -    def __init__(self, xyz):
    38          super(XYZPlugin, self).__init__(xyz)
    39
    40      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    41
    42 -    def prepare(self):
    43          FSRule.extend("magic", self._transform, self._match)
    44
    45      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    46
    47 -    def finalize(self):
    48          FSRule.unextend("magic")
    49
    50      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    51
    52 -    def _transform(self, arg):
    53          """
    54          Transformation function for magic FSRule
    55          """
    56
    57          # Treat input argument as a regular expression to match on
    58          #`file` output
    59
    60          return re.compile(arg, re.I)
    61
    62      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    63
    64 -    def _match(self, vfsobj, arg):
    65          """
    66          Match function for magic
    67          """
    68
    69          try:
    70              result = subprocess.Popen([
    71                  "/usr/bin/env", "file", "-b",
    72                  vfsobj.path], stdout=subprocess.PIPE).communicate()[0].strip()
    73
    74              return arg.search(result) is not None
    75          except Exception:
    76              return False
    77