示例#1
0
def read_lullabies(file):
    '''
    file: json file from which to read the sceneries.
    See README.md for expected format.
    
    returns: a dictionary of (name -> Lullaby)
    '''
    lullabies = {}
    sceneries = read_sceneries(file)
    with open(file) as fd:
        data = json.load(fd)
        for name, lullaby_json in data['lullabies'].items():
            # FIXME: typecheck!
            if ' ' in name:
                raise util.AlarmException("Lullaby name may not contain spaces ('%s')" % name) # must be identifiable by CommandDispatcher
            if not name or name == '' or name in lullabies:
                raise util.AlarmException("Lullabies must have a unique name. Invalid or duplicate name: '%s'" % name)
            scenery_key = lullaby_json['scenery'] if 'scenery' in lullaby_json else 'UNDEFINED'
            if not scenery_key in sceneries:
                raise util.AlarmException("Invalid scenery key: '%s' for lullaby '%s'" % (scenery_key, name))
            scenery = sceneries[scenery_key]
            duration = lullaby_json['duration'] if 'duration' in lullaby_json else -1
            lullaby = SceneryLullaby(name = name, scenery = scenery, duration = duration)
            lullabies[name] = lullaby
    return lullabies
示例#2
0
def read_sceneries(file):
    '''
    file: json file from which to read the sceneries.
    See README.md for expected format.
    
    returns: a dictionary of (name -> Scenery)
    '''
    sceneries = {}
    with open(file) as fd:
        data = json.load(fd)
        for name, scenery_json in data['sceneries'].items():
            rd,gd,bd = scenery_json['rgb_deltas']
            rmin,gmin,bmin = scenery_json['rgb_min'] if 'rgb_min' in scenery_json else (0,0,0)
            rmax,gmax,bmax = scenery_json['rgb_max'] if 'rgb_max' in scenery_json else (255,255,255)
            rinit,ginit,binit = scenery_json['rgb_init'] if 'rgb_init' in scenery_json else (0,0,0) 
            sleep = scenery_json['sleep']
            channels = []
            files = scenery_json['files']
            for group in files:
                sound_files = []
                if not isinstance(group, list):
                    group = [group]
                for f in group:
                    if os.path.isdir(f):
                        files = list(os.walk(f))[0]
                        sound_files.extend(map(lambda s: "".join((files[0], s)), files[2])) # 0: root, 1: subdirs, 2: files
                    else:
                        sound_files.append(f)
                channels.append(sound_files)
            if name in sceneries:
                raise util.AlarmException("Duplicate name for scenery: '%s'. Name must be unique" % (name,))
            sceneries[name] = Scenery(name, channels, rd,gd,bd, rmin,gmin,bmin, rmax,gmax,bmax, sleep, init = (rinit,ginit,binit))
    return sceneries
示例#3
0
def read_alarms(file, scheduler = None):
    '''
    file: json file from which to read the alarms.
    See README.md for expected format.
    scheduler: scheduler into which to load the alarms. If no scheduler
               is passed a new one will be created
    returns: the scheduler into which the alarms have been read
    '''
    sceneries = read_sceneries(file)
    if not scheduler:
        scheduler = Scheduler()
    with open(file) as fd:
        data = json.load(fd)
        for alarm_json in data['alarms']:
            # FIXME: typecheck!
            active = alarm_json['active'] if 'active' in alarm_json else True
            if active:
                duration = alarm_json['duration'] if 'duration' in alarm_json else -1
                name = alarm_json['name'] if 'name' in alarm_json else ''
                hour = alarm_json['hour'] # mandatory!
                minute = alarm_json['minute'] if 'minute' in alarm_json else 0
                second = alarm_json['second'] if 'second' in alarm_json else 0
                days = alarm_json['days'] if 'days' in alarm_json else []
                scenery_key = alarm_json['scenery'] if 'scenery' in alarm_json else 'UNDEFINED'
                if not scenery_key in sceneries:
                    raise util.AlarmException("Invalid scenery key: '%s' for alarm '%s'" % (scenery_key, name))
                scenery = sceneries[scenery_key]
                alarm = SceneryAlarm(name = name, hour = hour, minute = minute, second = second, days = days, scenery = scenery, duration = duration)
                scheduler.add_alarm(alarm)
    return scheduler
示例#4
0
 def days(self, values):
     if not functools.reduce(lambda v, n: v and
                             (n in Alarm.DAYS), values, True):
         raise util.AlarmException("Invalid weekday value in: %s" %
                                   (str(values), ))
     self._days = values
示例#5
0
 def second(self, value):
     if not (0 <= value < 59):
         raise util.AlarmException("Invalid second: %d" % (value, ))
     self._second = value
示例#6
0
 def minute(self, value):
     if not (0 <= value < 59):
         raise util.AlarmException("Invalid minute: %d" % (value, ))
     self._minute = value
示例#7
0
 def hour(self, value):
     if not (0 <= value < 23):
         raise util.AlarmException("Invalid hour: %d" % (value, ))
     self._hour = value