Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Created June 17, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickelectric/a648d05800703af9e46a218ed496e9b1 to your computer and use it in GitHub Desktop.
Save patrickelectric/a648d05800703af9e46a218ed496e9b1 to your computer and use it in GitHub Desktop.
import collections
import jinja2
import json
import re
import requests
from bs4 import BeautifulSoup
# Maybe we should generate it from wpa_cli help
class Parser():
URL = 'https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html'
commands = collections.OrderedDict()
__got_first_command = False
@staticmethod
def generate_pretty_info(info, prefix):
if len(info) < 70:
return info
final_string = ''
words = info.replace('\n', ' ').split(' ')
for word in words:
final_string += word + ' '
if len(final_string.split('\n')[-1]) > 70:
final_string += '\n' + prefix
return final_string
@staticmethod
def filter_arguments(argument):
required_regex_result = re.findall(r'\<(?P<required>[A-z|\s|\_]*)\>', argument)
optional_regex_result = re.findall(r'\[(?P<optional>[A-z|\s|\_]*)\]', argument)
required_regex_result = [x.replace(' ', '_') for x in required_regex_result]
optional_regex_result = [x.replace(' ', '_') for x in optional_regex_result]
return (required_regex_result, optional_regex_result)
@staticmethod
def generate_arguments(argument, with_value = True):
required_arguments, optional_arguments = Parser.filter_arguments(argument)
argument_string = required_arguments
for optional_argument in optional_arguments:
argument_string += [optional_argument + (' = \'\'' if with_value else '')]
args = ', '.join(argument_string)
if not args:
return 'self'
return 'self, ' + args
def generate_wpa_supplicant_command(self, command_name):
if not command_name in self.commands:
return ''
command_dict = self.commands[command_name]
command_string = '\'' + command_dict['command'] + '\''
if command_dict['arguments']:
required_arguments, optional_arguments = Parser.filter_arguments(command_dict['arguments'])
arguments = required_arguments + optional_arguments
command_string += ' + \' \' + \' \'.join([{}])'.format(', '.join(arguments))
return command_string
@staticmethod
def is_command_item(item):
# Check if the element is not empty
# Commands are inside 'h2' elements
# Commands contain two things, the tag and the string with the command name
# Commands have a tag id with ctrl_iface prefix
return item and \
item.name == 'h2' and \
len(item.contents) == 2 and \
'ctrl_iface' in item.contents[0].attrs['id']
def run(self):
response = requests.get(self.URL)
soup = BeautifulSoup(response.text, "html.parser")
textblock = soup.find("div", {"class": "textblock"})
for element in textblock:
if Parser.is_command_item(element):
_, command = element.contents
if command not in self.commands:
command = command.strip()
arguments = command.split(' ')
command = arguments.pop(0)
command_name = command.lower().replace('-', '_')
arguments = ' '.join(arguments) # put arguments together again
self.commands[command_name] = {'command': command}
self.commands[command_name]['arguments'] = arguments
self.__got_first_command = True
elif self.__got_first_command:
# Get last command
command = list(self.commands.keys())[-1]
if element.name == 'p':
self.commands[command]['info'] = ' '.join(str(x) for x in element.contents)
if element.name == 'pre':
self.commands[command]['output'] = element.string
parser = Parser()
parser.run()
#print(json.dumps(parser.commands, sort_keys=True, indent=4))
template = jinja2.Template("""
{% for command in parser.commands.keys() -%}
def send_command_{{command}}({{parser.generate_arguments(parser.commands[command].arguments)}}):
\"\"\"Send message: {{parser.commands[command].command}}
{{parser.generate_pretty_info(parser.commands[command].info, ' ')}}
\"\"\"
self.send_command({{parser.generate_wpa_supplicant_command(command)}})
{% endfor %}
""")
output = template.render(parser = parser)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment