Example #1
0
File: next.py Project: ekohl/next
def main():
    (options, conf, args) = config.parse_opts()

    try:  # the database_path is usually the show_path, but can be defined in conf
        if ConfKeys.DB_PATH in conf:
            database_path = os.path.join(conf[ConfKeys.DB_PATH], u".next.db")
        else:
            database_path = os.path.join(conf[ConfKeys.SHOW_PATH], u".next.db")
        database_path = os.path.expanduser(os.path.expandvars(database_path))
    except KeyError:
        print (u"No show_path or database_path defined in configuration, aborting!")
        sys.exit(-1)

    # initialize the sqlite database
    try:
        if os.path.exists(database_path) or os.access(os.path.dirname(database_path), os.W_OK | os.R_OK):
            conf[ConfKeys.DB_CONN] = db.initialize(database_path)
        else:
            print (
                u'Could not access shows database, path "{0}" does not exist or we don\'t have write access!'.format(
                    database_path
                )
            )
            sys.exit(-1)

    except sqlite3.OperationalError:
        print (u'Could not access shows database, are the permissions correct for "{0}"?'.format(database_path))
        sys.exit(-1)

    # first check for commandline options
    if options.func:
        try:
            options.func()
        except UserCancelled:
            print "User cancelled!"
        except KeyboardInterrupt:
            print "\nUser pressed ^C!"
        sys.exit(0)

    # couple of usecases:
    # 1. there is an argument provided. this is probably a show that the user wants
    #    us to start, so let's start it.
    # 2. there are no arguments provided. provide the user with a query what he wants

    if len(args):
        # 1. user provided a showname, find it in the db, then play it.
        s = db.find_show(conf, u" ".join(args))
        if s:
            player.play_next(conf, s)
        else:
            print u'Show "{0}" could not be found!'.format(u" ".join(args))
    else:
        # 2. user provided nothing, popup a list of options
        ui = TUI(conf)
        try:
            ui.cmdloop()
        except KeyboardInterrupt:
            print "\nUser pressed ^C, exitting!"
Example #2
0
File: tui.py Project: ekohl/next
 def do_random(self, line=None):
     '''
     A TUI function that plays a new ep from a random show
     '''
     all_shows = db.all_shows(self.conf)
     if not all_shows:
         print u'There are no shows to play!'
         return
     s = db.find_show(self.conf, random.choice(all_shows).name)
     player.play_next(self.conf, s)
Example #3
0
File: tui.py Project: ekohl/next
    def do_play(self, line=None):
        '''
        A TUI function that plays a new episode from the user-specified show
        '''
        all_shows = db.all_shows(self.conf)
        if not all_shows:
            print u'There are no shows to play!'
            return
        if line:
            candidates = filter(lambda s : set(line.split()) <= set(s.name.lower().split()), all_shows)
            if not candidates:
                print u'No show found for "{0}"'.format(line)
                return
            #just assume the first show
            show = candidates[0]
        else:
            print u'Which show would you like to play the next ep from?'
            print_shows(all_shows)
            number = int(get_input(u'Show number: ', range(1, len(all_shows) + 1)))
            show = all_shows[number - 1]

        if show:
            player.play_next(self.conf, show)