hrm_omero.misc

Miscellaneous functions used across the package.

  1"""Miscellaneous functions used across the package."""
  2
  3import os
  4
  5from loguru import logger as log
  6
  7
  8class OmeroId:
  9
 10    """Representation of a (group-qualified) OMERO object ID.
 11
 12    The purpose of this class is to facilitate parsing and access of the
 13    ubiquitious target IDs denoting objects in OMERO. The constructor takes
 14    the common string of the form `G:[gid]:[type]:[iid]` as an input and sets
 15    the properties `group`, `obj_type` and `obj_id` accordingly after validating
 16    their contents for having reasonable values.
 17
 18    Attributes
 19    ----------
 20    group : str
 21        The OMERO group ID as an int-like `str`.
 22    obj_type : str
 23        The OMERO object type, e.g. `Experimenter`, `Image`, ...
 24    obj_id : str
 25        The OMERO object ID as an int-like `str`.
 26    """
 27
 28    def __init__(self, id_str):
 29        self.group = None
 30        self.obj_type = None
 31        self.obj_id = None
 32        self.parse_id_str(id_str)
 33
 34    def parse_id_str(self, id_str):
 35        """Parse and validate an ID string of the form `G:[gid]:[type]:[oid]`
 36
 37        The method will parse the given string and set the object's `group`, `obj_type`
 38        and `obj_id` values accordingly. In case for `id_str` the special value `ROOT`
 39        was supplied, `group` and `obj_id` will be set to `-1` whereas `obj_type` will
 40        be set to `BaseTree`.
 41
 42        Parameters
 43        ----------
 44        id_str : str
 45            The ID of an OMERO object, e.g.
 46            * `G:23:Image:42`
 47            * `G:4:Dataset:765487`
 48            * special case `ROOT`, same as `G:-1:BaseTree:-1`
 49
 50        Raises
 51        ------
 52        ValueError
 53            Raised in case a malformed `id_str` was given.
 54        """
 55        log.trace(f"Parsing ID string: [{id_str}]")
 56        if id_str == "ROOT":
 57            self.group = -1
 58            self.obj_type = "BaseTree"
 59            self.obj_id = -1
 60            log.debug(f"Converted special ID 'ROOT' to [{str(self)}].")
 61            return
 62
 63        try:
 64            group_type, group_id, obj_type, obj_id = id_str.split(":")
 65            int(group_id)  # raises a TypeError if cast to int fails
 66            int(obj_id)  # raises a TypeError if cast to int fails
 67            if group_type != "G":
 68                raise ValueError(f"Invalid group qualifier '{group_type}'.")
 69            if obj_type not in [
 70                "Image",
 71                "Dataset",
 72                "Project",
 73                "Experimenter",
 74                "ExperimenterGroup",
 75            ]:
 76                raise ValueError(f"Invalid object type '{obj_type}'.")
 77            if int(obj_id) < 1:
 78                raise ValueError(f"Invalid object ID '{obj_id}'.")
 79        except (ValueError, TypeError) as err:
 80            # pylint: disable-msg=raise-missing-from
 81            msg = f"Malformed id_str '{id_str}', expecting `G:[gid]:[type]:[oid]`."
 82            raise ValueError(msg, err)
 83
 84        log.debug(f"Validated ID string: group={group_id}, {obj_type}={obj_id}")
 85        self.group = group_id
 86        self.obj_type = obj_type
 87        self.obj_id = obj_id
 88
 89    def __str__(self):
 90        return f"G:{self.group}:{self.obj_type}:{self.obj_id}"
 91
 92
 93def printlog(level, message):
 94    """Simple wrapper to push a message to stdout and logging.
 95
 96    Note that something very similiar (or identical) could be achieved by adding a log
 97    handler that emits to stdout.
 98
 99    Parameters
100    ----------
101    level : str
102        The log level of the message as defined by loguru.
103    message : str
104        The message to be printed and logged.
105    """
106    print(message)
107    log.log(level, message)
108
109
110def changemodes(basepath, elements, dmode=0o775, fmode=0o664):
111    """Recursive `chmod` function in the spirit of `os.makedirs()`.
112
113    Parameters
114    ----------
115    basepath : str
116        The base path where to look for the given elements.
117    elements : list(str)
118        A list of file and directory names relative to `basepath`.
119    dmode : int, optional
120        The mode to use for directories, by default `0o775`
121    fmode : int, optional
122        The mode to use for files, by default `0o664`
123    """
124    for item in elements:
125        path = os.path.join(basepath, item)
126
127        if os.path.isdir(path):
128            log.trace(f"Adjusting permissions on [{path}] to [{dmode:o}]...")
129            os.chmod(path, mode=dmode)
130        else:
131            log.trace(f"Adjusting permissions on [{path}] to [{fmode:o}]...")
132            os.chmod(path, mode=fmode)
133
134        for dirpath, dirnames, filenames in os.walk(path):
135            for dname in dirnames:
136                log.trace(f"Adjusting permissions on [{dname}] to [{dmode:o}]...")
137                os.chmod(os.path.join(dirpath, dname), mode=dmode)
138            for fname in filenames:
139                log.trace(f"Adjusting permissions on [{fname}] to [{fmode:o}]...")
140                os.chmod(os.path.join(dirpath, fname), mode=fmode)
class OmeroId:
 9class OmeroId:
