Python源码示例:arrow.utcnow()

示例1
def rename_project(self, old_project, new_project):
        """Rename a project in all affected frames."""
        if old_project not in self.projects:
            raise WatsonError(u'Project "%s" does not exist' % old_project)

        updated_at = arrow.utcnow()
        # rename project
        for frame in self.frames:
            if frame.project == old_project:
                self.frames[frame.id] = frame._replace(
                    project=new_project,
                    updated_at=updated_at
                )

        self.frames.changed = True
        self.save() 
示例2
def rename_tag(self, old_tag, new_tag):
        """Rename a tag in all affected frames."""
        if old_tag not in self.tags:
            raise WatsonError(u'Tag "%s" does not exist' % old_tag)

        updated_at = arrow.utcnow()
        # rename tag
        for frame in self.frames:
            if old_tag in frame.tags:
                self.frames[frame.id] = frame._replace(
                    tags=[new_tag if t == old_tag else t for t in frame.tags],
                    updated_at=updated_at
                )

        self.frames.changed = True
        self.save() 
示例3
def sync(watson):
    """
    Get the frames from the server and push the new ones.

    The URL of the server and the User Token must be defined via the
    `watson config` command.

    Example:

    \b
    $ watson config backend.url http://localhost:4242
    $ watson config backend.token 7e329263e329
    $ watson sync
    Received 42 frames from the server
    Pushed 23 frames to the server
    """
    last_pull = arrow.utcnow()
    pulled = watson.pull()
    click.echo("Received {} frames from the server".format(len(pulled)))

    pushed = watson.push(last_pull)
    click.echo("Pushed {} frames to the server".format(len(pushed)))

    watson.last_sync = arrow.utcnow()
    watson.save() 
示例4
def __new__(cls, start, stop, project, id, tags=None, updated_at=None,):
        try:
            if not isinstance(start, arrow.Arrow):
                start = arrow.get(start)

            if not isinstance(stop, arrow.Arrow):
                stop = arrow.get(stop)

            if updated_at is None:
                updated_at = arrow.utcnow()
            elif not isinstance(updated_at, arrow.Arrow):
                updated_at = arrow.get(updated_at)
        except (ValueError, TypeError) as e:
            from .watson import WatsonError
            raise WatsonError(u"Error converting date: {}".format(e))

        start = start.to('local')
        stop = stop.to('local')

        if tags is None:
            tags = []

        return super(Frame, cls).__new__(
            cls, start, stop, project, id, tags, updated_at
        ) 
示例5
def test_report_current(mocker, config_dir):
    mocker.patch('arrow.utcnow', return_value=arrow.get(5000))

    watson = Watson(
        current={'project': 'foo', 'start': 4000},
        config_dir=config_dir
    )

    for _ in range(2):
        report = watson.report(
            arrow.utcnow(), arrow.utcnow(), current=True, projects=['foo']
        )
    assert len(report['projects']) == 1
    assert report['projects'][0]['name'] == 'foo'
    assert report['projects'][0]['time'] == pytest.approx(1000)

    report = watson.report(
        arrow.utcnow(), arrow.utcnow(), current=False, projects=['foo']
    )
    assert len(report['projects']) == 0

    report = watson.report(
        arrow.utcnow(), arrow.utcnow(), projects=['foo']
    )
    assert len(report['projects']) == 0 
示例6
def test_current_ranged_loans_filter(app):
    """Test ranged current loans filter."""
    with app.app_context():
        rfilter = overdue_loans_filter("field")

        current_loans_query = Terms(
            state=current_app.config["CIRCULATION_STATES_LOAN_ACTIVE"]
        )

        overdue = rfilter(["Overdue"])
        field = {"lt": str(arrow.utcnow().date())}
        assert overdue == Range(field=field) & current_loans_query

        upcoming = rfilter(["Upcoming return"])
        field = {
            "gte": str(arrow.utcnow().date()),
            "lte": str((arrow.utcnow() + timedelta(days=7)).date()),
        }
        assert upcoming == Range(field=field) & current_loans_query 
