Skip to content

Commit

Permalink
Merge pull request #14 from emillynge/feature/improve-cli
Browse files Browse the repository at this point in the history
Feature/improve cli
  • Loading branch information
coolbho3k authored Mar 2, 2018
2 parents 49ddd44 + 976c497 commit dfb1374
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@ As a library:
>>> p.get_comment('BC:EE:7B:00:00:00')
'ASUSTek COMPUTER INC.'

On the command line. Make sure manuf is in the same directory:
On the command line:

$ python manuf.py BC:EE:7B:00:00:00
$ python -m manuf BC:EE:7B:00:00:00
Vendor(manuf='AsustekC', comment='ASUSTek COMPUTER INC.')

Alternatively, if the library has been installed i.e. using pip

$ manuf BC:EE:7B:00:00:00
Vendor(manuf='AsustekC', comment='ASUSTek COMPUTER INC.')

Use a manuf file in a custom location:

$ python manuf.py --manuf ~/manuf BC:EE:7B:00:00:00
$ manuf --manuf ~/manuf BC:EE:7B:00:00:00
Vendor(manuf='AsustekC', comment='ASUSTek COMPUTER INC.')

Automatically update the manuf file from Wireshark's git:

$ python manuf.py --update --manuf ~/manuf BC:EE:7B:00:00:00
$ manuf --update BC:EE:7B:00:00:00
Vendor(manuf='AsustekC', comment='ASUSTek COMPUTER INC.')

Note, that this command will update the manuf file bundled with this package. If you do not wish to
modify this, or do not have permissions to do so, you must specify a custom manuf file to perform an update.

$ manuf --update --manuf ~/manuf BC:EE:7B:00:00:00
Vendor(manuf='AsustekC', comment='ASUSTek COMPUTER INC.')

Advantages
Expand Down
2 changes: 2 additions & 0 deletions manuf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import manuf
from .manuf import MacParser
2 changes: 2 additions & 0 deletions manuf/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .manuf import main
main()
34 changes: 24 additions & 10 deletions manuf/manuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from StringIO import StringIO
except ImportError:
from io import StringIO
import importlib
import os

# Vendor tuple
Vendor = namedtuple('Vendor', ['manuf', 'manuf_long', 'comment'])
Expand All @@ -60,8 +62,8 @@ class MacParser(object):
MANUF_URL = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf"
WFA_URL = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=wka"

def __init__(self, manuf_name="manuf", update=False):
self._manuf_name = manuf_name
def __init__(self, manuf_name=None, update=False):
self._manuf_name = manuf_name or self.get_packaged_manuf_file_path()
if update:
self.update()
else:
Expand Down Expand Up @@ -287,22 +289,34 @@ def _strip_mac(self, mac):
def _bits_left(mac_str):
return 48 - 4 * len(mac_str)

def main():
@staticmethod
def get_packaged_manuf_file_path():
"""
returns the path to manuf file bundled with the package.
:return:
"""
package_init_path = importlib.import_module(__package__).__file__
package_path = os.path.abspath(os.path.join(package_init_path, os.pardir))
manuf_file_path = os.path.join(package_path, 'manuf')
return manuf_file_path


def main(*input_args):
"""Simple command line wrapping for MacParser."""
argparser = argparse.ArgumentParser(description="Parser utility for Wireshark's OUI database.")
argparser.add_argument('-m', "--manuf",
help="manuf file path. Defaults to manuf in same directory",
action="store")
help="manuf file path. Defaults to manuf file packaged with manuf.py installation",
action="store",
default=None)

argparser.add_argument("-u", "--update",
help="update manuf file from the internet",
action="store_true")
argparser.add_argument("mac_address", nargs='?', help="MAC address to check")

args = argparser.parse_args()
if args.manuf:
parser = MacParser(manuf_name=args.manuf, update=args.update)
else:
parser = MacParser(update=args.update)
input_args = input_args or None # if main is called with explicit args parse these - else use sysargs
args = argparser.parse_args(args=input_args)
parser = MacParser(manuf_name=args.manuf, update=args.update)

if args.mac_address:
print(parser.get_all(args.mac_address))
Expand Down
8 changes: 4 additions & 4 deletions manuf/test/test_manuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class ManufTestCase(unittest.TestCase):
MANUF_URL = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf"

def setUp(self):
self.manuf = manuf.MacParser(manuf_name="test/manuf")
self.manuf = manuf.MacParser(manuf_name="manuf")
#
def test_update_update(self):
self.manuf.update(manuf_url=self.MANUF_URL, manuf_name="test/manuf_update")
assert os.path.exists("test/manuf_update")
os.remove("test/manuf_update")
self.manuf.update(manuf_url=self.MANUF_URL, manuf_name="manuf_update")
assert os.path.exists("manuf_update")
os.remove("manuf_update")

def test_getAll_whenMacValid_getVendor(self):
v = self.manuf.get_all("00:00:00:00:00:00")
Expand Down
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
url = 'https://github.com/coolbho3k/manuf/',
license = 'Apache License 2.0 or GPLv3',
keywords = ['manuf', 'mac address', 'networking'],
entry_points = {
'console_scripts': [
'manuf=manuf.manuf:main'
],
},
package_data = {
'manuf': ['manuf']
},
)

0 comments on commit dfb1374

Please sign in to comment.