10
11    """Representation of a (group-qualified) OMERO object ID.
12
13    The purpose of this class is to facilitate parsing and access of the
14    ubiquitious target IDs denoting objects in OMERO. The constructor takes
15    the common string of the form `G:[gid]:[type]:[iid]` as an input and sets
16    the properties `group`, `obj_type` and `obj_id` accordingly after validating
17    their contents for having reasonable values.
18
19    Attributes
20    ----------
21    group : str
22        The OMERO group ID as an int-like `str`.
23    obj_type : str
24        The OMERO object type, e.g. `Experimenter`, `Image`, ...
25    obj_id : str
26        The OMERO object ID as an int-like `str`.
27    """
28
29    def __init__(self, id_str):
30        self.group = None
31        self.obj_type = None
32        self.obj_id = None
33        self.parse_id_str(id_str)
34
35    def parse_id_str(self, id_str):
36        """Parse and validate an ID string of the form `G:[gid]:[type]:[oid]`
37
38        The method will parse the given string and set the object's `group`, `obj_type`
39        and `obj_id` values accordingly. In case for `id_str` the special value `ROOT`
40        was supplied, `group` and `obj_id` will be set to `-1` whereas `obj_type` will
41        be set to `BaseTree`.
42
43        Parameters
44        ----------
45        id_str : str
46            The ID of an OMERO object, e.g.
47            * `G:23:Image:42`
48            * `G:4:Dataset:765487`
49            * special case `ROOT`, same as `G:-1:BaseTree:-1`
50
51        Raises
52        ------
53        ValueError
54            Raised in case a malformed `id_str` was given.
55        """
56        log.trace(f"Parsing ID string: [{id_str}]")
57        if id_str == "ROOT":
58            self.group = -1
59            self.obj_type = "BaseTree"
60            self.obj_id = -1
61            log.debug(f"Converted special ID 'ROOT' to [{str(self)}].")
62            return
63
64        try:
65            group_type, group_id, obj_type, obj_id = id_str.split(":")
66            int(group_id)  # raises a TypeError if cast to int fails
67            int(obj_id)  # raises a TypeError if cast to int fails
68            if group_type != "G":
69                raise ValueError(f"Invalid group qualifier '{group_type}'.")
70            if obj_type not in [
71                "Image",
72                "Dataset",
73                "Project",
74                "Experimenter",
75                "ExperimenterGroup",
76            ]:
77                raise ValueError(f"Invalid object type '{obj_type}'.")
78            if int(obj_id) < 1:
79                raise ValueError(f"Invalid object ID '{obj_id}'.")
80        except (ValueError, TypeError) as err:
81            # pylint: disable-msg=raise-missing-from
82            msg = f"Malformed id_str '{id_str}', expecting `G:[gid]:[type]:[oid]`."
83            raise ValueError(msg, err)
84
85        log.debug(f"Validated ID string: group={group_id}, {obj_type}={obj_id}")
86        self.group = group_id
87        self.obj_type = obj_type
88        self.obj_id = obj_id
89
90    def __str__(self):
91        return f"G:{self.group}:{self.obj_type}:{self.obj_id}"

Representation of a (group-qualified) OMERO object ID.

The purpose of this class is to facilitate parsing and access of the ubiquitious target IDs denoting objects in OMERO. The constructor takes the common string of the form G:[gid]:[type]:[iid] as an input and sets the properties group, obj_type and obj_id accordingly after validating their contents for having reasonable values.

Attributes
  • group (str): The OMERO group ID as an int-like str.
  • obj_type (str): The OMERO object type, e.g. Experimenter, Image, ...
  • obj_id (str): The OMERO object ID as an int-like str.