示例7
def _create_on_loan_brwreq_with_pending_extension(
    patron_id, client, json_headers
):
    """Create a new ON_LOAN ILL borrowing request with pending extension."""
    today = arrow.utcnow().date().isoformat()
    brwreq, brwreq_pid = _create_on_loan_brwreq_random_dates(
        "1", client, json_headers
    )
    res = _request_extension_action(brwreq_pid, client, json_headers)
    assert res.status_code == 200

    brwreq = res.get_json()["metadata"]
    patron_loan = brwreq["patron_loan"]

    assert patron_loan["loan"]["state"] == "ITEM_ON_LOAN"
    assert "extension_count" not in patron_loan["loan"]
    assert patron_loan["extension"]["status"] == "PENDING"
    assert patron_loan["extension"]["request_date"] == today

    return brwreq, brwreq["pid"] 
示例8
def test_brwreq_create_loan_fails_on_loan_pid_already_attached(
    db, client, testdata, json_headers, users
):
    """Test borrowing requests create loan action fails on loan_pid already."""
    user_login(client, "librarian", users)

    # demo data "illbid-2" has the valid state `REQUESTED`
    pid = "illbid-2"

    rec = BorrowingRequest.get_record_by_pid(pid)
    rec.setdefault("patron_loan", {})
    rec["patron_loan"]["pid"] = "loanid-3"
    rec.commit()
    db.session.commit()

    # already with a loan pid for some reasons
    now = arrow.utcnow()
    future = now + timedelta(days=5)
    data = dict(
        loan_start_date=now.date().isoformat(),
        loan_end_date=future.date().isoformat(),
    )
    _assert_create_loan_action_fails(pid, data, client, json_headers) 
示例9
def overdue_agg():
    """Create a custom aggregation with dynamic dates."""
    return dict(
        filter=dict(terms=dict(
            state=current_app.config["CIRCULATION_STATES_LOAN_ACTIVE"])),
        aggs=dict(
            end_date=dict(
                range=dict(
                    field="end_date",
                    ranges=[{"key": "Overdue",
                             "to": str(
                                 (arrow.utcnow()).date())},
                            {"key": "Upcoming return",
                             "from": str(arrow.utcnow().date()),
                             "to": str(
                                 current_app.config["CIRCULATION_POLICIES"][
                                     "upcoming_return_range"]().date())
                             }
                            ],
                )
            )
        )
    ) 
示例10
def get_timedelta(expression):

    # TODO: Use pandas' Timedelta. Timedelta('1m2s')
    # http://pandas.pydata.org/pandas-docs/stable/timedeltas.html

    # FIXME: Sanitize expression
    code = expression
    delta_raw = code.replace('now-', '')
    if code != delta_raw:
        code = code.replace(delta_raw, 'delta')

    # "code" should now be "now-delta"
    #print 'code:', code

    now = datetime.utcnow()
    delta = tdelta(delta_raw)

    # FIXME: This is nasty
    try:
        td = eval(code)
    except:
        raise ValueError('Unknown expression: {expression}'.format(expression=expression))

    return td 
示例11
def print_positions(self, ps):
        if not ps:
            return

        symbol = helper.symbol(ps)
        hold_side = "long" if ps.side == OrderSide_Bid else "short" if ps.side == OrderSide_Ask else '--'
        last_tick = self.tick(symbol)
        timestamp = last_tick.utc_time if last_tick else arrow.utcnow()

        transact_time = arrow.get(ps.transact_time).to('local')
        current_time = arrow.get(timestamp).to('local')
        ps_fields = "{} {} volume/today={:.1f}/{:.1f}, available/today = {:.1f}/{:.1f}, frozen/today = {:.1f}/{:.1f}".format(
            symbol,
            hold_side,
            ps.volume,
            ps.volume_today,
            ps.available,
            ps.available_today,
            ps.order_frozen,
            ps.order_frozen_today
        )
        ps_info_ts = "{}, last transact time: {}, current time: {}".format(ps_fields, transact_time, current_time)
        self.logger.info(ps_info_ts) 
