#!/usr/bin/env python

# Script will import fakeredis and list what
# commands it does not have support for, based on the
# command list from:
# https://raw.github.com/antirez/redis-doc/master/commands.json
# Because, who wants to do this by hand...

from __future__ import print_function
import os
import json
import inspect
from collections import OrderedDict
import requests

import fakeredis

THIS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
COMMANDS_FILE = os.path.join(THIS_DIR, '.commands.json')
COMMANDS_URL = 'https://raw.github.com/antirez/redis-doc/master/commands.json'

if not os.path.exists(COMMANDS_FILE):
    contents = requests.get(COMMANDS_URL).content
    open(COMMANDS_FILE, 'wb').write(contents)
commands = json.load(open(COMMANDS_FILE), object_pairs_hook=OrderedDict)
for k, v in list(commands.items()):
    commands[k.lower()] = v
    del commands[k]


implemented_commands = set()
for name, method in inspect.getmembers(fakeredis.server.FakeSocket):
    if hasattr(method, '_fakeredis_sig'):
        implemented_commands.add(name)
# Currently no programmatic way to discover implemented subcommands
implemented_commands.add('script load')

unimplemented_commands = []
for command in commands:
    if command not in implemented_commands:
        unimplemented_commands.append(command)

# Group by 'group' for easier to read output
groups = OrderedDict()
for command in unimplemented_commands:
    group = commands[command]['group']
    groups.setdefault(group, []).append(command)

print("""

Unimplemented Commands
======================

All of the redis commands are implemented in fakeredis with
these exceptions:

""")

for group in groups:
    print(group)
    print("-" * len(str(group)))
    print()
    for command in groups[group]:
        print(" *", command)
    print("\n")
