<commit_before># Copyright 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from argparse import ArgumentParser
from cmd import Cmd
from functools import partial
from logging import debug

from . import ui
from . import format_object
from lib import model


class CommandMixin(object):
    def __init__(self, engine):
        self.engine = engine


class Quit(CommandMixin):
    def do_quit(self, line):
        self.engine.save()
        return True

    def help_quit(self):
        print("Quit the program")
    do_q = do_quit
    do_EOF = do_quit


class Show(CommandMixin):
    def do_show(self, line):
        show_planets = partial(format_object.print_object,
                               format_object.user_planets,
                               self.engine.state.user)
        show_user = partial(format_object.print_object, format_object.user,
                            self.engine.state.user)
        try:
            parser = ArgumentParser(prog='show',
                                    description=self.help_show(True))
            parser.add_argument('-p', '--planets', action='store_const',
                                dest='subject', const=show_planets)
            parser.add_argument('-u', '--user', action='store_const',
                                dest='subject', const=show_user)
            parser.add_argument('-v', '--verbose', action='store_true',
                                dest='verbose', default=False)

            (args, params) = parser.parse_known_args(line.split(' '))
            setattr(args, 'params', params)
            if args.subject is None:  # no arguments
                args.subject = show_planets
            args.subject(verbose=args.verbose)
        except SystemExit:
            pass
        return False

    def help_show(self, no_print=None):
        msg = "Show various things. Use 'show --help' for more."
        if no_print:
            return msg
        print(msg)
    do_sh = do_show
    do_s = do_show


class Debug(CommandMixin):
    def __print_state(self):
        print(self.engine.state)

    def do_dbg(self, line):
        try:
            parser = ArgumentParser(
                prog='dbg', description=self.help_dbg(True))
            parser.add_argument(
                '-ps', '--print-state', action='store_const',
                dest='action', const=self.print_state,
                help='print full game state')
            (args, unrecognized) = parser.parse_known_args(line.split(' '))
            if args.action is None:
                self.print_state()
            else:
                args.action()
        except SystemExit:
            pass
        return False

    def help_dbg(self, no_print=None):
        hlp = "Provides access to some debugging commands."
        if no_print:
            return hlp
        print(hlp)


class Planet(CommandMixin):
    def __build(self, args):
        if args.building is None:
            format_object.print_object(
                format_object.planet_available_buildings, self.engine,
                args.planet, args.verbose)
            return
        coord, planet = self.engine.state.user.get_planet(args.planet)
        if planet.build(args.building):
            print("Congrats, you just built a %s" % args.building)
        else:
            print("Construction Failed! Make sure you have enough "
                  "resources and try again.")

    def __show_planets(self, args):
        if args.planet in [None, '']:
            format_object.print_object(format_object.user_planets,
                                       self.engine.state.user, args.verbose)
            return
        coord, planet = self.engine.state.user.get_planet(args.planet)
        print(planet.show(verbose=args.verbose))

    def do_planet(self, line):
        try:
            parser = ArgumentParser(prog='planet',
                                    description=self.help_show(True))
            parser.add_argument('-v', '--verbose', action='store_true',
                                dest='verbose', default=False)
            parser.add_argument('-b', '--build', action='store_const',
                                dest='action', const=self.__build)
            parser.add_argument('planet', help='Limit the effect of the '
                                'command to this planet (if supported)',
                                default=None, nargs='?')
            parser.add_argument('building', help='Name of building type to '
                                'pass to the command %s (if supported, '
                                'PLANET must be supplied first)'
                                % str(model.get_all_building_abbr()),
                                default=None, nargs='?')

            (args, params) = parser.parse_known_args(line.split(' '))
            setattr(args, 'params', params)
            if args.action is None:
                args.action = self.__show_planets
            args.action(args=args)
        except SystemExit:
            pass
        return False

    def help_planet(self):
        print("Access to things on planets")
    do_p = do_planet
    do_pl = do_planet


class User(CommandMixin):
    def do_user(self, line):
        # TODO: implement change name
        print('Not implemented yet')

    def help_user(self):
        print("Access to user admin tasks")
    do_u = do_user


class SpaceCmdInterpreter(Cmd, Quit, Debug, Show, Planet, User):
    def __init__(self, engine):
        super(SpaceCmdInterpreter, self).__init__()
        self.engine = engine
        self.prompt = 'space> '
        self.doc_header = 'Space Commands'
        self.undoc_header = 'Alias Commands'

    def start(self):
        try:
            try:
                debug('Trying to load the saved game...')
                self.engine.load()
            except IOError:
                debug('No save game, starting new game...')
                self.start_new_game()
            debug('Starting interpreter...')
            self.cmdloop()
        except (KeyboardInterrupt, SystemExit):
            debug('Recieved Interrupt.')
        finally:
            self.engine.save()

    def start_new_game(self):
        msg = 'Do you want to start a new game?'
        ui.input_bool(msg) or sys.exit(0)
        debug('Creating a new game')
        self.engine.new_game(ui.get_new_game_info)<commit_msg>cmdline interpreter: add case to log unhandled exceptions before borking<commit_after># Copyright 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from argparse import ArgumentParser
from cmd import Cmd
from functools import partial
from logging import debug

from . import ui
from . import format_object
from lib import model