示例12
def query_ENTSOE(session, params, target_datetime=None, span=(-48, 24)):
    """
    Makes a standard query to the ENTSOE API with a modifiable set of parameters.
    Allows an existing session to be passed.
    Raises an exception if no API token is found.
    Returns a request object.
    """
    if target_datetime is None:
        target_datetime = arrow.utcnow()
    else:
        # make sure we have an arrow object
        target_datetime = arrow.get(target_datetime)
    params['periodStart'] = target_datetime.shift(hours=span[0]).format('YYYYMMDDHH00')
    params['periodEnd'] = target_datetime.shift(hours=span[1]).format('YYYYMMDDHH00')
    if 'ENTSOE_TOKEN' not in os.environ:
        raise Exception('No ENTSOE_TOKEN found! Please add it into secrets.env!')
        
    # Due to rate limiting, we need to spread our requests across different tokens
    tokens = os.environ['ENTSOE_TOKEN'].split(',')
    
    params['securityToken'] = np.random.choice(tokens)
    return session.get(ENTSOE_ENDPOINT, params=params) 
示例13
def expire(ttl):
    """
    Removed all endpoints that have not been recently updated.
    """
    print("[+] Staring expiration of old endpoints.")

    try:
        now = arrow.utcnow()
        expiration = now - timedelta(hours=ttl)
        endpoints = database.session_query(Endpoint).filter(
            cast(Endpoint.last_updated, ArrowType) <= expiration
        )

        for endpoint in endpoints:
            print(
                "[!] Expiring endpoint: {name} Last Updated: {last_updated}".format(
                    name=endpoint.name, last_updated=endpoint.last_updated
                )
            )
            database.delete(endpoint)
            metrics.send("endpoint_expired", "counter", 1)

        print("[+] Finished expiration.")
    except Exception as e:
        sentry.captureException() 
示例14
def query_common_name(common_name, args):
    """
    Helper function that queries for not expired certificates by common name (and owner)

    :param common_name:
    :param args:
    :return:
    """
    owner = args.pop("owner")
    if not owner:
        owner = "%"

    # only not expired certificates
    current_time = arrow.utcnow()

    result = (
        Certificate.query.filter(Certificate.cn.ilike(common_name))
        .filter(Certificate.owner.ilike(owner))
        .filter(Certificate.not_after >= current_time.format("YYYY-MM-DD"))
        .all()
    )

    return result 
示例15
def sync(source, user):
    new_certs, updated_certs, updated_certs_by_hash = sync_certificates(source, user)
    new_endpoints, updated_endpoints, updated_endpoints_by_hash = sync_endpoints(source)

    metrics.send("sync.updated_certs_by_hash",
                 "gauge", updated_certs_by_hash,
                 metric_tags={"source": source.label})

    metrics.send("sync.updated_endpoints_by_hash",
                 "gauge", updated_endpoints_by_hash,
                 metric_tags={"source": source.label})

    source.last_run = arrow.utcnow()
    database.update(source)

    return {
        "endpoints": (new_endpoints, updated_endpoints),
        "certificates": (new_certs, updated_certs),
    } 
示例16
def convert_validity_years(data):
    """
    Convert validity years to validity_start and validity_end

    :param data:
    :return:
    """
    if data.get("validity_years"):
        now = arrow.utcnow()
        data["validity_start"] = now.isoformat()

        end = now.shift(years=+int(data["validity_years"]))

        if not current_app.config.get("LEMUR_ALLOW_WEEKEND_EXPIRATION", True):
            if is_weekend(end):
                end = end.shift(days=-2)

        data["validity_end"] = end.isoformat()
    return data 
示例17
def determine_end_date(end_date):
    """
    Determine appropriate end date

    :param end_date:
    :return: validity_end
    """
    default_years = current_app.config.get("DIGICERT_DEFAULT_VALIDITY", 1)
    max_validity_end = arrow.utcnow().shift(years=current_app.config.get("DIGICERT_MAX_VALIDITY", default_years))

    if not end_date:
        end_date = arrow.utcnow().shift(years=default_years)

    if end_date > max_validity_end:
        end_date = max_validity_end
    return end_date 
