Skip to content

Commit

Permalink
Merge pull request #163 from dom-nie/mypy_cleanup
Browse files Browse the repository at this point in the history
Adding mypy + cleanup + further removal of python2 code
  • Loading branch information
bastelfreak committed Nov 6, 2019
2 parents 6f96622 + 1e70e1e commit 3e24f3c
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 249 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cache: pip
install:
- pip install -r requirements-test.txt
script:
- py.test --cov=pypuppetdb --pep8 -v
- py.test --pep8 --mypy --strict
- bandit -r pypuppetdb
- bandit -r tests -s B101
after_success:
Expand Down
65 changes: 31 additions & 34 deletions pypuppetdb/QueryBuilder.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import unicode_literals

import datetime
import json
import logging
import sys

from pypuppetdb.errors import *
from pypuppetdb.errors import APIError

log = logging.getLogger(__name__)

if sys.version_info >= (3, 0):
unicode = str


class BinaryOperator(object):
"""
Expand All @@ -39,6 +35,7 @@ class BinaryOperator(object):
:param value: The values of the field to match, or not match.
:type value: any
"""

def __init__(self, operator, field, value):
if isinstance(value, datetime.datetime):
value = str(value)
Expand All @@ -50,9 +47,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
return self.data

Expand All @@ -74,6 +68,7 @@ class BooleanOperator(object):
:param operator: The boolean query operation to perform.
:type operator: :obj:`string`
"""

def __init__(self, operator):
self.operator = operator
self.operations = []
Expand All @@ -97,9 +92,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
if len(self.operations) == 0:
raise APIError("At least one query operation is required")
Expand All @@ -116,6 +108,7 @@ class ExtractOperator(object):
an optional standard query and an optional group by clause including a
list of fields.
"""

def __init__(self):
self.fields = []
self.query = None
Expand Down Expand Up @@ -168,9 +161,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
if len(self.fields) == 0:
raise APIError("ExtractOperator needs at least one field")
Expand Down Expand Up @@ -199,13 +189,14 @@ class FunctionOperator(object):
functions with the exception of count require this value.
:type field: :obj:`str`
"""

def __init__(self, function, field=None, fmt=None):
if function not in ['count', 'avg', 'sum', 'min', 'max', 'to_string']:
raise APIError("Unsupport function: {0}".format(function))
elif (function != "count" and field is None):
elif function != "count" and field is None:
raise APIError("Function {0} requires a field value".format(
function))
elif (function == 'to_string' and fmt is None):
elif function == 'to_string' and fmt is None:
raise APIError("Function {0} requires an extra 'fmt' parameter")

self.arr = ['function', function]
Expand All @@ -222,9 +213,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
return self.arr

Expand All @@ -240,6 +228,7 @@ class SubqueryOperator(object):
:param endpoint: The name of the subquery object
:type function: :obj:`str`
"""

