Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Accessing 2 access points with 2 different interfaces #110

Open
wants to merge 2 commits into
base: error-message
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Contributing
------------

Please contribute! You can contribute by making a pull request or reporting
bugs. You can also submit feature requests or ask questions about how to do
things with wifi.

How to Submit an Issue
======================

Issues include bug reports, feature requests, documentation requests, and lots
of other things too.

1. Go to https:/rockymeza/wifi/issues to see the issue tracker.

2. Please make sure that the issue hasn't already been opened by somebody
else.

3. Create a New Issue. If it is a bug, please tell me what distro you are
using. I don't have the capacity to test against a bunch of distros, but I
do want wifi to work on more than just the one that I use.

How to Submit a Pull Request
============================

1. Clone the repo.

2. Run the tests. ::

$ python setup.py test

3. Create a topic branch.

4. Make your awesome changes.

Don't forget to add tests to make sure that your awesome changes will
continue to work for all of eternity.

5. Make sure that your code conforms to PEP8 and the Zen of Python as much as
possible (ignore E501, the line lengths requirement).

6. Get on GitHub and submit a pull request.

The best pull requests will not have PEP8 errors and will include tests. If
you don't have tests, I am still willing to accept the pull request, but it may
take a while for me to merge it as I would want to write the tests myself and I
don't have a ton of time I can devote to that (especially during the school
year).
30 changes: 18 additions & 12 deletions bin/wifi
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,48 @@ def find_cell(interface, query):
return cell


def scheme_for_ssid(interface, scheme, ssid=None):
def get_scheme_params(interface, scheme, ssid=None):
cell = find_cell(interface, ssid or scheme)

passkey = None if not cell.encrypted else input('passkey> ')

return Scheme.for_cell(interface, scheme, cell, passkey)
return interface, scheme, cell, passkey


def scan_command(args):
print_table([[cell.signal, cell.ssid, 'protected' if cell.encrypted else 'unprotected'] for cell in Cell.all(args.interface)])


def list_command(args):
for scheme in Scheme.all():
for scheme in Scheme.for_file(args.file).all():
print(scheme.name)


def show_command(args):
scheme = scheme_for_ssid(args.interface, args.scheme, args.ssid)
scheme = Scheme.for_file(args.file).for_cell(*get_scheme_params(args.interface, args.scheme, args.ssid))
print(scheme)


def add_command(args):
assert not Scheme.find(args.interface, args.scheme), "That scheme has already been used"
scheme_class = Scheme.for_file(args.file)
assert not scheme_class.find(args.interface, args.scheme), "That scheme has already been used"

scheme = scheme_for_ssid(args.interface, args.scheme, args.ssid)
scheme = scheme_class.for_cell(*get_scheme_params(args.interface, args.scheme, args.ssid))
scheme.save()


def connect_command(args):
scheme_class = Scheme.for_file(args.file)
if args.adhoc:
# ensure that we have the adhoc utility scheme
try:
adhoc_scheme = Scheme(args.interface, 'adhoc')
adhoc_scheme = scheme_class(args.interface, 'adhoc')
adhoc_scheme.save()
except AssertionError:
pass

scheme = scheme_for_ssid(args.interface, 'adhoc', args.scheme)
scheme = scheme_class.for_cell(*get_scheme_params(args.interface, 'adhoc', args.scheme, args.file))
else:
scheme = Scheme.find(args.interface, args.scheme)
scheme = scheme_class.find(args.interface, args.scheme)
assert scheme, "Couldn't find a scheme named %r, did you mean to use -a?" % args.scheme

scheme.activate()
Expand All @@ -86,6 +87,10 @@ parser.add_argument('-i',
'--interface',
default='wlan0',
help="Specifies which interface to use (wlan0, eth0, etc.)")
parser.add_argument('-f',
'--file',
default='/etc/network/interfaces',
help="Specifies which file for scheme storage.")

subparsers = parser.add_subparsers(title='commands')

