#! /usr/bin/env python

CopyrightInfo = '''
Copyright 2010 Nathan Wilcox

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys, pprint, __builtin__


def main(args = sys.argv[1:]):
    expr, strs = args[0], args[1:]
    scope = MagicScope(args = strs)
    for (i, arg) in enumerate(strs):
        scope['a%d' % i] = arg
    result = eval(expr, {}, scope)
    display(result)


def display(obj):
    if not hasattr(obj, 'next'):
        obj = [obj]

    for elem in obj:
        print str(elem)



class AutoImporter (object):
    def __init__(self, name, parent = None):
        self.name = name
        self.parent = parent
        mod = __import__(self.fullname)
        for name in self.fullname.split('.')[1:]:
            mod = getattr(mod, name)
        self.mod = mod

    @property
    def fullname(self):
        if self.parent:
            return '%s.%s' % (self.parent.fullname, self.name)
        else:
            return self.name

    def __getattr__(self, name):
        try:
            return getattr(self.mod, name)
        except AttributeError:
            return AutoImporter(name, self)



class MagicScope (dict):
    def __init__(self, **kw):
        dict.__init__(self, vars(__builtin__))
        self.update(kw)

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            try:
                method = getattr(self, 'magic_' + key)
            except AttributeError:
                return AutoImporter(key)

            return method()

    def magic_pp(self):
        return pprint.pprint

    def magic_ri(self):
        self['ri'] = ri = sys.stdin.read()
        return ri

    def magic_i(self):
        return self['ri'].strip()


if __name__ == '__main__':
    main()
