Пример #1
0
def session_client(func, *args, **kwargs):
    clientname = context.get('clientname')
    contributor_type = context.get('contributor_type')
    try:
        client = Client.select().where(Client.name == clientname).get()
    except Client.DoesNotExist:
        raise Exception('Client not found {}'.format(clientname or ''))
    session = Session.create(client=client, contributor_type=contributor_type)
    context.set('session', session)
    return func(*args, **kwargs)
Пример #2
0
def collect_report(func, chunk):
    # This is a process reporter instance.
    reporter = context.get('reporter')
    results = func(*chunk)
    reports = reporter._reports.copy()
    reporter.clear()
    return results, reports
Пример #3
0
def test_authorized_responses_contain_sessions_data(get):
    municipality = MunicipalityFactory(name="Cabour")
    resp = get('/municipality/{}'.format(municipality.id))
    assert resp.status_code == 200
    session = context.get('session')
    assert resp.headers['Session-Client'] == session.client.id
    assert resp.headers['Session-User'] == session.user.id
Пример #4
0
def batch(func, iterable, chunksize=1000, total=None, progress=True):
    # This is the main reporter instance.
    reporter = context.get('reporter')
    pool = (ProcessPoolExecutor if config.get('BATCH_EXECUTOR') == 'process'
            else ThreadPoolExecutor)
    bar = Bar(total=total, throttle=timedelta(seconds=1))
    workers = int(config.get('WORKERS', os.cpu_count()))
    chunk = []
    count = 0

    def loop():
        for reports in executor.map(collect_report, repeat(func), chunk):
            reporter.merge(reports)
            if progress:
                bar()

    with pool(max_workers=workers) as executor:

        for item in iterable:
            if not item:
                continue
            chunk.append(item)
            count += 1
            if count % 10000 == 0:
                loop()
                chunk = []
        if chunk:
            loop()
Пример #5
0
def batch(func, iterable, chunksize=1000, total=None, progress=True):
    # This is the main reporter instance.
    reporter = context.get('reporter')
    pool = (ProcessPoolExecutor if config.get('BATCH_EXECUTOR') == 'process'
            else ThreadPoolExecutor)
    bar = Bar(total=total, throttle=timedelta(seconds=1))
    workers = int(config.get('WORKERS', os.cpu_count()))
    chunk = []
    count = 0

    def loop():
        for reports in executor.map(collect_report, repeat(func), chunk):
            reporter.merge(reports)
            if progress:
                bar()

    with pool(max_workers=workers) as executor:

        for item in iterable:
            if not item:
                continue
            chunk.append(item)
            count += 1
            if count % 10000 == 0:
                loop()
                chunk = []
        if chunk:
            loop()
Пример #6
0
def dummytoken(token, **kwargs):
    """Create a dummy token for dev."""
    session = context.get('session')
    Token.delete().where(Token.access_token == token).execute()
    Token.create(session=session.pk, access_token=token, expires_in=3600*24,
                 token_type='Bearer', scope='*')
    reporter.notice('Created token', token)
Пример #7
0
def test_authorized_responses_contain_sessions_data(get):
    municipality = MunicipalityFactory(name="Cabour")
    resp = get('/municipality/{}'.format(municipality.id))
    assert resp.status_code == 200
    session = context.get('session')
    assert resp.headers['Session-Client'] == session.client.id
    assert resp.headers['Session-User'] == session.user.id
Пример #8
0
def log_headers(resp):
    session = context.get('session')
    if session:
        if session.client:
            resp.headers.add('Session-Client', session.client.id)
        if session.user:
            resp.headers.add('Session-User', session.user.id)
    return resp
Пример #9
0
def dummytoken(**kwargs):
    """Create a dummy token for dev."""
    session = context.get('session')
    Token.delete().where(Token.access_token == 'token').execute()
    Token.create(session=session.id,
                 access_token="token",
                 expires_in=3600 * 24)
    report('Created token', 'token', report.NOTICE)
Пример #10
0
def log_headers(resp):
    session = context.get('session')
    if session:
        if session.client:
            resp.headers.add('Session-Client', session.client.id)
        if session.user:
            resp.headers.add('Session-User', session.user.id)
    return resp
