Skip to content

Commit

Permalink
Merge pull request #137 from jakubplichta/py3
Browse files Browse the repository at this point in the history
Add support for py3
  • Loading branch information
jakubplichta authored Feb 20, 2019
2 parents 7620b99 + 1d4fe47 commit 0196b50
Show file tree
Hide file tree
Showing 39 changed files with 215 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ htmlcov/
nosetests.xml
coverage.xml
*,cover
.pytest_cache

# Translations
*.mo
Expand Down
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: python
python:
- "2.7"
install:
- pip install tox
script:
- tox
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"
install: pip install tox-travis
script: tox
branches:
only:
- master
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
grafana-dashboard-builder
Copyright 2015-2018 grafana-dashboard-builder contributors
Copyright 2015-2019 grafana-dashboard-builder contributors

This product includes grafana-dashboard-builder.
4 changes: 3 additions & 1 deletion bin/grafana_dashboard_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

from grafana_dashboards import cli

__author__ = 'Jakub Plichta <[email protected]>'
Expand Down
4 changes: 2 additions & 2 deletions grafana_dashboards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,5 +12,5 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals
__author__ = 'Jakub Plichta <[email protected]>'
14 changes: 8 additions & 6 deletions grafana_dashboards/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import imp
Expand All @@ -21,20 +23,20 @@

import yaml

from grafana_dashboards.client.grafana import GrafanaExporter
from grafana_dashboards.exporter import ProjectProcessor, FileExporter
from grafana_dashboards.client.elastic_search import ElasticSearchExporter
from grafana_dashboards.client.grafana import GrafanaExporter
from grafana_dashboards.common import get_component_type
from grafana_dashboards.config import Config
from grafana_dashboards.exporter import ProjectProcessor, FileExporter
from grafana_dashboards.parser import DefinitionParser

__author__ = 'Jakub Plichta <[email protected]>'


def _initialize_exporters(exporter_names, exporter_types, config):
exporters = dict([(get_component_type(exporter), exporter) for exporter in exporter_types])
exporters = dict([(name[:-9], exporter) for name, exporter in exporters.iteritems() if name[:-9] in exporter_names])
return [exporter(**config.get_config(name)) for (name, exporter) in exporters.iteritems()]
exporters = dict([(name[:-9], exporter) for name, exporter in exporters.items() if name[:-9] in exporter_names])
return [exporter(**config.get_config(name)) for (name, exporter) in exporters.items()]


def _process_paths(paths):
Expand Down Expand Up @@ -75,7 +77,7 @@ def main():
try:
imp.load_source('grafana_dashboards.components.$loaded', plugin)
except Exception as e:
print 'Cannot load plugin %s: %s' % (plugin, str(e))
print('Cannot load plugin %s: %s' % (plugin, str(e)))

if args.project:
logging.warn("Using deprecated option '--project'")
Expand Down
4 changes: 2 additions & 2 deletions grafana_dashboards/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,5 +12,5 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals
__author__ = 'Jakub Plichta <[email protected]>'
44 changes: 29 additions & 15 deletions grafana_dashboards/client/connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,12 +12,26 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

import base64
import cookielib
import json
import logging
import urllib2
from urlparse import urlparse

try:
from cookielib import CookieJar
except ImportError:
from http.cookiejar import CookieJar
try:
from urllib2 import build_opener, HTTPHandler, HTTPSHandler, HTTPCookieProcessor, HTTPDefaultErrorHandler, \
Request, BaseHandler
except ImportError:
from urllib.request import build_opener, HTTPHandler, HTTPSHandler, HTTPCookieProcessor, HTTPDefaultErrorHandler, \
Request, BaseHandler
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse

import requests
from requests_kerberos import HTTPKerberosAuth
Expand All @@ -37,24 +51,24 @@ def __init__(self, username, password, host, debug=0):
logger.debug('Creating new connection with username=%s host=%s', username, host)
self._host = host

base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
self._headers['Authorization'] = 'Basic %s' % base64string
base64string = base64.encodestring(('%s:%s' % (username, password)).encode('utf-8')).replace(b'\n', b'')
self._headers['Authorization'] = b'Basic ' + base64string

