Skip to content

Commit

Permalink
Improved scan results regex pattern. Closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ege committed Oct 26, 2021
1 parent 202b317 commit ef08be4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ var/

# Others
.DS_Store

# Generated
.idea
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

## [0.1.1] - 2021-10-27
### Added
- CHANGELOG file.

### Changed
- Improved scan result pattern matching.

[0.0.1]: https://github.com/egemenyildiz/airport-py/releases/tag/v0.1.1
3 changes: 3 additions & 0 deletions airport/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SCAN_RESULTS_GROUPED_PATTERN = (r'(?P<ssid>.+)\s+(?P<bssid>([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))\s+(?P<rssi>[0-9+\- '
r']{1,4})\s+(?P<channel>[0-9,\-+]{1,6})\s+(?P<high_throughput>[YN])\s+('
r'?P<country_code>[A-Z\-]{2,3})\s+(?P<security>.+)$')
34 changes: 19 additions & 15 deletions airport/_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
import subprocess
import re
import _datatypes as datatypes
import os
import os

from airport._constants import SCAN_RESULTS_GROUPED_PATTERN

AIRPORT_PATH = [os.environ.get(
'AIRPORT_PATH',
Expand All @@ -16,24 +19,24 @@ def call_cmd(*args):
return out


def split_lines(lines, start=0):
return lines.split('\n')[start:]
def split_lines(lines, start=0, remove_empty_lines=True):
split = lines.split('\n')[start:]
if remove_empty_lines:
return filter(None, split)
return split


def reformat_scan_results(results):
def reformat_scan_result_line(result):
try:
result = re.split('\s+', result)
node_args = (result[1:5] + [True if result[5] == 'Y' else False, ] +
[result[6] if result[6] != '--' else None] +
[' '.join(result[7:]).strip()])
except IndexError:
pass
else:
return datatypes.NodeResult(*node_args)
def clean_line(result):
matches = re.match(re.compile(SCAN_RESULTS_GROUPED_PATTERN, re.IGNORECASE), result)
if matches:
cleaned_node = {k: v.strip() for k, v in matches.groupdict().items()}
return datatypes.NodeResult(**cleaned_node)
logging.error('Parse error: {}'.format(result))

results_in_lines = split_lines(results, start=1)
return filter(None, map(reformat_scan_result_line, results_in_lines))
lines = split_lines(results, start=1)
cleaned = filter(None, map(clean_line, lines))
return cleaned


def reformat_info_result(result):
Expand All @@ -45,5 +48,6 @@ def reformat_info_result_line(line):
info_args = map(reformat_info_result_line, results_in_lines)
return datatypes.InfoResult(*info_args)


def build_airport_cmd(params):
return AIRPORT_PATH + params
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='airport-py',
version='0.1.0',
version='0.1.1',
description='Mac OS X airport command result parser',
long_description=readme,
author='Egemen Yildiz',
Expand Down

0 comments on commit ef08be4

Please sign in to comment.