Example #1
0
 def createRequest(self, method):
     if self.route_set:
         route = self.route_set[:]
         first_route = AddressHeader.parse(self.route_set[0])
         if not first_route.uri.lr:
             ruri = first_route.uri
             route.append(self.remote_target_uri)
         else:
             ruri = URI.parse(self.remote_target_uri)
     else:
         ruri = URI.parse(self.remote_target_uri)
         route = None
     request = Request(method, ruri)
     request.dialog = self
     h = request.headers
     if route:
         h['route'] = [AddressHeader.parse(s) for s in route]
     h['to'] = AddressHeader(uri=URI.parse(self.remote_uri),
                             params={'tag': self.remote_tag})
     h['from'] = AddressHeader(uri=URI.parse(self.local_uri),
                               params={'tag': self.local_tag})
     h['call-id'] = self.callid
     h['cseq'] = CSeqHeader(self.local_cseq, method)
     # XXX: follow 12.2.1.1 from rfc3261
     h['contact'] = AddressHeader.parse(self.local_target_uri)
     return request
Example #2
0
def load_meta(img_file: Path) -> ImageMetadata:
    """
    Load the metadata tuple for a given image file.

    If no metadata file is present, or it is currently inaccessible, return a blank metadata tuple.

    :arg img_file: a path pointing to a managed image for which we want to load metadata
    :return: the associated metadata as a tuple, or a blank metadata tuple
    """

    meta_file = _construct_metadata_path(img_file)

    if meta_file.exists():
        try:
            with meta_file.open() as mf:
                metadata = parse_xml(mf.read())

                # Check if 'file' is a valid URI, otherwise make it so (for retro-compatibility with older schema)
                if metadata.file.scheme is None:
                    metadata = _old_to_new_schema(img_file, metadata)
        except (OSError, ParseError):
            metadata = ImageMetadata(uuid3(NAMESPACE_URL, str(URI(img_file))),
                                     URI(img_file), None, None, None, None)
    else:
        metadata = ImageMetadata(uuid3(NAMESPACE_URL, str(URI(img_file))),
                                 URI(img_file), None, None, None, None)

    return metadata
Example #3
0
    def test_collective_disjunctive_filter(self):
        # Verify the effectiveness of multi-valued matchers, when evaluated in disjunction
        chars1 = ["al", "john", "jack"]
        chars2 = ["jm", "jr"]
        chars3 = ["jr"]

        el1 = ImageMetadata(uuid4(), URI("a.png"), "ghi", None, chars1, None)
        el2 = ImageMetadata(uuid4(), URI("b.png"), "nsh", None, chars2, None)
        el3 = ImageMetadata(uuid4(), URI("c.png"), "ShT", None, chars3, None)

        filter_builder = FilterBuilder()

        # Test disjunctive filtering with inclusion
        f = filter_builder.character_constraint("jm").character_constraint("al").characters_as_disjunctive(True) \
            .get_character_filter()
        self.assertTrue(f(el1))
        self.assertTrue(f(el2))
        self.assertFalse(f(el3))

        # Test disjunctive filtering with exclusion
        f = filter_builder.character_constraint("jack", True).get_character_filter()
        self.assertTrue(f(el1))
        self.assertTrue(f(el2))
        self.assertTrue(f(el3))

        filter_builder = FilterBuilder()
        f = filter_builder.characters_as_disjunctive(True).character_constraint("john", True) \
            .character_constraint("jack", True).get_character_filter()
        self.assertFalse(f(el1))
        self.assertTrue(f(el2))
        self.assertTrue(f(el3))
