#!/usr/bin/env python
from yapf import yapf_api

'''print all ACTION_COMMANDS and analys_commands in cpptraj

The output is used for checking if specific command is valid in pytraj.pmap

Example
-------
python  find_action_and_analysis_commands.py > cpptraj_commands.py

# reformat cpptraj_commands.py to make it prettier
yapf -i cpptraj_commands.py

CPPTRAJHOME must be set
'''
# parse those lines in cpptraj/src/Command.cpp
#  { ACTION, "image", Action_Image::Alloc, Action_Image::Help, AddAction },
#  { ANALYSIS, "multicurve", Analysis_Multicurve::Alloc, Analysis_Multicurve::Help, AddAnalysis },

import os

cpptrajhome = os.environ.get('CPPTRAJHOME', '')

if not cpptrajhome:
    raise EnvironmentError('must set CPPTRAJHOME')

with open(cpptrajhome + '/src/Command.cpp') as fh:
    lines = fh.readlines()
    cpptraj_actlist = [line.split()[1]
                        for line in lines if line.startswith('#include "Action_')]
    cpptraj_actlist = sorted([word.replace('"Action_', '').replace('.h"', '').lower()
                              for word in cpptraj_actlist])
    cpptraj_analist = [line.split()[1]
                       for line in lines if line.startswith('#include "Analysis_')]
    cpptraj_analist = sorted([word.replace('"Analysis_', '').replace('.h"', '').lower()
                              for word in cpptraj_analist])

excluded_command_from_pmap = [
    'hbond'
]

code = """
# this file was auto-generated by scripts/find_action_and_ANALYSIS_COMMANDS.py"
# require: yapf

ACTION_COMMANDS = {cpptraj_actlist}

ANALYSIS_COMMANDS = {cpptraj_analist}

PMAP_EXCLUDED_COMMANDS = {excluded_command_from_pmap}
""".format(cpptraj_actlist=cpptraj_actlist,
           cpptraj_analist=cpptraj_analist,
           excluded_command_from_pmap=excluded_command_from_pmap)
print(yapf_api.FormatCode(code)[0])
