Example #1
0
def get(query="",
        format="html",
        numOfVersions=1,
        user="",
        semanticQuery=False,
        exact=False):
    """
    read environment description from the database

    query : search query

    format : output format (html/json)

    numOfVersions : allows to get older version of the object description

    semantic_query : is the query a semantic query?

    exact : search the specific environment or for an area
    """
    def addEnv(row):
        """
        add results to the output dictionary
        """
        rating = row.columns['info:rating'].value
        output = {
            "id": row.row,
            "description": row.columns['info:description'].value,
            "author": row.columns['info:author'].value,
            "rating": rating,
            "environments": list(),
            "files": {}
        }

        #check subscription
        if user != "" and format == "html":
            scannerSub = client.scannerOpenWithPrefix(
                "Subscriptions", user + "#Environments#" + row.row, [])
            subRes = client.scannerGet(scannerSub)
            if subRes and subRes[0].row == user + "#Environments#" + row.row:
                output['subscribed'] = True
            else:
                output['subscribed'] = False

        if row.columns.has_key('info:modified_by'):
            output['modified_by'] = row.columns['info:modified_by'].value

        for r in row.columns:
            if r.startswith("file:"):
                url_ = res[0].columns[r].value
                file_ = roboearth.UPLOAD_DIR + url_[len(roboearth.BINARY_ROOT
                                                        ):]
                output["files"][r[5:]] = {
                    'url': url_,
                    'mime-type': m.from_file(file_)
                }

        if row.columns.has_key("info:lat"):
            lat = row.columns['info:lat'].value
            lng = row.columns['info:lng'].value
            delta = 0.002607
            output["location"] = {"latitude": lat, "longitude": lng}

            if format == "html":
                output['location']["html"] = GeoData.getEmbeddedMap(
                    float(lat), float(lng), delta)
            else:
                output['location']['osm'] = {
                    'info':
                    GeoData.reverseGeocoding(float(lat), float(lng), delta),
                    'raw_map_data':
                    GeoData.getRawData(float(lat), float(lng), delta)
                }

        versions = client.getVer("Elements", row.row, "owl:description",
                                 numOfVersions)
        if format == "html":
            for v in versions:
                try:
                    output['environments'].append({
                        'timestamp':
                        time.ctime(int(v.timestamp) / 1000),
                        'environment':
                        xml.dom.minidom.parseString(
                            v.value).toprettyxml(indent="    ")
                    })
                except:
                    output['environments'].append({
                        'timestamp':
                        time.ctime(int(v.timestamp) / 1000),
                        'environment':
                        v.value
                    })
                output['fullStars'] = range(int(rating))
                output['emptyStars'] = range(10 - int(rating))
        else:
            for v in versions:
                output['environments'].append({
                    'timestamp':
                    v.timestamp,
                    'environment':
                    v.value.replace("\n", "")
                })

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("download", "Environment", row.row)

        return output

    # semantic query get send to the reasoning server
    if semanticQuery:
        query = sesame.get(
            query.replace("SELECT source FROM CONTEXT source", ""), "elements")
        if query['status'] == 0:
            query = [q.rsplit("/", 1)[1] for q in query['stdout']]
        else:
            raise roboearth.DBReadErrorException(query['stderr'])
    else:
        query = [query]

    transport = roboearth.openDBTransport()
    client = transport['client']

    m = magic.Magic(mime=True)

    result = list()
    for q in query:
        scanner = client.scannerOpenWithPrefix("Elements", q.lower(), [])
        res = client.scannerGet(scanner)
        while res:
            if ((semanticQuery == False and exact == False) or res[0].row == q
                ) and res[0].columns['info:type'].value == 'environment':
                result.append(addEnv(res[0]))
            res = client.scannerGet(scanner)

        client.scannerClose(scanner)

    roboearth.closeDBTransport(transport)

    return result
