How to use the datapackage.exceptions.DataPackageException function in datapackage

To help you get started, we’ve selected a few datapackage examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Spine-project / Spine-Toolbox / spinetoolbox / widgets / spine_datapackage_widget.py View on Github external
anything_updated = False
        rows = range(top_left.row(), bottom_right.row() + 1)
        error_log = ""
        for row in rows:
            # Remove previous foreign key
            self.datapackage.remove_foreign_keys_row(row, resource)
            # Add new foreign key if possible
            row_data = self.foreign_keys_model._main_data[row][0:3]
            if all(row_data):
                fields_str, reference_resource, reference_fields_str = row_data
                fields = fields_str.split(",")
                reference_fields = reference_fields_str.split(",")
                try:
                    self.datapackage.insert_foreign_key(row, resource, fields, reference_resource, reference_fields)
                    anything_updated = True
                except DataPackageException as e:
                    v_section = self.foreign_keys_model.headerData(row, Qt.Vertical)
                    error_log += "<p>Unable to add foreign key at row {0}: '{1}'</p>".format(v_section, e)
        if anything_updated:
            self.msg.emit("Successfully updated foreign keys.")
        if error_log:
            self.msg_error.emit(error_log)
github frictionlessdata / datapackage-py / datapackage / helpers.py View on Github external
# Force UTF8 encoding for 'text/plain' sources
                req.encoding = 'utf8'
                the_descriptor = req.json()
        except (IOError, requests.exceptions.RequestException) as error:
            message = 'Unable to load JSON at "%s"' % descriptor
            six.raise_from(exceptions.DataPackageException(message), error)
        except ValueError as error:
            # Python2 doesn't have json.JSONDecodeError (use ValueErorr)
            message = 'Unable to parse JSON at "%s". %s' % (descriptor, error)
            six.raise_from(exceptions.DataPackageException(message), error)

    if hasattr(the_descriptor, 'read'):
        try:
            the_descriptor = json.load(the_descriptor)
        except ValueError as e:
            six.raise_from(exceptions.DataPackageException(str(e)), e)

    if not isinstance(the_descriptor, dict):
        msg = 'Data must be a \'dict\', but was a \'{0}\''
        raise exceptions.DataPackageException(msg.format(type(the_descriptor).__name__))

    return the_descriptor
github oemof / oemof-tabular / src / oemof / tabular / cli.py View on Github external
)
                            )

        except Exception:
            # TODO: Fix this.
            # Again, this contains the same errors as above.
            # Also: when re-raising an exception, don't cover up the original
            # exception type and message.
            # See 715fa6aeecb45837c5679657a3a094d151862820 and its parent for
            # an IMHO better way of dealing with this.
            # So again:
            #   * fix how the Exception is raised,
            #   * write a test to trigger this line,
            #   * fix the generated message and
            #   * use the test to check the the exception's type and message.
            raise exceptions.DataPackageException(
                (
                    "Could not read resource {} from datpackage "
                    "with name {}".format(r.name, p.descriptor['name'])
                )
            )
    print("Successfully tested datapackage {}.".format(p.descriptor['name']))
github frictionlessdata / datapackage-py / datapackage / helpers.py View on Github external
if not value.startswith('http'):
                    fullpath = os.path.join(base_path, value)
                response = requests.get(fullpath)
                response.raise_for_status()
                descriptor[property] = response.json()
            except Exception as error:
                message = 'Not resolved Remote URI "%s" for resource.%s' % (value, property)
                six.raise_from(
                    exceptions.DataPackageException(message),
                    error
                )

        # URI -> Local
        else:
            if not is_safe_path(value):
                raise exceptions.DataPackageException(
                    'Not safe path in Local URI "%s" '
                    'for resource.%s' % (value, property))
            if not base_path:
                raise exceptions.DataPackageException(
                    'Local URI "%s" requires base path '
                    'for resource.%s' % (value, property))
            fullpath = os.path.join(base_path, value)
            try:
                with io.open(fullpath, encoding='utf-8') as file:
                    descriptor[property] = json.load(file)
            except Exception as error:
                message = 'Not resolved Local URI "%s" for resource.%s' % (value, property)
                six.raise_from(
                    exceptions.DataPackageException(message),
                    error
                )
github frictionlessdata / datapackage-py / datapackage / package.py View on Github external
def _validate_zip(the_zip):
    """Validate zipped data package
    """
    datapackage_jsons = [f for f in the_zip.namelist() if f.endswith('datapackage.json')]
    if len(datapackage_jsons) != 1:
        msg = 'DataPackage must have only one "datapackage.json" (had {n})'
        raise exceptions.DataPackageException(msg.format(n=len(datapackage_jsons)))
github frictionlessdata / data-quality-cli / data_quality / tasks / base_task.py View on Github external
self.result_file = os.path.join(self.data_dir, self.config['result_file'])
        self.run_file = os.path.join(self.data_dir, self.config['run_file'])
        self.source_file = os.path.join(self.data_dir, self.config['source_file'])
        self.performance_file = os.path.join(self.data_dir,
                                             self.config['performance_file'])
        self.publisher_file = os.path.join(self.data_dir,
                                           self.config['publisher_file'])
        self.cache_dir = self.config['cache_dir']
        self.data_key = self.config['goodtables']['arguments']['batch']['data_key']
        datapkg_file_path = self.config.get('datapackage_file', 'datapackage.json')
        if not os.path.isabs(datapkg_file_path):
            datapkg_file_path = os.path.join(os.path.dirname(self.data_dir),
                                             datapkg_file_path)
        try:
            self.datapackage = datapackage.DataPackage(datapkg_file_path)
        except datapackage.exceptions.DataPackageException as e:
            raise ValueError(('A datapackage couldn\'t be created because of the '
                              'following error: "{0}". Make sure the file is not '
                              'empty and use "dq init" command.').format(e))
        self.all_scores = []
github frictionlessdata / datapackage-py / datapackage / helpers.py View on Github external
message = 'Unable to load JSON at "%s"' % descriptor
            six.raise_from(exceptions.DataPackageException(message), error)
        except ValueError as error:
            # Python2 doesn't have json.JSONDecodeError (use ValueErorr)
            message = 'Unable to parse JSON at "%s". %s' % (descriptor, error)
            six.raise_from(exceptions.DataPackageException(message), error)

    if hasattr(the_descriptor, 'read'):
        try:
            the_descriptor = json.load(the_descriptor)
        except ValueError as e:
            six.raise_from(exceptions.DataPackageException(str(e)), e)

    if not isinstance(the_descriptor, dict):
        msg = 'Data must be a \'dict\', but was a \'{0}\''
        raise exceptions.DataPackageException(msg.format(type(the_descriptor).__name__))

    return the_descriptor
github frictionlessdata / datapackage-py / datapackage / resource.py View on Github external
> Only for tabular resources
        > It has the same API as `resource.iter` except for

        # Arguments
            limit (int): limit count of rows to read and return

        # Returns
            list[]: returns rows

        """

        # Error for non tabular
        if not self.tabular:
            message = 'Methods iter/read are not supported for non tabular data'
            raise exceptions.DataPackageException(message)

        # Get integrity
        if integrity:
            integrity = self.__get_integrity()

        # Get relations
        if relations and not foreign_keys_values:
            relations = self.__get_relations()

        return self.__get_table().read(
            integrity=integrity, relations=relations,
            foreign_keys_values=foreign_keys_values, **options)