Example #4
0
    def test_single_element_filter(self):
        # Verify the effectiveness of single-valued matchers
        id1, filename1, author1, universe1 = uuid4(), URI("01.png"), "bdhnd", "fotwf"
        id2, filename2, author2, universe2 = uuid4(), URI("02.png"), "shndl", None
        id3, filename3, author3, universe3 = uuid4(), URI("03.png"), "okn", "ph"

        el1 = ImageMetadata(id1, filename1, author1, universe1, None, None)
        el2 = ImageMetadata(id2, filename2, author2, universe2, None, None)
        el3 = ImageMetadata(id3, filename3, author3, universe3, None, None)

        filter_builder = FilterBuilder()

        # Test constraints satisfied
        filter_builder.filename_constraint(filename1.path.name) \
            .filename_constraint(filename2.path.name) \
            .filename_constraint(filename3.path.name)
        filename_filter = filter_builder.get_filename_filter()
        self.assertTrue(filename_filter(el1))
        self.assertTrue(filename_filter(el2))
        self.assertTrue(filename_filter(el3))

        # Test implicit exclusion
        filter_builder.author_constraint(author1)
        author_filter = filter_builder.get_author_filter()
        self.assertTrue(author_filter(el1))
        self.assertFalse(author_filter(el2))
        self.assertFalse(author_filter(el3))

        # Test explicit exclusion
        filter_builder.id_constraint(str(id2), True)
        id_filter = filter_builder.get_id_filter()
        self.assertTrue(id_filter(el1))
        self.assertFalse(id_filter(el2))
        self.assertTrue(id_filter(el3))
Example #5
0
    def parse(self, sourceExpressionString):
        """Parses the given 'sourceExpressionString' according to the parameters set in this object. Returns the
        appropriate type of SourceExpression, or SourceExpression.INVALID() in case of a parsing error."""
        sourceExpressionString = sourceExpressionString.strip()

        if sourceExpressionString.lower() == "'unsafe-eval'":
            return SourceExpression.UNSAFE_EVAL()
        elif sourceExpressionString.lower() == "'unsafe-inline'":
            return SourceExpression.UNSAFE_INLINE()
        elif sourceExpressionString.lower() == "'self'":
            return SelfSourceExpression.SELF()
        elif sourceExpressionString == "*":
            return URISourceExpression(None, "*", None, None)

        sourceExpressionParsedAsURI = self._parser.parse(
            sourceExpressionString)
        if sourceExpressionParsedAsURI == URI.EMPTY(
        ) or sourceExpressionParsedAsURI == URI.INVALID():
            return SourceExpression.INVALID()
        if sourceExpressionParsedAsURI.getScheme(
        ) is not None and sourceExpressionParsedAsURI.getScheme().lower(
        ) not in self._knownSchemes:
            return SourceExpression.INVALID()

        port = sourceExpressionParsedAsURI.getPort()
        if port not in ('*', None):  # convert port if it should be a number
            port = int(sourceExpressionParsedAsURI.getPort())
        return URISourceExpression(sourceExpressionParsedAsURI.getScheme(),
                                   sourceExpressionParsedAsURI.getHost(), port,
                                   sourceExpressionParsedAsURI.getPath())
Example #6
0
 def matches(self,
             resourceURI,
             protectedDocumentURI,
             schemePortMappings=defaults.schemePortMappings):
     """
     Returns whether the given resourceURI matches this source expression.
     
     'resourceURI' is an URI object corresponding to the resource that is attempted to be loaded/executed.
     Can be either one of the special URI.UNSAFE_EVAL() / URI.UNSAFE_INLINE() URIs, or a regular URI.
     In the latter case, escaped characters in the path of the URI should already have been decoded. 
     If 'resourceURI' designates a directory (as opposed to a file), its path must end with a '/'. 
     May not be None.
     
     'protectedDocumentURI' is the URI of the document in the context of which 'resourceURI' is being 
     attempted to be loaded/executed (the host document). May not be None.
     
     'schemePortMappings': A dictionary with mappings from (lowercase) scheme names to the corresponding
     default port. Will be used if ports are missing in the 'resourceURI' or 'protectedDocumentURI'. 
     
     This implementation requires schemes to be present in both URIs, and either port numbers or a successful
     scheme-to-port-number look up in 'schemePortMappings' for both URIs (otherwise, False is returned).
     For details about the implementation, see http://www.w3.org/TR/2014/WD-CSP11-20140211/#matching
     """
     if self == SourceExpression.UNSAFE_EVAL() and resourceURI == URI.EVAL(
     ):
         return True
     elif self == SourceExpression.UNSAFE_INLINE(
     ) and resourceURI == URI.INLINE():
         return True
     return False