Example #2
0
def get(query="",
        user="",
        format="html",
        numOfVersions=1,
        semanticQuery=False,
        exact=False):
    """
    read recipe to the database

    query : search query

    format : output format (html/json)

    numOfVersions : allows to get older version of the object description

    semantic_query : is the query a semantic query?

    exact : search for the exact object or for a class of objects
    """
    def addRecipe(row):
        """
        add results to the output dictionary
        """
        rating = row.columns['info:rating'].value

        output = {
            "id": row.row,
            "description": row.columns['info:description'].value,
            "author": row.columns['info:author'].value,
            "rating": rating,
            "recipes": list()
        }

        #check subscription
        if user != "" and format == "html":
            scannerSub = client.scannerOpenWithPrefix(
                "Subscriptions", user + "#Recipes#" + row.row, [])
            subRes = client.scannerGet(scannerSub)
            if subRes and subRes[0].row == user + "#Recipes#" + row.row:
                output['subscribed'] = True
            else:
                output['subscribed'] = False

        if row.columns.has_key('info:modified_by'):
            output['modified_by'] = row.columns['info:modified_by'].value

        versions = client.getVer("Elements", row.row, "owl:description",
                                 numOfVersions)
        if format == "html":
            for v in versions:
                try:
                    output['recipes'].append({
                        'timestamp':
                        time.ctime(int(v.timestamp) / 1000),
                        'recipe':
                        xml.dom.minidom.parseString(
                            v.value).toprettyxml(indent="    ")
                    })
                except:
                    output['recipes'].append({
                        'timestamp':
                        time.ctime(int(v.timestamp) / 1000),
                        'recipe':
                        v.value
                    })
                output['fullStars'] = range(int(round(float(rating))))
                output['emptyStars'] = range(10 - int(round(float(rating))))

        else:
            for v in versions:
                output['recipes'].append({
                    'timestamp': v.timestamp,
                    'recipe': v.value.replace("\n", "")
                })

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("download", "Action Recipe", row.row)

        return output

    # semantic query get send to the reasoning server
    if semanticQuery:
        query = sesame.get(
            query.replace("SELECT source FROM CONTEXT source", ""), "elements")
        if query['status'] == 0:
            query = [q.rsplit("/", 1)[1] for q in query['stdout']]
        else:
            raise roboearth.DBReadErrorException(query['stderr'])
    else:
        query = [query]

    transport = roboearth.openDBTransport()
    client = transport['client']

    result = list()
    for q in query:
        scanner = client.scannerOpenWithPrefix("Elements", q.lower(), [])
        res = client.scannerGet(scanner)
        while res:
            if ((semanticQuery == False and exact == False) or res[0].row
                    == q) and res[0].columns['info:type'].value == 'recipe':
                result.append(addRecipe(res[0]))
            res = client.scannerGet(scanner)

        client.scannerClose(scanner)

    roboearth.closeDBTransport(transport)

    return result
Example #3
0
def get(query="", format="html", numOfVersions = 1, user="", semanticQuery = False, exact=False):
    """
    read environment description from the database

    query : search query

    format : output format (html/json)

    numOfVersions : allows to get older version of the object description

    semantic_query : is the query a semantic query?

    exact : search the specific environment or for an area
    """


    def addEnv(row):
        """
        add results to the output dictionary
        """
        rating = row.columns['info:rating'].value
        output = {"id" : row.row,
                  "description" : row.columns['info:description'].value,
                  "author" : row.columns['info:author'].value,
                  "rating" : rating,
                  "environments" : list(),
                  "files" : {}}

        #check subscription
        if user != "" and format=="html":
            scannerSub = client.scannerOpenWithPrefix("Subscriptions", user+"#Environments#"+row.row, [ ])
            subRes = client.scannerGet(scannerSub)
            if subRes and subRes[0].row == user+"#Environments#"+row.row:
                output['subscribed'] = True
            else:
                output['subscribed'] = False

        if row.columns.has_key('info:modified_by'):
            output['modified_by'] = row.columns['info:modified_by'].value
            
        for r in row.columns:
            if r.startswith("file:"):
                url_ = res[0].columns[r].value
                file_ = roboearth.UPLOAD_DIR+url_[len(roboearth.BINARY_ROOT):]
                output["files"][r[5:]] = {'url' : url_,
                                          'mime-type' : m.from_file(file_)} 

        if row.columns.has_key("info:lat"):
            lat = row.columns['info:lat'].value
            lng = row.columns['info:lng'].value
            delta = 0.002607
            output["location"] = {"latitude" : lat,
                                  "longitude" : lng}

            if format=="html":
                output['location']["html"] = GeoData.getEmbeddedMap(float(lat), float(lng), delta)
            else:
                output['location']['osm'] = {'info' : GeoData.reverseGeocoding(float(lat), float(lng), delta),
                                             'raw_map_data' : GeoData.getRawData(float(lat), float(lng), delta)}

            
        versions = client.getVer("Elements", row.row, "owl:description", numOfVersions)
        if format=="html":
            for v in versions:
                try:
                    output['environments'].append({ 'timestamp' : time.ctime(int(v.timestamp)/1000),
                                               'environment' : xml.dom.minidom.parseString(v.value).toprettyxml(indent="    ") })
                except:
                    output['environments'].append({ 'timestamp' : time.ctime(int(v.timestamp)/1000),
                                                    'environment' : v.value})
                output['fullStars'] = range(int(rating))
                output['emptyStars'] = range(10 - int(rating))
        else:
            for v in versions:
                    output['environments'].append({ 'timestamp' : v.timestamp,
                                                    'environment' : v.value.replace("\n","")})

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("download", "Environment", row.row)

        return output

    # semantic query get send to the reasoning server
    if semanticQuery:
        query = sesame.get(query.replace("SELECT source FROM CONTEXT source", ""), "elements")
        if query['status'] == 0:
            query = [q.rsplit("/", 1)[1] for q in query['stdout']]
        else:
            raise roboearth.DBReadErrorException(query['stderr'])
    else:
        query = [query]

    transport = roboearth.openDBTransport()
    client = transport['client']
    
    m = magic.Magic(mime=True)

    result = list()
    for q in query:
        scanner = client.scannerOpenWithPrefix("Elements", q.lower(), [ ])
        res = client.scannerGet(scanner)
        while res:
            if ((semanticQuery == False and exact == False) or res[0].row == q) and res[0].columns['info:type'].value == 'environment':
                result.append(addEnv(res[0])) 
            res = client.scannerGet(scanner)

        client.scannerClose(scanner)

    roboearth.closeDBTransport(transport)

    return result
