commit 7cc01b838d6332fea3e392c80236138845d61502
parent 3fcd327a63ae331e641565db0e178a9a64b4b0dd
Author: Árni Dagur <arni@dagur.eu>
Date: Mon, 18 Jul 2022 00:28:30 +0200
Add aliases argument to Engine.add_resource
Co-Authored-By: Albert Kim <alkim@alkim.org>
Diffstat:
4 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
## Unreleased
---
+### Added
+* Added `aliases` optional argument to `Engine.add_resource`
+
### Changes
* Update PyO3 dependency to `0.16`.
* Update upstream dependency to `0.5.6`.
diff --git a/adblock/adblock.pyi b/adblock/adblock.pyi
@@ -117,7 +117,13 @@ class Engine:
pass
def tag_exists(self, tag: str) -> bool:
pass
- def add_resource(self, name: str, content_type: str, content: str) -> bool:
+ def add_resource(
+ self,
+ name: str,
+ content_type: str,
+ content: str,
+ aliases: Optional[List[str]] = None,
+ ) -> bool:
pass
def url_cosmetic_resources(self, url: str) -> UrlSpecificResources:
pass
diff --git a/src/lib.rs b/src/lib.rs
@@ -530,16 +530,27 @@ impl Engine {
///
/// # Arguments
/// * `name`: Represents the primary name of the resource, often a filename
- /// * `content_type`: How to interpret the resource data within `content`
+ /// * `content_type`: How to interpret the resource data within `content`.
+ /// Use `"template"` if wanting to specify a template resource type.
/// * `content`: The resource data, encoded using standard base64 configuration
- #[pyo3(text_signature = "($self, name, content_type, content)")]
- pub fn add_resource(&mut self, name: &str, content_type: &str, content: &str) -> PyResult<()> {
+ /// * `aliases`: List of aliases for the resource
+ #[pyo3(text_signature = "($self, name, content_type, content, aliases)")]
+ pub fn add_resource(
+ &mut self,
+ name: &str,
+ content_type: &str,
+ content: &str,
+ aliases: Option<Vec<String>>,
+ ) -> PyResult<()> {
let result = self.engine.add_resource(Resource {
name: name.to_string(),
- aliases: vec![],
- kind: ResourceType::Mime(MimeType::from(std::borrow::Cow::from(
- content_type.to_string(),
- ))),
+ aliases: aliases.unwrap_or_default(),
+ kind: match content_type {
+ "template" => ResourceType::Template,
+ _ => ResourceType::Mime(MimeType::from(std::borrow::Cow::from(
+ content_type.to_string(),
+ ))),
+ },
content: content.to_string(),
});
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
@@ -30,6 +30,9 @@ def test_add_resource_error():
# }
# xOO6ww== => base64.b64encode('你好'.encode('gbk'))
engine.add_resource(
- name="aa", content_type="application/javascript", content="xOO6ww=="
+ name="aa",
+ content_type="application/javascript",
+ content="xOO6ww==",
+ aliases=[],
)
assert "invalid utf content" in str(exc.value)