Beispiel #1
0
def run(opts):
    """Run the examples"""

    # Set up certificate verification and CA bundle
    # NOTE(dtroyer): This converts from the usual OpenStack way to the single
    #                requests argument and is an app-specific thing because
    #                we want to be like OpenStackClient.
    if opts.os_cacert:
        verify = opts.os_cacert
    else:
        verify = not opts.insecure

    # get a session
    # common.make_session() does all the ugly work of mapping
    # CLI options/env vars to the required plugin arguments.
    # The returned session will have a configured auth object
    # based on the selected plugin's available options.
    # So to do...oh, just go to api.auth.py and look at what it does.
    session = common.make_session(opts, verify=verify)

    # Extract an endpoint
    auth_ref = session.auth.get_auth_ref(session)

    if opts.os_url:
        endpoint = opts.os_url
    else:
        endpoint = auth_ref.service_catalog.url_for(
            service_type='object-store',
            endpoint_type='public',
        )

    # At this point we have a working session with a configured authentication
    # plugin.  From here on it is the app making the decisions.  Need to
    # talk to two clouds?  Go back and make another session but with opts
    # set to different credentials.  Or use a config file and load it
    # directly into the plugin.  This example doesn't show that (yet).
    # Want to work ahead?  Look into the plugin load_from_*() methods
    # (start in keystoneclient/auth/base.py).

    # This example is for the Object Store API so make one
    obj_api = object_store.APIv1(
        session=session,
        service_type='object-store',
        endpoint=endpoint,
    )

    # Do useful things with it

    c_list = obj_api.container_list()
    print("Name\tCount\tBytes")
    for c in c_list:
        print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))

    if len(c_list) > 0:
        # See what is in the first container
        o_list = obj_api.object_list(c_list[0]['name'])
        print("\nObject")
        for o in o_list:
            print("%s" % o)
def run(opts):
    """Run the examples"""

    # Set up certificate verification and CA bundle
    # NOTE(dtroyer): This converts from the usual OpenStack way to the single
    #                requests argument and is an app-specific thing because
    #                we want to be like OpenStackClient.
    if opts.os_cacert:
        verify = opts.os_cacert
    else:
        verify = not opts.insecure

    # get a session
    # common.make_session() does all the ugly work of mapping
    # CLI options/env vars to the required plugin arguments.
    # The returned session will have a configured auth object
    # based on the selected plugin's available options.
    # So to do...oh, just go to api.auth.py and look at what it does.
    session = common.make_session(opts, verify=verify)

    # Extract an endpoint
    auth_ref = session.auth.get_auth_ref(session)

    if opts.os_url:
        endpoint = opts.os_url
    else:
        endpoint = auth_ref.service_catalog.url_for(
            service_type='object-store',
            endpoint_type='public',
        )

    # At this point we have a working session with a configured authentication
    # plugin.  From here on it is the app making the decisions.  Need to
    # talk to two clouds?  Go back and make another session but with opts
    # set to different credentials.  Or use a config file and load it
    # directly into the plugin.  This example doesn't show that (yet).
    # Want to work ahead?  Look into the plugin load_from_*() methods
    # (start in keystoneclient/auth/base.py).

    # This example is for the Object Store API so make one
    obj_api = object_store.APIv1(
        session=session,
        service_type='object-store',
        endpoint=endpoint,
    )

    # Do useful things with it

    c_list = obj_api.container_list()
    print("Name\tCount\tBytes")
    for c in c_list:
        print("%s\t%d\t%d" % (c['name'], c['count'], c['bytes']))

    if len(c_list) > 0:
        # See what is in the first container
        o_list = obj_api.object_list(c_list[0]['name'])
        print("\nObject")
        for o in o_list:
            print("%s" % o)
import json
import pathlib
import re

from common import make_session
from requests import ConnectionError

DATA_DIR = pathlib.Path("data") / "spenergynetworks"

if __name__ == "__main__":
    DATA_DIR.mkdir(parents=True, exist_ok=True)

    with make_session() as session:
        # Website is quite slow so have a high timeout threshold
        try:
            r = session.get(
                "https://www.spenergynetworks.co.uk/pages/power_cuts_map.aspx",
                timeout=30)
        except ConnectionError:
            pass
        else:
            r.raise_for_status()

            if r.url in [
                    "https://www.spenergynetworks.co.uk/pages/power_cuts_not_available.aspx",
                    "https://www.spenergynetworks.co.uk/pages/500.aspx"
            ]:
                # Not available
                pass
            else:
                m = re.search("arrPowercutsPostcodes: (?P<data>\[.*\]),",