commit 55611e2c74aec18a60d56ae63e92cc3bc6f4f6b7
parent bee204e07a380477371a560479158c86abfba731
Author: Árni Dagur <arni@dagur.eu>
Date: Sun, 7 Jun 2020 17:51:42 -0400
Allow more options when constructing Engine (#6)
Diffstat:
5 files changed, 33 insertions(+), 106 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
@@ -72,6 +72,9 @@ jobs:
- name: Build Python package
run: poetry run maturin develop --release
+ - name: Run Python tests
+ run: poetry run pytest -vv --color=yes
+
python-publish:
needs: build
runs-on: ${{ matrix.os }}
@@ -162,4 +165,4 @@ jobs:
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- publish_dir: ./target/github-pages
-\ No newline at end of file
+ publish_dir: ./target/github-pages
diff --git a/.gitignore b/.gitignore
@@ -95,17 +95,12 @@ tags
# End of https://www.gitignore.io/api/vim,emacs,visualstudiocode
# -- Python --
-# Python virtual environment
-.env
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
-# C extensions
-*.so
-
# Distribution / packaging
.Python
build/
@@ -127,93 +122,9 @@ share/python-wheels/
*.egg
MANIFEST
-# PyInstaller
-# Usually these files are written by a python script from a template
-# before PyInstaller builds the exe, so as to inject date/other infos into it.
-*.manifest
-*.spec
-
-# Installer logs
-pip-log.txt
-pip-delete-this-directory.txt
-
-# Unit test / coverage reports
-htmlcov/
-.tox/
-.nox/
-.coverage
-.coverage.*
-.cache
-nosetests.xml
-coverage.xml
-*.cover
-*.py,cover
-.hypothesis/
-.pytest_cache/
-cover/
-
-# Translations
-*.mo
-*.pot
-
-# Django stuff:
-*.log
-local_settings.py
-db.sqlite3
-db.sqlite3-journal
-
-# Flask stuff:
-instance/
-.webassets-cache
-
-# Scrapy stuff:
-.scrapy
-
-# Sphinx documentation
-docs/_build/
-
-# PyBuilder
-.pybuilder/
-target/
-
-# Jupyter Notebook
-.ipynb_checkpoints
-
-# IPython
-profile_default/
-ipython_config.py
-
-# pyenv
-# For a library or package, you might want to ignore these files since the code is
-# intended to run in multiple environments; otherwise, check them in:
-# .python-version
-
-# pipenv
-# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
-# However, in case of collaboration, if having platform-specific dependencies or dependencies
-# having no cross-platform support, pipenv may install dependencies that don't work, or not
-# install all needed dependencies.
-#Pipfile.lock
-
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
-# Celery stuff
-celerybeat-schedule
-celerybeat.pid
-
-# SageMath parsed files
-*.sage.py
-
-# Environments
-.env
-.venv
-env/
-venv/
-ENV/
-env.bak/
-venv.bak/
-
# Spyder project settings
.spyderproject
.spyproject
@@ -221,20 +132,10 @@ venv.bak/
# Rope project settings
.ropeproject
-# mkdocs documentation
-/site
-
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
-# Pyre type checker
-.pyre/
-
-# pytype static type analyzer
-.pytype/
-
-# Cython debug symbols
-cython_debug/
-
+# poetry
+poetry.lock
diff --git a/pyproject.toml b/pyproject.toml
@@ -13,3 +13,4 @@ python = "^3.5"
[tool.poetry.dev-dependencies]
maturin = "*"
+pytest = "*"
diff --git a/src/lib.rs b/src/lib.rs
@@ -230,7 +230,7 @@ impl PyObjectProtocol for HostnameSpecificResources {
///
/// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType
#[pyclass]
-#[text_signature = "($self, network_filters)"]
+#[text_signature = "($self, network_filters=None, load_network=True, load_cosmetic=False, debug=False)"]
pub struct Engine {
engine: RustEngine,
}
@@ -239,8 +239,21 @@ pub struct Engine {
impl Engine {
/// Create a new adblocking engine
#[new]
- pub fn from_rules(network_filters: Vec<String>) -> Self {
- let engine = RustEngine::from_rules(&network_filters);
+ #[args(network_filters="None", load_network=true, load_cosmetic=false, debug=false)]
+ pub fn new(
+ network_filters: Option<Vec<String>>,
+ load_network: bool,
+ load_cosmetic: bool,
+ debug: bool,
+ ) -> Self {
+ let filters = network_filters.unwrap_or(Vec::new());
+ let engine = RustEngine::from_rules_parametrised(
+ &filters,
+ load_network,
+ load_cosmetic,
+ debug,
+ true,
+ );
Self { engine }
}
diff --git a/tests/test_engine.py b/tests/test_engine.py
@@ -0,0 +1,10 @@
+import adblock
+
+def test_engine_arguments():
+ # None of these should panic
+ adblock.Engine()
+ adblock.Engine([])
+ adblock.Engine(network_filters=None)
+ adblock.Engine(network_filters=[])
+ adblock.Engine(load_network=False, load_cosmetic=True, debug=False)
+ adblock.Engine(debug=True)