Skip to content

Commit

Permalink
Merge branch 'code-formatting' into tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Aug 21, 2023
2 parents 0e01166 + 5f68a39 commit df40a40
Show file tree
Hide file tree
Showing 52 changed files with 1,681 additions and 1,335 deletions.
48 changes: 23 additions & 25 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@ jobs:
run: |
sudo apt-get update --fix-missing
- name: Workaround - install CMake 3.25.2 since 3.26.0 doesn't work
run: |
sudo apt remove cmake
sudo apt purge --auto-remove cmake
wget http://www.cmake.org/files/v3.25/cmake-3.25.2.tar.gz
tar xf cmake-3.25.2.tar.gz
cd cmake-3.25.2
./configure
make
sudo make install
hash -r
cd -
- name: Show CMake version
run: |
cmake --version
- name: Install dependencies
run: |
sudo apt-get install \
Expand All @@ -45,16 +28,26 @@ jobs:
- name: Install Python dependencies
run: |
pip install --upgrade pip -r requirements.txt
pip install -r requirements_full.txt
pip install -r requirements_dev.txt
pip install -r requirements_doc.txt
python3 -m pip install --upgrade pip -r requirements.txt
python3 -m pip install -r requirements_full.txt
python3 -m pip install -r requirements_dev.txt
python3 -m pip install -r requirements_doc.txt
- name: check numpy
run: |
which python3
python3 --version
pip3 --version
python3 -m pip --version
python3 -c 'import numpy as np; print(np.get_include())'
- name: Workaround - install CMake 3.25.2 since 3.26.0 doesn't work
# Normally we would install cmake with the package manager, but
# ubuntu 20.04 doesn't seems to keep older versions of cmake around...
# Fortunately, there exists a pip package
run: |
python3 -m pip install cmake==3.25.2
cmake --version
- name: configure
run: |
mkdir build
Expand All @@ -63,27 +56,32 @@ jobs:
CFLAGS='-Wno-missing-field-initializers' \
cmake .. -DFORCE_EXAMPLES=ON -DWITH_FORTRAN=YES
cd -
- name: make
run: |
cd build
make
cmake --build .
cd -
- name: install
run: |
cd build
make install
cmake --install .
cd -
- name: make test
run: |
cd build
ctest || ctest --rerun-failed --output-on-failure -V
cd -
- name: build Linux wheel
run: |
cd python
pip install numpy wheel
python3 -m pip install numpy wheel
python3 setup.py bdist_wheel
cd -
- name: Install python package via setup.py and test the installation
run: |
cd python
Expand Down
46 changes: 27 additions & 19 deletions bindings/python/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
class DataModelError(dlite.DLiteError):
"""Raised if the datamodel is inconsistent."""


class MissingDimensionError(DataModelError):
"""Raised if a dimension referred to in a property is not defined."""


class UnusedDimensionError(DataModelError):
"""Raised if a dimension is not referred to in any property."""

Expand All @@ -38,7 +40,7 @@ def __init__(self, uri, schema=None, description=None):
schema = property(
lambda self: self._schema,
lambda self, schema: self._set_schema(schema),
doc='Meta-metadata for the datamodel.',
doc="Meta-metadata for the datamodel.",
)

def _set_schema(self, schema):
Expand All @@ -49,17 +51,17 @@ def _set_schema(self, schema):
elif isinstance(schema, str):
self._schema = dlite.get_instance(schema)
else:
TypeError('`schema` must be a string or a DLite metadata schema.')
TypeError("`schema` must be a string or a DLite metadata schema.")

def add_dimension(self, name, description):
"""Add dimension with given `name` and description to data model."""
if name in self.dimensions:
raise KeyError(f'A dimension named "{name}" already exists')
self.dimensions[name] = dlite.Dimension(name, description)