OmeroId(id_str)
29    def __init__(self, id_str):
30        self.group = None
31        self.obj_type = None
32        self.obj_id = None
33        self.parse_id_str(id_str)
def parse_id_str(self, id_str):
35    def parse_id_str(self, id_str):
36        """Parse and validate an ID string of the form `G:[gid]:[type]:[oid]`
37
38        The method will parse the given string and set the object's `group`, `obj_type`
39        and `obj_id` values accordingly. In case for `id_str` the special value `ROOT`
40        was supplied, `group` and `obj_id` will be set to `-1` whereas `obj_type` will
41        be set to `BaseTree`.
42
43        Parameters
44        ----------
45        id_str : str
46            The ID of an OMERO object, e.g.
47            * `G:23:Image:42`
48            * `G:4:Dataset:765487`
49            * special case `ROOT`, same as `G:-1:BaseTree:-1`
50
51        Raises
52        ------
53        ValueError
54            Raised in case a malformed `id_str` was given.
55        """
56        log.trace(f"Parsing ID string: [{id_str}]")
57        if id_str == "ROOT":
58            self.group = -1
59            self.obj_type = "BaseTree"
60            self.obj_id = -1
61            log.debug(f"Converted special ID 'ROOT' to [{str(self)}].")
62            return
63
64        try:
65            group_type, group_id, obj_type, obj_id = id_str.split(":")
66            int(group_id)  # raises a TypeError if cast to int fails
67            int(obj_id)  # raises a TypeError if cast to int fails
68            if group_type != "G":
69                raise ValueError(f"Invalid group qualifier '{group_type}'.")
70            if obj_type not in [
71                "Image",
72                "Dataset",
73                "Project",
74                "Experimenter",
75                "ExperimenterGroup",
76            ]:
77                raise ValueError(f"Invalid object type '{obj_type}'.")
78            if int(obj_id) < 1:
79                raise ValueError(f"Invalid object ID '{obj_id}'.")
80        except (ValueError, TypeError) as err:
81            # pylint: disable-msg=raise-missing-from
82            msg = f"Malformed id_str '{id_str}', expecting `G:[gid]:[type]:[oid]`."
83            raise ValueError(msg, err)
84
85        log.debug(f"Validated ID string: group={group_id}, {obj_type}={obj_id}")
86        self.group = group_id
87        self.obj_type = obj_type
88        self.obj_id = obj_id

Parse and validate an ID string of the form G:[gid]:[type]:[oid]

The method will parse the given string and set the object's group, obj_type and obj_id values accordingly. In case for id_str the special value ROOT was supplied, group and obj_id will be set to -1 whereas obj_type will be set to BaseTree.

Parameters
  • id_str (str): The ID of an OMERO object, e.g.
    • G:23:Image:42
    • G:4:Dataset:765487
    • special case ROOT, same as G:-1:BaseTree:-1
Raises
  • ValueError: Raised in case a malformed id_str was given.
def printlog(level, message):
 94def printlog(level, message):
 95    """Simple wrapper to push a message to stdout and logging.
 96
 97    Note that something very similiar (or identical) could be achieved by adding a log
 98    handler that emits to stdout.
 99
100    Parameters
101    ----------
102    level : str
103        The log level of the message as defined by loguru.
104    message : str
105        The message to be printed and logged.
106    """
107    print(message)
108    log.log(level, message)

Simple wrapper to push a message to stdout and logging.

Note that something very similiar (or identical) could be achieved by adding a log handler that emits to stdout.

Parameters
  • level (str): The log level of the message as defined by loguru.
  • message (str): The message to be printed and logged.
def changemodes(basepath, elements, dmode=509, fmode=436):
111def changemodes(basepath, elements, dmode=0o775, fmode=0o664):
112    """Recursive `chmod` function in the spirit of `os.makedirs()`.
113
114    Parameters
115    ----------
116    basepath : str
117        The base path where to look for the given elements.
118    elements : list(str)
119        A list of file and directory names relative to `basepath`.
120    dmode : int, optional
121        The mode to use for directories, by default `0o775`
122    fmode : int, optional
123        The mode to use for files, by default `0o664`
124    """
125    for item in elements:
126        path = os.path.join(basepath, item)
127
128        if os.path.isdir(path):
129            log.trace(f"Adjusting permissions on [{path}] to [{dmode:o}]...")
130            os.chmod(path, mode=dmode)
131        else:
132            log.trace(f"Adjusting permissions on [{path}] to [{fmode:o}]...")
133            os.chmod(path, mode=fmode)
134
135        for dirpath, dirnames, filenames in os.walk(path):
136            for dname in dirnames:
137                log.trace(f"Adjusting permissions on [{dname}] to [{dmode:o}]...")
138                os.chmod(os.path.join(dirpath, dname), mode=dmode)
139            for fname in filenames:
140                log.trace(f"Adjusting permissions on [{fname}] to [{fmode:o}]...")
141                os.chmod(os.path.join(dirpath, fname), mode=fmode)

Recursive chmod function in the spirit of os.makedirs().

Parameters
  • basepath (str): The base path where to look for the given elements.
  • elements (list(str)): A list of file and directory names relative to basepath.
  • dmode (int, optional): The mode to use for directories, by default 0o775
  • fmode (int, optional): The mode to use for files, by default 0o664