Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 09450ebe93
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 113 lines (89 sloc) 2.452 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
"""
Alarm clock logic
"""


class Manager(object):
    """Manages Alarm Programs"""

    def __init__(self):
        self.programs = {}

    def register(self, program):
        """
Register a new alarm clock program
"""
        if not program.nid:
            raise ValueError, "program id (nid) is not set"
        if program.nid in self.programs and self.programs[program.nid] != program:
            raise ValueError, "program id (nid) already exists"

        self.programs[program.nid] = program

    def list_programs(self):
        """
Return a list of all programs
"""
        return [program for program in self.programs.iteritems()]

    @property
    def choices_programs(self):
        """
Return a choices list used in django
"""
        rv = []
        for program in self.programs.iteritems():
            rv.append((program.nid, program))
        return rv

    def is_valid_program_id(self, nid):
        for program in self.programs.iteritems():
            if program.nid == nid:
                return True

    def create_session(self, user_name, program):
        pass

manager = Manager()


class AlarmInstance(object):
    """
Running instance of a alarm clock.
It is assigned to a user.
"""

    def __init__(self, user, detector, program=None):
        self.user = user
        self.detector = detector
        self.program = program


class BaseAction(object):
    """
Action to take
"""
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs

    def execute(self):
        raise NotImplemented


class ExecuteAction(BaseAction):
    """
Executes external program
"""
    def execute(self):
        import subprocess
        self.popen = subprocess.Popen(self.args)
        self.popen.communicate()

class BaseAlarm(object):
    """
Base Class for all alarm logics
"""
    # numeric id. must be unique and change never
    nid = None
    title = None

    def __init__(self, manager, session):
        self.manager = manager
        self.session = session
        self.next_alarm = None

    def check(self):
        """
Returns a alarm action to execute
"""
        return None

    def snooze(self):
        """
User pressed snooze
"""
        pass

    def feed(self, entry):
        """
Feed one new Entry into the Alarm program.
"""
        pass
Something went wrong with that request. Please try again.