Skip to content

Commit

Permalink
re #2 - 2to3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Cockburn committed Nov 22, 2018
1 parent 091f851 commit c439c16
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 186 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ docs/_build/

# PyBuilder
target/

.idea/
48 changes: 24 additions & 24 deletions examples/simple_stateful.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from pyptables import default_tables, restore
from pyptables.rules import Rule, Accept

# get a default set of tables and chains
tables = default_tables()

# get the forward chain of the filter tables
forward = tables['filter']['FORWARD']

# any packet matching an established connection should be allowed
forward.append(Accept(match='conntrack', ctstate='ESTABLISHED'))

# add rules to the forward chain for DNS, HTTP and HTTPS ports
forward.append(Accept(proto='tcp', dport='53'))
forward.append(Accept(proto='tcp', dport='80'))
forward.append(Accept(proto='tcp', dport='443'))

# any packet not matching a rules will be dropped
forward.policy = Rule.DROP

# write the rules into the kernel
restore(tables)

print tables.to_iptables()
from pyptables import default_tables, restore
from pyptables.rules import Rule, Accept

# get a default set of tables and chains
tables = default_tables()

# get the forward chain of the filter tables
forward = tables['filter']['FORWARD']

# any packet matching an established connection should be allowed
forward.append(Accept(match='conntrack', ctstate='ESTABLISHED'))

# add rules to the forward chain for DNS, HTTP and HTTPS ports
forward.append(Accept(proto='tcp', dport='53'))
forward.append(Accept(proto='tcp', dport='80'))
forward.append(Accept(proto='tcp', dport='443'))

# any packet not matching a rules will be dropped
forward.policy = Rule.DROP

# write the rules into the kernel
restore(tables)

print(tables.to_iptables())
9 changes: 5 additions & 4 deletions pyptables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import re
import subprocess

from tables import Tables, Table
from chains import BuiltinChain, UserChain
from rules import Rule, Accept, Drop, Jump, Redirect, Return, Log, CustomRule
from rules.matches import Match
from pyptables.tables import Tables, Table
from pyptables.chains import BuiltinChain, UserChain
from pyptables.rules import Rule, Accept, Drop, Jump, Redirect, Return, Log, CustomRule
from pyptables.rules.matches import Match


def default_tables():
"""Generate a set of iptables containing all the default tables and chains"""
Expand Down
2 changes: 1 addition & 1 deletion pyptables/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
if '--line-numbers' in sys.argv:
output = add_line_numbers(output)

print output
print(output)
5 changes: 3 additions & 2 deletions pyptables/chains.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from collections import namedtuple

from base import DebugObject
from pyptables.base import DebugObject


class AbstractChain(DebugObject, list):
"""Represents an iptables Chain. Holds a number of Rule objects in a list-like fashion"""
Expand All @@ -27,7 +28,7 @@ def to_iptables(self):
'rules': rule_output,
},
)
except Exception, e: # pragma: no cover
except Exception as e: # pragma: no cover
e.iptables_path = getattr(e, 'iptables_path', [])
e.iptables_path.insert(0, self.name)
raise
Expand Down
2 changes: 1 addition & 1 deletion pyptables/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
rules for iptables.
"""

from base import AbstractRule, CustomRule, Rule, CompositeRule
from pyptables.rules.base import AbstractRule, CustomRule, Rule, CompositeRule

from pyptables.chains import AbstractChain as _AbstractChain

Expand Down
2 changes: 1 addition & 1 deletion pyptables/rules/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _update_args(self, args, kwargs):
def __iter__(self):
kwargs = dict(self.kwargs) # duplicate dictionary, as it is modified below
for argument in self.known_args:
for key in kwargs.keys():
for key in kwargs:
if argument.matches(key):
value = kwargs.pop(key)
yield argument.bind(key, value)
Expand Down
9 changes: 5 additions & 4 deletions pyptables/rules/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import itertools

from ..base import DebugObject
from pyptables.base import DebugObject

from pyptables.rules.arguments import UnboundArgument, ArgumentList
from pyptables.rules.matches import Match

from arguments import UnboundArgument, ArgumentList
from matches import Match

class AbstractRule(DebugObject):
"""Represents an iptables rule"""
Expand All @@ -19,7 +20,7 @@ def to_iptables(self, prefix=''):
'header': self._header(),
'rules': self._rule_definition(prefix),
}
except Exception, e: # pragma: no cover
except Exception as e: # pragma: no cover
e.iptables_path = getattr(e, 'iptables_path', [])
e.iptables_path.insert(0, "Rule:\n created: %s\n comment: %s" % (self.debug_info(), self.comment))
raise
Expand Down
2 changes: 1 addition & 1 deletion pyptables/rules/forwarding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
rules for iptables.
"""

from base import ForwardingRule
from pyptables.rules.forwarding.base import ForwardingRule
7 changes: 4 additions & 3 deletions pyptables/rules/forwarding/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
Locations represent a network location.
"""

from ...base import DebugObject
from ..arguments import ArgumentList
from hosts import Hosts
from pyptables.base import DebugObject
from pyptables.rules.arguments import ArgumentList
from pyptables.rules.forwarding.hosts import Hosts


class Location(DebugObject):
"""Represents a network location"""
Expand Down
2 changes: 1 addition & 1 deletion pyptables/rules/input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
rules for iptables.
"""

from base import InputRule
from pyptables.rules.input.base import InputRule
2 changes: 1 addition & 1 deletion pyptables/rules/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import partial
from random import Random

from . import Rule
from pyptables.rules import Rule

_marks = []
class Mark(Rule):
Expand Down
9 changes: 5 additions & 4 deletions pyptables/tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

from base import DebugObject
from pyptables.base import DebugObject


class Tables(DebugObject, OrderedDict):
"""Dictionary like top-level container of iptables, holds a number of Table objects."""
Expand All @@ -19,7 +20,7 @@ def to_iptables(self):
'header': header,
'tables': table_output,
}
except Exception, e: #pragma: no cover
except Exception as e: # pragma: no cover
e.iptables_path = getattr(e, 'iptables_path', [])
e.iptables_path.insert(0, "Tables")
e.message = "Iptables error at:\n %s\n\nError message: %s" % ("\n".join(e.iptables_path).replace('\n', '\n '), e.message)
Expand Down Expand Up @@ -64,7 +65,7 @@ def to_iptables(self):
'rules': "\n\n".join([result.rules for result in chain_results]),
'footer': 'COMMIT'
}
except Exception, e: #pragma: no cover
except Exception as e: # pragma: no cover
e.iptables_path = getattr(e, 'iptables_path', [])
e.iptables_path.insert(0, self.name)
raise
Expand All @@ -78,4 +79,4 @@ def append(self, chain):
return chain

def __repr__(self):
return "<Table: %s - %s>" % (self.name, self.values())
return "<Table: %s - %s>" % (self.name, list(self.values()))
Loading

0 comments on commit c439c16

Please sign in to comment.