#!/usr/bin/env python """ The latest version of this package is available at: <http://github.com/jantman/wifi-survey-heatmap> ################################################################################## Copyright 2018 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com> This file is part of wifi-survey-heatmap, also known as wifi-survey-heatmap. wifi-survey-heatmap is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. wifi-survey-heatmap is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with wifi-survey-heatmap. If not, see <http://www.gnu.org/licenses/>. The Copyright and Authors attributions contained herein may not be removed or otherwise altered, except to add the Author attribution of a contributor to this work. (Additional Terms pursuant to Section 7b of the AGPL v3) ################################################################################## While not legally required, I sincerely request that anyone who finds bugs please submit them at <https://github.com/jantman/wifi-survey-heatmap> or to me via email, and that you send any contributions or improvements either as a pull request on GitHub, or to me via email. ################################################################################## AUTHORS: Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com> ################################################################################## """ import sys import argparse import logging import os from wifi_survey_heatmap.collector import Collector FORMAT = "[%(asctime)s %(levelname)s] %(message)s" logging.basicConfig(level=logging.WARNING, format=FORMAT) logger = logging.getLogger() class CliWrapper(object): def run(self, ifname, server): if os.geteuid() != 0: raise RuntimeError('ERROR: This script must be run as root/sudo.') c = Collector(ifname, server) print(c.run()) def parse_args(argv): """ parse arguments/options this uses the new argparse module instead of optparse see: <https://docs.python.org/2/library/argparse.html> """ p = argparse.ArgumentParser(description='wifi scan CLI') p.add_argument('-v', '--verbose', dest='verbose', action='count', default=0, help='verbose output. specify twice for debug-level output.') p.add_argument('INTERFACE', type=str, help='Wireless interface name') p.add_argument('SERVER', type=str, help='iperf3 server IP or hostname') args = p.parse_args(argv) return args def set_log_info(): """set logger level to INFO""" set_log_level_format(logging.INFO, '%(asctime)s %(levelname)s:%(name)s:%(message)s') def set_log_debug(): """set logger level to DEBUG, and debug-level output format""" set_log_level_format( logging.DEBUG, "%(asctime)s [%(levelname)s %(filename)s:%(lineno)s - " "%(name)s.%(funcName)s() ] %(message)s" ) def set_log_level_format(level, format): """ Set logger level and format. :param level: logging level; see the :py:mod:`logging` constants. :type level: int :param format: logging formatter format string :type format: str """ formatter = logging.Formatter(fmt=format) logger.handlers[0].setFormatter(formatter) logger.setLevel(level) def main(): args = parse_args(sys.argv[1:]) # set logging level if args.verbose > 1: set_log_debug() elif args.verbose == 1: set_log_info() CliWrapper().run(args.INTERFACE, args.SERVER) if __name__ == "__main__": main()