#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import
from __future__ import print_function

import pygrep
import argparse


parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', action='store_true',
                    help='only gather the list of entrypoints')
parser.add_argument('ident')
parser.add_argument('path', nargs='+')

args = parser.parse_args()
if args.list:
    entrypoints = set()
    def handler(path, context, node, ident):
        entrypoints.add('.'.join(ident))
else:
    def handler(path, context, node, ident):
        fd = ''
        if context:
            fd = '(%s)' % '.'.join([t[0] for t in context])
        print('%s%s:%s\t%s' % (path, fd, node.lineno, '.'.join(ident)))

pygrep.handleIdent(args.ident, args.path, handler)
if args.list:
    print('\n'.join(sorted(entrypoints)))