def add_property(self, name, type, shape=None, unit=None, description=None,
dims=None):
def add_property(
self, name, type, shape=None, unit=None, description=None, dims=None
):
"""Add property to data model.
Parameters:
Expand Down Expand Up @@ -91,14 +93,18 @@ def add_property(self, name, type, shape=None, unit=None, description=None,
)

def _get_dims_variables(self):
"""Returns a set of all dimension names referred to in property dims."""
"""Returns a set of all dimension names referred to in property shapes.
"""
names = set()
for prop in self.properties.values():
if prop.shape is not None:
for dim in prop.shape:
tree = ast.parse(dim)
names.update(node.id for node in ast.walk(tree)
if isinstance(node, ast.Name))
names.update(
node.id
for node in ast.walk(tree)
if isinstance(node, ast.Name)
)
return names

def get_missing_dimensions(self):
Expand All @@ -108,23 +114,24 @@ def get_missing_dimensions(self):
return self._get_dims_variables().difference(self.dimensions)

def get_unused_dimensions(self):
"""Returns a set of dimensions not referred to in any property shapes."""
"""Returns a set of dimensions not referred to in any property shapes.
"""
return set(self.dimensions).difference(self._get_dims_variables())

def validate(self):
"""Raises an exception if there are missing or unused dimensions."""
missing = self.get_missing_dimensions()
if missing:
raise MissingDimensionError(f'Missing dimensions: {missing}')
raise MissingDimensionError(f"Missing dimensions: {missing}")
unused = self.get_unused_dimensions()
if unused:
raise UnusedDimensionError(f'Unused dimensions: {unused}')
raise UnusedDimensionError(f"Unused dimensions: {unused}")

def get(self):
"""Returns a DLite Metadata created from the datamodel."""
self.validate()
dims = [len(self.dimensions), len(self.properties)]
if 'nrelations' in self.schema:
if "nrelations" in self.schema:
dims.append(len(self.relations))

# Hmm, there seems to be a bug when instantiating from schema.
Expand All @@ -134,15 +141,16 @@ def get(self):
# For now, lets assume that it is EntitySchema.
if self.schema.uri != dlite.ENTITY_SCHEMA:
raise NotImplementedError(
f'Currently only entity schema is supported')
f"Currently only entity schema is supported"
)

#meta = self.schema(dims, id=self.uri)
#meta.description = self.description
#meta['dimensions'] = list(self.dimensions.values())
#meta['properties'] = list(self.properties.values())
#if 'relations' in meta:
# meta = self.schema(dims, id=self.uri)
# meta.description = self.description
# meta['dimensions'] = list(self.dimensions.values())
# meta['properties'] = list(self.properties.values())
# if 'relations' in meta:
# meta['relations'] = self.relations
#return meta
# return meta

return dlite.Instance.create_metadata(
uri=self.uri,
Expand Down
34 changes: 31 additions & 3 deletions bindings/python/dlite-storage-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Override default __init__()
def __init__(self, driver_or_url, location=None, options=None):
loc = str(location) if location else None
_dlite.Instance_swiginit(self, _dlite.new_Storage(
_dlite.Storage_swiginit(self, _dlite.new_Storage(
driver_or_url=driver_or_url, location=loc, options=options))

def __enter__(self):
Expand Down Expand Up @@ -41,10 +41,25 @@
)

@classmethod
def create_from_url(cls, url):
def from_url(cls, url):
"""Create a new storage from `url`."""
return cls(url)

@classmethod
def load_plugins(cls):
"""Load all storage plugins."""
_dlite._load_all_storage_plugins()

@classmethod
def unload_plugin(cls, name):
"""Unload storage plugin with this name."""
_dlite._unload_storage_plugin(str(name))

@classmethod
def plugin_help(cls, name):
"""Return documentation of storage plogin with this name."""
return _dlite._storage_plugin_help(name)

def instances(self, pattern=None):
"""Returns an iterator over all instances in storage whos
metadata URI matches `pattern`."""
Expand All @@ -64,6 +79,7 @@

driver = property(get_driver,
doc='Name of driver associated with this storage')

%}
}

Expand All @@ -77,12 +93,24 @@
%}
}


%extend StoragePluginIter {
%pythoncode %{
# Override default __init__()
def __init__(self):
"""Iterates over loaded storage plugins."""
_dlite.StoragePluginIter_swiginit(
self, _dlite.new_StoragePluginIter())
# Keep a reference to self, such that it is not garbage-collected
# before end of iterations
if not hasattr(_dlite, "_storage_plugin_iters"):
_dlite._storage_plugin_iters = {}
_dlite._storage_plugin_iters[id(self.iter)] = self

def __next__(self):
name = self.next()
if not name:
# Delete reference to iterator object stored away in __init__()
del _dlite._storage_plugin_iters[id(self.this)]
raise StopIteration()
return name
%}
Expand Down
22 changes: 18 additions & 4 deletions bindings/python/dlite-storage.i
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

%{
#include "dlite.h"
#include "dlite-errors.h"
#include "dlite-storage.h"
#include "dlite-storage-plugins.h"

char *_storage_plugin_help(const char *name) {
const DLiteStoragePlugin *api = dlite_storage_plugin_get(name);
if (api->help) return api->help(api);
return dlite_err(dliteUnsupportedError,
"\"%s\" storage does not support help", name), NULL;
}

%}


Expand Down Expand Up @@ -68,8 +77,8 @@ enum _DLiteIDFlag {
%feature("docstring", "\
Represents a data storage.

Parameters
----------
Arguments
---------
driver_or_url : string
Name of driver used to connect to the storage or, if `location` is not
given, the URL to the storage:
Expand Down Expand Up @@ -195,9 +204,9 @@ Iterates over loaded storage plugins.
DLiteStoragePluginIter *iter;
};
%}
%feature("docstring", "") new_StoragePluginIter;
%extend StoragePluginIter {
StoragePluginIter(void) {
//dlite_storage_plugin_load_all();
DLiteStoragePluginIter *iter = dlite_storage_plugin_iter_create();
return (struct StoragePluginIter *)iter;
}
Expand All @@ -218,9 +227,14 @@ Iterates over loaded storage plugins.
}


%rename(storage_unload) dlite_storage_plugin_unload;
%rename(_load_all_storage_plugins) dlite_storage_plugin_load_all;
int dlite_storage_plugin_load_all();

%rename(_unload_storage_plugin) dlite_storage_plugin_unload;
int dlite_storage_plugin_unload(const char *name);

char *_storage_plugin_help(const char *name);


/* -----------------------------------
* Target language-spesific extensions
Expand Down
Loading

0 comments on commit df40a40

Please sign in to comment.