diff options
| author | Árni Dagur <arni@dagur.eu> | 2022-07-18 00:28:30 +0200 |
|---|---|---|
| committer | Árni Dagur <arni@dagur.eu> | 2022-07-18 00:42:08 +0200 |
| commit | 7cc01b838d6332fea3e392c80236138845d61502 (patch) | |
| tree | 9413b7d50d428508c2eb0255b761177365429630 | |
| parent | 3fcd327a63ae331e641565db0e178a9a64b4b0dd (diff) | |
Add aliases argument to Engine.add_resource
Co-Authored-By: Albert Kim <alkim@alkim.org>
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | adblock/adblock.pyi | 8 | ||||
| -rw-r--r-- | src/lib.rs | 25 | ||||
| -rw-r--r-- | tests/test_exceptions.py | 5 |
4 files changed, 32 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c596f1..7e66291 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
| @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch | |||
| 5 | 5 | ||
| 6 | ## Unreleased | 6 | ## Unreleased |
| 7 | --- | 7 | --- |
| 8 | ### Added | ||
| 9 | * Added `aliases` optional argument to `Engine.add_resource` | ||
| 10 | |||
| 8 | ### Changes | 11 | ### Changes |
| 9 | * Update PyO3 dependency to `0.16`. | 12 | * Update PyO3 dependency to `0.16`. |
| 10 | * Update upstream dependency to `0.5.6`. | 13 | * Update upstream dependency to `0.5.6`. |
diff --git a/adblock/adblock.pyi b/adblock/adblock.pyi index 544a30d..6c4398f 100644 --- a/adblock/adblock.pyi +++ b/adblock/adblock.pyi | |||
| @@ -117,7 +117,13 @@ class Engine: | |||
| 117 | pass | 117 | pass |
| 118 | def tag_exists(self, tag: str) -> bool: | 118 | def tag_exists(self, tag: str) -> bool: |
| 119 | pass | 119 | pass |
| 120 | def add_resource(self, name: str, content_type: str, content: str) -> bool: | 120 | def add_resource( |
| 121 | self, | ||
| 122 | name: str, | ||
| 123 | content_type: str, | ||
| 124 | content: str, | ||
| 125 | aliases: Optional[List[str]] = None, | ||
| 126 | ) -> bool: | ||
| 121 | pass | 127 | pass |
| 122 | def url_cosmetic_resources(self, url: str) -> UrlSpecificResources: | 128 | def url_cosmetic_resources(self, url: str) -> UrlSpecificResources: |
| 123 | pass | 129 | pass |
| @@ -530,16 +530,27 @@ impl Engine { | |||
| 530 | /// | 530 | /// |
| 531 | /// # Arguments | 531 | /// # Arguments |
| 532 | /// * `name`: Represents the primary name of the resource, often a filename | 532 | /// * `name`: Represents the primary name of the resource, often a filename |
| 533 | /// * `content_type`: How to interpret the resource data within `content` | 533 | /// * `content_type`: How to interpret the resource data within `content`. |
| 534 | /// Use `"template"` if wanting to specify a template resource type. | ||
| 534 | /// * `content`: The resource data, encoded using standard base64 configuration | 535 | /// * `content`: The resource data, encoded using standard base64 configuration |
| 535 | #[pyo3(text_signature = "($self, name, content_type, content)")] | 536 | /// * `aliases`: List of aliases for the resource |
| 536 | pub fn add_resource(&mut self, name: &str, content_type: &str, content: &str) -> PyResult<()> { | 537 | #[pyo3(text_signature = "($self, name, content_type, content, aliases)")] |
| 538 | pub fn add_resource( | ||
| 539 | &mut self, | ||
| 540 | name: &str, | ||
| 541 | content_type: &str, | ||
| 542 | content: &str, | ||
| 543 | aliases: Option<Vec<String>>, | ||
| 544 | ) -> PyResult<()> { | ||
| 537 | let result = self.engine.add_resource(Resource { | 545 | let result = self.engine.add_resource(Resource { |
| 538 | name: name.to_string(), | 546 | name: name.to_string(), |
| 539 | aliases: vec![], | 547 | aliases: aliases.unwrap_or_default(), |
| 540 | kind: ResourceType::Mime(MimeType::from(std::borrow::Cow::from( | 548 | kind: match content_type { |
| 541 | content_type.to_string(), | 549 | "template" => ResourceType::Template, |
| 542 | ))), | 550 | _ => ResourceType::Mime(MimeType::from(std::borrow::Cow::from( |
| 551 | content_type.to_string(), | ||
| 552 | ))), | ||
| 553 | }, | ||
| 543 | content: content.to_string(), | 554 | content: content.to_string(), |
| 544 | }); | 555 | }); |
| 545 | 556 | ||
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index e1c357c..6832c65 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py | |||
| @@ -30,6 +30,9 @@ def test_add_resource_error(): | |||
| 30 | # } | 30 | # } |
| 31 | # xOO6ww== => base64.b64encode('你好'.encode('gbk')) | 31 | # xOO6ww== => base64.b64encode('你好'.encode('gbk')) |
| 32 | engine.add_resource( | 32 | engine.add_resource( |
| 33 | name="aa", content_type="application/javascript", content="xOO6ww==" | 33 | name="aa", |
| 34 | content_type="application/javascript", | ||
| 35 | content="xOO6ww==", | ||
| 36 | aliases=[], | ||
| 34 | ) | 37 | ) |
| 35 | assert "invalid utf content" in str(exc.value) | 38 | assert "invalid utf content" in str(exc.value) |