def test_issue_003_path_on_path_division():
	base = URI("http://ats.example.com/job/listing")
	
	# scrape the listing, identify a job URL from that listing
	target = URI("detail/sample-job")  # oh no, it's relative!
	
	# And it's resolved.
	assert str(base / target) == "http://ats.example.com/job/detail/sample-job"
def test_issue_003_path_like_division_trailing():
	base = URI("http://example.com/foo/")
	assert str(base) == "http://example.com/foo/"
	assert str(base / "bar.html") == "http://example.com/foo/bar.html"
	
	base = URI("http://example.com/foo")
	assert str(base) == "http://example.com/foo"
	assert str(base / "bar.html") == "http://example.com/bar.html"
Example #9
0
    def test_empty_filter_single(self):
        # Check the effects of the absence of constraints on single-valued matchers
        el1 = ImageMetadata(uuid4(), URI('xxx'), None, None, None, None)
        el2 = ImageMetadata(uuid4(), URI('kkk'), None, None, None, None)

        empty_filter = FilterBuilder().get_id_filter()
        self.assertTrue(empty_filter(el1))
        self.assertTrue(empty_filter(el2))
Example #10
0
    def test_none_match_collective(self):
        # Check the effects of None constraints on multi-valued matchers
        el1 = ImageMetadata(uuid4(), URI('aaa'), None, None, None, None)
        el2 = ImageMetadata(uuid4(), URI('yyy'), None, None, None, ["fta"])

        none_filter = FilterBuilder().tag_constraint(None).get_tag_filter()
        self.assertTrue(none_filter(el1))
        self.assertFalse(none_filter(el2))
Example #11
0
    def test_none_match_single(self):
        # Check the effects of None constraints on single-valued matchers
        el1 = ImageMetadata(uuid4(), URI('fff'), None, 'u', None, None)
        el2 = ImageMetadata(uuid4(), URI('zzz'), None, None, None, None)

        none_filter = FilterBuilder().universe_constraint(None).get_universe_filter()
        self.assertFalse(none_filter(el1))
        self.assertTrue(none_filter(el2))
Example #12
0
    def test_empty_filter_collective(self):
        # Check the effects of the absence of constraints on multi-valued matchers
        el1 = ImageMetadata(uuid4(), URI('xxx'), None, None, None, ["nl", "ll"])
        el2 = ImageMetadata(uuid4(), URI('kkk'), None, None, None, None)

        empty_filter = FilterBuilder().get_tag_filter()
        self.assertTrue(empty_filter(el1))
        self.assertTrue(empty_filter(el2))
Example #13
0
def test_link_init() -> None:
    """Can init a new link."""
    sut = Link(href=URI("/test"))
    assert sut is not None
    assert sut.href == URI("/test")

    sut = Link(href=URI("/test"), rel=["self"])
    assert sut is not None
    assert sut.href == URI("/test")
    assert sut.rel == ["self"]
Example #14
0
def test_mixin_add_link() -> None:
    """Can add a new entry."""
    sut = LinksMixin()
    sut.add_link(key="link1", link=Link(href=URI("/test1")))
    sut.add_link(key="link2", link=Link(href=URI("/test2")))
    assert sut.links is not None
    assert sut.links["link1"] is not None
    assert sut.links["link1"].href == URI("/test1")
    assert sut.links["link2"] is not None
    assert sut.links["link2"].href == URI("/test2")
Example #15
0
    def to_uri(set_uri: str, scheme=None):
        AssertUtils.assert_not_null("to_uri", set_uri)

        if scheme:
            if not set_uri.startswith("/"):
                set_uri = "/" + set_uri
            uri_p = URI(
                scheme=scheme,
                path=set_uri.replace(" ", "%20"))
        else:
            uri_p = URI(set_uri.replace(" ", "%20"))
        return uri_p if uri_p.scheme else URI(
            scheme="string",
            path=set_uri.replace(" ", "%20"))
