def parse(fileName):
    parserData = ParserData()

    # Open the file and get data
    f = open(fileName)
    data = ""
    for line in f:
        data += line

    splittedData = data.strip().split("---")

    if len(splittedData) < 2:
        print """Error: the file is malformatted.
The main groups are missing."""
        return parserData

    # Parse the first group (class name and includes)
    firstGroup = splittedData[0]
    del splittedData[0]

    splittedFirstGroup = firstGroup.strip().split("\n")
    if len(splittedFirstGroup) == 0:
        print """Error: the file is malformatted
The first group should have at least the name of the class"""
        return parserData

    # The class name is the first line
    parserData.className = splittedFirstGroup[0]
    del splittedFirstGroup[0]

    # The other lines are headers
    for splittedFirstGroupItem in splittedFirstGroup:
        strippedSplittedItem = splittedFirstGroupItem.strip()
        if strippedSplittedItem != "":
            parserData.includes.append(strippedSplittedItem)

    # Parse the variables
    for groupData in splittedData:
        group = _createGroup(groupData)
        if group["name"] != "" and group["type"] != "":
            parserData.variables.append(group)

    # Find the parent class
    parserData.baseClass = "ObjectBase"
    haveId = False
    haveName = False
    for variable in parserData.variables:
        if variable["name"] == "id" and variable["type"] == "QString":
            haveId = True
        if variable["name"] == "name" and variable["type"] == "QString":
            haveName = True

    if haveId and haveName:
        parserData.baseClass = "NamedObject"
    elif haveId:
        parserData.baseClass = "Object"

    # Find if there are sub-objects
    for variable in parserData.variables:
        if qfbtools.isPointer(variable["type"]):
            parserData.haveSubObjects = True
    return parserData