class CommandMixin(object):
    def __init__(self, engine):
        self.engine = engine


class Quit(CommandMixin):
    def do_quit(self, line):
        self.engine.save()
        return True

    def help_quit(self):
        print("Quit the program")
    do_q = do_quit
    do_EOF = do_quit


class Show(CommandMixin):
    def do_show(self, line):
        show_planets = partial(format_object.print_object,
                               format_object.user_planets,
                               self.engine.state.user)
        show_user = partial(format_object.print_object, format_object.user,
                            self.engine.state.user)
        try:
            parser = ArgumentParser(prog='show',
                                    description=self.help_show(True))
            parser.add_argument('-p', '--planets', action='store_const',
                                dest='subject', const=show_planets)
            parser.add_argument('-u', '--user', action='store_const',
                                dest='subject', const=show_user)
            parser.add_argument('-v', '--verbose', action='store_true',
                                dest='verbose', default=False)

            (args, params) = parser.parse_known_args(line.split(' '))
            setattr(args, 'params', params)
            if args.subject is None:  # no arguments
                args.subject = show_planets
            args.subject(verbose=args.verbose)
        except SystemExit:
            pass
        return False

    def help_show(self, no_print=None):
        msg = "Show various things. Use 'show --help' for more."
        if no_print:
            return msg
        print(msg)
    do_sh = do_show
    do_s = do_show


class Debug(CommandMixin):
    def __print_state(self):
        print(self.engine.state)

    def do_dbg(self, line):
        try:
            parser = ArgumentParser(
                prog='dbg', description=self.help_dbg(True))
            parser.add_argument(
                '-ps', '--print-state', action='store_const',
                dest='action', const=self.print_state,
                help='print full game state')
            (args, unrecognized) = parser.parse_known_args(line.split(' '))
            if args.action is None:
                self.print_state()
            else:
                args.action()
        except SystemExit:
            pass
        return False

    def help_dbg(self, no_print=None):
        hlp = "Provides access to some debugging commands."
        if no_print:
            return hlp
        print(hlp)


class Planet(CommandMixin):
    def __build(self, args):
        if args.building is None:
            format_object.print_object(
                format_object.planet_available_buildings, self.engine,
                args.planet, args.verbose)
            return
        coord, planet = self.engine.state.user.get_planet(args.planet)
        if planet.build(args.building):
            print("Congrats, you just built a %s" % args.building)
        else:
            print("Construction Failed! Make sure you have enough "
                  "resources and try again.")

    def __show_planets(self, args):
        if args.planet in [None, '']:
            format_object.print_object(format_object.user_planets,
                                       self.engine.state.user, args.verbose)
            return
        coord, planet = self.engine.state.user.get_planet(args.planet)
        print(planet.show(verbose=args.verbose))

    def do_planet(self, line):
        try:
            parser = ArgumentParser(prog='planet',
                                    description=self.help_show(True))
            parser.add_argument('-v', '--verbose', action='store_true',
                                dest='verbose', default=False)
            parser.add_argument('-b', '--build', action='store_const',
                                dest='action', const=self.__build)
            parser.add_argument('planet', help='Limit the effect of the '
                                'command to this planet (if supported)',
                                default=None, nargs='?')
            parser.add_argument('building', help='Name of building type to '
                                'pass to the command %s (if supported, '
                                'PLANET must be supplied first)'
                                % str(model.get_all_building_abbr()),
                                default=None, nargs='?')

            (args, params) = parser.parse_known_args(line.split(' '))
            setattr(args, 'params', params)
            if args.action is None:
                args.action = self.__show_planets
            args.action(args=args)
        except SystemExit:
            pass
        return False

    def help_planet(self):
        print("Access to things on planets")
    do_p = do_planet
    do_pl = do_planet


class User(CommandMixin):
    def do_user(self, line):
        # TODO: implement change name
        print('Not implemented yet')

    def help_user(self):
        print("Access to user admin tasks")
    do_u = do_user


class SpaceCmdInterpreter(Cmd, Quit, Debug, Show, Planet, User):
    def __init__(self, engine):
        super(SpaceCmdInterpreter, self).__init__()
        self.engine = engine
        self.prompt = 'space> '
        self.doc_header = 'Space Commands'
        self.undoc_header = 'Alias Commands'

    def start(self):
        try:
            try:
                debug('Trying to load the saved game...')
                self.engine.load()
            except IOError:
                debug('No save game, starting new game...')
                self.start_new_game()
            debug('Starting interpreter...')
            self.cmdloop()
        except (KeyboardInterrupt, SystemExit):
            debug('Recieved Interrupt.')
        except Exception:
            debug('Received Unknown Exception, exiting...',
                  exc_info=True, stack_info=True)
        finally:
            self.engine.save()

    def start_new_game(self):
        msg = 'Do you want to start a new game?'
        ui.input_bool(msg) or sys.exit(0)
        debug('Creating a new game')
        self.engine.new_game(ui.get_new_game_info)
{'fraction_non_alphanumeric': 0.0967672875187326, 'fraction_numerical': 0.0012845215157353885, 'mean_word_length': 1.8477951635846372, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '90090', 'n_tokens_mistral': 3832, 'n_tokens_neox': 3688, 'n_words': 990}