def __init__(self, endpoint):
if endpoint not in ['catalogs', 'edges', 'environments', 'events',
'facts', 'fact_contents', 'fact_paths', 'nodes',
Expand All @@ -262,9 +251,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
return self.arr

Expand All @@ -279,6 +265,7 @@ class InOperator(object):
:param field: The name of the subquery object
:type function: :obj:`str`
"""

def __init__(self, field):
self.query = None
self.arr = ['in', field]
Expand All @@ -301,9 +288,11 @@ def add_array(self, values):
if self.query is not None:
raise APIError("Only one array is supported by the InOperator")
elif isinstance(values, list):
def depth(L): return (isinstance(L, list) and len(L) != 0) \
and max(map(depth, L))+1
if (depth(values) == 1):
def depth(l):
return (isinstance(l, list) and len(l) != 0) \
and max(map(depth, l)) + 1

if depth(values) == 1:
self.query = True
self.arr.append(['array', values])
else:
Expand All @@ -320,9 +309,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
return self.arr

Expand All @@ -341,6 +327,7 @@ class FromOperator(object):
note: only supports single entity From operations
"""

def __init__(self, endpoint):
valid_entities = ["aggregate_event_counts", "catalogs", "edges",
"environments", "event_counts", "events", "facts",
Expand Down Expand Up @@ -371,12 +358,14 @@ def add_query(self, query):
"Operator Objects")

def add_order_by(self, fields):
def depth(L): return isinstance(L, list) and max(map(depth, L))+1
def depth(l):
return isinstance(l, list) and max(map(depth, l)) + 1

fields_depth = depth(fields)

if isinstance(fields, list):
if fields_depth == 1 or fields_depth == 2:
self.order_by = fields
self.order_by = fields
else:
raise APIError("ExtractOperator.add_order_by only "
"supports lists of fields of depth "
Expand Down Expand Up @@ -404,9 +393,6 @@ def __repr__(self):
def __str__(self):
return json.dumps(self.json_data())

def __unicode__(self):
return json.dumps(self.json_data())

def json_data(self):
if self.query is None:
raise APIError("FromOperator needs one main query")
Expand Down Expand Up @@ -443,6 +429,7 @@ class EqualsOperator(BinaryOperator):
:param value: The value of the field to match, or not match.
:type value: any
"""

def __init__(self, field, value):
super(EqualsOperator, self).__init__("=", field, value)

Expand All @@ -467,6 +454,7 @@ class GreaterOperator(BinaryOperator):
:param value: Matches if the field is greater than this value.
:type value: Number, timestamp or array
"""

def __init__(self, field, value):
super(GreaterOperator, self).__init__(">", field, value)

Expand All @@ -491,6 +479,7 @@ class LessOperator(BinaryOperator):
:param value: Matches if the field is less than this value.
:type value: Number, timestamp or array
"""

def __init__(self, field, value):
super(LessOperator, self).__init__("<", field, value)

Expand All @@ -516,6 +505,7 @@ class GreaterEqualOperator(BinaryOperator):
this value.
:type value: Number, timestamp or array
"""

def __init__(self, field, value):
super(GreaterEqualOperator, self).__init__(">=", field, value)

Expand All @@ -541,6 +531,7 @@ class LessEqualOperator(BinaryOperator):
this value.
:type value: Number, timestamp or array
"""

def __init__(self, field, value):
super(LessEqualOperator, self).__init__("<=", field, value)

Expand All @@ -565,6 +556,7 @@ class RegexOperator(BinaryOperator):
:param value: Matches if the field matches this regular expression.
:type value: :obj:`string`
"""

def __init__(self, field, value):
super(RegexOperator, self).__init__("~", field, value)

Expand All @@ -590,6 +582,7 @@ class RegexArrayOperator(BinaryOperator):
:param value: Matches if the field matches this regular expression.
:type value: :obj:`list`
"""

def __init__(self, field, value):
super(RegexArrayOperator, self).__init__("~>", field, value)

Expand Down Expand Up @@ -617,6 +610,7 @@ class NullOperator(BinaryOperator):
not null (if False)
:type value: :obj:`bool`
"""

def __init__(self, field, value):
if type(value) != bool:
raise APIError("NullOperator value must be boolean")
Expand All @@ -643,6 +637,7 @@ class AndOperator(BooleanOperator):
op.add(EqualsOperator("catalog_environment", "production"))
op.add(EqualsOperator("facts_environment", "production"))
"""

def __init__(self):
super(AndOperator, self).__init__("and")

Expand All @@ -664,6 +659,7 @@ class OrOperator(BooleanOperator):
op.add(EqualsOperator("name", "hostname"))
op.add(EqualsOperator("name", "architecture"))
"""

def __init__(self):
super(OrOperator, self).__init__("or")

Expand All @@ -687,6 +683,7 @@ class NotOperator(BooleanOperator):
op = NotOperator()
op.add(EqualsOperator("osfamily", "RedHat"))
"""

def __init__(self):
super(NotOperator, self).__init__("not")

Expand Down
13 changes: 3 additions & 10 deletions pypuppetdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import unicode_literals

"""
pypuppetdb PuppetDB API library
Expand Down Expand Up @@ -47,7 +47,7 @@
osfamily/host1
osfamily/host2
That querries PuppetDB for the 'osfamily' fact and will yield Fact objects,
That queries PuppetDB for the 'osfamily' fact and will yield Fact objects,
one per node this fact is found on.
>>> resources = db.resources('file')
Expand All @@ -60,14 +60,7 @@

from pypuppetdb.api import BaseAPI

try: # Python 2.7+
from logging import NullHandler
except ImportError: # pragma: notest
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())
logging.getLogger(__name__).addHandler(logging.NullHandler())


def connect(host='localhost', port=8080, ssl_verify=False, ssl_key=None,
Expand Down

0 comments on commit 3e24f3c

Please sign in to comment.