Пример #11
0
def dummytoken(token, **kwargs):
    """Create a dummy token for dev."""
    session = context.get('session')
    Token.delete().where(Token.access_token == token).execute()
    Token.create(session=session.pk,
                 access_token=token,
                 expires_in=3600 * 24,
                 token_type='Bearer',
                 scope='*')
    reporter.notice('Created token', token)
Пример #12
0
def collect_report(func, *args, **kwargs):
    # This is a process reporter instance.
    reporter = context.get('reporter')
    if not reporter:
        # In thread mode, reporter is not shared with subthreads.
        reporter = Reporter(config.get('VERBOSE'))
        context.set('reporter', reporter)
    func(*args, **kwargs)
    reports = reporter._reports.copy()
    reporter.clear()
    return reports
Пример #13
0
def collect_report(func, *args, **kwargs):
    # This is a process reporter instance.
    reporter = context.get('reporter')
    if not reporter:
        # In thread mode, reporter is not shared with subthreads.
        reporter = Reporter(config.get('VERBOSE'))
        context.set('reporter', reporter)
    func(*args, **kwargs)
    reports = reporter._reports.copy()
    reporter.clear()
    return reports
Пример #14
0
def dummytoken(token, **kwargs):
    """Create a dummy token for dev."""
    session = context.get('session')
    Token.delete().where(Token.access_token == token).execute()
    Token.create(session=session.pk,
                 access_token=token,
                 expires_in=3600 * 24,
                 token_type='Bearer',
                 scopes="municipality_write postcode_write \
                 group_write housenumber_write position_write bal".split(),
                 status='dev')
    reporter.notice('Created token', token)
Пример #15
0
def session(func, *args, **kwargs):
    session = context.get('session')
    if not session:
        qs = User.select().where(User.is_staff == True)
        username = config.get('SESSION_USER')
        if username:
            qs = qs.where(User.username == username)
        try:
            user = qs.get()
        except User.DoesNotExist:
            abort('Admin user not found {}'.format(username or ''))
        session = Session.create(user=user)
        context.set('session', session)
    return func(*args, **kwargs)
Пример #16
0
def session(func, *args, **kwargs):
    session = context.get('session')
    if not session:
        qs = User.select().where(User.is_staff == True)
        username = config.get('SESSION_USER')
        if username:
            qs = qs.where(User.username == username)
        try:
            user = qs.get()
        except User.DoesNotExist:
            abort('Admin user not found {}'.format(username or ''))
        session = Session.create(user=user)
        context.set('session', session)
    return func(*args, **kwargs)
Пример #17
0
def batch(func, iterable, chunksize=1000, total=None, progress=True):
    # This is the main reporter instance.
    reporter = context.get('reporter')
    bar = Bar(total=total, throttle=timedelta(seconds=1))
    workers = int(config.get('WORKERS', os.cpu_count()))

    with ChunkedPool(processes=workers) as pool:
        try:
            for results, reports in pool.imap_unordered(
                    func, iterable, chunksize):
                reporter.merge(reports)
                bar(step=len(results))
                yield from results
            bar.finish()
        except Exception as e:
            print("\n" + e.args[0])
            pool.terminate()
Пример #18
0
 def on_post_bal(self, req, resp, *args, **kwargs):
     """Import file at BAL format."""
     data = req.get_param('data', required=True)
     bal(StringIO(data.value.decode('utf-8-sig')))
     reporter = context.get('reporter')
     resp.json(report=reporter)
Пример #19
0
def report(name, item, level=1):
    reporter = context.get('reporter')
    if not reporter:
        print("Reporter not set!")
        return
    reporter(name, item, level=level)
Пример #20
0
def bal_post():
    """Import file at BAL format."""
    data = request.files['data']
    bal(StringIO(data.read().decode('utf-8-sig')))
    reporter = context.get('reporter')
    return dumps({'report': reporter})
Пример #21
0
def dummytoken(**kwargs):
    """Create a dummy token for dev."""
    session = context.get('session')
    Token.delete().where(Token.access_token == 'token').execute()
    Token.create(session=session.id, access_token="token", expires_in=3600*24)
    report('Created token', 'token', report.NOTICE)
Пример #22
0
def report(name, item, level=1):
    reporter = context.get('reporter')
    if not reporter:
        print("Reporter not set!")
        return
    reporter(name, item, level=level)