Example #16
0
def _old_to_new_schema(img_path: Path, old_meta: ImageMetadata):
    return ImageMetadata(img_id=old_meta.img_id,
                         file=URI(img_path),
                         author=old_meta.author,
                         universe=old_meta.universe,
                         characters=old_meta.characters,
                         tags=old_meta.tags)
def test_issue_003_path_like_division_operators():
	base = URI("http://example.com/foo/bar.html")
	assert str(base / "baz.html") == 'http://example.com/foo/baz.html'
	assert str(base // "cdn.example.com" / "baz.html") == 'http://cdn.example.com/baz.html'
	assert str(base / "/diz") == 'http://example.com/diz'
	assert str(base / "#diz") == 'http://example.com/foo/bar.html#diz'
	assert str(base / "https://example.com") == 'https://example.com/'
Example #18
0
def parse_xml(data: str) -> ImageMetadata:
    """Parse an XML containing image metadata.

    :param data: a string containing valid image metadata
    :return: an image metadata object"""

    image_elem = ElTree.fromstring(data)
    img_id = image_elem.get('id')
    file = image_elem.get('file')

    # If we were presented with a legacy XML not containing 'file', use the legacy name 'filename'
    if file is None:
        file = image_elem.get('filename')

    author = image_elem.find("./author")
    universe = image_elem.find("./universe")
    characters = [
        char.text for char in image_elem.findall("./characters/character")
    ]
    tags = [tag.text for tag in image_elem.findall("./tags/tag")]

    return ImageMetadata(
        img_id=UUID(img_id),
        file=URI(file),
        author=author.text if author is not None else None,
        universe=universe.text if universe is not None else None,
        characters=characters if len(characters) != 0 else None,
        tags=tags if len(tags) != 0 else None)
Example #19
0
 def parse(cls, s):
     if '\r\n\r\n' not in s:
         raise MessageParsingError("Bad message (no CRLFCRLF found)")
     head, content = s.split('\r\n\r\n', 1)
     if not content:
         content = None
     if '\r\n' not in head:
         first_line = head
         hdrs = ''
     else:
         first_line, hdrs = head.split('\r\n', 1)
     headers = Headers.parse(hdrs)
     r = first_line.split(None, 2)
     if len(r) != 3:
         raise MessageParsingError("Bad first line: " + first_line)
     if r[0] == cls.version:
         code = int(r[1])
         reason = r[2]
         return Response(code, reason, headers, content)
     elif r[2] == cls.version:
         method = r[0]
         uri = URI.parse(r[1])
         return Request(method, uri, headers, content)
     else:
         raise MessageParsingError("Bad first line: " + first_line)
Example #20
0
 def parse(cls, s):
     display_name, uri, rest = cls._parse_nameaddr(s)
     if rest:
         params = cls._parse_params(rest)
     else:
         params = None
     uri = URI.parse(uri)
     return cls(display_name, uri, params)
Example #21
0
 def parse(cls, s):
     display_name, uri, rest = cls._parse_nameaddr(s)
     if rest:
         params = cls._parse_params(rest)
     else:
         params = None
     uri = URI.parse(uri)
     return cls(display_name, uri, params)
Example #22
0
class CloudAppClient:
    base: ClassVar[URI] = URI('https://my.cl.ly')
    serialization: ClassVar[
        str] = "application/json"  # Used for Accept and Content-Type headers.

    session: Session

    def __init__(self) -> None:
        """Initialize the client interface."""

        super().__init__()

        self.session = Session()

        self.session.headers['User-Agent'] = 'Ruby.CloudApp.API'
        self.session.headers['Accept'] = self.serialization
        self.session.headers['Content-Type'] = self.serialization

        if 'CLOUDAPP_USER' in environ:
            self.authenticate(environ['CLOUDAPP_USER'],
                              environ['CLOUDAPP_PASSWORD'])

    def authenticate(self, email: str, password: str):  # -> CloudAppClient:
        """Preserve authentication credentials for later use by RPC calls."""

        self.session.headers.pop('Authorization', None)
        self.session.auth = HTTPDigestAuth(email, password)

        return self

    # Internal Mechanisms

    def __call__(self, path: str, method='get', **params):
        """Issue an API call."""
        uri: URI = self.base / path
        return self.session.request(method, uri, params)

    def __getitem__(self, slug: str) -> Drop:
        """Retrieve a Drop by slug."""

        return Drop(slug, self)

    def __iter__(self) -> Iterable[Drop]:
        """Iterate all known drops."""

        return Drop[self]

    def _parse_errors(self, result):
        if isinstance(result, Mapping):
            return [f"{k}: {v}" for k, v in result.items()]

        if isinstance(result, str):
            return [result]

        if isinstance(result, Collection):
            return result

        return []
Example #23
0
    def meta_extractor(v: View) -> ImageMetadata:
        characters = v.get_characters()
        characters = characters.split(', ') if characters is not None else None

        tags = v.get_tags()
        tags = tags.split(', ') if tags is not None else None

        return ImageMetadata(v.image_id, URI(v._image_path), v.get_author(),
                             v.get_universe(), characters, tags)
Example #24
0
    def write(self) -> None:
        """
        Persist the updated metadata.

        :raise OSError: when the metadata file couldn't be opened
        """

        meta_obj = ImageMetadata(self._id, URI(self._image_path), self.author,
                                 self.universe, self.characters, self.tags)
        write_meta(meta_obj, self._image_path)
Example #25
0
def test_mixin_init() -> None:
    """Can init a new mixin."""
    sut = LinksMixin()
    assert sut is not None

    link = Link(href=URI("/test"))
    sut = LinksMixin(links={"self": link})
    assert sut is not None
    assert sut.links is not None
    assert sut.links["self"] == link
Example #26
0
    def test_legacy_load(self):
        image_uri = URI(self.test_path / "test.png")
        with (self.test_path / "test.xml").open('w') as f:
            f.write(
                "<image id=\"97ed6183-73a0-46ea-b51d-0721b0fbd357\" filename=\"test.png\"></image>"
            )

        loaded = load_meta(Path(image_uri.path))
        self.assertEqual(
            ImageMetadata(UUID('97ed6183-73a0-46ea-b51d-0721b0fbd357'),
                          image_uri, None, None, None, None), loaded)
Example #27
0
 def resolve(set_uri: URI, target: str):
     try:
         uri_s = str(set_uri)
         set_uri = PathUtils.to_uri(uri_s + ("" if uri_s.endswith("/") else "/"))
     except AssertException:
         return uri
     else:
         target = parse.quote(
             target,
             encoding=SystemUtils.LOCAL_ENCODING)
         return set_uri.resolve(target.replace("+", "%20").replace("%21", "!").replace("%27", "'").replace(
             "%28", "(").replace("%29", ")").replace("%7E", "~"))
Example #28
0
 def createResponse(self, code=None, reason=None):
     response = Response(code, reason)
     response.transaction = self.transaction
     for x in ('from', 'to', 'call-id', 'cseq', 'via'):
         response.headers[x] = deepcopy(self.headers[x])
     if not self.has_totag:
         response.headers['to'].params['tag'] = self.response_totag
     if self.dialog:
         if 'record-route' in self.headers:
             response.headers['record-route'] = self.headers['record-route']
         contact_uri = URI.parse(self.dialog.local_target_uri)
         response.headers['contact'] = AddressHeader(uri=contact_uri)
     return response
Example #29
0
    def test_collective_conjunctive_filter(self):
        # Verify the effectiveness of multi-valued matchers, when evaluated in conjunction
        tags1 = ["y", "an", "hry"]
        tags2 = ["an", "mb", "sty", "rp"]
        tags3 = ["ll", "vnl"]

        el1 = ImageMetadata(uuid4(), URI("a.png"), "ghi", None, None, tags1)
        el2 = ImageMetadata(uuid4(), URI("b.png"), "nsh", None, None, tags2)
        el3 = ImageMetadata(uuid4(), URI("c.png"), "ShT", None, None, tags3)

        filter_builder = FilterBuilder()

        # Test conjunctive filtering with inclusion
        f = filter_builder.tag_constraint("an").get_tag_filter()
        self.assertTrue(f(el1))
        self.assertTrue(f(el2))
        self.assertFalse(f(el3))

        # Test conjunctive filtering with exclusion
        f = filter_builder.tag_constraint("y", True).get_tag_filter()
        self.assertFalse(f(el1))
        self.assertTrue(f(el2))
        self.assertFalse((f(el3)))
Example #30
0
    def test_load_actual(self):
        image_uri = URI(self.test_path / "test.png")
        with (self.test_path / "test.xml").open('w') as f:
            f.write(
                "<image id=\"97ed6183-73a0-46ea-b51d-0721b0fbd357\" file=\"" +
                str(image_uri) + "\">" +
                "<author>a</author><universe>u</universe>" +
                "<characters><character>x</character><character>y</character></characters>"
                + "<tags><tag>f</tag><tag>a</tag></tags></image>")

        self.assertEqual(
            ImageMetadata(UUID('97ed6183-73a0-46ea-b51d-0721b0fbd357'),
                          image_uri, "a", "u", ["x", "y"], ["f", "a"]),
            load_meta(Path(self.test_dir.name) / "test.png"))
Example #31
0
 def createRequest(self, method):
     if self.route_set:
         route = self.route_set[:]
         first_route = AddressHeader.parse(self.route_set[0])
         if not first_route.uri.lr:
             ruri = first_route.uri
             route.append(self.remote_target_uri)
         else:
             ruri = URI.parse(self.remote_target_uri)
     else:
         ruri = URI.parse(self.remote_target_uri)
         route = None
     request = Request(method, ruri)
     request.dialog = self
     h = request.headers
     if route:
         h['route'] = [AddressHeader.parse(s) for s in route]
     h['to'] = AddressHeader(uri=URI.parse(self.remote_uri), params={'tag': self.remote_tag})
     h['from'] = AddressHeader(uri=URI.parse(self.local_uri), params={'tag': self.local_tag})
     h['call-id'] = self.callid
     h['cseq'] = CSeqHeader(self.local_cseq, method)
     # XXX: follow 12.2.1.1 from rfc3261
     h['contact'] = AddressHeader.parse(self.local_target_uri)
     return request
Example #32
0
    def test_metadata_read(self):
        # Write some metadata for one of the images
        meta1 = ImageMetadata(
            img_id=UUID('f32ed6ad-1162-4ea6-b243-1e6c91fb7eda'),
            file=URI(self.test_path / '01.png'),
            author="a",
            universe="p",
            characters=["x", "y"],
            tags=["t", "f"])
        write_meta(meta1, (self.test_path / '01.png'))

        specimen = GtkView(self.test_path)

        # Collect metadata from the specimen
        results = dict()
        for _ in range(0, 2):
            specimen.load_next()
            results[specimen.filename] = TestView.meta_extractor(specimen)

        self.assertEqual(meta1, results["01.png"])
        self.assertEqual(
            ImageMetadata(results["02.png"].img_id,
                          URI(self.test_path / "02.png"), None, None, None,
                          None), results["02.png"])
Example #33
0
    def test_store(self):
        image_uri = URI(self.test_path / "test.png")
        write_meta(
            ImageMetadata(UUID('97ed6183-73a0-46ea-b51d-0721b0fbd357'),
                          image_uri, "a", "u", ["x", "y"], ["f", "a"]),
            Path(self.test_dir.name) / "test.png")

        with (self.test_path / "test.xml") as f:
            result = f.read_text()

        self.assertEqual(
            "<image id=\"97ed6183-73a0-46ea-b51d-0721b0fbd357\" file=\"" +
            str(image_uri) + "\">" +
            "<author>a</author><universe>u</universe>" +
            "<characters><character>x</character><character>y</character></characters>"
            + "<tags><tag>f</tag><tag>a</tag></tags></image>", result)
Example #34
0
    def load(self, id):
        """
        Takes a SHA-1 id
        """
        cursor = self.db.cursor()

        sql = """SELECT
        uri, location, status, created, updated
        FROM urldammit_uris WHERE id = %s"""
        cursor.execute(sql, (id,))
        row = cursor.fetchone()

        if not row:
            return None

        data = {}
        data["uri"] = None
        if isinstance(row[0], unicode):
            data["uri"] = row[0].encode("utf8")

        data["location"] = None
        if isinstance(row[1], unicode):
            data["location"] = row[1].encode("utf8")

        data["status"] = int(row[2])
        data["created"] = row[3]
        data["updated"] = row[4]

        data["tags"] = None
        sql = "SELECT tag FROM urldammit_tags WHERE id = %s"
        cursor.execute(sql, (id,))
        rows = cursor.fetchall()
        for row in rows:
            if data["tags"] == None:
                data["tags"] = []
            data["tags"].append(row[0].encode("utf8"))

        data["pairs"] = None
        sql = "SELECT pair_key, pair_value FROM urldammit_pairs WHERE id = %s"
        cursor.execute(sql, (id,))
        rows = cursor.fetchall()
        for row in rows:
            if data["pairs"] == None:
                data["pairs"] = dict()
            data["pairs"][row[0].encode("utf8")] = row[1].encode("utf8")

        return URI.load(data)
Example #35
0
    def parseJsonDict(self, jsonReport):
        """
        Parses the given 'jsonReport' according to the parameters set in the constructor of this ReportParser 
        and returns a Report object. 'jsonReport' is expected to be a Python dict object with attribute names
        and values corresponding to the definition of CSP violation reports. If 'jsonReport' cannot be parsed 
        because it is syntactically invalid (or empty), Report.INVALID() will be returned.

        Depending on the configuration of this ReportParser object, some attributes will be parsed to replace their
        plain string values with a more high-level object representation.
        """
        
        # replace names
        renamedReport = dict(map(lambda (key, val): (self._replaceName(key), val), jsonReport.iteritems()))
                
        # convert data in report
        convertedReport = {}
        deferredSelfURIs = set([]) # all key names that have URIs that are exactly 'self' (handle after parsing everything else)
        for (key, value) in renamedReport.iteritems():
            if key in self._uriKeys:
                if value.lower().strip() == "self":
                    deferredSelfURIs.add(key)
                    continue
                else:
                    value = self._uriParser.parse(value)
            elif key in self._directiveKeys:
                value = self._directiveParser.parse(value)
            elif key in self._policyKeys:
                value = self._policyParser.parse(value)
            
            if value in (URI.INVALID(), Directive.INVALID(), Policy.INVALID()):
                if self._strict:
                    return Report.INVALID()
                else:
                    continue
            convertedReport[key] = value
            
        # handle deferred parsing of 'self' URIs (they represent the document-uri)
        for key in deferredSelfURIs:
            if "document-uri" in self._uriKeys and "document-uri" in convertedReport:
                convertedReport[key] = convertedReport["document-uri"]
            elif self._strict:
                return Report.INVALID()
            
        for requiredKey in self._requiredKeys:
            if not requiredKey in convertedReport:
                return Report.INVALID()
        return Report(convertedReport)
Example #36
0
    def load(self, id):
        record = self.db.get(id, None)
        if not record: return None

        data = {}
        data['meta'] = {}
        
        for k, v in record.items():
            k = k.encode('utf-8')
            if k == 'tags':
                try:
                    data[k] = [tag.encode('utf-8') for tag in v]
                except:
                    data[k] = None
            elif k == 'pairs':
                data[k] = contract_dict(v)
            elif k == '_rev':
                data['meta']['_rev'] = v
            elif isinstance(v, unicode):
                data[k] = v.encode('utf-8')
            else:
                data[k] = v

        return URI.load(data)