Example #1
0
    def set_entity(self, ent: str, val: str) -> None:
        """
        Sets entity value. Old (unmodified) value is backuped

        Parameters
        ----------
        ent: str
            name of entity to set
        val: str
            value of entity
        """
        val = check_type("val", str, val)
        attr = check_type("ent", str, ent)

        if attr in self.entity:
            if self.entity[attr] == val:
                return

            if attr not in self._bk_entity:
                self._bk_entity[attr] = self.entity[attr]

            if val:
                self.entity[attr] = val
            else:
                self.entity.remove(attr)
            return

        else:
            if val == "":
                return
            self.entity[attr] = val
            if attr not in self._bk_entity:
                self._bk_entity[attr] = None
Example #2
0
    def set_json_field(self, field: str, val: object) -> None:
        """
        Sets the json field value. The old (unmodified) value is backuped

        Parameters:
        -----------
        field: str
            name of field to set
        val: object
            value to set
        """
        if isinstance(val, str):
            val = val.encode('unicode_escape').decode()
        attr = check_type("field", str, field)

        if attr in self.json:
            if self.json[attr] == val:
                return

            if attr not in self._bk_json:
                self._bk_json[attr] = self.json[attr]

            if val:
                self.json[attr] = val
            else:
                self.json.remove(attr)
            return

        else:
            if val == "":
                return
            self.json[attr] = val
            if attr not in self._bk_json:
                self._bk_json[attr] = None
Example #3
0
    def set_attribute(self, attr: str, val: object) -> None:
        """
        Sets attribute value. Old (unmodified) value is backuped

        Parameters
        ----------
        attr: str
            name of attribute to set
        value: object
            value to match given attribute
        """
        if isinstance(val, str):
            val = val.encode('unicode_escape').decode()
        attr = check_type("attr", str, attr)
        if attr in self.attribute:
            if self.attribute[attr] == val:
                return

            if attr not in self._bk_attribute:
                self._bk_attribute[attr] = self.attribute[attr]

                self.attribute[attr] = val
            return

        else:
            if val == "":
                return
            self.attribute[attr] = val
            if attr not in self._bk_attribute:
                self._bk_attribute[attr] = None
Example #4
0
def ImportPlugins(plugin_file):
    """
    Import aviable plugins from given file

    Parameters
    ----------
    plugin_file : str
        path to the plugin file

    Returns
    -------
    int
        number of imported plugin functions

    Raises
    ------
    exceptions.PluginNotfound :
        if plugin file not found
    exceptions.PluginModuleNotFound :
        if inable to load plugin module
    """
    plugin_file = check_type("plugin_file", str, plugin_file)
    if plugin_file == "":
        return 0

    global file

    file = str(plugin_file)

    global active_plugins
    active_plugins = dict()

    if not os.path.isfile(file):
        raise exceptions.PluginNotFoundError(
            "Plug-in file {} not found".format(file))

    pl_name = os.path.splitext(os.path.basename(file))[0]
    logger.info("Loading module {} from {}".format(pl_name, file))
    spec = importlib.util.spec_from_file_location(pl_name, file)
    if spec is None:
        raise exceptions.PluginModuleNotFoundError(
            "Unable to load module {} from {}".format(pl_name, file))
    itertools = importlib.util.module_from_spec(spec)
    # Adding plugin directory to path
    # Need better solution
    sys.path.append(os.path.dirname(file))
    #
    spec.loader.exec_module(itertools)
    f_list = dir(itertools)
    for ep in entry_points:
        if ep in f_list and callable(getattr(itertools, ep)):
            logger.debug("Entry point {} found".format(ep))
            active_plugins[ep] = getattr(itertools, ep)
    if len(active_plugins) == 0:
        logger.warning("Plugin {} loaded but "
                       "no compatible functions found".format(pl_name))
    return len(active_plugins)
Example #5
0
    def __init__(self, *,
                 modality: str = "",
                 attribute: dict = {},
                 entity: OrderedDict = {},
                 json: OrderedDict = {},
                 suffix: str = "",
                 provenance: str = None,
                 example: str = None,
                 ):
        """
        Run class contains all information needed to identify and generate
        the bidsified name. All init parameters must be named.

        Parameters
        ----------
        modality: str
            the modality corresponding to given run
        attribute: dict
            dict of attributes (keys) and values (value) to match
        entity: OrderedDict
            dict of bids fields (keys) and values (value) to
            generate bidsified name
        json: OrderedDict
            dict of json fields (keys) and values (value) to
            fill sidecar json file
        suffix: str
            suffix associated with given run
        provenance: str
            path to the file from which this run was generated
        example: str
            example of bidsified name generated from provenance
            file
        """
        self._bk_modality = None
        self._bk_model = None
        self._bk_attribute = dict()
        self._bk_entity = dict()
        self._bk_suffix = None
        self._bk_json = dict()
        self.writable = True
        self.template = False
        self.checked = False
        self.example = example

        self.provenance = provenance
        self._modality = check_type("modality", str, modality)
        self._model = self._modality
        self._suffix = check_type("suffix", str, suffix)
        self.attribute = dict(check_type("attribute", dict, attribute))
        self.entity = OrderedDict(check_type("entity", dict, entity))
        # Checking if values of entity are strings
        for key in self.entity:
            if self.entity[key] is None:
                continue
            if not isinstance(self.entity[key], str):
                logger.warning("Modality {} ({}): bids entity {} value "
                               "is not string. "
                               "May lead to unexpected results."
                               .format(self._modality, self.provenance,
                                       key))
                self.entity[key] = str(self.entity[key])
        self.json = OrderedDict(check_type("json", dict, json))