def _copySessionToForm(self, session): """ Copy relevant fields from Session to SessionForm.""" sessionForm = SessionForm() for field in sessionForm.all_fields(): if field.name == "speakerKey": speaker = session.speaker.get() if speaker: setattr(sessionForm, "speakerKey", speaker.key.urlsafe()) elif field.name == "date": setattr(sessionForm, "date", str(session.date.date())) elif field.name == "startTime": setattr(sessionForm, "startTime", str(session.startTime.time())) elif field.name == "endTime": setattr(sessionForm, "endTime", str(session.endTime.time())) elif field.name == "duration": setattr(sessionForm, "duration", duration(session.startTime, session.endTime)) elif field.name == "websafeKey": setattr(sessionForm, field.name, session.key.urlsafe()) elif hasattr(session, field.name): setattr(sessionForm, field.name, getattr(session, field.name)) sessionForm.check_initialized() return sessionForm
def post(self): logger.info("POST Request received.") # assume bad result, create temp file. payload = {'status': 'failure', 'count': 0, 'meta': {}} # Parse the request into a dict() which contains the raw wav data. parse = reqparse.RequestParser() parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files') args = parse.parse_args() # Save the data as a temporary file filename = str(uuid.uuid4()) try: audio_file = args['file'] audio_file.save(filename) except AttributeError: if audio_file is None: logger.error("Audio data not received") return payload logger.info("Analyzing temp file: {0}".format(filename)) # Extract the words and perform an analysis. try: words = utils.speech_rec(filename) analysis = voice_analyzer(filename) except: logger.error("File does not appear to be a valid wav file") os.remove(filename) logger.debug("Temp file removed. Was {0}".format(filename)) return payload # Tag the words payload['meta']['text'] = utils.pos_tagger([words['meta']['text']]) payload['meta']['gender'] = analysis['gender'] payload['meta']['age'] = analysis['age'] payload['meta']['dialect'] = analysis['dialect'] # Count the words for word in payload['meta']['text']: payload['count'] += len(word) duration = utils.duration(filename) logger.info("Analysis completed") # Check for errors if 'error' not in payload: payload['status'] = 'success' payload['meta']['duration'] = duration # Remove temp file os.remove(filename) logger.debug("Temp file removed. Was {0}".format(filename)) return payload
def post(self): """ HTTP POST. Form with a 'file' field. :file: (WAV) waveform audio Via HTTP POST form-data. :returns: dict() Meta-information of the audio. """ tempfile = str(uuid.uuid4()) log.info("POST Request received. using temp file {}".format(tempfile)) payload = { 'status': 'failure', 'count': 0, 'meta': {} } parse = reqparse.RequestParser() parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files') args = parse.parse_args() try: audio_file = args['file'] audio_file.save(tempfile) except AttributeError: if audio_file is None: log.error('Audio data not received') payload['meta']['error'] = 'audio data not received' payload['meta']['parameter'] = '\'file\' not present' return payload log.info('Analyzing temp file: {}'.format(tempfile)) try: payload['meta'].update(speech_rec(tempfile)) payload['meta'].update(voice_analyzer(tempfile)) except Exception as exc: log.error('File {} does not appear to be a valid'.format(tempfile)) log.debug(exc) os.remove(tempfile) log.debug('Temp file removed. Was {}'.format(tempfile)) payload['meta']['error'] = 'file does not appear to be a valid' return payload payload['meta']['text'] = pos_tagger([payload['meta']['text']]) payload['meta']['duration'] = duration(tempfile) payload['count'] = len(payload['meta']['text']) if 'error' not in payload['meta']: payload['status'] = 'success' os.remove(tempfile) log.info("Process completed, removed tempfile {}".format(tempfile)) return payload
def do_status(self, *args): "Display rtorrent status" status = RTorrent().get_status() if status['rpc']: print "Version rtorrent %s/%s" % (status['client_version'], status['library_version']) print "Server %s" % status['server'] print "Rate \xe2\x86\x93%s/s \xe2\x86\x91%s/s" % (filesizeformat(status['down_rate']), filesizeformat(status['up_rate'])) print "UP Time %s" % duration(status['up_time']) print "Space Left %s on %s" % (filesizeformat(status['free']), config.root_dir) else: print "No rtorrent found on %(server)s" % status
def get_torrents(self): if not self.rpc: return functions = ("get_name", "get_down_rate", "get_up_rate", "get_completed_bytes", "get_completed_chunks", "get_chunk_size", "get_ratio", "get_size_chunks", "get_hashing", "get_complete", "is_active", "is_open", "get_hash", "get_peers_connected", "get_peers_not_connected", "get_peers_accounted", "is_private", "get_chunks_hashed", "get_tied_to_file") fnames = [f[4:] if f.startswith('get_') else f for f in functions] for values in (dict(zip(fnames, torrent)) for torrent in self.rpc.d.multicall("main", *( "d.%s=" % f for f in functions))): t = Torrent(**values) t.completed = t.completed_chunks * t.chunk_size t.size = t.size_chunks * t.chunk_size t.hashed_percent = t.chunks_hashed * 100 / t.size_chunks t.percent = t.completed * 100 / t.size if t.down_rate != 0: t.eta = duration(timedelta(seconds = (t.size - t.completed) / t.down_rate)) yield (t.hash, t)
p.join() oie_data_tok = dict(zip(keys, tok_vals)) print('Loading contexts...') id_context_map = dataset.get_linked_contexts(dtype).items() print('Starting to construct graphs...') out_dir = os.path.join(data_dir, 'chunks', 'kg', dtype) if not os.path.exists(out_dir): os.makedirs(out_dir) output = {} load_precomputed(out_dir, output) already_n = len(output) id_context_map_todo = [item for item in id_context_map if not item[0] in output] n = len(id_context_map_todo) print('Already computed {} outputs. Doing {} more...'.format(already_n, n)) for i in tqdm(range(n)): construct_graph(id_context_map_todo[i], out_dir=out_dir) load_precomputed(out_dir, output) duration(start_time) out_fn = os.path.join(data_dir, 'kg_{}.pk'.format(dtype)) print('Dumping {} knowledge graphs to {}'.format(len(output), out_fn)) with open(out_fn, 'wb') as fd: pickle.dump(output, fd) print('Done! Now can safely remove cached') print('Removing temporary chunks...') shutil.rmtree(out_dir) os.mkdir(out_dir)
def do_queue(self, args): "Handle the torrent queue\nqueue [push <torrent>|remove <torrent>]" args = shlex.split(args) if not args: for t in RTorrent().get_queue(): print "%s" % t.basename print " %s%s - %s old" % (filesizeformat(t.total_size), ' in %d files' % len(t.files) if len(t.files) > 1 else '', duration(t.date)) elif len(args) == 2 and args[0] == 'push': RTorrent().push(args[1]) print "Pushed '%s'" % args[1] elif len(args) == 2 and args[0] == 'remove': RTorrent().unqueue(args[1]) print "Removed '%s' from queue" % args[1] else: self.error("malformed command, try 'help queue'")