Ejemplo n.º 2
0
def createSource(className, includes, baseClass, variables,
                 implementationData):
    source = copyright
    source += """
/**
 * @file """ + className.lower() + """.cpp
 * @brief Implementation of QFB::""" + className + """
 */

#include \"""" + className.lower() + """.h\"
#include \"private/helper_p.h\"
#include \"private/objectbase_p.h\"
#include \"private/object_creator_p.h\"

namespace QFB
{

"""

    for variable in variables:
        if variable["type"] != "TODO" and variable[
                "type"] != "NO" and variable["type"] != "":
            if variable["name"] != "id" and variable["name"] != "name":
                splittedName = qfbtools.split(variable["name"])
                source += """/**
 * @internal
 * @brief """ + qfbtools.staticKey(splittedName, className) + """
 */
static const char *""" + qfbtools.staticKey(splittedName, className) + " = "
                source += "\"" + variable["name"] + "\";\n"

    source += """

/**
 * @internal
 * @short Private class for QFB::""" + className + """
 */
class """ + className + """Private: public ObjectBasePrivate
{
public:
    /**
     * @internal
     * @short Default constructor
     */
    explicit """ + className + """Private();
"""
    for variable in variables:
        if qfbtools.isPointer(variable["type"]) or variable["isList"]:
            splittedName = qfbtools.split(variable["name"])
            readableName = " ".join(splittedName)
            if variable["isList"]:
                readableName = "List of " + readableName
            else:
                readableName = readableName[0].upper() + readableName[1:]
            source += """    /**
     * @internal
     * @short """ + readableName + """
     */
"""
            if not variable["isList"]:
                source += "    " + variable["type"] + " " + qfbtools.camelCase(
                    splittedName) + ";\n"
            else:
                source += "    QList<" + variable[
                    "type"] + "> " + qfbtools.camelCase(splittedName)
                source += ";\n"
    source += """};

""" + className + "Private::" + className + """Private():
    ObjectBasePrivate()
{
}

////// End of private class //////

"""

    source += className + "::" + className + """(QObject *parent):
    """ + baseClass + """(parent)
{
}

"""

    source += className + "::" + className
    source += """(const QVariantMap propertiesMap, QObject *parent):
    """ + baseClass + """(*(new """ + className + """Private), parent)
{
    Q_D(""" + className + """);
    d->propertiesMap = propertiesMap;
    // >>>>> custom object creation code
"""
    if implementationData.objectCreationCode != "":
        source += implementationData.objectCreationCode
    else:
        source += """    // TODO: check object creation
    // It was done automatically by a script
"""

        for variable in variables:
            if qfbtools.isPointer(variable["type"]) or variable["isList"]:
                splittedName = qfbtools.split(variable["name"])
                source += "    // Create " + qfbtools.camelCase(
                    splittedName) + "\n"
                if not variable["isList"]:
                    source += "    QVariantMap " + qfbtools.camelCase(
                        splittedName) + "Data = "
                    source += "d->propertiesMap.take("
                    source += qfbtools.staticKey(splittedName,
                                                 className) + ")."
                    source += "toMap();\n"
                    source += "    d->" + qfbtools.camelCase(
                        splittedName) + " = createObject<"
                    source += variable["type"][:-1].strip()
                    source += ">(" + qfbtools.camelCase(splittedName)
                    source += "Data, this);\n"
                else:
                    source += "    QVariantList " + qfbtools.camelCase(
                        splittedName) + "Data = "
                    source += "d->propertiesMap.take("
                    source += qfbtools.staticKey(splittedName,
                                                 className) + ")."
                    source += "toList();\n"
                    source += "    d->" + qfbtools.camelCase(
                        splittedName) + " = "
                    if variable["type"].strip() == "QString":
                        source += "createStringList"
                    else:
                        source += "createList<"
                        source += variable["type"][:-1].strip() + ">"
                    source += "(" + qfbtools.camelCase(splittedName) + "Data"
                    if variable["type"].strip() == "QString":
                        source += ");\n"
                    else:
                        source += ", this);\n"

    source += """    // <<<<< custom object creation code
}

"""
    for variable in variables:
        if variable["type"] != "TODO" and variable[
                "type"] != "NO" and variable["type"] != "":
            splittedName = qfbtools.split(variable["name"])
            if variable["name"] in implementationData.variablesCode:
                source += variable["type"] + " "
                source += className + "::" + qfbtools.camelCase(
                    splittedName) + """() const
{
    Q_D(const """ + className + """);
    // >>>>> property """ + variable["name"] + """
"""
                source += implementationData.variablesCode[variable["name"]]
                source += """    // <<<<< property """ + variable["name"] + """
}

"""

            elif variable["name"] != "id" and variable["name"] != "name":
                if variable["isList"]:
                    source += "QList<" + variable["type"] + "> "
                else:
                    source += variable["type"] + " "
                source += className + "::" + qfbtools.camelCase(
                    splittedName) + """() const
{
    Q_D(const """ + className + """);
    // >>>>> property """ + variable["name"] + """
"""
                if variable["isList"]:
                    source += "    return d->" + qfbtools.camelCase(
                        splittedName) + ";\n"
                else:
                    if variable["type"] in knownTypes:
                        source += "    return d->propertiesMap.value("
                        source += qfbtools.staticKey(splittedName, className)
                        source += ")." + knownTypes[variable["type"]] + "();\n"
                    elif variable["type"] == "QUrl":
                        source += "    return parseUrl(d->propertiesMap.value("
                        source += qfbtools.staticKey(splittedName, className)
                        source += ").toString());\n"
                    elif qfbtools.isPointer(variable["type"]):
                        source += "    return d->" + qfbtools.camelCase(
                            splittedName) + ";\n"
                    else:
                        source += "    // TODO: define the returned data\n"
                source += """    // <<<<< property """ + variable["name"] + """
}

"""

    source += """
// >>>>> custom source code
"""

    source += implementationData.sourceCode

    source += """// <<<<< custom source code

}
"""

    sourceFile = open(className.lower() + ".cpp", "w")
    sourceFile.write(source)
    sourceFile.close()
