python3-cyberfusion-ferm-support 1.1.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """Empty file to turn this into a package."""
@@ -0,0 +1,75 @@
1
+ """Helpers for ferm configuration."""
2
+
3
+ import os
4
+ import subprocess
5
+ from typing import List
6
+
7
+ from cyberfusion.Common import find_executable
8
+ from cyberfusion.FermSupport.configuration.custom_lines import CustomLine
9
+ from cyberfusion.FermSupport.configuration.variables import (
10
+ Variable,
11
+ VariableValue,
12
+ )
13
+ from cyberfusion.FermSupport.exceptions import ConfigInvalidError
14
+ from cyberfusion.SystemdSupport.units import Unit
15
+
16
+
17
+ class Configuration:
18
+ """Represents configuration."""
19
+
20
+ def __init__(self, *, path: str) -> None:
21
+ """Set attributes."""
22
+ self.path = path
23
+
24
+ self.variables: List[Variable] = []
25
+ self.custom_lines: List[CustomLine] = []
26
+
27
+ def add_variable(self, *, name: str, values: List[str]) -> None:
28
+ """Add variable."""
29
+ self.variables.append(
30
+ Variable(
31
+ name=name,
32
+ values=[VariableValue(content=value) for value in values],
33
+ )
34
+ )
35
+
36
+ def add_custom_line(self, *, contents: str) -> None:
37
+ """Add custom line."""
38
+ self.custom_lines.append(CustomLine(contents=contents))
39
+
40
+ def __str__(self) -> str:
41
+ """Get string representation."""
42
+ return (
43
+ "\n".join([str(line) for line in self.variables + self.custom_lines]) + "\n"
44
+ )
45
+
46
+ @property
47
+ def is_valid(self) -> bool:
48
+ """Get if config is valid."""
49
+ try:
50
+ subprocess.run([find_executable("ferm"), "--noexec", self.path], check=True)
51
+ except subprocess.CalledProcessError:
52
+ return False
53
+
54
+ return True
55
+
56
+ def save(self) -> bool:
57
+ """Get file object."""
58
+ if not os.path.exists(self.path):
59
+ with open(self.path, "w") as f:
60
+ pass
61
+
62
+ original_contents = open(self.path, "r").read()
63
+
64
+ with open(self.path, "w") as f:
65
+ f.write(str(self))
66
+
67
+ changed = original_contents != open(self.path, "r").read()
68
+
69
+ if changed:
70
+ if not self.is_valid:
71
+ raise ConfigInvalidError
72
+
73
+ Unit(f"ferm.{Unit.SUFFIX_SERVICE}").restart()
74
+
75
+ return changed
@@ -0,0 +1,13 @@
1
+ """Helpers for ferm custom lines."""
2
+
3
+
4
+ class CustomLine:
5
+ """Represents custom line."""
6
+
7
+ def __init__(self, *, contents: str) -> None:
8
+ """Set attributes."""
9
+ self.contents = contents
10
+
11
+ def __str__(self) -> str:
12
+ """Get string representation."""
13
+ return self.contents
@@ -0,0 +1,42 @@
1
+ """Helpers for ferm variables."""
2
+
3
+ from typing import List
4
+
5
+
6
+ class Variable:
7
+ """Represents variable."""
8
+
9
+ def __init__(self, *, name: str, values: List["VariableValue"]) -> None:
10
+ """Set attributes."""
11
+ self._name = name
12
+ self.values = values
13
+
14
+ @property
15
+ def name(self) -> str:
16
+ """Get name."""
17
+ return self._name.upper()
18
+
19
+ def __str__(self) -> str:
20
+ """Get string representation."""
21
+ lines = []
22
+
23
+ lines.append(f"@def ${self.name} = (")
24
+
25
+ for value in self.values:
26
+ lines.append(" " + str(value))
27
+
28
+ lines.append(");")
29
+
30
+ return "\n".join(lines)
31
+
32
+
33
+ class VariableValue:
34
+ """Represents variable value."""
35
+
36
+ def __init__(self, *, content: str) -> None:
37
+ """Set attributes."""
38
+ self.content = content
39
+
40
+ def __str__(self) -> str:
41
+ """Get string representation."""
42
+ return self.content
@@ -0,0 +1,7 @@
1
+ """Exceptions."""
2
+
3
+
4
+ class ConfigInvalidError(Exception):
5
+ """Config is invalid."""
6
+
7
+ pass
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.1
2
+ Name: python3-cyberfusion-ferm-support
3
+ Version: 1.1.2.3
4
+ Summary: Library for ferm.
5
+ Author-email: Cyberfusion <support@cyberfusion.io>
6
+ Project-URL: Source, https://github.com/CyberfusionIO/python3-cyberfusion-ferm-support
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: cached-property ==1.5.2
9
+ Requires-Dist: python3-cyberfusion-common ~=2.0
10
+ Requires-Dist: python3-cyberfusion-systemd-support ~=1.0
11
+
12
+ # python3-cyberfusion-ferm-support
13
+
14
+ Library for [ferm](http://ferm.foo-projects.org/).
15
+
16
+ # Install
17
+
18
+ ## PyPI
19
+
20
+ Run the following command to install the package from PyPI:
21
+
22
+ pip3 install python3-cyberfusion-ferm-support
23
+
24
+ ## Debian
25
+
26
+ Run the following commands to build a Debian package:
27
+
28
+ mk-build-deps -i -t 'apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y'
29
+ dpkg-buildpackage -us -uc
30
+
31
+ # Configure
32
+
33
+ No configuration is supported.
34
+
35
+ # Usage
36
+
37
+ ## Example
38
+
39
+ ```python
40
+ from cyberfusion.FermSupport.configuration import Configuration
41
+
42
+ c = Configuration(path="/etc/ferm/vars.d/example.conf")
43
+
44
+ c.add_variable(
45
+ name="EXAMPLE",
46
+ values=["2001:0db8::/32"]
47
+ )
48
+
49
+ c.save()
50
+ ```
@@ -0,0 +1,9 @@
1
+ cyberfusion/FermSupport/__init__.py,sha256=8JByOElHmHLFiNx9xTHIU6dy5AjPQQmpPLFq5IR4bpM,46
2
+ cyberfusion/FermSupport/configuration/__init__.py,sha256=1sTNrVys65Kqa7SlB4AoFgiTa55MsHsY5tzwl8hFOMw,2121
3
+ cyberfusion/FermSupport/configuration/custom_lines.py,sha256=IElDtYccl89Mn2epZdTwZW_6U3zvMO7CGXGGwYk5f48,306
4
+ cyberfusion/FermSupport/configuration/variables.py,sha256=gTPwQ_CJ5cIUwHUKmXkpgbBpAxpiz9_wouLUMld9U3w,921
5
+ cyberfusion/FermSupport/exceptions/__init__.py,sha256=HbLbR-C62FtD6fcSizxv2I5A5_1oyWhIV9H1KKJTiI0,96
6
+ python3_cyberfusion_ferm_support-1.1.2.3.dist-info/METADATA,sha256=eHIF9LwJProGEQK6mh9-CnhnBY31CocwKi-3IWs0upw,1115
7
+ python3_cyberfusion_ferm_support-1.1.2.3.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
8
+ python3_cyberfusion_ferm_support-1.1.2.3.dist-info/top_level.txt,sha256=ss011q9S6SL_KIIyq7iujFmIYa0grSjlnInO7cDkeag,12
9
+ python3_cyberfusion_ferm_support-1.1.2.3.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (73.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+