Skip to content

Commit b2b7dbc

Browse files
committed
modernize, skip non-wifi ci
1 parent 228492f commit b2b7dbc

File tree

9 files changed

+169
-104
lines changed

9 files changed

+169
-104
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.mypy_cache/
12
.pytest_cache/
23
*.log
34
*.txt

MozLoc.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
Uses ``nmcli`` from Linux only. Could be extended to other tools and OS.
99
"""
1010
from mozloc import logwifiloc
11+
from argparse import ArgumentParser
1112

1213

13-
if __name__ == '__main__':
14+
def main():
1415
"""
1516
output: lat lon [deg] accuracy [m]
1617
"""
17-
import signal
18-
signal.signal(signal.SIGINT, signal.SIG_DFL)
19-
20-
from argparse import ArgumentParser
2118
p = ArgumentParser()
22-
p.add_argument('logfile',help='logfile to append location to',nargs='?')
23-
p.add_argument('-T','--cadence',help='how often to ping [sec]. Some laptops cannot go faster than 30 sec.',
24-
default=60,type=float)
19+
p.add_argument('logfile', help='logfile to append location to', nargs='?')
20+
p.add_argument('-T', '--cadence', help='how often to ping [sec]. Some laptops cannot go faster than 30 sec.',
21+
default=60, type=float)
2522
p = p.parse_args()
2623

27-
2824
logwifiloc(p.cadence, p.logfile)
25+
26+
27+
if __name__ == '__main__':
28+
main()

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[![Python versions (PyPI)](https://img.shields.io/pypi/pyversions/mozilla-location-python.svg)](https://pypi.python.org/pypi/mozilla-location-python)
2-
32
[![Distribution format (PyPI)](https://img.shields.io/pypi/format/mozilla-location-python.svg)](https://pypi.python.org/pypi/mozilla-location-python)
43

54
# mozilla-location-python

csv2kml.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#!/usr/bin/env python
22
"""convert logged positions to KML"""
33
from mozloc import csv2kml
4+
from argparse import ArgumentParser
45

5-
if __name__ == '__main__':
6-
from argparse import ArgumentParser
6+
7+
def main():
78
p = ArgumentParser()
8-
p.add_argument('logfn',help='csv logfile to read')
9-
p.add_argument('kmlfn',help='kml filename to write')
9+
p.add_argument('logfn', help='csv logfile to read')
10+
p.add_argument('kmlfn', help='kml filename to write')
1011
p = p.parse_args()
1112

12-
1313
csv2kml(p.logfn, p.kmlfn)
14+
15+
16+
if __name__ == '__main__':
17+
main()

mozloc/__init__.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@
66
from datetime import datetime
77
from time import sleep
88
from pathlib import Path
9+
from typing import Dict, Any, Optional
910
#
10-
URL='https://location.services.mozilla.com/v1/geolocate?key=test'
11-
NMCMD = ['nmcli','-g','SSID,BSSID,FREQ,SIGNAL','device','wifi'] # Debian stretch, Ubuntu 18.04
12-
NMLEG = ['nmcli','-t','-f','SSID,BSSID,FREQ,SIGNAL','device','wifi'] # ubuntu 16.04
13-
NMSCAN = ['nmcli','device','wifi','rescan']
14-
HEADER='time lat lon accuracy NumBSSIDs'
11+
URL = 'https://location.services.mozilla.com/v1/geolocate?key=test'
12+
NMCMD = ['nmcli', '-g', 'SSID,BSSID,FREQ,SIGNAL', 'device', 'wifi'] # Debian stretch, Ubuntu 18.04
13+
NMLEG = ['nmcli', '-t', '-f', 'SSID,BSSID,FREQ,SIGNAL', 'device', 'wifi'] # ubuntu 16.04
14+
NMSCAN = ['nmcli', 'device', 'wifi', 'rescan']
15+
HEADER = 'time lat lon accuracy NumBSSIDs'
1516

1617
# %%
17-
def logwifiloc(T:float, logfile:Path):
18+
19+
20+
def logwifiloc(T: float, logfile: Path):
1821

1922
if logfile:
2023
logfile = Path(logfile).expanduser()
2124
with logfile.open('a') as f:
2225
f.write(HEADER+'\n')
2326

24-
2527
print(f'updating every {T} seconds')
2628
print(HEADER)
2729

2830
nm_config_check()
29-
sleep(0.5) # nmcli errored for less than about 0.2 sec.
31+
sleep(0.5) # nmcli errored for less than about 0.2 sec.
3032
while True:
3133
loc = get_nmcli()
3234
if loc is None:
@@ -42,34 +44,37 @@ def logwifiloc(T:float, logfile:Path):
4244

4345
sleep(T)
4446

47+
4548
def nm_config_check():
46-
# %% check that NetworkManager CLI is available and WiFi is active
49+
# %% check that NetworkManager CLI is available and WiFi is active
4750
try:
48-
ret = subprocess.check_output(['nmcli','-t','radio','wifi'], universal_newlines=True, timeout=1.).strip().split(':')
51+
ret = subprocess.check_output(['nmcli', '-t', 'radio', 'wifi'], universal_newlines=True, timeout=1.).strip().split(':')
4952
except FileNotFoundError:
5053
raise OSError('CUrrently this program relies on NetworkManager')
5154

52-
assert 'enabled' in ret and not 'disabled' in ret,'must enable WiFi, perhaps via nmcli radio wifi on'
55+
assert 'enabled' in ret and 'disabled' not in ret, 'must enable WiFi, perhaps via nmcli radio wifi on'
5356

5457
# %%
55-
def get_nmcli() -> dict:
58+
59+
60+
def get_nmcli() -> Optional[Dict[str, Any]]:
5661

5762
ret = subprocess.check_output(NMCMD, universal_newlines=True, timeout=1.)
58-
sleep(0.5) # nmcli errored for less than about 0.2 sec.
63+
sleep(0.5) # nmcli errored for less than about 0.2 sec.
5964
try:
60-
subprocess.check_call(NMSCAN, timeout=1.) # takes several seconds to update, so do it now.
65+
subprocess.check_call(NMSCAN, timeout=1.) # takes several seconds to update, so do it now.
6166
except subprocess.CalledProcessError as e:
6267
logging.error(f'consider slowing scan cadence. {e}')
6368

6469
dat = pandas.read_csv(StringIO(ret), sep=r'(?<!\\):', index_col=False,
6570
header=0, encoding='utf8', engine='python',
66-
dtype=str, usecols=[0,1,3],
67-
names=['ssid','macAddress','signalStrength'])
71+
dtype=str, usecols=[0, 1, 3],
72+
names=['ssid', 'macAddress', 'signalStrength'])
6873
# %% optout
6974
dat = dat[~dat['ssid'].str.endswith('_nomap')]
7075
# %% cleanup
71-
dat['ssid'] = dat['ssid'].str.replace('nan','')
72-
dat['macAddress'] = dat['macAddress'].str.replace(r'\\:',':')
76+
dat['ssid'] = dat['ssid'].str.replace('nan', '')
77+
dat['macAddress'] = dat['macAddress'].str.replace(r'\\:', ':')
7378
# %% JSON
7479
jdat = dat.to_json(orient='records')
7580
jdat = '{ "wifiAccessPoints":' + jdat + '}'
@@ -79,21 +84,22 @@ def get_nmcli() -> dict:
7984
req = requests.post(URL, data=jdat)
8085
if req.status_code != 200:
8186
logging.error(req.text)
82-
return
87+
return None
8388
except requests.exceptions.ConnectionError as e:
8489
logging.error(f'no network connection. {e}')
85-
return
90+
return None
8691
# %% process MLS response
8792
jres = req.json()
8893
loc = jres['location']
8994
loc['accuracy'] = jres['accuracy']
90-
loc['N'] = dat.shape[0] # number of BSSIDs used
95+
loc['N'] = dat.shape[0] # number of BSSIDs used
9196
loc['t'] = datetime.now()
9297

9398
return loc
9499
# %%
95100

96-
def csv2kml(csvfn:Path, kmlfn:Path):
101+
102+
def csv2kml(csvfn: Path, kmlfn: Path):
97103
from simplekml import Kml
98104

99105
"""
@@ -108,7 +114,7 @@ def csv2kml(csvfn:Path, kmlfn:Path):
108114
dat = pandas.read_csv(csvfn, sep=' ', index_col=0, header=0)
109115

