def build(T, serializable, path = ""):
        result = []
        properties = ElementsHelper.sortedProperties(T)
        for _id in properties:
            currentPath = str(_id) if path == None or path == '' else ''.join([path, '.', str(_id)])

            propertyInfo = properties[_id]
            if isinstance(propertyInfo['Type'],list):
                propertyType = propertyInfo['Type'][0]
            else:
                propertyType = propertyInfo['Type']

            propertyValue = ParametersConverter.getValue(propertyInfo, serializable)

            if propertyValue is None:
                continue

            if propertyType == 'long' or propertyType == 'long?' or propertyType == TextAlignment or propertyType == Color or propertyType == 'bool':
                value = propertyValue
                if propertyType == 'bool' or type(propertyValue) == bool:
                    value = 1 if propertyValue else 0
                elif propertyType == TextAlignment:
                    value = TextAlignment.fromJSON(propertyValue)
                elif propertyType == Color:
                    value = Color.fromJSON(propertyValue)
                elif propertyType == 'long' or propertyType == 'long?':
                    value = int(value)

                logging.debug(f"{currentPath} '{propertyInfo['Name']}': {value}")
                result.append(Parameter(_id, value))
            elif propertyType == ParameterFlags:
                flags = ParameterFlags.fromJSON(propertyValue)
                logging.debug(f"{currentPath} '{propertyInfo['Name']}': {flags}")
                result.append(Parameter(_id, None, flags = flags))

            else:
                if isinstance(propertyValue,list):
                    for i in propertyValue:
                        innerParameters = ParametersConverter.build(propertyType, i, currentPath)

                        if len(innerParameters) > 0:
                            logging.debug(f"{currentPath} '{propertyInfo['Name']}'")
                            result.append(Parameter(_id, innerParameters))
                        else:
                            logging.debug(f"{currentPath} '{propertyInfo['Name']}': Skipped because of empty1")
                    continue
                innerParameters = ParametersConverter.build(propertyType, propertyValue, currentPath)
                if len(innerParameters) > 0:
                    logging.debug(f"{currentPath} '{propertyInfo['Name']}'")
                    result.append(Parameter(_id, innerParameters))
                else:
                    logging.debug(f"{currentPath} '{propertyInfo['Name']}': Skipped because of empty2")

        return result
Пример #2
0
    def parse(paramType, descriptor, path=""):
        assert (type(descriptor) == type([]))
        assert (type(path) == type(""))
        properties = ElementsHelper.sortedProperties(paramType)
        result = paramType()
        currentType = paramType

        for parameter in descriptor:
            parameterId = parameter.getId()
            currentPath = str(parameterId) if not path else os.path.join(
                path, '.', str(parameterId))
            if parameterId not in properties:
                logging.warn(
                    f"[ParamConv:parse] currentPath {currentPath} / Parameter {parameterId} isn't supported for {currentType}"
                )
                raise IndexError(
                    f"Parameter {parameterId} isn't supported for {currentType}"
                )

            propertyInfo = properties[parameterId]
            propertyType = propertyInfo['Type']

            propertyInfoName = propertyInfo['Name']

            if propertyType == 'long' or propertyType == 'long?' or propertyType == TextAlignment or propertyType == Color or propertyType == 'bool':
                if propertyType == TextAlignment:
                    setattr(result, propertyInfoName,
                            TextAlignment(parameter.getValue()))
                elif propertyType == Color:
                    setattr(result, propertyInfoName,
                            Color(parameter.getValue()))
                elif propertyType == 'bool':
                    setattr(result, propertyInfoName, parameter.getValue() > 0)
                elif propertyType == 'long':
                    setattr(result, propertyInfoName,
                            ulong2long(parameter.getValue()))
                else:
                    setattr(result, propertyInfoName,
                            ulong2long(parameter.getValue() or None))

            elif propertyType == '[]':
                assert (False)  # not tested yet
            else:
                tmp = propertyType()
                for x in parameter.getChildren():
                    psd = ParametersConverter.parse(propertyType, [x],
                                                    currentPath)
                    import json

                    for kk in psd.__dict__:
                        vv = psd.__dict__[kk]
                        setattr(tmp, kk, vv)
                setattr(result, propertyInfoName, tmp)
        return result
    def drawImages(drawer, images, spacing, alignment, box):
        assert (type(images) == list)
        assert (type(spacing) == int)
        assert (type(alignment) == int)

        spacing = uint2int(spacing)

        (bitmapWidth,
         bitmapHeight) = DrawerHelper.calculateBounds(images, spacing)

        from watchFaceParser.models.textAlignment import TextAlignment
        alignmentFlag = TextAlignment(alignment)

        x = 0
        y = 0
        if alignmentFlag.hasFlag(TextAlignment.Left):
            x = box.getX()
        elif alignmentFlag.hasFlag(TextAlignment.Right):
            x = box.getRight() - bitmapWidth + 1
        else:
            x = (box.getLeft() + box.getRight() - bitmapWidth) >> 1

        if alignmentFlag.hasFlag(TextAlignment.Top):
            y = box.getTop()
        elif alignmentFlag.hasFlag(TextAlignment.Bottom):
            y = box.getBottom() - bitmapHeight + 1
        else:
            y = (box.getTop() + box.getBottom() - bitmapHeight) >> 1

        if x < box.getLeft():
            x = box.getLeft()
        if y < box.getTop():
            y = box.getTop()

        for image in images:
            temp = image.getBitmap()
            drawer.paste(temp, (x, y), temp)

            imageWidth = image.getBitmap().size[0]
            x += imageWidth + int(spacing)