target.py 2.09 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2
# (C) 2018 The University of Chicago
# See COPYRIGHT in top-level directory.
Matthieu Dorier's avatar
Matthieu Dorier committed
3 4
import _pybaketarget
import base64
Matthieu Dorier's avatar
Matthieu Dorier committed
5

Matthieu Dorier's avatar
Matthieu Dorier committed
6
class BakeTargetID():
7 8 9 10 11 12 13
    """
    The BakeTargetID class is a wrapper for a bake_target_id_t
    object at C level. It can be used by client and server
    interfaces. It can be converted into a string using the
    __str__ method, and be deserialized from a string using
    the BakeTargetID.from_str static method.
    """
Matthieu Dorier's avatar
Matthieu Dorier committed
14

Matthieu Dorier's avatar
Matthieu Dorier committed
15
    def __init__(self, tid):
16 17 18
        """
        Constructor. Not supposed to be called by users.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
19 20 21
        self._tid = tid

    def __str__(self):
22 23 24
        """
        Converts the BakeTargetID into a string.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
25 26 27 28
        return str(self._tid)

    @staticmethod
    def from_str(string):
29 30 31 32 33
        """
        Converts a string representation of the BakeTargetID into
        a concrete BakeTargetID object.
        """
        tid = _pybaketarget.target_id_from_string(str(string))
Matthieu Dorier's avatar
Matthieu Dorier committed
34 35 36 37 38
        if(tid is None):
            return None
        else:
            return BakeTargetID(tid)

39 40 41 42
# ================================================================== #
# ================================================================== #
# ================================================================== #

Matthieu Dorier's avatar
Matthieu Dorier committed
43
class BakeRegionID():
44 45 46 47 48 49
    """
    The BakeRegionID is a wrapper for a bake_region_id_t object at
    C level. It provides functionalities to convert a BakeRegionID
    from/to a string representation, and can be used by the server
    and client API.
    """
Matthieu Dorier's avatar
Matthieu Dorier committed
50 51

    def __init__(self, rid):
52 53 54
        """
        Constructor. Not supposed to be called by users.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
55 56 57
        self._rid = rid
    
    def __str__(self):
58 59 60
        """
        Converts the BakeRegionID into a string.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
61 62 63 64
        return base64.b64encode(str(self._rid))

    @staticmethod
    def from_str(string):
65 66 67 68 69
        """
        Converts a string representing a BakeRegionID into a concrete
        BakeRegionID object.
        """
        rid = _pybaketarget.region_id_from_string(base64.b64decode(str(string)))
Matthieu Dorier's avatar
Matthieu Dorier committed
70 71 72 73
        if(rid is None):
            return None
        else:
            return BakeRegionID(rid)