Exemplo n.º 1
0
def test_pairs(code, exception):
    stamp = datetime.now().timestamp()
    testfile = FIXTURE_PATH.joinpath(f"test-{stamp}.py")
    testfile.write_text(code)
    plugin = Python()
    with exception:
        try:
            next(plugin.pairs(testfile))
        except Exception:
            raise
        finally:
            remove(testfile.as_posix())
Exemplo n.º 2
0
 def load_plugin(self) -> Optional[object]:
     """Loads the correct plugin for a given file"""
     if not self.filepath.exists():
         return None
     elif not self.filepath.is_file():
         return None
     elif self.filepath.stat().st_size < 7:
         return None
     elif self.filepath.suffix in [".dist", ".template"]:
         self.filename = self.filepath.stem
         self.filetype = self.filename.split(".")[-1]
     if self.filetype in ["yaml", "yml"]:
         return Yml()
     elif self.filetype == "json":
         return Json()
     elif self.filetype == "xml":
         return Xml()
     elif self.filetype.startswith("npmrc"):
         return Npmrc()
     elif self.filetype.startswith("pypirc"):
         return Pypirc()
     elif self.filepath.name == "pip.conf":
         return Pip()
     elif self.filetype in [
             "conf", "cfg", "config", "ini", "env", "credentials"
     ]:
         if self.filepath.open("r").readline().startswith("<?xml "):
             return Xml()
         else:
             return Config()
     elif self.filetype == "properties":
         return Jproperties()
     elif self.filetype.startswith(("sh", "bash", "zsh", "env")):
         return Shell()
     elif self.filepath.name.startswith("Dockerfile"):
         return Dockerfile()
     elif self.filetype.startswith("htpasswd"):
         return Htpasswd()
     elif self.filetype == "txt":
         return Plaintext()
     elif self.filetype.startswith("htm"):
         return Html()
     elif self.filetype == "py":
         return Python()
     elif self.filetype == "js":
         return Javascript()
     elif self.filetype == "java":
         return Java()
     elif self.filetype == "go":
         return Go()
     elif self.filetype.startswith("php"):
         return Php()
     return None
Exemplo n.º 3
0
class WhisperPlugins:
    def __init__(self, filename: str):
        """Inits the correct plugin for a given filename"""
        self.filename = filename
        self.filepath = Path(filename)
        self.filetype = self.filepath.name.split(".")[-1]
        self.plugin = None
        if not self.filepath.exists():
            self.plugin = None
        elif not self.filepath.is_file():
            self.plugin = None
        elif self.filepath.stat().st_size < 7:
            self.plugin = None
        elif self.filetype in ["yaml", "yml"]:
            self.plugin = Yml()
        elif self.filetype == "json":
            self.plugin = Json()
        elif self.filetype == "xml":
            self.plugin = Xml()
        elif self.filetype.startswith("npmrc"):
            self.plugin = Npmrc()
        elif self.filetype.startswith("pypirc"):
            self.plugin = Pypirc()
        elif self.filepath.name == "pip.conf":
            self.plugin = Pip()
        elif self.filetype in ["conf", "cfg", "config", "ini", "env", "credentials"]:
            if self.filepath.open("r").readline().startswith("<?xml "):
                self.plugin = Xml()
            else:
                self.plugin = Config()
        elif self.filetype.startswith(("sh", "bash", "zsh", "env")):
            self.plugin = Shell()
        elif self.filepath.name.startswith("Dockerfile"):
            self.plugin = Dockerfile()
        elif self.filetype.startswith("htpasswd"):
            self.plugin = Htpasswd()
        elif self.filetype == "txt":
            self.plugin = Plaintext()
        elif self.filetype == "py":
            self.plugin = Python()

    def pairs(self):
        if self.plugin:
            yield from self.plugin.pairs(self.filepath)
Exemplo n.º 4
0
def test_traverse_parse(code, key, value, exception):
    plugin = Python()
    ast = astroid.parse(code)
    pairs = plugin.traverse(ast)
    with exception:
        assert next(pairs) == (key, value)
Exemplo n.º 5
0
def test_is_value(value, expectation):
    plugin = Python()
    assert plugin.is_value(value) == expectation
Exemplo n.º 6
0
def test_is_key(key, expectation):
    plugin = Python()
    assert plugin.is_key(key) == expectation
Exemplo n.º 7
0
    def load_plugin(self) -> Optional[object]:
        """Loads the correct plugin for a given file"""

        # First, check if the file is binary
        try:
            for line in self.filepath.open("r"):
                continue
        except Exception as e:
            if guess_type(self.filepath) == (None, None):
                return Bin()

        if not self.filepath.exists():
            return None
        elif not self.filepath.is_file():
            return None
        elif self.filepath.stat().st_size < 7:
            return None
        elif self.filepath.suffix in [".dist", ".template"]:
            self.filename = self.filepath.stem
            self.filetype = self.filename.split(".")[-1]
        if self.filetype in ["yaml", "yml"]:
            return Yml()
        elif self.filetype == "json":
            return Json()
        elif self.filetype == "xml":
            return Xml()
        elif self.filetype.startswith("npmrc"):
            return Npmrc()
        elif self.filetype.startswith("pypirc"):
            return Pypirc()
        elif self.filepath.name == "pip.conf":
            return Pip()
        elif self.filetype in [
                "conf", "cfg", "config", "ini", "env", "credentials"
        ]:
            if self.filepath.open("r").readline().startswith("<?xml "):
                return Xml()
            else:
                return Config()
        elif self.filetype == "properties":
            return Jproperties()
        elif self.filetype.startswith(("sh", "bash", "zsh", "env")):
            # This is less restrictive, since considers a fs/usr/bin/sha384sum filetype as sha384sum
            return Shell()
        elif self.filepath.name.startswith("Dockerfile"):
            return Dockerfile()
        elif self.filetype.startswith("htpasswd"):
            return Htpasswd()
        elif self.filetype == "txt":
            return Plaintext()
        elif self.filetype == "py":
            return Python()
        elif self.filetype == "js":
            return Javascript()
        elif self.filetype == "java":
            return Java()
        elif self.filetype == "go":
            return Go()
        elif self.filetype.startswith("php"):
            return Php()
        return None