示例18
def log(message):
    click.echo("[{0}] {1}".format(arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ'), message)) 
示例19
def generate(cls, config, description, output):
        fname_fmt = config.new_migration_name
        text_cql_fmt = config.new_cql_migration_text
        text_py_fmt = config.new_python_migration_text

        clean_desc = re.sub(r'[\W\s]+', '_', description)
        next_version = len(config.migrations) + 1
        date = arrow.utcnow()

        format_args = {
            'desc': clean_desc,
            'full_desc': description,
            'next_version': next_version,
            'date': date,
            'keyspace': config.keyspace
        }

        if output == "python":
            file_extension = ".py"
            file_content = text_py_fmt.format(**format_args)
        else:
            file_extension = ".cql"
            file_content = text_cql_fmt.format(**format_args)

        fname = fname_fmt.format(**format_args) + file_extension
        new_path = os.path.join(config.migrations_path, fname)

        cls._create_file(new_path, file_content)

        return new_path 
示例20
def test_sorted_groupby_reverse(watson):
    end = arrow.utcnow()
    watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
    watson.add('bar', end.shift(hours=-1), end, ['A'])

    result = list(sorted_groupby(
        watson.frames,
        operator.attrgetter('day'),
        reverse=True))
    assert result[0][0] > result[1][0]


# frames_to_csv 
示例21
def test_flatten_report_for_csv(watson):
    now = arrow.utcnow().ceil('hour')
    watson.add('foo', now.shift(hours=-4), now, ['A', 'B'])
    watson.add('foo', now.shift(hours=-5), now.shift(hours=-4), ['A'])
    watson.add('foo', now.shift(hours=-7), now.shift(hours=-5), ['B'])

    start = now.shift(days=-1)
    stop = now
    result = flatten_report_for_csv(watson.report(start, stop))

    assert len(result) == 3

    assert result[0]['from'] == start.format('YYYY-MM-DD 00:00:00')
    assert result[0]['to'] == stop.format('YYYY-MM-DD 23:59:59')
    assert result[0]['project'] == 'foo'
    assert result[0]['tag'] == ''
    assert result[0]['time'] == (4 + 1 + 2) * 3600

    assert result[1]['from'] == start.format('YYYY-MM-DD 00:00:00')
    assert result[1]['to'] == stop.format('YYYY-MM-DD 23:59:59')
    assert result[1]['project'] == 'foo'
    assert result[1]['tag'] == 'A'
    assert result[1]['time'] == (4 + 1) * 3600

    assert result[2]['from'] == start.format('YYYY-MM-DD 00:00:00')
    assert result[2]['to'] == stop.format('YYYY-MM-DD 23:59:59')
    assert result[2]['project'] == 'foo'
    assert result[2]['tag'] == 'B'
    assert result[2]['time'] == (4 + 2) * 3600 
示例22
def test_json_arrow_encoder():
    with pytest.raises(TypeError):
        json_arrow_encoder(0)

    with pytest.raises(TypeError):
        json_arrow_encoder('foo')

    with pytest.raises(TypeError):
        json_arrow_encoder(None)

    now = arrow.utcnow()
    assert json_arrow_encoder(now) == now.for_json() 
示例23
def process_subscription_form(request):
    # Avoid circular dependency
    from .forms import SubscriptionCreateForm

    form = SubscriptionCreateForm(request.POST)

    if form.is_valid():
        # Create a temporary subscription object to modify it's fields
        subscription = form.save(commit=False)

        # Attach request user to subscription before save
        subscription.user = request.user

        # Set subscription start and end dates
        # based on current day
        today = arrow.utcnow()

        # Start date is Today
        subscription.start_date = today.date()

        # End date is today plus subscription duration
        subscription.end_date = today.shift(
            years=+subscription.duration).date()

        subscription.save()

        # set the order in the session
        request.session["subscription_id"] = subscription.id

        # redirect for payment
        return redirect(
            reverse(
                "payment:process",
                kwargs={
                    "previous_page": "subscribe"
                }
            )
        ) 
示例24
def is_public_access(self):
        """
        Check whether article should be accessible to all readers
        or only subscribers based on issue publication date.
        """
        parent_issue = self.get_parent()

        today = arrow.utcnow()

        six_months_ago = today.shift(months=-6).date()

        # Issues older than six months are public access
        return parent_issue.specific.publication_date <= six_months_ago 
示例25
def update_templates(version_data):
    version_data["debian_ts"] = arrow.utcnow().format(
        'ddd, D MMM YYYY h:mm:ss Z')
    for path in TEMPLATES:
        if not path.endswith(".in"):
            continue

        target = path[:-3]
        with open(target, "wt") as outfd:
            outfd.write(open(path, "rt").read() % version_data) 
示例26
def update_templates(version_data):
    version_data["debian_ts"] = arrow.utcnow().format(
        'ddd, D MMM YYYY h:mm:ss Z')
    for path in TEMPLATES:
        if not path.endswith(".in"):
            continue

        target = path[:-3]
        with open(target, "wt") as outfd:
            outfd.write(open(path, "rt").read() % version_data) 
示例27
def _create_on_loan_brwreq_random_dates(patron_id, client, json_headers):
    """Create a new ON_LOAN ILL borrowing request with random start/end."""
    loan_start_date = arrow.utcnow()
    loan_end_date = loan_start_date + timedelta(days=15)
    brwreq = _create_on_loan_brwreq(
        patron_id,
        loan_start_date.date().isoformat(),
        loan_end_date.date().isoformat(),
        client,
        json_headers,
    )
    return brwreq, brwreq["pid"] 
示例28
def test_brwreq_accept_decline_extension_only_librarian(
    client, testdata, json_headers, users
):
    """Test that only librarian can accept or decline an extension."""

    tests = [_accept_extension_action, _decline_extension_action]
    for action in tests:
        user_login(client, "librarian", users)

        # create request
        brwreq, brwreq_pid = _create_on_loan_brwreq_with_pending_extension(
            "1", client, json_headers
        )

        loan_end_date = arrow.utcnow() + timedelta(days=15)
        data = dict(loan_end_date=loan_end_date.date().isoformat())

        # accept/decline

        # anonymous, forbidden
        user_logout(client)
        res = action(brwreq_pid, data, client, json_headers)
        assert res.status_code == 401

        # patron, forbidden
        user_login(client, "patron1", users)
        res = action(brwreq_pid, data, client, json_headers)
        assert res.status_code == 403

        # librarian, success
        user_login(client, "librarian", users)
        res = action(brwreq_pid, data, client, json_headers)
        assert res.status_code == 200 
示例29
def test_brwreq_accept_decline_extension_should_fail_when_loan_not_active(
    client, testdata, json_headers, users
):
    """Test that accept or decline an extension fails on loan not active."""

    tests = [_accept_extension_action, _decline_extension_action]
    for action in tests:
        user_login(client, "librarian", users)

        # create request
        brwreq, brwreq_pid = _create_on_loan_brwreq_with_pending_extension(
            "1", client, json_headers
        )

        # check-in loan
        loan_pid = brwreq["patron_loan"]["pid"]
        url = url_for(
            "invenio_circulation_loan_actions.loanid_actions",
            pid_value=loan_pid,
            action="checkin",
        )
        item_pid = dict(type=BORROWING_REQUEST_PID_TYPE, value=brwreq["pid"])
        params = dict(
            document_pid=brwreq["document_pid"],
            item_pid=item_pid,
            patron_pid=brwreq["patron_pid"],
            transaction_location_pid="locid-1",
            transaction_user_pid="1",
        )
        res = client.post(url, headers=json_headers, data=json.dumps(params))
        assert res.status_code == 202

        # accept extension
        loan_end_date = arrow.utcnow() + timedelta(days=15)
        data = dict(loan_end_date=loan_end_date.date().isoformat())

        res = action(brwreq_pid, data, client, json_headers)
        assert res.status_code == 400 
示例30
def test_brwreq_create_loan_fails_on_wrong_loan_end_date(
    db, client, testdata, json_headers, users
):
    """Test borrowing requests create loan action fails on wrong end date."""
    user_login(client, "librarian", users)

    # demo data "illbid-2" has the valid state `REQUESTED`
    pid = "illbid-2"
    # missing date param
    _assert_create_loan_action_fails(pid, dict(), client, json_headers)

    now = arrow.utcnow()

    # wrong format date
    future = now + timedelta(days=5)
    data = dict(
        loan_start_date=now.isoformat(),
        loan_end_date=future.date().isoformat(),
    )
    _assert_create_loan_action_fails(pid, data, client, json_headers)
    data = dict(
        loan_start_date=now.date().isoformat(),
        loan_end_date=future.isoformat(),
    )
    _assert_create_loan_action_fails(pid, data, client, json_headers)

    # past date
    past = now - timedelta(days=15)
    data = dict(
        loan_start_date=now.date().isoformat(),
        loan_end_date=past.date().isoformat(),
    )
    _assert_create_loan_action_fails(pid, data, client, json_headers)