def main(taskId=None): logging.basicConfig(level=logging.DEBUG) from app import audio_server from application import application as app with app.app_context(): tasks = Task.query\ .filter_by(status=Task.STATUS_ACTIVE)\ .filter_by(auto_loading=True)\ .filter(or_(Task.taskType == TaskType.TRANSCRIPTION, Task.taskType == TaskType.AUDIO_CHECKING))\ .all() log.info("tasks found: {0}".format(len(tasks))) log.debug("api class:{0}".format( current_app.config["AUDIO_SERVER_API_CLS"])) for task in tasks: log.info("uploading audio for {0}".format(task.display_name)) try: audio_server.api.start_load(task.task_id, current_app.config["ENV"]) # wait so that the audio server staggers requests slightly time.sleep(10) except Exception, e: log.error("error starting upload for {0}".format(task.task_id)) log.error(get_traceback_str()) continue
def queue_daemon(queue, rv_ttl=500): from application import application while 1: print('[daemon]: waiting for instruction...') msg = redis_connection.blpop(queue) print('[daemon]: received!') try: func, key, args, kwargs = pickle.loads(msg[1]) except Exception as e: try: print(f'[daemon]: failed to unpickle {e}') except (TypeError, IndexError): pass else: try: print(f'[daemon]: calling {func.__name__}') with application.app_context(): rv = func(*args, **kwargs) print('[daemon]: complete!') except Exception as e: print(f'[daemon]: {e}') rv = e if rv is not None: redis_connection.set(key, pickle.dumps(rv)) redis_connection.expire(key, rv_ttl) print(f'[daemon]: stored return value at {key}') print('[daemon]: sleeping for a bit...') time.sleep(0.25)
def queue_daemon(queue, rv_ttl=500): from application import application while 1: print('[daemon]: waiting for instruction...') msg = redis_connection.blpop(queue) print('[daemon]: received!') try: func, key, args, kwargs = pickle.loads(msg[1]) except Exception as e: try: print(f'[daemon]: failed to unpickle {e}') except (TypeError, IndexError): pass else: try: print(f'[daemon]: calling {func.__name__}') with application.app_context(): rv = func(*args, **kwargs) print('[daemon]: complete!') except Exception as e: print(f'[daemon]: {e}') rv = e if rv is not None: redis_connection.set(key, pickle.dumps(rv)) redis_connection.expire(key, rv_ttl) print(f'[daemon]: stored return value at {key}') print('[daemon]: sleeping for a bit...') time.sleep(0.25)
def export_slices(session_id: int): with application.app_context(): label_session = sessions.get_session_by_id(db.session, session_id) if label_session is None: print('Session with id {} not found'.format(session_id)) return session_slices_dir_path = os.path.join( EXPORTED_SLICES_DIR_PATH, label_session.session_name + ' Slices') session_slices_dir_path = inc_dir_name(session_slices_dir_path) os.makedirs(session_slices_dir_path, exist_ok=True) slices = sampling.get_slices_from_session(label_session) dataset = backend.get_dataset(label_session.dataset) for sl in slices: d_img = backend.get_image(dataset, sl.image_name) sl_max = backend.get_image_info(d_img)[1] sl_img = backend.get_slice(d_img, sl.slice_index, sl.slice_type, 0, sl_max) save_name = '{}_{}_{}.png'.format(sl.image_name, sl.slice_type.name, sl.slice_index) save_path = os.path.join(session_slices_dir_path, save_name) sl_img.save(save_path) print('Saved {}'.format(save_path))
def call_phone(number, message): with application.app_context(): account_sid = current_app.config['TWILIO_ACCOUNT_SID'] auth_token = current_app.config['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) cleaned_message = urllib.parse.quote(message) url = "http://twimlets.com/message?Message%5B0%5D={}".format( cleaned_message) client.calls.create(to=number, from_="+12027602953", url=url)
def setUp(self): """ start application test client, This function runs once before each member function unit test. """ application.config['TESTING'] = True application.config['WTF_CSRF_ENABLED'] = False self.app = application.test_client() self.app_context = application.app_context() self.app_context.push()
def client(): db_fd, application.config['DATABASE'] = tempfile.mkstemp() application.config['TESTING'] = True application.config['DEBUG'] = False with application.test_client() as client: with application.app_context(): database.init_app(application) yield client os.close(db_fd) os.unlink(application.config['DATABASE'])
def setUp(self): db = mongomock.MongoClient().pli # This function takes a collection initializing function # and a DB and calls the function, returning the DB def fold_cols(db, f): f(db) return db # We fold the db over the initializers, so we got all the collections pli.config['db'] = reduce(fold_cols, self.db_inits(), db) pli.config['gridfs'] = mongomock.gridfs.MockGridFS(pli.config['db']) pli.config['object_id'] = mongomock.ObjectId self.ctx = pli.app_context() self.ctx.push()
def del_all(): """ drop all tables """ with application.app_context(): from apps.users.models import User, Invite from apps.projects.models import Project, Collaborators from apps.tasks.models import Task Task.query.delete() Invite.query.delete() Collaborators.query.delete() Project.query.delete() User.query.delete() db.session.commit()
def client(): application.config['TESTING'] = True with application.test_client() as client: with application.app_context(): start_server() yield client
def init_db(): with app.app_context(): engine = get_engine() metadata.create_all(engine)
from application import db, application with application.app_context(): db.create_all()
def run_test(s): with pli.app_context(): f(s)
headers=headers, json=data) logger.info(resp.status_code) try: logger.info(resp.json()) except: logger.info(resp.content) user.deleted_count = user.deleted_count + len(comments) db.session.commit() logger.info(f'[{user.name}] deleted {len(comments)} comments') return True if __name__ == '__main__': from application import application db.init_app(application) application.app_context().push() while True: for user in User.query.all(): # perform delete and retry if the token is expired succ = delete_comments(user) if not succ: logger.info( f'[{user.name}] token expired, requesting a new one...') token = token_manager.refresh_token(user) user.token = token db.session.commit() delete_comments(user) time.sleep(60)
def testStartDateForm(self): with application.app_context(): with application.test_request_context(): f = forms.StartDateForm() print f.dt
def send_async_email(application, msg): with application.app_context(): mail.send(msg)
def generate_fake(): """ generate fake users with projects & tasks """ with application.app_context(): from apps.users.models import User from apps.projects.models import Project from apps.tasks.models import Task fake = Faker() users = [] for _ in range(5): user = User( name=fake.name(), email=fake.email(), password=generate_password_hash('123456') ) db.session.add(user) db.session.flush() users.append(user) # save users db.session.commit() projects = [] for user in users: for _ in range(random.randint(3, 5)): is_shared = fake.boolean(chance_of_getting_true=60) project = Project( owner_id=user.id, is_shared=is_shared, name=fake.word() ) db.session.add(project) db.session.flush() if is_shared: rand_count = random.randint(1, 2) rand_collabs = random.sample(users, k=rand_count) while user.id in [x.id for x in rand_collabs]: rand_collabs = random.sample(users, k=rand_count) for collab in rand_collabs: project.collaborators.append(collab) db.session.add(project) db.session.flush() projects.append(project) # save projects db.session.commit() for project in projects: tasks = [] all_collabs = [x for x in project.collaborators.all()] all_collabs.append(project.owner) for _ in range(random.randint(4, 6)): creator = random.choice(all_collabs) notify_date = fake.date_time_this_month(before_now=False, after_now=True) task = Task( creator_id=creator.id, project_id=project.id, text=' '.join(fake.words(nb=random.randint(1, 2))), note=fake.sentence(), notification_date=notify_date, completion_date=notify_date ) if project.is_shared: is_assigned = fake.boolean(chance_of_getting_true=70) if is_assigned: assigned_to_user = random.choice(all_collabs) task.assigned_to_user_id = assigned_to_user.id db.session.add(task) db.session.flush() tasks.append(task) for _ in range(random.randint(1, 2)): task = random.choice(tasks) task.is_completed = True task.completed_at = task.completion_date task.completed_by_user_id = task.assigned_to_user_id or task.creator_id db.session.add(task) db.session.flush() project.tasks_order = list([x.id for x in tasks]) db.session.add(project) db.session.flush() # save tasks db.session.commit()
from application import application from albumlist.delayed.queued import deferred_ping_albumlistbot if __name__ == '__main__': with application.app_context(): deferred_ping_albumlistbot()
def testStartDateForm(self): with application.app_context(): with application.test_request_context(): f = forms.StartDateForm() print f.dt