Python oslo_serialization.jsonutils.load() Examples

The following are code examples for showing how to use oslo_serialization.jsonutils.load(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: vendordata_json.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(JsonFileVendorData, self).__init__(*args, **kwargs)
        data = {}
        fpath = CONF.vendordata_jsonfile_path
        logprefix = "%s[%s]:" % (file_opt.name, fpath)
        if fpath:
            try:
                with open(fpath, "r") as fp:
                    data = jsonutils.load(fp)
            except IOError as e:
                if e.errno == errno.ENOENT:
                    LOG.warning(_LW("%(logprefix)s file does not exist"),
                                {'logprefix': logprefix})
                else:
                    LOG.warning(_LW("%(logprefix)s unexpected IOError when "
                                    "reading"), {'logprefix': logprefix})
                raise e
            except ValueError:
                LOG.warning(_LW("%(logprefix)s failed to load json"),
                            {'logprefix': logprefix})
                raise

        self._data = data 
Example 2
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: scheduler_options.py    Apache License 2.0 6 votes vote down vote up
def get_configuration(self, filename=None):
        """Check the json file for changes and load it if needed."""
        if not filename:
            filename = CONF.scheduler_json_config_location
        if not filename:
            return self.data
        if self.last_checked:
            now = self._get_time_now()
            if now - self.last_checked < datetime.timedelta(minutes=5):
                return self.data

        last_modified = self._get_file_timestamp(filename)
        if (not last_modified or not self.last_modified or
                last_modified > self.last_modified):
            self.data = self._load_file(self._get_file_handle(filename))
            self.last_modified = last_modified
        if not self.data:
            self.data = {}

        return self.data 
Example 3
Project: nova   Author: ZhanHan   File: vendordata_json.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(JsonFileVendorData, self).__init__(*args, **kwargs)
        data = {}
        fpath = CONF.api.vendordata_jsonfile_path
        logprefix = "vendordata_jsonfile_path[%s]:" % fpath
        if fpath:
            try:
                with open(fpath, "rb") as fp:
                    data = jsonutils.load(fp)
            except IOError as e:
                if e.errno == errno.ENOENT:
                    LOG.warning(_LW("%(logprefix)s file does not exist"),
                                {'logprefix': logprefix})
                else:
                    LOG.warning(_LW("%(logprefix)s unexpected IOError when "
                                    "reading"), {'logprefix': logprefix})
                raise
            except ValueError:
                LOG.warning(_LW("%(logprefix)s failed to load json"),
                            {'logprefix': logprefix})
                raise

        self._data = data 
Example 4
Project: networking-bagpipe   Author: openstack   File: impex2dot.py    Apache License 2.0 6 votes vote down vote up
def request(options, server, args):
    target_url = "http://%s:%d/%s/%s" % (server, options.port,
                                         options.prefix, "/".join(args))
    try:
        os.environ['NO_PROXY'] = server
        os.environ['no_proxy'] = server
        response = urllib.request.urlopen(target_url)

        if response.getcode() == 200:
            return jsonutils.load(response)
    except urllib.error.HTTPError as e:
        if e.code == 404:
            return {}
        print("Error requesting %s" % target_url)
        raise
    except Exception:
        print("Error requesting %s" % target_url)
        raise 
Example 5
Project: stx-nova   Author: starlingx-staging   File: vendordata_json.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(JsonFileVendorData, self).__init__(*args, **kwargs)
        data = {}
        fpath = CONF.api.vendordata_jsonfile_path
        logprefix = "vendordata_jsonfile_path[%s]:" % fpath
        if fpath:
            try:
                with open(fpath, "rb") as fp:
                    data = jsonutils.load(fp)
            except IOError as e:
                if e.errno == errno.ENOENT:
                    LOG.warning("%(logprefix)s file does not exist",
                                {'logprefix': logprefix})
                else:
                    LOG.warning("%(logprefix)s unexpected IOError when "
                                "reading", {'logprefix': logprefix})
                raise
            except ValueError:
                LOG.warning("%(logprefix)s failed to load json",
                            {'logprefix': logprefix})
                raise

        self._data = data 
Example 6
Project: python-opflex-agent   Author: noironetworks   File: namespace_proxy.py    Apache License 2.0 6 votes vote down vote up
def get_network_id(self, domain_id, remote_address):
        filedir = '/var/lib/neutron/opflex_agent'
        filename = 'instance_networks.state'
        fqfn = '%s/%s' % (filedir, filename)

        nets = None
        try:
            with open(fqfn, "r") as f:
                nets = jsonutils.load(f)
        except Exception as e:
            LOG.warning("Exception in reading file: %s" % str(e))

        if nets:
            if domain_id in nets:
                if remote_address in nets[domain_id]:
                    return nets[domain_id][remote_address]
        LOG.warning("IP address not found: domain=%s, addr=%s" % (
                    domain_id, remote_address))
        return None 
Example 7
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: policy_fixture.py    Apache License 2.0 5 votes vote down vote up
def _prepare_policy(self):
        policy = jsonutils.load(open(CONF.oslo_policy.policy_file))

        # Convert all actions to require specified role
        for action, rule in six.iteritems(policy):
            policy[action] = 'role:%s' % self.role

        self.policy_dir = self.useFixture(fixtures.TempDir())
        self.policy_file = os.path.join(self.policy_dir.path,
                                            'policy.json')
        with open(self.policy_file, 'w') as f:
            jsonutils.dump(policy, f) 
Example 8
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: scheduler_options.py    Apache License 2.0 5 votes vote down vote up
def _load_file(self, handle):
        """Decode the JSON file. Broken out for testing."""
        try:
            return jsonutils.load(handle)
        except ValueError:
            LOG.exception(_LE("Could not decode scheduler options"))
            return {} 
Example 9
Project: ara-archive   Author: dmsimard   File: test_callback.py    GNU General Public License v3.0 5 votes vote down vote up
def test_playbook_persistence(self):
        r_playbook = m.Playbook.query.first()
        tmpfile = os.path.join(self.app.config['ARA_TMP_DIR'], 'ara.json')

        with open(tmpfile, 'rb') as file:
            data = jsonutils.load(file)
        self.assertEqual(r_playbook.id, data['playbook']['id']) 
Example 10
Project: kuryr-kubernetes   Author: openstack   File: main.py    Apache License 2.0 5 votes vote down vote up
def run():
    if six.PY3:
        d = jsonutils.load(sys.stdin.buffer)
    else:
        d = jsonutils.load(sys.stdin)
    cni_conf = utils.CNIConfig(d)
    args = (['--config-file', cni_conf.kuryr_conf] if 'kuryr_conf' in d
            else [])

    try:
        if cni_conf.debug:
            args.append('-d')
    except AttributeError:
        pass
    config.init(args)
    config.setup_logging()

    # Initialize o.vo registry.
    k_objects.register_locally_defined_vifs()
    os_vif.initialize()

    runner = cni_api.CNIDaemonizedRunner()

    def _timeout(signum, frame):
        runner._write_dict(sys.stdout, {
            'msg': 'timeout',
            'code': k_const.CNI_TIMEOUT_CODE,
        })
        LOG.debug('timed out')
        sys.exit(1)

    signal.signal(signal.SIGALRM, _timeout)
    signal.alarm(_CNI_TIMEOUT)
    status = runner.run(os.environ, cni_conf, sys.stdout)
    LOG.debug("Exiting with status %s", status)
    if status:
        sys.exit(status) 
Example 11
Project: stx-nova   Author: starlingx-staging   File: json_ref.py    Apache License 2.0 5 votes vote down vote up
def _resolve_ref(ref, base_path):
    file_path, _, json_path = ref.partition('#')
    if json_path:
        raise NotImplementedError('JSON refs with JSON path after the "#" is '
                                  'not yet supported')

    path = os.path.join(base_path, file_path)
    # binary mode is needed due to bug/1515231
    with open(path, 'r+b') as f:
        ref_value = jsonutils.load(f)
        base_path = os.path.dirname(path)
        res = resolve_refs(ref_value, base_path)
        return res 
Example 12
Project: ironic-tempest-plugin   Author: openstack   File: introspection_manager.py    Apache License 2.0 5 votes vote down vote up
def rule_import(self, rule_path):
        with open(rule_path, 'rb') as fp:
            rules = json.load(fp)
        self.introspection_client.create_rules(rules) 
Example 13
Project: searchlight   Author: openstack   File: test_api.py    Apache License 2.0 5 votes vote down vote up
def _modify_policy_file(self, rules):
        with open(self.policy_file, 'r+b') as policy_file:
            existing_policy = jsonutils.load(policy_file)

        existing_policy.update(rules)

        with open(self.policy_file, 'w') as policy_file:
            jsonutils.dump(existing_policy, policy_file)

        time.sleep(2) 
Example 14
Project: searchlight   Author: openstack   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _load_fixture_data(self, name):
        base_dir = "searchlight/tests/functional/data"
        # binary mode is needed due to bug/1515231
        with open(os.path.join(base_dir, name), 'r+b') as f:
            return jsonutils.load(f) 
Example 15
Project: searchlight   Author: openstack   File: test_glance_plugins.py    Apache License 2.0 5 votes vote down vote up
def _get_glance_image_owner_and_count(self):
        with open(generate_load_data.IMAGES_FILE, "r+b") as file:
            images_data = jsonutils.load(file)
        if len(images_data) > 0:
            return len(images_data), images_data[0]['owner'] 
Example 16
Project: searchlight   Author: openstack   File: test_glance_plugins.py    Apache License 2.0 5 votes vote down vote up
def _get_glance_metadefs_owner_and_count(self):
        with open(generate_load_data.METADEFS_FILE, "r+b") as file:
            metadefs_data = jsonutils.load(file)
        if len(metadefs_data) > 0:
            return len(metadefs_data), metadefs_data[0]['owner'] 
Example 17
Project: searchlight   Author: openstack   File: mock_glance_pyclient.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        # Load Images from file
        self._images = []
        with open(generate_load_data.IMAGES_FILE, "r+b") as file:
            image_data = jsonutils.load(file)
        for image in image_data:
            fake_image = FakeImage(**image)
            self._images.append(fake_image)
        self.images = FakeImages(self._images)

        # Load Images members from file
        self._images_members_dict = dict()
        self._image_members_list = []
        with open(generate_load_data.IMAGE_MEMBERS_FILE, "r+b") as file:
            image_members_data = jsonutils.load(file)
        for image_id, image_members in image_members_data.items():
            for image_member in image_members:
                fake_image_member = FakeImageMember(**image_member)
                self._image_members_list.append(fake_image_member)
            self._images_members_dict[image_id] = self._image_members_list
        self.image_members = FakeImageMembers(self._images_members_dict)

        # Load Metadef namespaces from file
        self._metadefs_namespace = []
        self.metadefs_namespace = []
        with open(generate_load_data.METADEFS_FILE, "r+b") as file:
            metadefs_namespace_data = jsonutils.load(file)
        for metadef_namespace in metadefs_namespace_data:
            fake_namespace = FakeNamespace(**metadef_namespace)
            self._metadefs_namespace.append(fake_namespace)
        self.metadefs_namespace = FakeNamespaces(self._metadefs_namespace) 
Example 18
Project: kuryr-kubernetes   Author: openshift   File: main.py    Apache License 2.0 5 votes vote down vote up
def run():
    if six.PY3:
        d = jsonutils.load(sys.stdin.buffer)
    else:
        d = jsonutils.load(sys.stdin)
    cni_conf = utils.CNIConfig(d)
    args = (['--config-file', cni_conf.kuryr_conf] if 'kuryr_conf' in d
            else [])

    try:
        if cni_conf.debug:
            args.append('-d')
    except AttributeError:
        pass
    config.init(args)
    config.setup_logging()

    # Initialize o.vo registry.
    k_objects.register_locally_defined_vifs()
    os_vif.initialize()

    runner = cni_api.CNIDaemonizedRunner()

    def _timeout(signum, frame):
        runner._write_dict(sys.stdout, {
            'msg': 'timeout',
            'code': k_const.CNI_TIMEOUT_CODE,
        })
        LOG.debug('timed out')
        sys.exit(1)

    signal.signal(signal.SIGALRM, _timeout)
    signal.alarm(_CNI_TIMEOUT)
    status = runner.run(os.environ, cni_conf, sys.stdout)
    LOG.debug("Exiting with status %s", status)
    if status:
        sys.exit(status) 
Example 19
Project: python-freezerclient   Author: openstack   File: utils.py    Apache License 2.0 5 votes vote down vote up
def doc_from_json_file(path_to_file):
    """Build a json from a file in the file system
    :param path_to_file: path to file
    :return: in memory file in json format
    """
    with open(path_to_file, 'rb') as fd:
        try:
            return json.load(fd)
        except Exception as err:
            logging.error(err)
            raise Exception('Unable to load conf file. {0}'.format(err)) 
Example 20
Project: masakari   Author: openstack   File: json_ref.py    Apache License 2.0 5 votes vote down vote up
def _resolve_ref(ref, base_path):
    file_path, _, json_path = ref.partition('#')
    if json_path:
        raise NotImplementedError('JSON refs with JSON path after the "#" is '
                                  'not yet supported')

    path = os.path.join(base_path, file_path)
    # binary mode is needed due to bug/1515231
    with open(path, 'r+b') as f:
        ref_value = jsonutils.load(f)
        base_path = os.path.dirname(path)
        res = resolve_refs(ref_value, base_path)
        return res 
Example 21
Project: dragonflow   Author: openstack   File: datapath.py    Apache License 2.0 5 votes vote down vote up
def _get_app_class(self, app_type):
        """Get an application class (Python class) by app name"""
        mgr = stevedore.NamedExtensionManager(
            'dragonflow.controller.apps',
            [app_type],
            invoke_on_load=False,
        )
        for ext in mgr:
            return ext.plugin
        else:
            raise RuntimeError(_('Failed to load app {0}').format(app_type)) 
Example 22
Project: dragonflow   Author: openstack   File: datapath.py    Apache License 2.0 5 votes vote down vote up
def load_datapath_from_file_stream(stream):
    dp_allocs_dict = jsonutils.load(stream)
    return load_datapath_from_dict(dp_allocs_dict) 
Example 23
Project: freezer-tempest-plugin   Author: openstack   File: test_backup_compress.py    Apache License 2.0 5 votes vote down vote up
def _metadata(self):
        path = os.path.join(self.storage_tree.path, 'metadata.json')
        with open(path, 'r') as f:
            return json.load(f) 
Example 24
Project: freezer-tempest-plugin   Author: openstack   File: base.py    Apache License 2.0 5 votes vote down vote up
def load_metadata(path):
    """Given a metadata path, return a dict containing parsed values.

    :param path: the path to load
    :return: a metadata dict
    """
    with open(path, 'r') as f:
        return json.load(f) 
Example 25
Project: watcher   Author: openstack   File: test_notifications.py    Apache License 2.0 5 votes vote down vote up
def load_message(self, filename):
        cwd = os.path.abspath(os.path.dirname(__file__))
        data_folder = os.path.join(cwd, "data")

        with open(os.path.join(data_folder, filename), 'rb') as json_file:
            json_data = jsonutils.load(json_file)

        return json_data 
Example 26
Project: watcher   Author: openstack   File: test_cinder_notifications.py    Apache License 2.0 5 votes vote down vote up
def load_message(filename):
        cwd = os.path.abspath(os.path.dirname(__file__))
        data_folder = os.path.join(cwd, "data")

        with open(os.path.join(data_folder, filename), 'rb') as json_file:
            json_data = jsonutils.load(json_file)

        return json_data 
Example 27
Project: watcher   Author: openstack   File: test_nova_notifications.py    Apache License 2.0 5 votes vote down vote up
def load_message(filename):
        cwd = os.path.abspath(os.path.dirname(__file__))
        data_folder = os.path.join(cwd, "data")

        with open(os.path.join(data_folder, filename), 'rb') as json_file:
            json_data = jsonutils.load(json_file)

        return json_data 
Example 28
Project: python-opflex-agent   Author: noironetworks   File: as_metadata_manager.py    Apache License 2.0 5 votes vote down vote up
def read_jsonfile(name):
    retval = {}
    try:
        with open(name, "r") as f:
            retval = jsonutils.load(f)
    except Exception as e:
        LOG.warn("Exception in reading file: %s" % str(e))
    return retval 
Example 29
Project: keystone_docker   Author: tobegit3hub   File: cli.py    Apache License 2.0 5 votes vote down vote up
def upload_configs_to_database(self, file_name, domain_name):
        """Upload configs from file and load into database.

        This method will be called repeatedly for all the config files in the
        config directory. To provide a better UX, we differentiate the error
        handling in this case (versus when the user has asked for a single
        config file to be uploaded).

        """
        try:
            self.upload_config_to_database(file_name, domain_name)
        except ValueError:
            # We've already given all the info we can in a message, so carry
            # on to the next one
            pass
        except Exception:
            # Some other error occurred relating to this specific config file
            # or domain. Since we are trying to upload all the config files,
            # we'll continue and hide this exception. However, we tell the
            # user how to get more info about this error by re-running with
            # just the domain at fault. When we run in single-domain mode we
            # will NOT hide the exception.
            print(_('To get a more detailed information on this error, re-run '
                    'this command for the specific domain, i.e.: '
                    'keystone-manage domain_config_upload --domain-name %s') %
                  domain_name)
            pass 
Example 30
Project: keystone_docker   Author: tobegit3hub   File: cli.py    Apache License 2.0 5 votes vote down vote up
def read_domain_configs_from_files(self):
        """Read configs from file(s) and load into database.

        The command line parameters have already been parsed and the CONF
        command option will have been set. It is either set to the name of an
        explicit domain, or it's None to indicate that we want all domain
        config files.

        """
        domain_name = CONF.command.domain_name
        conf_dir = CONF.identity.domain_config_dir
        if not os.path.exists(conf_dir):
            print(_('Unable to locate domain config directory: %s') % conf_dir)
            raise ValueError

        if domain_name:
            # Request is to upload the configs for just one domain
            fname = DOMAIN_CONF_FHEAD + domain_name + DOMAIN_CONF_FTAIL
            self.upload_config_to_database(
                os.path.join(conf_dir, fname), domain_name)
            return

        # Request is to transfer all config files, so let's read all the
        # files in the config directory, and transfer those that match the
        # filename pattern of 'keystone.<domain_name>.conf'
        for r, d, f in os.walk(conf_dir):
            for fname in f:
                if (fname.startswith(DOMAIN_CONF_FHEAD) and
                        fname.endswith(DOMAIN_CONF_FTAIL)):
                    if fname.count('.') >= 2:
                        self.upload_configs_to_database(
                            os.path.join(r, fname),
                            fname[len(DOMAIN_CONF_FHEAD):
                                  -len(DOMAIN_CONF_FTAIL)])
                    else:
                        LOG.warn(_LW('Ignoring file (%s) while scanning '
                                     'domain config directory'), fname) 
Example 31
Project: keystone_docker   Author: tobegit3hub   File: cli.py    Apache License 2.0 5 votes vote down vote up
def read_rules(path):
        try:
            with open(path) as file:
                return jsonutils.load(file)
        except ValueError as e:
            raise SystemExit(_('Error while parsing rules '
                               '%(path)s: %(err)s') % {'path': path, 'err': e})