Skip to content

Commit

Permalink
python2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
p-alik committed May 6, 2019
1 parent 978738f commit 53ea1a4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 38 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ dist: xenial
language: python
# default 3.6
# https://changelog.travis-ci.com/upcoming-python-default-version-update-96873
python: '3.5'
python: '2.7'
sudo: true
env:
- TOX_ENV=py35
- TOX_ENV=py27
install:
- pip install tox
- pip install coveralls
Expand All @@ -19,5 +19,5 @@ after_success:

matrix:
include:
- python: 3.5
env: TOX_ENV=py35
- python: 2.7
env: TOX_ENV=py27
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The package contains Rest API client implementation to communicate with `GenieAC
Requirements
============

* Python 3.5 over
* Python 2.7


Setup
Expand Down
6 changes: 3 additions & 3 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let pkgs = import <nixpkgs> {};
in pkgs.mkShell {
buildInputs = [
pkgs.python35Packages.autopep8
pkgs.python35Packages.pip
pkgs.python27Packages.autopep8
pkgs.python27Packages.pip
];

shellHook = ''
alias pip="PIP_PREFIX='$TEMP/_build/pip_packages' \pip"
export PYTHONPATH="$TEMP/_build/pip_packages/lib/python3.5/site-packages:$PYTHONPATH"
export PYTHONPATH="$TEMP/_build/pip_packages/lib/python2.7/site-packages:$PYTHONPATH"
PATH="$TEMP/_build/pip_packages/bin:$PATH"
alias tox="TOX_TESTENV_PASSENV=\"PYTHONPATH\" \tox"
alias autopep8="autopep8 --in-place --max-line-length=80 --aggressive"
Expand Down
18 changes: 10 additions & 8 deletions genieacs/nbi/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""genieacs.nbi.client."""

from typing import Dict, List
import urllib.request
from __future__ import absolute_import

import urllib2

from genieacs.nbi.utils import build_request, build_post_request


class Client:
class Client(object):
"""GenieACS NBI Client."""

address = "http://localhost:7557"
Expand Down Expand Up @@ -48,9 +50,9 @@ def dispatch_device_task(self, device_id, data, connection_request=False):

def dispatch_preset(
self,
weight: int,
preconditions: Dict,
configurations: List[Dict]):
weight,
preconditions,
configurations):
"""Prepare and execute add preset request."""
data = {
"weight": weight,
Expand Down Expand Up @@ -78,8 +80,8 @@ def delete_device(self, device_id):

def perform_request(self, request):
"""Execute request and return response content."""
opener = urllib.request.build_opener(
urllib.request.HTTPHandler(debuglevel=1 if self.verbose else 0)
opener = urllib2.build_opener(
urllib2.HTTPHandler(debuglevel=1 if self.verbose else 0)
)
response = opener.open(request, timeout=self.timeout)
return response.read()
9 changes: 6 additions & 3 deletions genieacs/nbi/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""genieacs.nbi.utils."""
from __future__ import absolute_import
import json
import urllib.request
import urllib2


def build_request(url, method="GET", data=None):
"""Prepare and return urllib.request.Request."""
js_str = json.dumps(data).encode("utf-8") if data is not None else None
return urllib.request.Request(
req = urllib2.Request(
url,
method=method,
data=js_str
)
req.get_method = lambda: method

return req


def build_post_request(url, data):
Expand Down
4 changes: 2 additions & 2 deletions genieacs/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""genieacs.test."""

from __future__ import absolute_import
import unittest
from genieacs.nbi import client

Expand Down Expand Up @@ -30,8 +31,7 @@ def test_url(self):
self.assertEqual(self.instance.url(suffix),
"/".join([self.instance.address, suffix]))
args = ("bar", 1, 2)
url = "/".join(["{}", suffix]
).format(self.instance.address, *args)
url = "/".join(["{}", suffix]).format(self.instance.address, *args)
self.assertEqual(self.instance.url(suffix, *args), url)

def test_devices_url(self):
Expand Down
11 changes: 6 additions & 5 deletions genieacs/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""genieacs.test."""

from __future__ import absolute_import
import unittest
from os import getpid
from genieacs.nbi.utils import build_request, build_post_request
Expand All @@ -15,19 +16,19 @@ def test_build_request(self):
data = {"foo": getpid(), "method": method}
req = build_request(
url=_url, method=method, data=data)
self.assertEqual(req.method, method)
self.assertEqual(req.full_url, _url)
self.assertEqual(req.get_method(), method)
self.assertEqual(req.get_full_url(), _url)
self.assertIsNotNone(req.data)

req = build_request(url="http://xyz")
self.assertEqual(req.method, "GET")
self.assertEqual(req.get_method(), "GET")
self.assertIsNone(req.data)

def test_build_post_request(self):
"""Test genieacs.nbi.utils.build_post_request()."""
_url = "/".join(["http://xyz", "abc"])
data = {"foo": getpid()}
req = build_post_request(url=_url, data=data)
self.assertEqual(req.method, "POST")
self.assertEqual(req.full_url, _url)
self.assertEqual(req.get_method(), "POST")
self.assertEqual(req.get_full_url(), _url)
self.assertIsNotNone(req.data)
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
"""setup.py"""

from __future__ import with_statement
from __future__ import absolute_import
import os
import sys
from setuptools import find_packages
from setuptools import setup
from setuptools.command.test import test as TestCommand
from io import open

class Tox(TestCommand):
user_options = [('tox-args=', 'a', 'Arguments to pass to tox')]
Expand Down Expand Up @@ -39,8 +42,8 @@ def read_content(filepath):
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
Expand Down
15 changes: 5 additions & 10 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py35,
py27,
pycodestyle
pydocstyle,
docs
Expand All @@ -25,33 +25,28 @@ show-source=True
statistics=True
exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,setup.py,docs

[testenv:py35]
[testenv:py27]
deps=
{[py]deps}
basepython = python3.5
basepython = python2.7

[testenv:pypy]
deps=
{[py]deps}
basepython = pypy

[testenv:pypy3]
deps=
{[py]deps}
basepython = pypy3

[testenv:pycodestyle]
deps=
{[py]deps}
pycodestyle
basepython = python3.5
basepython = python2.7
commands = pycodestyle --first

[testenv:pydocstyle]
deps=
pydocstyle
commands = pydocstyle genieacs
basepython = python3.5
basepython = python2.7

[testenv:docs]
deps=
Expand Down

0 comments on commit 53ea1a4

Please sign in to comment.