def parse(fileName):
    parserData = ParserData()

    # Open the file and get data
    f = open(fileName)
    data = ""
    for line in f:
        data += line

    splittedData = data.strip().split("---")

    if len(splittedData) < 2:
        print """Error: the file is malformatted.
The main groups are missing."""
        return parserData

    # Parse the first group (class name and includes)
    firstGroup = splittedData[0]
    del splittedData[0]

    splittedFirstGroup = firstGroup.strip().split("\n")
    if len(splittedFirstGroup) == 0:
        print """Error: the file is malformatted
The first group should have at least the name of the class"""
        return parserData

    # The class name is the first line
    parserData.className = splittedFirstGroup[0]
    del splittedFirstGroup[0]

    # The other lines are headers
    for splittedFirstGroupItem in splittedFirstGroup:
        strippedSplittedItem = splittedFirstGroupItem.strip()
        if strippedSplittedItem != "":
            parserData.includes.append(strippedSplittedItem)

    # Parse the variables
    for groupData in splittedData:
        group = _createGroup(groupData)
        if group["name"] != "" and group["type"] != "":
            parserData.variables.append(group)

    # Find the parent class
    parserData.baseClass = "ObjectBase"
    haveId = False
    haveName = False
    for variable in parserData.variables:
        if variable["name"] == "id" and variable["type"] == "QString":
            haveId = True
        if variable["name"] == "name" and variable["type"] == "QString":
            haveName = True

    if haveId and haveName:
        parserData.baseClass = "NamedObject"
    elif haveId:
        parserData.baseClass = "Object"

    # Find if there are sub-objects
    for variable in parserData.variables:
        if qfbtools.isPointer(variable["type"]):
            parserData.haveSubObjects = True
    return parserData