110116
t = dat.index.tolist()
111-
lla = dat.loc[:,['lon','lat']].values
117+
lla = dat.loc[:, ['lon', 'lat']].values
112118
# %% write KML
113119
"""
114120
http://simplekml.readthedocs.io/en/latest/geometries.html#gxtrack
@@ -118,7 +124,7 @@ def csv2kml(csvfn:Path, kmlfn:Path):
118124
kml = Kml(name='My Kml')
119125
trk = kml.newgxtrack(name='My Track')
120126
trk.newwhen(t) # list of times. MUST be format 2010-05-28T02:02:09Z
121-
trk.newgxcoord(lla.tolist()) #list of lon,lat,alt, NOT ndarray!
127+
trk.newgxcoord(lla.tolist()) # list of lon,lat,alt, NOT ndarray!
122128

123129
# just a bunch of points
124130
# for i,p in enumerate(lla): # iterate over rows

setup.cfg

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[metadata]
2+
name = mozilla-location-python
3+
version = 0.5.1
4+
author = Michael Hirsch, Ph.D.
5+
url = https://github.com/scivision/mozilla-location-wifi-python
6+
description = Using Mozilla Location services, log location vs. time using WiFi or convert to KML.
7+
keywords =
8+
wifi
9+
geolocation
10+
classifiers =
11+
Development Status :: 4 - Beta
12+
Environment :: Console
13+
Intended Audience :: Information Technology
14+
Intended Audience :: System Administrators
15+
License :: OSI Approved :: MIT License
16+
Operating System :: OS Independent
17+
Programming Language :: Python :: 3.6
18+
Programming Language :: Python :: 3.7
19+
Topic :: System :: Networking
20+
Topic :: Utilities
21+
license_file = LICENSE
22+
long_description = file: README.md
23+
long_description_content_type = text/markdown
24+
25+
[options]
26+
python_requires = >= 3.6
27+
setup_requires =
28+
setuptools >= 38.6
29+
pip >= 10
30+
twine >= 1.11
31+
include_package_data = True
32+
packages = find:
33+
install_requires =
34+
pandas
35+
requests
36+
37+
[options.extras_require]
38+
tests =
39+
pytest
40+
pytest-cov
41+
coveralls
42+
flake8
43+
mypy
44+
kml =
45+
simplekml
46+
47+
[options.entry_points]
48+
console_scripts =
49+
MozLoc = MozLoc:main
50+
csv2kml = csv2kml:main
51+
52+
53+
[flake8]
54+
max-line-length = 132
55+
exclude = .git,__pycache__,.eggs/,doc/,docs/,build/,dist/,archive/
56+
57+
[coverage:run]
58+
cover_pylib = false
59+
omit =
60+
/home/travis/virtualenv/*
61+
*/site-packages/*
62+
*/bin/*
63+
64+
[coverage:report]
65+
exclude_lines =
66+
pragma: no cover
67+
def __repr__
68+
RuntimeError
69+
NotImplementedError
70+
ImportError
71+
KeyError
72+
FileNotFoundError
73+
CalledProcessError
74+
logging.warning
75+
ValueError
76+
TypeError

setup.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,3 @@
11
#!/usr/bin/env python
2-
install_requires = ['pandas','requests']
3-
tests_require = ['nose','coveralls']
4-
# %%
5-
from setuptools import setup,find_packages
6-
7-
setup(name='mozilla-location-python',
8-
packages=find_packages(),
9-
author='Michael Hirsch, Ph.D.',
10-
version='0.5.1',
11-
url='https://github.com/scivision/mozilla-location-wifi-python',
12-
long_description=open('README.md').read(),
13-
long_description_content_type="text/markdown",
14-
description='Using Mozilla Location services, log location vs. time using WiFi or convert to KML.',
15-
install_requires=install_requires,
16-
tests_require=tests_require,
17-
extras_require={'tests':tests_require,
18-
'kml':['simplekml']},
19-
python_requires='>=3.6',
20-
classifiers=[
21-
'Development Status :: 4 - Beta',
22-
'Environment :: Console',
23-
'Intended Audience :: Information Technology',
24-
'Intended Audience :: System Administrators',
25-
'License :: OSI Approved :: MIT License',
26-
'Operating System :: OS Independent',
27-
'Programming Language :: Python :: 3.6',
28-
'Programming Language :: Python :: 3.7',
29-
'Topic :: System :: Networking',
30-
'Topic :: Utilities',
31-
],
32-
)
33-
2+
from setuptools import setup
3+
setup()

tests/test_all.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
11
#!/usr/bin/env python
2-
from numpy.testing import run_module_suite
2+
import pytest
33
import mozloc
44
import datetime
5+
import os
6+
7+
CI = bool(os.environ['CI']) if 'CI' in os.environ else False
8+
59

610
def test_nm_loc():
711
loc = mozloc.get_nmcli()
8-
assert isinstance(loc,dict)
9-
assert isinstance(loc['t'],datetime.datetime)
12+
assert isinstance(loc, dict)
13+
assert isinstance(loc['t'], datetime.datetime)
1014

15+
pytest.mark.skipif(CI, reason="CI doesn't have WiFi")
1116
def test_nm_connection():
1217
mozloc.nm_config_check()
1318

1419

15-
def gen_kml():
16-
from simplekml import Kml
17-
# Data for the track
18-
when = ["2010-05-28T02:02:09Z",
19-
"2010-05-28T02:02:35Z",
20-
"2010-05-28T02:02:44Z",
21-
"2010-05-28T02:02:53Z",
22-
"2010-05-28T02:02:54Z",
23-
"2010-05-28T02:02:55Z",
24-
"2010-05-28T02:02:56Z"]
25-
26-
coord = [(-122.207881,37.371915,156.000000),
27-
(-122.205712,37.373288,152.000000),
28-
(-122.204678,37.373939,147.000000),
29-
(-122.203572,37.374630,142.199997),
30-
(-122.203451,37.374706,141.800003),
31-
(-122.203329,37.374780,141.199997),
32-
(-122.203207,37.374857,140.199997)]
33-
34-
35-
kml = Kml(name="Tracks",)
36-
trk = kml.newgxtrack(name='test')
37-
38-
trk.newwhen(when)
39-
trk.newgxcoord(coord)
40-
41-
kml.save('Test.kml')
42-
43-
4420
if __name__ == '__main__':
45-
run_module_suite()
21+
pytest.main()

tests/test_kml.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from simplekml import Kml
3+
4+
5+
def gen_kml():
6+
# Data for the track
7+
when = ["2010-05-28T02:02:09Z",
8+
"2010-05-28T02:02:35Z",
9+
"2010-05-28T02:02:44Z",
10+
"2010-05-28T02:02:53Z",
11+
"2010-05-28T02:02:54Z",
12+
"2010-05-28T02:02:55Z",
13+
"2010-05-28T02:02:56Z"]
14+
15+
coord = [(-122.207881, 37.371915, 156.000000),
16+
(-122.205712, 37.373288, 152.000000),
17+
(-122.204678, 37.373939, 147.000000),
18+
(-122.203572, 37.374630, 142.199997),
19+
(-122.203451, 37.374706, 141.800003),
20+
(-122.203329, 37.374780, 141.199997),
21+
(-122.203207, 37.374857, 140.199997)]
22+
23+
kml = Kml(name="Tracks",)
24+
trk = kml.newgxtrack(name='test')
25+
26+
trk.newwhen(when)
27+
trk.newgxcoord(coord)
28+
29+
kml.save('Test.kml')
30+
31+
32+
if __name__ == '__main__':
33+
pytest.main()

0 commit comments

Comments
 (0)