Skip to content

Commit

Permalink
Added Python3 support (using "futurize" tool from "future" module)
Browse files Browse the repository at this point in the history
  • Loading branch information
initialed85 committed Apr 25, 2018
1 parent 6c4a968 commit 1840238
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ pip install wmi-client-wrapper
## usage

```
import wmi_client_wrapper as wmi
from wmi_client_wrapper import WmiClientWrapper
wmic = wmi.WmiClientWrapper(
wmic = WmiClientWrapper(
username="Administrator",
password="password",
host="192.168.1.149",
)
output = wmic.query("SELECT * FROM Win32_Processor")
#get FibrePort Info
# get FibrePort Info
wmic = wmi.WmiClientWrapper(
username="Administrator",
password="password",
Expand Down
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# because testing
mock
mock==2.0.0

# because pass-through
sh
sh==1.12.14

# to assist with Python2/3 compatibility
future==0.16.0
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
# There's some intersection with requirements.txt but pypi can't handle git
# dependencies. So it's better to just manually list dependencies again.
requires = [
"mock",
"sh",
'mock==2.0.0',
'sh==1.12.14',
'future==0.16.0',
]

setup(
name="wmi-client-wrapper",
version="0.0.12",
version="0.0.13",
description="Linux-only wrapper around wmi-client for WMI (Windows)",
long_description=open("README.md", "r").read(),
license="BSD",
Expand Down
19 changes: 15 additions & 4 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
import unittest

import mock
from future import standard_library

import wmi_client_wrapper as wmi

import os
standard_library.install_aliases()


datapath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/")


class TestCases(unittest.TestCase):
def test_object_creation_raises_without_username(self):
with self.assertRaises(Exception):
Expand Down Expand Up @@ -81,6 +89,7 @@ def test__parse_wmic_output_merging(self):

self.assertNotIn({"AcceptPause": "CLASS: Win32_Service"}, result)


class DictionaryWalkingTestCases(unittest.TestCase):
def test_basic_dictionary_output(self):
incoming = {}
Expand Down Expand Up @@ -147,6 +156,7 @@ def test_lists_with_embedded_dictionaries(self):
self.assertIn("beep", output[0])
self.assertEqual(output[0]["beep"], None)


class MoreTestCases(unittest.TestCase):
def setUp(self):
self.wmic = wmi.WmiClientWrapper(
Expand All @@ -162,12 +172,13 @@ def test__make_credential_args(self):
args = self.wmic._make_credential_args()

@mock.patch("wmi_client_wrapper.wrapper.WmiClientWrapper._parse_wmic_output")
@mock.patch("wmi_client_wrapper.wrapper.sh.wmic")
def test_query_calls(self, mock_sh_wmic, mock_parser):
@mock.patch("wmi_client_wrapper.wrapper.sh")
def test_query_calls(self, mock_sh, mock_parser):
self.wmic.query("")

self.assertTrue(mock_sh_wmic.called)
self.assertTrue(mock_sh.wmic.called)
self.assertTrue(mock_parser.called)


if __name__ == "__main__":
unittest.main()
5 changes: 4 additions & 1 deletion wmi_client_wrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from wrapper import WmiClientWrapper
from .wrapper import WmiClientWrapper

# avoid unused import warning
_ = WmiClientWrapper
28 changes: 21 additions & 7 deletions wmi_client_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@
There are a handful of injection vulnerabilities in this, so don't expose it
directly to end-users.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import csv
from builtins import object, str
from io import StringIO
from sys import version_info

import sh
from StringIO import StringIO
from future import standard_library

standard_library.install_aliases()


# because standard_library.install_aliases() replaces str with newstr which upsets csv module
if version_info[0] < 3:
from past.types.oldstr import oldstr as str


class WmiClientWrapper(object):
"""
Expand All @@ -22,7 +36,7 @@ class WmiClientWrapper(object):
def __init__(self, username="Administrator", password=None, host=None, namespace='//./root/cimv2', delimiter="\01"):
assert username
assert password
assert host # assume host is up
assert host # assume host is up

# store the credentials for later
self.username = username
Expand All @@ -31,7 +45,7 @@ def __init__(self, username="Administrator", password=None, host=None, namespace

self.delimiter = delimiter
self.namespace = namespace

def _make_credential_args(self):
"""
Makes credentials that get passed to wmic. This assembles a list of
Expand All @@ -53,8 +67,8 @@ def _make_credential_args(self):

arguments.append(hostaddr)
# the format for namespace
space= "--namespace={namespace}".format(namespace=self.namespace)
space = "--namespace={namespace}".format(namespace=self.namespace)

arguments.append(space)
return arguments

Expand Down Expand Up @@ -139,7 +153,7 @@ def _parse_wmic_output(cls, output, delimiter="\01"):

strio = StringIO(section)

moredata = list(csv.DictReader(strio, delimiter=delimiter))
moredata = list(csv.DictReader(strio, delimiter=str(delimiter)))
items.extend(moredata)

# walk the dictionaries!
Expand Down Expand Up @@ -171,7 +185,7 @@ def _fix_dictionary_output(cls, incoming):
elif isinstance(incoming, dict):
output = dict()

for (key, value) in incoming.items():
for (key, value) in list(incoming.items()):
if value == "(null)":
output[key] = None
elif value == "True":
Expand Down

0 comments on commit 1840238

Please sign in to comment.