target.py 2.52 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.
        """
25 26
        if(self._tid is None):
            return str(None)
Matthieu Dorier's avatar
Matthieu Dorier committed
27 28
        byte_string = _pybaketarget.target_id_to_string(self._tid)
        return byte_string.decode()
Matthieu Dorier's avatar
Matthieu Dorier committed
29 30

    @staticmethod
Matthieu Dorier's avatar
Matthieu Dorier committed
31
    def from_str(byte_string):
32 33 34 35
        """
        Converts a string representation of the BakeTargetID into
        a concrete BakeTargetID object.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
36 37 38
        if(isinstance(byte_string,str)):
            byte_string = byte_string.encode()
        tid = _pybaketarget.target_id_from_string(byte_string)
Matthieu Dorier's avatar
Matthieu Dorier committed
39 40 41 42 43
        if(tid is None):
            return None
        else:
            return BakeTargetID(tid)

44 45 46 47
# ================================================================== #
# ================================================================== #
# ================================================================== #

Matthieu Dorier's avatar
Matthieu Dorier committed
48
class BakeRegionID():
49 50 51 52 53 54
    """
    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
55 56

    def __init__(self, rid):
57 58 59
        """
        Constructor. Not supposed to be called by users.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
60 61 62
        self._rid = rid
    
    def __str__(self):
63 64 65
        """
        Converts the BakeRegionID into a string.
        """
66 67 68
        if(self._rid is None):
            return str(None)
        a = _pybaketarget.region_id_to_string(self._rid)
Matthieu Dorier's avatar
Matthieu Dorier committed
69
        return base64.b64encode(a).decode()
Matthieu Dorier's avatar
Matthieu Dorier committed
70 71

    @staticmethod
Matthieu Dorier's avatar
Matthieu Dorier committed
72
    def from_str(byte_string):
73 74 75 76
        """
        Converts a string representing a BakeRegionID into a concrete
        BakeRegionID object.
        """
Matthieu Dorier's avatar
Matthieu Dorier committed
77 78 79
        if(isinstance(byte_string,str)):
            byte_string = byte_string.encode()
        rid = _pybaketarget.region_id_from_string(base64.b64decode(byte_string))
Matthieu Dorier's avatar
Matthieu Dorier committed
80 81 82 83
        if(rid is None):
            return None
        else:
            return BakeRegionID(rid)