self._opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=debug),
urllib2.HTTPSHandler(debuglevel=debug),
urllib2.HTTPCookieProcessor(cookielib.CookieJar()),
LoggingHandler(),
urllib2.HTTPDefaultErrorHandler())
self._opener = build_opener(HTTPHandler(debuglevel=debug),
HTTPSHandler(debuglevel=debug),
HTTPCookieProcessor(CookieJar()),
LoggingHandler(),
HTTPDefaultErrorHandler())

def make_request(self, uri, body=None):
request = urllib2.Request('{0}{1}'.format(self._host, uri),
json.dumps(body) if body else None,
headers=self._headers)
request = Request('{0}{1}'.format(self._host, uri),
json.dumps(body) if body else None,
headers=self._headers)
response_body = self._opener.open(request).read()
return {} if (response_body is None or response_body == '') else json.loads(response_body)


class LoggingHandler(urllib2.BaseHandler):
class LoggingHandler(BaseHandler):
def __init__(self):
pass

Expand Down
6 changes: 4 additions & 2 deletions grafana_dashboards/client/elastic_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,12 +12,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

import json
import logging
import os

from grafana_dashboards.exporter import DashboardExporter
from grafana_dashboards.client.connection import Connection, KerberosConnection
from grafana_dashboards.exporter import DashboardExporter

__author__ = 'Jakub Plichta <[email protected]>'

Expand Down
4 changes: 3 additions & 1 deletion grafana_dashboards/client/grafana.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

import logging
import os

Expand Down
4 changes: 3 additions & 1 deletion grafana_dashboards/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

import re

__author__ = 'Jakub Plichta <[email protected]>'
Expand Down
14 changes: 7 additions & 7 deletions grafana_dashboards/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,12 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import base
import dashboards
import panels
import projects
import rows
import templates
from grafana_dashboards.components import base
from grafana_dashboards.components import dashboards
from grafana_dashboards.components import panels
from grafana_dashboards.components import projects
from grafana_dashboards.components import rows
from grafana_dashboards.components import templates

__author__ = 'Jakub Plichta <[email protected]>'
__all__ = [
Expand Down
4 changes: 3 additions & 1 deletion grafana_dashboards/components/annotations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

from grafana_dashboards.components.base import JsonListGenerator, JsonGenerator

__author__ = 'Jakub Plichta <[email protected]>'
Expand Down
4 changes: 3 additions & 1 deletion grafana_dashboards/components/axes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

from grafana_dashboards.components.base import JsonListGenerator, JsonGenerator

__author__ = 'Jakub Plichta <[email protected]>'
Expand Down
15 changes: 11 additions & 4 deletions grafana_dashboards/components/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,13 +12,20 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

import logging
import string

from grafana_dashboards import errors
from grafana_dashboards.common import get_component_type
from grafana_dashboards.context import Context

try:
basestring
except NameError:
basestring = str

__author__ = 'Jakub Plichta <[email protected]>'


Expand Down Expand Up @@ -55,7 +62,7 @@ def __init__(self):
self._components[clazz] = {}

def _class_for_type(self, component_type):
if isinstance(component_type, str):
if isinstance(component_type, basestring):
component_type = self._types.get(component_type)
if self._components.get(component_type) is None:
raise errors.UnregisteredComponentError("No component of type '%s' found!" % component_type)
Expand Down Expand Up @@ -159,15 +166,15 @@ def gen_json_from_data(self, data, context):
return result_list

def gen_item_json(self, items, result_list):
if isinstance(items, str):
if isinstance(items, basestring):
# this is component without context
result_list += self.registry.get_component(type(self), items).gen_json()
else:
self._gen_item_json_with_context(items, result_list)

def _gen_item_json_with_context(self, items, result_list):
# TODO add check for dictionary
for (item_type, item_data) in items.iteritems():
for (item_type, item_data) in items.items():
if item_type not in self.component_item_types:
# this is named component with context
for context in Context.create_context(item_data, get_placeholders(item_type)):
Expand Down
6 changes: 4 additions & 2 deletions grafana_dashboards/components/dashboards.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 grafana-dashboard-builder contributors
# Copyright 2015-2019 grafana-dashboard-builder contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,9 +12,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals

from grafana_dashboards.common import get_component_type
from grafana_dashboards.components.annotations import Annotations
from grafana_dashboards.components.base import JsonGenerator
from grafana_dashboards.common import get_component_type
from grafana_dashboards.components.rows import Rows
from grafana_dashboards.components.templates import Templates

Expand Down
Loading

0 comments on commit 0196b50

Please sign in to comment.