Skip to content

Commit 4171615

Browse files
author
heikoschmidt
committed
implement include and exclude filter at output
1 parent e372c34 commit 4171615

File tree

6 files changed

+72
-4
lines changed

6 files changed

+72
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ subtleties. If you have unusual requirements, it is better to write the proxy sc
1010
## I would still like to implement this
1111

1212
- [x] ~~automatic sorting with overlapping hostnames~~
13+
- [x] ~~implement include and exclude filter at output~~
1314
- [ ] automatic sorting with overlapping network masks
1415
- [ ] publish on pypi
15-
- [ ] implement filter at output
1616
- [ ] add tag to automatically add local networks to the proxy
1717

1818

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pypacer"
7-
version = "0.1.3"
7+
version = "0.1.4"
88
dynamic = ["dependencies"]
99
authors = [
1010
{ name = "Heiko Schmidt", email = "73h@gmx.net" },

src/examples/unittests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ proxies:
1717
- "foo.example.net"
1818
- route: "PROXY netmask.example.com"
1919
description: a proxy for netmask
20+
tags:
21+
- foo
2022
targets:
2123
- "93.184.0.0/16"
2224
- route: "PROXY ip.example.com"

src/pypacer/pypacer.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,46 @@ def load_from_yaml(self, stream: str):
2121
y = yaml.safe_load(stream)
2222
self.load_from_dict(y)
2323

24-
def output(self) -> str:
24+
def output(self, excludes: list[str] = None, includes: list[str] = None) -> str:
25+
"""Returns the finished ProxyScript as a string
26+
27+
Parameters
28+
----------
29+
excludes : list[str], optional
30+
A list of tags. Proxies that have this tag are excluded.
31+
32+
includes : list[str], optional
33+
A list of tags. Proxies that have this tag are included. Other proxies are not included.
34+
35+
"""
2536
if self.config is None:
2637
raise Exception("No config loaded, use load_from_yaml or load_from_dict first.")
2738
config = copy.deepcopy(self.config)
39+
40+
# handle includes
41+
if includes:
42+
proxies = []
43+
for proxy in config.proxies:
44+
include = False
45+
for tag in proxy.tags:
46+
if tag in includes:
47+
include = True
48+
if include:
49+
proxies.append(proxy)
50+
config.proxies = proxies
51+
52+
# handle excludes
53+
if excludes:
54+
proxies = []
55+
for proxy in config.proxies:
56+
exclude = False
57+
for tag in proxy.tags:
58+
if tag in excludes:
59+
exclude = True
60+
if not exclude:
61+
proxies.append(proxy)
62+
config.proxies = proxies
63+
2864
default = config.get_default_proxy()
2965
config.reorganize_proxies()
3066
config.recognize_overlaps()

src/pypacer/pypacerconfig.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ def recognize_overlaps(self):
7575

7676
def get_default_proxy(self) -> Proxy:
7777
defaults = [p for p in self.proxies if "default" in p.tags]
78-
return defaults[0] if len(defaults) > 0 else [p for p in self.proxies][0]
78+
if len(defaults) == 0:
79+
if len(self.proxies) == 0:
80+
return Proxy(route="DIRECT")
81+
return self.proxies[0]
82+
return defaults[0]

src/tests/pypacer_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ def test_load_config_from_yaml(self):
2323
output = p.output()
2424
open(os.path.join(location, "..", "examples", "unittests.pac"), "w").write(output)
2525

26+
def test_exclude_by_tag(self):
27+
p = PyPacer()
28+
p.load_from_yaml(self.pac_file)
29+
output = p.output(excludes=["foo"])
30+
self.assertTrue("PROXY netmask.example.com" not in output)
31+
32+
def test_exclude_by_tags(self):
33+
p = PyPacer()
34+
p.load_from_yaml(self.pac_file)
35+
output = p.output(excludes=["foo", "default"])
36+
self.assertTrue("PROXY netmask.example.com" not in output)
37+
self.assertTrue("PROXY default.example.com" not in output)
38+
39+
def test_include_by_tag(self):
40+
p = PyPacer()
41+
p.load_from_yaml(self.pac_file)
42+
output = p.output(includes=["foo"])
43+
self.assertTrue("PROXY netmask.example.com" in output)
44+
45+
def test_include_by_tags(self):
46+
p = PyPacer()
47+
p.load_from_yaml(self.pac_file)
48+
output = p.output(includes=["foo", "default"])
49+
self.assertTrue("PROXY netmask.example.com" in output)
50+
self.assertTrue("PROXY default.example.com" in output)
51+
2652
def test_output(self):
2753
p = PyPacer()
2854
p.load_from_yaml(self.pac_file)

0 commit comments

Comments
 (0)