def generate_metadata(self): """ Output metadata as a .json dictionary. """ say('Generating metadata for ' + self.id) self._fill_info() timestamp_label = self._check_wrist('label') metadata_path = '_'.join( [self._base_path + timestamp_label, self.id + '.json']) metadata_str = json.dumps(self.info, sort_keys=True, indent=4) self._write_file(metadata_path, metadata_str, 'a+')
def _disconnect(self, this_event): if this_event == 'INIT_EVENT': self.printf('Disco nnecting...') self._start_timer('disconnecting') self._start_thread(self._break_comms, 'DISCONNECTOR') elif this_event == 'DISCONNECTED_EVENT': self._next_state = 'SLEEPING' elif this_event == 'DISCONNECT_TIMEOUT_EVENT': say('Channel clogged; cannot disconnect', 'error') self._next_state = 'SLEEPING' else: time.sleep(0.3) self._test_comms() time.sleep(0.3)
def _write_file(self, path, data, write_option='', overwrite=False): _write_options = ['w', 'w+', 'a', 'a+'] _default_write_option = 'a+' if not write_option or write_option not in _write_options: write_option = _default_write_option if os.path.isfile(path) and write_option.find('a') == -1: if overwrite: write_option = 'w+' else: say( 'Could not write file ' + path + ' with write option ' + write_option, 'warning') return False with open(path, write_option) as fp: fp.write(data) return True
def load_garden(self, config_label='_default'): """ Try to load configurations for a garden. :in: config_label (str) - file name in the './configs/' directory, minus the '.json' :out: config {dict} - formatted as shown above """ # TODO: (@nubby) import garden data too? config_path = ''.join([self.config_dir, config_label, '.json']) try: say('Loading config file from ' + config_path) assert (os.path.isfile(config_path)) except AssertionError: say( 'Config file ' + str(config_path) + ' not found; loading defaults', 'warning') config_path = ''.join([self.config_dir, '_default.json']) with open(config_path, 'r') as fp_config: config = json.load(fp_config) return config
def setup(self, config): """ Creates a point-cloud of all positions in garden. :in: config {dict} - definied above """ say('Initializing garden, ' + self.label) try: # Find boundaries of garden. x_dim = int(config['size'][0] / config['resolution'] + 1) y_dim = int(config['size'][1] / config['resolution'] + 1) z_dim = int(config['size'][2] / config['resolution'] + 1) # Initialize an empty array of the right size. self.spacetime = [[[None for z_i in range(z_dim)] for y_i in range(y_dim)] for x_i in range(x_dim)] # ...and fill it on up. for x_i in range(x_dim): x = float(x_i) * config['resolution'] for y_i in range(y_dim): y = float(y_i) * config['resolution'] for z_i in range(z_dim): z = float(z_i) * config['resolution'] self.spacetime[x_i][y_i][z_i] = Point([x, y, z]) say('Garden assembled', 'success') except: say('Error assembling garden', 'error')
def printf(self, prompt, flag=''): if DEBUG: say(str(self) + ' : ' + prompt, flag)
def set(self, duration): if not self.active.value > 0: self.duration = duration elif DEBUG: say('Cannot set an active timer')