Example #4
0
def get(query="", user="", format="html", numOfVersions = 1, semanticQuery = False, exact=False):
    """
    read recipe to the database

    query : search query

    format : output format (html/json)

    numOfVersions : allows to get older version of the object description

    semantic_query : is the query a semantic query?

    exact : search for the exact object or for a class of objects
    """

    def addRecipe(row):
        """
        add results to the output dictionary
        """
        rating = row.columns['info:rating'].value
        
        output = {"id" : row.row,
                  "description" : row.columns['info:description'].value,
                  "author" : row.columns['info:author'].value,
                  "rating" : rating,
                  "recipes" : list()}

        #check subscription
        if user != "" and format=="html":
            scannerSub = client.scannerOpenWithPrefix("Subscriptions", user+"#Recipes#"+row.row, [ ])
            subRes = client.scannerGet(scannerSub)
            if subRes and subRes[0].row == user+"#Recipes#"+row.row:
                output['subscribed'] = True
            else:
                output['subscribed'] = False


        if row.columns.has_key('info:modified_by'):
            output['modified_by'] = row.columns['info:modified_by'].value
            
        versions = client.getVer("Elements", row.row, "owl:description", numOfVersions)
        if format=="html":
            for v in versions:
                try:
                    output['recipes'].append({ 'timestamp' : time.ctime(int(v.timestamp)/1000),
                                               'recipe' : xml.dom.minidom.parseString(v.value).toprettyxml(indent="    ") })
                except:
                    output['recipes'].append({ 'timestamp' : time.ctime(int(v.timestamp)/1000),
                                               'recipe' : v.value})
                output['fullStars'] = range(int(round(float(rating))))
                output['emptyStars'] = range(10 - int(round(float(rating))))
                
        else:
            for v in versions:
                    output['recipes'].append({ 'timestamp' : v.timestamp,
                                               'recipe' : v.value.replace("\n","")})

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("download", "Action Recipe", row.row)

        return output

    # semantic query get send to the reasoning server
    if semanticQuery:
        query = sesame.get(query.replace("SELECT source FROM CONTEXT source", ""), "elements")
        if query['status'] == 0:
            query = [q.rsplit("/", 1)[1] for q in query['stdout']]
        else:
            raise roboearth.DBReadErrorException(query['stderr'])
    else:
        query = [query]


    transport = roboearth.openDBTransport()
    client = transport['client']

    result = list()
    for q in query:
        scanner = client.scannerOpenWithPrefix("Elements", q.lower(), [ ])
        res = client.scannerGet(scanner)
        while res:
            if ((semanticQuery == False and exact == False) or res[0].row == q) and res[0].columns['info:type'].value == 'recipe':
                result.append(addRecipe(res[0])) 
            res = client.scannerGet(scanner)

        client.scannerClose(scanner)

    roboearth.closeDBTransport(transport)

    return result