diff options
| author | Árni Dagur <arni@dagur.eu> | 2020-06-07 17:51:42 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-07 17:51:42 -0400 |
| commit | 55611e2c74aec18a60d56ae63e92cc3bc6f4f6b7 (patch) | |
| tree | 33024145cf356662b9d8994fca02124a79ad0e57 | |
| parent | bee204e07a380477371a560479158c86abfba731 (diff) | |
Allow more options when constructing Engine (#6)
| -rw-r--r-- | .github/workflows/ci.yml | 5 | ||||
| -rw-r--r-- | .gitignore | 103 | ||||
| -rw-r--r-- | pyproject.toml | 1 | ||||
| -rw-r--r-- | src/lib.rs | 19 | ||||
| -rw-r--r-- | tests/test_engine.py | 10 |
5 files changed, 33 insertions, 105 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5c429c..18cdc06 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml | |||
| @@ -72,6 +72,9 @@ jobs: | |||
| 72 | - name: Build Python package | 72 | - name: Build Python package |
| 73 | run: poetry run maturin develop --release | 73 | run: poetry run maturin develop --release |
| 74 | 74 | ||
| 75 | - name: Run Python tests | ||
| 76 | run: poetry run pytest -vv --color=yes | ||
| 77 | |||
| 75 | python-publish: | 78 | python-publish: |
| 76 | needs: build | 79 | needs: build |
| 77 | runs-on: ${{ matrix.os }} | 80 | runs-on: ${{ matrix.os }} |
| @@ -162,4 +165,4 @@ jobs: | |||
| 162 | uses: peaceiris/actions-gh-pages@v3 | 165 | uses: peaceiris/actions-gh-pages@v3 |
| 163 | with: | 166 | with: |
| 164 | github_token: ${{ secrets.GITHUB_TOKEN }} | 167 | github_token: ${{ secrets.GITHUB_TOKEN }} |
| 165 | publish_dir: ./target/github-pages \ No newline at end of file | 168 | publish_dir: ./target/github-pages |
| @@ -95,17 +95,12 @@ tags | |||
| 95 | # End of https://www.gitignore.io/api/vim,emacs,visualstudiocode | 95 | # End of https://www.gitignore.io/api/vim,emacs,visualstudiocode |
| 96 | 96 | ||
| 97 | # -- Python -- | 97 | # -- Python -- |
| 98 | # Python virtual environment | ||
| 99 | .env | ||
| 100 | 98 | ||
| 101 | # Byte-compiled / optimized / DLL files | 99 | # Byte-compiled / optimized / DLL files |
| 102 | __pycache__/ | 100 | __pycache__/ |
| 103 | *.py[cod] | 101 | *.py[cod] |
| 104 | *$py.class | 102 | *$py.class |
| 105 | 103 | ||
| 106 | # C extensions | ||
| 107 | *.so | ||
| 108 | |||
| 109 | # Distribution / packaging | 104 | # Distribution / packaging |
| 110 | .Python | 105 | .Python |
| 111 | build/ | 106 | build/ |
| @@ -127,93 +122,9 @@ share/python-wheels/ | |||
| 127 | *.egg | 122 | *.egg |
| 128 | MANIFEST | 123 | MANIFEST |
| 129 | 124 | ||
| 130 | # PyInstaller | ||
| 131 | # Usually these files are written by a python script from a template | ||
| 132 | # before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
| 133 | *.manifest | ||
| 134 | *.spec | ||
| 135 | |||
| 136 | # Installer logs | ||
| 137 | pip-log.txt | ||
| 138 | pip-delete-this-directory.txt | ||
| 139 | |||
| 140 | # Unit test / coverage reports | ||
| 141 | htmlcov/ | ||
| 142 | .tox/ | ||
| 143 | .nox/ | ||
| 144 | .coverage | ||
| 145 | .coverage.* | ||
| 146 | .cache | ||
| 147 | nosetests.xml | ||
| 148 | coverage.xml | ||
| 149 | *.cover | ||
| 150 | *.py,cover | ||
| 151 | .hypothesis/ | ||
| 152 | .pytest_cache/ | ||
| 153 | cover/ | ||
| 154 | |||
| 155 | # Translations | ||
| 156 | *.mo | ||
| 157 | *.pot | ||
| 158 | |||
| 159 | # Django stuff: | ||
| 160 | *.log | ||
| 161 | local_settings.py | ||
| 162 | db.sqlite3 | ||
| 163 | db.sqlite3-journal | ||
| 164 | |||
| 165 | # Flask stuff: | ||
| 166 | instance/ | ||
| 167 | .webassets-cache | ||
| 168 | |||
| 169 | # Scrapy stuff: | ||
| 170 | .scrapy | ||
| 171 | |||
| 172 | # Sphinx documentation | ||
| 173 | docs/_build/ | ||
| 174 | |||
| 175 | # PyBuilder | ||
| 176 | .pybuilder/ | ||
| 177 | target/ | ||
| 178 | |||
| 179 | # Jupyter Notebook | ||
| 180 | .ipynb_checkpoints | ||
| 181 | |||
| 182 | # IPython | ||
| 183 | profile_default/ | ||
| 184 | ipython_config.py | ||
| 185 | |||
| 186 | # pyenv | ||
| 187 | # For a library or package, you might want to ignore these files since the code is | ||
| 188 | # intended to run in multiple environments; otherwise, check them in: | ||
| 189 | # .python-version | ||
| 190 | |||
| 191 | # pipenv | ||
| 192 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
| 193 | # However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
| 194 | # having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
| 195 | # install all needed dependencies. | ||
| 196 | #Pipfile.lock | ||
| 197 | |||
| 198 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow | 125 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow |
| 199 | __pypackages__/ | 126 | __pypackages__/ |
| 200 | 127 | ||
| 201 | # Celery stuff | ||
| 202 | celerybeat-schedule | ||
| 203 | celerybeat.pid | ||
| 204 | |||
| 205 | # SageMath parsed files | ||
| 206 | *.sage.py | ||
| 207 | |||
| 208 | # Environments | ||
| 209 | .env | ||
| 210 | .venv | ||
| 211 | env/ | ||
| 212 | venv/ | ||
| 213 | ENV/ | ||
| 214 | env.bak/ | ||
| 215 | venv.bak/ | ||
| 216 | |||
| 217 | # Spyder project settings | 128 | # Spyder project settings |
| 218 | .spyderproject | 129 | .spyderproject |
| 219 | .spyproject | 130 | .spyproject |
| @@ -221,20 +132,10 @@ venv.bak/ | |||
| 221 | # Rope project settings | 132 | # Rope project settings |
| 222 | .ropeproject | 133 | .ropeproject |
| 223 | 134 | ||
| 224 | # mkdocs documentation | ||
| 225 | /site | ||
| 226 | |||
| 227 | # mypy | 135 | # mypy |
| 228 | .mypy_cache/ | 136 | .mypy_cache/ |
| 229 | .dmypy.json | 137 | .dmypy.json |
| 230 | dmypy.json | 138 | dmypy.json |
| 231 | 139 | ||
| 232 | # Pyre type checker | 140 | # poetry |
| 233 | .pyre/ | 141 | poetry.lock |
| 234 | |||
| 235 | # pytype static type analyzer | ||
| 236 | .pytype/ | ||
| 237 | |||
| 238 | # Cython debug symbols | ||
| 239 | cython_debug/ | ||
| 240 | |||
diff --git a/pyproject.toml b/pyproject.toml index 530fc34..61530aa 100644 --- a/pyproject.toml +++ b/pyproject.toml | |||
| @@ -13,3 +13,4 @@ python = "^3.5" | |||
| 13 | 13 | ||
| 14 | [tool.poetry.dev-dependencies] | 14 | [tool.poetry.dev-dependencies] |
| 15 | maturin = "*" | 15 | maturin = "*" |
| 16 | pytest = "*" | ||
| @@ -230,7 +230,7 @@ impl PyObjectProtocol for HostnameSpecificResources { | |||
| 230 | /// | 230 | /// |
| 231 | /// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType | 231 | /// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType |
| 232 | #[pyclass] | 232 | #[pyclass] |
| 233 | #[text_signature = "($self, network_filters)"] | 233 | #[text_signature = "($self, network_filters=None, load_network=True, load_cosmetic=False, debug=False)"] |
| 234 | pub struct Engine { | 234 | pub struct Engine { |
| 235 | engine: RustEngine, | 235 | engine: RustEngine, |
| 236 | } | 236 | } |
| @@ -239,8 +239,21 @@ pub struct Engine { | |||
| 239 | impl Engine { | 239 | impl Engine { |
| 240 | /// Create a new adblocking engine | 240 | /// Create a new adblocking engine |
| 241 | #[new] | 241 | #[new] |
| 242 | pub fn from_rules(network_filters: Vec<String>) -> Self { | 242 | #[args(network_filters="None", load_network=true, load_cosmetic=false, debug=false)] |
| 243 | let engine = RustEngine::from_rules(&network_filters); | 243 | pub fn new( |
| 244 | network_filters: Option<Vec<String>>, | ||
| 245 | load_network: bool, | ||
| 246 | load_cosmetic: bool, | ||
| 247 | debug: bool, | ||
| 248 | ) -> Self { | ||
| 249 | let filters = network_filters.unwrap_or(Vec::new()); | ||
| 250 | let engine = RustEngine::from_rules_parametrised( | ||
| 251 | &filters, | ||
| 252 | load_network, | ||
| 253 | load_cosmetic, | ||
| 254 | debug, | ||
| 255 | true, | ||
| 256 | ); | ||
| 244 | Self { engine } | 257 | Self { engine } |
| 245 | } | 258 | } |
| 246 | 259 | ||
diff --git a/tests/test_engine.py b/tests/test_engine.py new file mode 100644 index 0000000..602d794 --- /dev/null +++ b/tests/test_engine.py | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | import adblock | ||
| 2 | |||
| 3 | def test_engine_arguments(): | ||
| 4 | # None of these should panic | ||
| 5 | adblock.Engine() | ||
| 6 | adblock.Engine([]) | ||
| 7 | adblock.Engine(network_filters=None) | ||
| 8 | adblock.Engine(network_filters=[]) | ||
| 9 | adblock.Engine(load_network=False, load_cosmetic=True, debug=False) | ||
| 10 | adblock.Engine(debug=True) | ||
