def send(): params = bottle.request.query if 'text' not in params: raise HTTPError(400, 'Need text to send!') if 'label' in params: label = params.label.lower() else: label = 'A' text = params.text color = constants.get_color(params.get('color', 'NONE')) mode = constants.get_mode(params.get('mode', 'COMPRESSED_ROTATE')) text = color + text text = general.parse_colors(text) if label == 'A': text = parse_labels(text) memory_entry = sign.find_entry(read_raw_memory_table(), label) if len(text) >= memory_entry['size']: raise HTTPError(400, 'Not enough space allocated. Need at least %s bytes.' % (len(text) + 1)) if label == 'A': sign.write(alphasign.Text(text, label=label, mode=mode)) else: sign.write(alphasign.String(text, label=label)) return {'result': 'Sucessfully sent text'}
def send(): params = bottle.request.query if 'text' not in params: raise HTTPError(400, 'Need text to send!') if 'label' in params: label = params.label.lower() else: label = 'A' text = params.text color = constants.get_color(params.get('color', 'NONE')) mode = constants.get_mode(params.get('mode', 'COMPRESSED_ROTATE')) text = color + text text = general.parse_colors(text) if label == 'A': text = parse_labels(text) memory_entry = sign.find_entry(read_raw_memory_table(), label) if len(text) >= memory_entry['size']: raise HTTPError( 400, 'Not enough space allocated. Need at least %s bytes.' % (len(text) + 1)) if label == 'A': sign.write(alphasign.Text(text, label=label, mode=mode)) else: sign.write(alphasign.String(text, label=label)) return {'result': 'Sucessfully sent text'}
def check_ready(): raw_table = read_raw_memory_table() bad = { 'result': 'Sign is NOT ready for dead-simple.', 'recommendation': 'hit <(URL)/dead-simple/reset> first.', 'ready': False } good = { 'result': 'Sign IS ready for dead-simple!', 'recommendation': 'hit <(URL)/dead-simple/send?text=<text>> to get started', 'ready': True } text = sign.find_entry(raw_table, 'A') if text is None or text['type'] != 'TEXT': return bad for label in constants.dead_simple_string_labels: string = sign.find_entry(raw_table, label) if string is None or string['type'] != 'STRING': return bad return good
def show_text(data): label = data['label'] memory_entry = sign.find_entry(read_raw_memory_table(), label) if memory_entry['type'] != 'TEXT': raise HTTPError(400, 'The data at label %s must be of type TEXT. It is of type %s' % (label, memory_entry['type'])) sign.set_run_sequence([alphasign.Text(label=label)]) return {'result': 'Sign showing text at %s' % label}
def show_text(data): label = data['label'] memory_entry = sign.find_entry(read_raw_memory_table(), label) if memory_entry['type'] != 'TEXT': raise HTTPError( 400, 'The data at label %s must be of type TEXT. It is of type %s' % (label, memory_entry['type'])) sign.set_run_sequence([alphasign.Text(label=label)]) return {'result': 'Sign showing text at %s' % label}
def validate_label(label, type_name=None, raw_table=None): if label not in constants.valid_labels: raise bottle.HTTPError(400, 'label %s is invalid' % label) if label in constants.counter_labels: raise bottle.HTTPError(400, 'label cannot be a counter (1-5)') if type_name is not None: if raw_table is None: raw_table = read_raw_memory_table() memory_entry = sign.find_entry(raw_table, label) if memory_entry is None: raise bottle.HTTPError(400, 'label %s not in memory table' % label) if memory_entry['type'] == type_name: raise bottle.HTTPError(400, 'label %s is of type %s, not %s' % (label, type_name, memory_entry['type']))
def write_file(request, label): memory_table = read_raw_memory_table() if 'type' in request: general.validate_label(label, request['type'], memory_table) memory_entry = sign.find_entry(memory_table, label) memory_table = sign.parse_raw_memory_table(memory_table) file_type = memory_entry['type'] if file_type == 'TEXT' or file_type == 'STRING': data = request['text'] #Prepend color. Ignore invalid colors. data = constants.get_color(request.get('color', 'NO_COLOR')) + data #parse colors data = general.parse_colors(data) #text-specific processing if file_type == 'TEXT': data = general.parse_labels(data, memory_table) #check size if len(data) > memory_entry['size']: raise HTTPError( 400, 'Not enough memory allocated. Requires %s, only %s allocated.' % (len(data), memory_entry['size'])) if file_type == 'TEXT': mode = constants.get_mode(request.get('mode', 'HOLD')) obj = alphasign.Text(data, label=label, mode=mode) elif file_type == 'STRING': obj = alphasign.String(data, label=label) elif file_type == 'DOTS': data = request['data'] rows = memory_entry['rows'] columns = memory_entry['columns'] obj = alphasign.Dots(rows, columns, label=label) for i, row in enumerate(data[:rows]): obj.set_row(i, row) sign.write(obj) return {'result': 'memory written successfully'}
def validate_label(label, type_name=None, raw_table=None): if label not in constants.valid_labels: raise bottle.HTTPError(400, 'label %s is invalid' % label) if label in constants.counter_labels: raise bottle.HTTPError(400, 'label cannot be a counter (1-5)') if type_name is not None: if raw_table is None: raw_table = read_raw_memory_table() memory_entry = sign.find_entry(raw_table, label) if memory_entry is None: raise bottle.HTTPError(400, 'label %s not in memory table' % label) if memory_entry['type'] == type_name: raise bottle.HTTPError( 400, 'label %s is of type %s, not %s' % (label, type_name, memory_entry['type']))
def write_file(request, label): memory_table = read_raw_memory_table() if 'type' in request: general.validate_label(label, request['type'], memory_table) memory_entry = sign.find_entry(memory_table, label) memory_table = sign.parse_raw_memory_table(memory_table) file_type = memory_entry['type'] if file_type == 'TEXT' or file_type == 'STRING': data = request['text'] #Prepend color. Ignore invalid colors. data = constants.get_color(request.get('color', 'NO_COLOR')) + data #parse colors data = general.parse_colors(data) #text-specific processing if file_type == 'TEXT': data = general.parse_labels(data, memory_table) #check size if len(data) > memory_entry['size']: raise HTTPError(400, 'Not enough memory allocated. Requires %s, only %s allocated.' % (len(data), memory_entry['size'])) if file_type == 'TEXT': mode = constants.get_mode(request.get('mode', 'HOLD')) obj = alphasign.Text(data, label=label, mode=mode) elif file_type == 'STRING': obj = alphasign.String(data, label=label) elif file_type == 'DOTS': data = request['data'] rows = memory_entry['rows'] columns = memory_entry['columns'] obj = alphasign.Dots(rows, columns, label=label) for i, row in enumerate(data[:rows]): obj.set_row(i, row) sign.write(obj) return {'result': 'memory written successfully'}
def parse_labels(text, memory=None): '''This function scans the text for label flags (ie, {C}) and replaces them with their alphasign call-character equivelents. It depends on the current memory table of the sign. ''' types = {'STRING': alphasign.String, 'DOTS': alphasign.Dots} if memory is None: memory = sign.parse_raw_memory_table(read_raw_memory_table()) memory_types = {entry['label']: types[entry['type']] for entry in memory if entry['type'] != 'TEXT'} def replacer(label): if label in memory_types: return memory_types[label](label=label).call() return None return parse_generic(text, replacer)
def parse_labels(text, memory=None): '''This function scans the text for label flags (ie, {C}) and replaces them with their alphasign call-character equivelents. It depends on the current memory table of the sign. ''' types = {'STRING': alphasign.String, 'DOTS': alphasign.Dots} if memory is None: memory = sign.parse_raw_memory_table(read_raw_memory_table()) memory_types = { entry['label']: types[entry['type']] for entry in memory if entry['type'] != 'TEXT' } def replacer(label): if label in memory_types: return memory_types[label](label=label).call() return None return parse_generic(text, replacer)
def check_ready(): raw_table = read_raw_memory_table() bad = {'result': 'Sign is NOT ready for dead-simple.', 'recommendation':'hit <(URL)/dead-simple/reset> first.', 'ready': False} good = {'result': 'Sign IS ready for dead-simple!', 'recommendation': 'hit <(URL)/dead-simple/send?text=<text>> to get started', 'ready': True} text = sign.find_entry(raw_table, 'A') if text is None or text['type'] != 'TEXT': return bad for label in constants.dead_simple_string_labels: string = sign.find_entry(raw_table, label) if string is None or string['type'] != 'STRING': return bad return good