コード例 #1
0
 def test_urldict_set_error(self, tmpdir):
     tmpfile = tmpdir.join("tmpfile")
     with open(tmpfile, "w") as tfile:
         tfile.write(input_file_content_error)
     with open(tmpfile, "r") as tfile:
         try:
             URLDict.load(tfile)
         except ValueError:
             assert True
         else:
             assert False
コード例 #2
0
    def test_urldict_get(self, tmpdir):
        tmpfile = tmpdir.join("tmpfile")
        with open(tmpfile, "w") as tfile:
            tfile.write(input_file_content)
        with open(tmpfile, "r") as tfile:
            urldict = URLDict.load(tfile)

        f = tflow.tflow(resp=tutils.tresp())
        f.request.url = url
        selection = urldict[f]
        assert "body" in selection[0]
        assert new_content_body in selection[0]["body"]
        assert "title" in selection[0]
        assert new_content_title in selection[0]["title"]

        selection_get = urldict.get(f)
        assert "body" in selection_get[0]
        assert new_content_body in selection_get[0]["body"]
        assert "title" in selection_get[0]
        assert new_content_title in selection_get[0]["title"]

        try:
            urldict["body"]
        except KeyError:
            assert True
        else:
            assert False

        assert urldict.get("body", default="default") == "default"
コード例 #3
0
    def configure(self, updated):
        if self.OPT_MAPPING_FILE in updated:
            self.filename = updated[self.OPT_MAPPING_FILE]
            with open(self.filename, "r") as f:
                self.mapping_templates = URLDict.load(f)

        if self.OPT_MAP_PERSISTENT in updated:
            self.persistent = updated[self.OPT_MAP_PERSISTENT]
コード例 #4
0
    def test_urldict_dumps(self, tmpdir):
        tmpfile = tmpdir.join("tmpfile")
        with open(tmpfile, "w") as tfile:
            tfile.write(input_file_content)
        with open(tmpfile, "r") as tfile:
            urldict = URLDict.load(tfile)

        dump = urldict.dumps()
        assert dump == input_file_content
コード例 #5
0
    def test_urldict_dump(self, tmpdir):
        tmpfile = tmpdir.join("tmpfile")
        outfile = tmpdir.join("outfile")
        with open(tmpfile, "w") as tfile:
            tfile.write(input_file_content)
        with open(tmpfile, "r") as tfile:
            urldict = URLDict.load(tfile)
        with open(outfile, "w") as ofile:
            urldict.dump(ofile)

        with open(outfile, "r") as ofile:
            output = ofile.read()
        assert output == input_file_content
コード例 #6
0
    def __init__(self, filename: str, persistent: bool = False) -> None:
        """ Initializes the mapping add-on

        Args:
            filename: str that provides the name of the file in which the urls and css selectors to mapped content is
                stored.
            persistent: bool that indicates whether to store all new content in the configuration file.

        Example:
            The file in which the mapping config is given should be in the following format:
            {
                "http://10.10.10.10": {
                    "body": "My Text"
                },
                "<URL>": {
                    "<css selector>": "Replace with this"
                }
            }
        """
        self.filename = filename
        self.persistent = persistent
        self.logger = logging.getLogger(self.__class__.__name__)
        with open(filename, "r") as f:
            self.mapping_templates = URLDict.load(f)
コード例 #7
0
 def test_urldict_loads(self):
     urldict = URLDict.loads(input_file_content)
     dump = urldict.dumps()
     assert dump == input_file_content
コード例 #8
0
 def test_urldict_empty(self):
     urldict = URLDict()
     dump = urldict.dumps()
     assert dump == '{}'