Ejemplo n.º 4
0
def createSource(className, includes, baseClass, variables, implementationData):
    source = copyright
    source += """
/**
 * @file """ + className.lower() + """.cpp
 * @brief Implementation of QFB::""" + className + """
 */

#include \"""" + className.lower() + """.h\"
#include \"private/helper_p.h\"
#include \"private/objectbase_p.h\"
#include \"private/object_creator_p.h\"

namespace QFB
{

"""

    for variable in variables:
        if variable["type"] != "TODO" and variable["type"] != "NO" and variable["type"] != "":
            if variable["name"] != "id" and variable["name"] != "name":
                splittedName = qfbtools.split(variable["name"])
                source += """/**
 * @internal
 * @brief """ + qfbtools.staticKey(splittedName, className) + """
 */
static const char *""" + qfbtools.staticKey(splittedName, className) + " = "
                source += "\"" + variable["name"] + "\";\n"


    source += """

/**
 * @internal
 * @short Private class for QFB::""" + className + """
 */
class """ + className + """Private: public ObjectBasePrivate
{
public:
    /**
     * @internal
     * @short Default constructor
     */
    explicit """ + className + """Private();
"""
    for variable in variables:
        if qfbtools.isPointer(variable["type"]) or variable["isList"]:
            splittedName = qfbtools.split(variable["name"])
            readableName = " ".join(splittedName)
            if variable["isList"]:
                readableName = "List of " + readableName
            else:
                readableName = readableName[0].upper() + readableName[1:]
            source += """    /**
     * @internal
     * @short """ + readableName + """
     */
"""
            if not variable["isList"]:
                source += "    " + variable["type"] + " " + qfbtools.camelCase(splittedName) + ";\n"
            else:
                source += "    QList<" + variable["type"] + "> " + qfbtools.camelCase(splittedName)
                source += ";\n"
    source += """};

""" + className + "Private::" + className + """Private():
    ObjectBasePrivate()
{
}

////// End of private class //////

"""

    source += className + "::" + className + """(QObject *parent):
    """ + baseClass + """(parent)
{
}

"""

    source += className + "::" + className
    source += """(const QVariantMap propertiesMap, QObject *parent):
    """ + baseClass + """(*(new """ +  className + """Private), parent)
{
    Q_D(""" + className + """);
    d->propertiesMap = propertiesMap;
    // >>>>> custom object creation code
"""
    if implementationData.objectCreationCode != "":
        source += implementationData.objectCreationCode
    else:
        source += """    // TODO: check object creation
    // It was done automatically by a script
"""

        for variable in variables:
            if qfbtools.isPointer(variable["type"]) or variable["isList"]:
                splittedName = qfbtools.split(variable["name"])
                source += "    // Create " + qfbtools.camelCase(splittedName) + "\n"
                if not variable["isList"]:
                    source += "    QVariantMap " + qfbtools.camelCase(splittedName) + "Data = "
                    source += "d->propertiesMap.take("
                    source += qfbtools.staticKey(splittedName, className) + ")."
                    source += "toMap();\n"
                    source += "    d->" + qfbtools.camelCase(splittedName) + " = createObject<"
                    source += variable["type"][:-1].strip()
                    source += ">(" + qfbtools.camelCase(splittedName)
                    source += "Data, this);\n"
                else:
                    source += "    QVariantList " + qfbtools.camelCase(splittedName) + "Data = "
                    source += "d->propertiesMap.take("
                    source += qfbtools.staticKey(splittedName, className) + ")."
                    source += "toList();\n"
                    source += "    d->" + qfbtools.camelCase(splittedName) + " = "
                    if variable["type"].strip() == "QString":
                        source += "createStringList"
                    else:
                        source += "createList<"
                        source += variable["type"][:-1].strip() + ">"
                    source += "(" + qfbtools.camelCase(splittedName) + "Data"
                    if variable["type"].strip() == "QString":
                        source += ");\n"
                    else:
                        source += ", this);\n"


    source += """    // <<<<< custom object creation code
}

"""
    for variable in variables:
        if variable["type"] != "TODO" and variable["type"] != "NO" and variable["type"] != "":
            splittedName = qfbtools.split(variable["name"])
            if variable["name"] in implementationData.variablesCode:
                source += variable["type"] + " "
                source +=  className + "::" + qfbtools.camelCase(splittedName) + """() const
{
    Q_D(const """ + className + """);
    // >>>>> property """ + variable["name"] + """
"""
                source += implementationData.variablesCode[variable["name"]]
                source += """    // <<<<< property """ + variable["name"] + """
}

"""

            elif variable["name"] != "id" and variable["name"] != "name":
                if variable["isList"]:
                    source += "QList<" + variable["type"] + "> "
                else:
                    source += variable["type"] + " "
                source +=  className + "::" + qfbtools.camelCase(splittedName) + """() const
{
    Q_D(const """ + className + """);
    // >>>>> property """ + variable["name"] + """
"""
                if variable["isList"]:
                    source += "    return d->" + qfbtools.camelCase(splittedName) + ";\n"
                else:
                    if variable["type"] in knownTypes:
                        source += "    return d->propertiesMap.value("
                        source += qfbtools.staticKey(splittedName, className)
                        source += ")." + knownTypes[variable["type"]] + "();\n"
                    elif variable["type"] == "QUrl":
                        source += "    return parseUrl(d->propertiesMap.value("
                        source += qfbtools.staticKey(splittedName, className)
                        source += ").toString());\n"
                    elif qfbtools.isPointer(variable["type"]):
                        source += "    return d->" + qfbtools.camelCase(splittedName) + ";\n"
                    else:
                        source += "    // TODO: define the returned data\n"
                source += """    // <<<<< property """ + variable["name"] + """
}

"""

    source += """
// >>>>> custom source code
"""

    source += implementationData.sourceCode

    source += """// <<<<< custom source code

}
"""

    sourceFile = open(className.lower() + ".cpp", "w")
    sourceFile.write(source)
    sourceFile.close()