Expand Down Expand Up @@ -125,7 +130,8 @@ parser_connect.add_argument('-a',
parser_connect.set_defaults(func=connect_command)


parser_connect.options = [scheme.name for scheme in Scheme.all()]
# TODO: how to specify the correct interfaces file to work off of.
parser_connect.get_options = lambda: [scheme.name for scheme in Scheme.all()]


def autocomplete(position, wordlist):
Expand All @@ -134,7 +140,7 @@ def autocomplete(position, wordlist):
else:
try:
prev = wordlist[position - 1]
ret = subparsers.choices[prev].options
ret = subparsers.choices[prev].get_options()
except (IndexError, KeyError, AttributeError):
ret = []

Expand Down
16 changes: 9 additions & 7 deletions tests/test_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@

class TestSchemes(TestCase):
def setUp(self):
self.tempfile, Scheme.interfaces = tempfile.mkstemp()
self.tempfile, interfaces = tempfile.mkstemp()

with open(Scheme.interfaces, 'w') as f:
with open(interfaces, 'w') as f:
f.write(NETWORK_INTERFACES_FILE)

self.Scheme = Scheme.for_file(interfaces)

def test_scheme_extraction(self):
work, coffee, home, coffee2 = extract_schemes(NETWORK_INTERFACES_FILE)

Expand All @@ -53,22 +55,22 @@ def test_scheme_extraction(self):
assert coffee.options['wireless-essid'] == 'Coffee WiFi'

def test_str(self):
scheme = Scheme('wlan0', 'test')
scheme = self.Scheme('wlan0', 'test')
assert str(scheme) == 'iface wlan0-test inet dhcp\n'

scheme = Scheme('wlan0', 'test', {
scheme = self.Scheme('wlan0', 'test', {
'wpa-ssid': 'workwifi',
})

self.assertEqual(str(scheme), 'iface wlan0-test inet dhcp\n wpa-ssid workwifi\n')

def test_find(self):
work = Scheme.find('wlan0', 'work')
work = self.Scheme.find('wlan0', 'work')

assert work.options['wpa-ssid'] == 'workwifi'

def test_save(self):
scheme = Scheme('wlan0', 'test')
scheme = self.Scheme('wlan0', 'test')
scheme.save()

assert Scheme.find('wlan0', 'test')
assert self.Scheme.find('wlan0', 'test')
15 changes: 14 additions & 1 deletion wifi/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import wifi.subprocess_compat as subprocess
from wifi.pbkdf2 import pbkdf2_hex
from wifi.utils import ensure_file_exists


def configuration(cell, passkey=None):
Expand Down Expand Up @@ -44,6 +45,17 @@ class provides a Python interface to the /etc/network/interfaces

interfaces = '/etc/network/interfaces'

@classmethod
def for_file(cls, interfaces):
"""
A class factory for providing a nice way to specify the interfaces file
that you want to use. Use this instead of directly overwriting the
interfaces Class attribute if you care about thread safety.
"""
return type(cls)(cls.__name__, (cls,), {
'interfaces': interfaces,
})

def __init__(self, interface, name, options=None):
self.interface = interface
self.name = name
Expand All @@ -66,6 +78,7 @@ def all(cls):
"""
Returns an generator of saved schemes.
"""
ensure_file_exists(cls.interfaces)
with open(cls.interfaces, 'r') as f:
return extract_schemes(f.read())

Expand Down Expand Up @@ -96,7 +109,7 @@ def save(self):
"""
Writes the configuration to the :attr:`interfaces` file.
"""
assert not Scheme.find(self.interface, self.name)
assert not self.find(self.interface, self.name), "This scheme already exists"

with open(self.interfaces, 'a') as f:
f.write('\n')
Expand Down
7 changes: 7 additions & 0 deletions wifi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ def print_table(matrix, sep=' ', file=sys.stdout, *args, **kwargs):

for row in matrix:
print(format.format(*row).strip(), file=file, *args, **kwargs)


def ensure_file_exists(filename):
"""
http://stackoverflow.com/a/12654798/1013960
"""
open(filename, 'a').close()