rpi_video_looper/Adafruit_Video_Looper/model.py

40 lines
1.3 KiB
Python
Raw Normal View History

2015-02-11 01:34:46 +00:00
# Copyright 2015 Adafruit Industries.
# Author: Tony DiCola
# License: GNU GPLv2, see LICENSE.txt
2015-03-07 09:47:04 +00:00
import random
2015-02-11 01:34:46 +00:00
class Playlist(object):
"""Representation of a playlist of movies."""
2015-03-07 09:47:04 +00:00
def __init__(self, movies, is_random):
2015-02-11 01:34:46 +00:00
"""Create a playlist from the provided list of movies."""
self._movies = movies
self._index = None
2015-03-07 09:47:04 +00:00
self._is_random = is_random
2015-02-11 01:34:46 +00:00
def get_next(self):
"""Get the next movie in the playlist. Will loop to start of playlist
after reaching end.
"""
# Check if no movies are in the playlist and return nothing.
if len(self._movies) == 0:
return None
2015-03-07 09:47:04 +00:00
# Start Random movie
if self._is_random:
self._index = random.randrange(0, len(self._movies))
2015-02-11 01:34:46 +00:00
else:
2015-03-07 09:47:04 +00:00
# Start at the first movie and increment through them in order.
if self._index is None:
self._index = 0
else:
self._index += 1
# Wrap around to the start after finishing.
if self._index >= len(self._movies):
self._index = 0
2015-02-11 01:34:46 +00:00
return self._movies[self._index]
def length(self):
"""Return the number of movies in the playlist."""
return len(self._movies)