示例#1
0
def getSubscriptions(username):
    """ returns all subscriptions of a given user
    """
    transport = roboearth.openDBTransport()
    client = transport['client']

    scanner = client.scannerOpenWithPrefix("Subscriptions", username, [ ])
    user = client.scannerGet(scanner)

    result = { }
    result['Robots'] = list()
    result['Recipes'] = list()
    result['Objects'] = list()
    result['Environments'] = list()
    result['Users'] = list()
    while user:
        uname, table, uid = user[0].row.split('#',2)
        result[table].append({'id' : uid,
                              'url' : roboearth.DOMAIN+table.lower()+"/"+"result?query="+uid+"&exact=True"})        
        user = client.scannerGet(scanner)

    client.scannerClose(scanner)
    roboearth.closeDBTransport(transport)

    return result
示例#2
0
def subscribe(username, table, uid):
    transport = roboearth.openDBTransport()
    client = transport['client']
    client.mutateRow('Elements', uid, [Mutation(column="subscriber:"+username, value=username)])
    client.mutateRow("Subscriptions", username+"#"+table+"#"+uid,
                     [Mutation(column="info:"+table, value=uid)])
    roboearth.closeDBTransport(transport)
示例#3
0
def set(id_, class_, description, environment, author, lat=None, lng=None, files=None):
    """
    write environment description to the database

    class_ : environment class

    id_ : unified identifier of environment

    environment : owl description

    author: author of the description
    """

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

    mutation_list =list()

    #create paths
    path = class_.replace(' ', '').lower().strip('.') +  '.' +id_.replace(' ', '').lower().strip('.')
    wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/', path.replace('.', '/'))
    path = os.path.join(roboearth.UPLOAD_DIR, 'elements/', path.replace('.', '/'))

    if class_.lower() == "osm":
        try: # try geocoding
            lat, lng = GeoData.geocoding(query=id_, service="osm")
            mutation_list.append(Mutation(column="info:lat", value=lat))
            mutation_list.append(Mutation(column="info:lng", value=lng))
            envName = class_.replace(' ', '').lower().strip('.') + '.' + lat.replace(".", ",") + '.'+lng.replace(".", ",")

        except Exception, err:
            raise roboearth.FormInvalidException("For class \"osm\" the identifier has to consist of two float (<lat>;<lng>) or of an description of a location (e.g. address): " + err.__str__())
示例#4
0
def upload(request, identifier, author):
    """
    add binary file to object description

    identifier : object identifier

    author: author of the description
    """

    try:
        file_ = request.FILES['file']
        type_ = request.POST['type']

        upload_path = os.path.join(roboearth.UPLOAD_DIR, "elements/", identifier.replace('.', '/'))
        transport = roboearth.openDBTransport()
        client = transport['client']

        hdfs.upload_file(file_, upload_path)

        # add reference to table
        client.mutateRow("Elements", identifier,
                         [Mutation(column="info:modified_by", value=author),
                          Mutation(column="file:"+type_, value=roboearth.DOMAIN + os.path.join("data/", "elements/", identifier.replace('.', '/'), file_.name))])
        roboearth.closeDBTransport(transport)
        return True
    except Exception, e:
        return False
示例#5
0
def update(id_, environment, author):
    """

    id_ : robot identifier

    environment : reference to the related environment

    author : author who submitted the data
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        client.mutateRow("Elements", id_,
                          [Mutation(column="info:author", value=author)])

        # push update to subscribers
        scanner = client.scannerOpenWithPrefix("Elements", id_, [ ])
        res = client.scannerGet(scanner)
        for r in res[0].columns:
            if r.startswith("subscriber:"):
                client.mutateRow("Users", res[0].columns[r].value,
                                 [Mutation(column="news:", value="Robots#"+id_)])
        client.scannerClose(scanner)

        roboearth.closeDBTransport(transport)
        
        return {'id' : id_}
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Robots table: " + err.__str__())
示例#6
0
def getLocation(query="", obj="", numOfVersions = 1, exact = False):
    """
    read object location from the database

    query : search query (describes the location)

    object : search a special object at the location

    numOfVersions : allows to get older version of the object location

    exact : search for the exact object location or for an area
    """


    def addObject(row):
        """
        add results to the output dictionary
        """

        output = {"id" : row.row,
                  "author" : row.columns['info:author'].value,
                  "object" : row.columns['object:id'].value,
                  "environment" : row.columns['environment:id'].value,
                  "room_number" : row.columns['environment:room'].value,
                  "posX" : row.columns['position:x'].value,
                  "posY" : row.columns['position:y'].value,
                  "posZ" : row.columns['position:z'].value,                  
                  "delta" : row.columns['position:delta'].value}
                  
        return output

    
    transport = roboearth.openDBTransport()
    client = transport['client']
    scanner = client.scannerOpenWithPrefix("ObjectLocations", query.lower(), [ ])
    result = list()

    res = client.scannerGet(scanner)
    while res:
        if (exact==False or res[0].row == query) and (obj == "" or res[0].columns['object:id'].value.find(obj) > -1):
            result.append(addObject(res[0])) 
        res = client.scannerGet(scanner)

    client.scannerClose(scanner)
    roboearth.closeDBTransport(transport)

    return result
示例#7
0
def create(user_name):
    """
    create hbase table for a new user

    user_name : user name of the new user
    """

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

    try:
        client.mutateRow("Users", user_name, [])

        roboearth.closeDBTransport(transport)

    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to users table: " + err.__str__())
示例#8
0
def setLocation(environment, room_number, object_, posX, posY, posZ, delta, author):
    """
    write a object location to the database

    environment : reference to an environment, stored at the database

    room_number: the room where the object is located

    object : reference to the object description

    posX, posY, posZ : coordiantes which describes the exact location

    delta : impreciseness of position data

    author : author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
 
    try:
        scanner = client.scannerOpenWithPrefix("Objects", object_.lower(), [ ]) 
        if not client.scannerGet(scanner):
            raise roboearth.NoDBEntryFoundException("Couldn't connect location with the named object. Object doesn't exits in database")
        scanner = client.scannerOpenWithPrefix("Environments", environment.lower(), [ ])
        if not client.scannerGet(scanner):
            raise roboearth.NoDBEntryFoundException("Couldn't connect location with the named environment. Environment doesn't exits in database")

        id_ = environment + '.' + room_number + '.' + posX + '.' + posY + '.' + posZ + '.' + delta

        if getLocation(query=id_, exact = True):
            return None

        client.mutateRow("ObjectLocations", id_,
                         [Mutation(column="object:id", value=object_),
                          Mutation(column="info:author", value=author),
                          Mutation(column="environment:id", value=environment),
                          Mutation(column="environment:room", value=room_number),
                          Mutation(column="position:x", value=posX),
                          Mutation(column="position:y", value=posY),
                          Mutation(column="position:z", value=posZ),
                          Mutation(column="position:delta", value=delta)])
        roboearth.closeDBTransport(transport)
        return {'id' : id_, 'posX' : posX, 'posY' : posY, 'posZ' : posZ, 'delta' : delta, 'object' : object_, 'environment' : environment, 'room_number' : room_number}
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write dataop to Object table: " + err.__str__())
示例#9
0
def set(id_, class_, description, recipe, author):
    """
    write recipe to the database

    id_ : recipe identifier

    class_: recipe class

    description: human readable description

    recipe: owl description

    author: author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    recipeName = class_.replace(' ', '').lower().strip('.') + '.' + id_.replace(' ', '').lower().strip('.')
    try:
        # recipe already exist
        if get(query=recipeName, exact=True):
            return None

        #write data to hbase
        client.mutateRow("Elements", recipeName,
                         [Mutation(column="info:description", value=description),
                          Mutation(column="info:author", value=author),
                          Mutation(column="info:rating", value="1"),
                          Mutation(column="info:type", value="recipe"),
                          Mutation(column="owl:description", value=recipe)])

        #write data to sesame
        sesame_ret = sesame.set(recipe, recipeName, "elements")
        if sesame_ret != "0":
            hbase_op.delete_row("Elements", recipeName)
            raise IllegalArgument(sesame_ret)
            
        client.mutateRow("Users", author,
                         [Mutation(column="element:"+recipeName, value="")])

        roboearth.closeDBTransport(transport)      

        return {'id' : recipeName, 'description' : description, 'recipe' : recipe}
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Action Recipe table: " + err.__str__())
示例#10
0
def list_all(table):
    """
    returns a list of all entries of a table
    """
    transport = roboearth.openDBTransport()
    client = transport['client']
    scanner = client.scannerOpenWithPrefix(table, "", [ ])
    result = list()

    res = client.scannerGet(scanner)
    while res:
        result.append(res[0].row) 
        res = client.scannerGet(scanner)

    client.scannerClose(scanner)
    roboearth.closeDBTransport(transport)

    return result
示例#11
0
def get(query="", numOfVersions = 1, user="", format="html", exact=False):
    """
    read a robot location from the database

    query : search query

    numOfVersions : allows to get older version of the location

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

    def addObject(row):
        output = {"id" : row.row,
                  "description" : row.columns['info:description'].value,
                  "srdl" : row.columns['owl:description'].value,
                  "picture" : row.columns['info:picture'].value,
                  "author" : row.columns['info:author'].value}

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

    
    transport = roboearth.openDBTransport()
    client = transport['client']
    scanner = client.scannerOpenWithPrefix("Elements", query.lower(), [ ])
    result = list()

    res = client.scannerGet(scanner)
    while res:
        if (exact==False or res[0].row == query) and res[0].columns['info:type'].value == 'robot':
            result.append(addObject(res[0])) 
        res = client.scannerGet(scanner)

    client.scannerClose(scanner)
    roboearth.closeDBTransport(transport)

    return result
示例#12
0
def finalDelete(request):
    """ execute delete operation after the user approved the operation
    """
    try:
        if request.POST['binary'] == "0":
            transport = roboearth.openDBTransport()
            client = transport['client']
            scanner = client.scannerOpenWithPrefix("Elements", request.POST['rowKey'], [ ])
            res = client.scannerGet(scanner)
            
            subscribers = []
            for r in res[0].columns:
                if r.startswith("file:") or r.startswith("info:picture"):
                    hdfs.rm_file(res[0].columns[r].value.replace(roboearth.BINARY_ROOT, roboearth.UPLOAD_DIR))
                if r.startswith("subscriber:"):
                    subscribers.append(res[0].columns[r].value)
            
            client.scannerClose(scanner)
            
            for subscriber in subscribers:
                scannersub = client.scannerOpenWithPrefix("Subscriptions", subscriber, [ ])
                user = client.scannerGet(scannersub)
                if user:
                    uname, table, uid = user[0].row.split('#',2)
                    if uid == request.POST['rowKey']:
                        hbase.delete_row(table="Subscriptions", rowKey=uname+"#"+table+"#"+uid)        
                client.scannerClose(scannersub)
            
            roboearth.closeDBTransport(transport)
            
            removeEmptyFolders(roboearth.UPLOAD_DIR+'/elements')
            
            hbase.delete_row(request.POST['table'], request.POST['rowKey'])
            hbase.delete_column('Users', request.user.username, request.POST['table'].lower()[0:len(request.POST['table'])-1]+':'+request.POST['rowKey'])
            
            return  HttpResponse(success(request))
        else:
            hdfs.rm_file(request.POST['file'].replace(roboearth.BINARY_ROOT, roboearth.UPLOAD_DIR))
            hbase.delete_column(request.POST['table'], request.POST['rowKey'], 'file:'+request.POST['colID'])
            return  HttpResponse(success(request))

            
    except roboearth.DBWriteErrorException, err:
        return HttpResponse(error(request, errorType=2, errorMessage=err.__str__()))
示例#13
0
def update(id_, data, author):
    """
    update an existing object description at the database

    id_ : complete identifier (primary key) of the object

    data: dictionary with the updated data ('description' (human readable) and
    'object_description' (OWL))

    author: author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        mutation_list = [ ]
        if data.has_key('description'):
            mutation_list.append(Mutation(column="info:description", value=data['description']))            
        if data.has_key('object_description'):
            mutation_list.append(Mutation(column="owl:description", value=data['object_description']))            
        
        client.mutateRow("Elements", id_,
                         [Mutation(column="info:modified_by", value=author)] +
                         mutation_list)

        if data.has_key('object_description'):
            sesame.rm(id_, "Elements")
            sesame.set(data['object_description'], id_, "Elements")

        # push update to subscribers
        scanner = client.scannerOpenWithPrefix("Elements", id_, [ ])
        res = client.scannerGet(scanner)
        for r in res[0].columns:
            if r.startswith("subscriber:"):
                client.mutateRow("Users", res[0].columns[r].value,
                                 [Mutation(column="news:", value="Objects#"+id_)])

        client.scannerClose(scanner)
        roboearth.closeDBTransport(transport)

        return True
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Elements table: " + err.__str__())
示例#14
0
def set(id_, author, description, srdl, picture):
    """
    write a robot location to the database

    id_ : robot identifier

    author : author who submitted the data

    description : description of the reobot
    
    srdl: Semantic Robot Description of the robot

    picture: picture of the robot

    
    """

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

    if get(query=id_, exact=True):
        return None
    
    try:
        #create paths
        path = id_.replace(' ', '').lower().strip('.') +  '.' +id_.replace(' ', '').lower().strip('.')
        wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/', id_.replace(' ', '').lower().strip('.'), picture.name)
        hdfs.upload_file(picture, os.path.join(roboearth.UPLOAD_DIR, 'elements/', id_.replace(' ', '').lower().strip('.')))
        
        client.mutateRow("Elements", id_.lower(),
                             [Mutation(column="info:author", value=author),
                              Mutation(column="info:description", value=description),
                              Mutation(column="owl:description", value=srdl),
                              Mutation(column="info:picture", value=wwwPath),
                              Mutation(column="info:type", value='robot')])
        client.mutateRow("Users", author,
                         [Mutation(column="element:"+id_, value="")])

        roboearth.closeDBTransport(transport)
        return {'id' : id_, 'description' : description, 'srdl' : srdl}
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Robot table: " + err.__str__())
示例#15
0
def update_rating(id_, rating):
    """
    update the rating of an existing recipe at the database

    id_ : complete identifier (primary key) of the recipe

    rating : new rating
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        client.mutateRow("Elements", id_,
                         [Mutation(column="info:rating", value=str(rating))])

        roboearth.closeDBTransport(transport)

        return True
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't update Action Recipe rating: " + err.__str__())
示例#16
0
def getNewsfeed(username):
    """ returns the last 20 updates of the subscriptions from a specific user
    """
    transport = roboearth.openDBTransport()
    client = transport['client']

    results = list()
    items = client.getVer("Users", username, "news:", 20)
    for i in items:
        table, uid = i.value.split('#', 1)
        scanner2 = client.scannerOpenWithPrefix(table, uid, [ ])
        d = client.scannerGet(scanner2)
        results.append({'description' : d[0].columns['info:description'].value,
                        'timestamp' : time.ctime(int(i.timestamp)/1000),
                        'url' : roboearth.DOMAIN+table.lower()+"/"+"result?query="+uid+"&exact=True",
                        'table' : table[:len(table)-1],
                        'id' : uid})
        client.scannerClose(scanner2)

    return results
示例#17
0
def myRobots(user, numOfVersions = 1, exact=False):
    """
    get all robots from a specific user
    """
    
    transport = roboearth.openDBTransport()
    client = transport['client']

    scanner = client.scannerOpenWithPrefix("Users", user, [ ])
    
    res = client.scannerGet(scanner)

    output = []
    for r in res[0].columns:
        if r.startswith("robot:"):
            output.append(get(query=r[6:], exact=True)[0])

    client.scannerClose(scanner)
    roboearth.closeDBTransport(transport)
    return output
示例#18
0
def updateLocation(id_, posX, posY, posZ, delta, author):
    """
    update existing object location at the database

    id_ : complete identifier (primary key) of the data

    posX, posY, posZ : coordiantes which describes the exact location

    delta : impreciseness of position data

    author: author of the description
    """

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        oldLocation = getLocation(id_)
        object_ = oldLocation[0]['object']
        environment = oldLocation[0]['environment']
        room_number = oldLocation[0]['room_number']
        newID = environment + '.' + room_number + '.' + posX + '.' + posY + '.' + posZ + '.' + delta
        
        client.mutateRow("ObjectLocations", newID,
                         [Mutation(column="object:id", value=object_),
                          Mutation(column="info:author", value=author),
                          Mutation(column="environment:id", value=environment),
                          Mutation(column="environment:room", value=room_number),
                          Mutation(column="position:x", value=posX),
                          Mutation(column="position:y", value=posY),
                          Mutation(column="position:z", value=posZ),
                          Mutation(column="position:delta", value=delta)])

        roboearth.closeDBTransport(transport)

        hbase_op.delete_row("ObjectLocations", id_)
        
        return {'id' : id_, 'posX' : posX, 'posY' : posY, 'posZ' : posZ, 'delta' : delta, 'object' : object_, 'environment' : environment, 'room_number' : room_number}
    except (IOError, IllegalArgument), err:
        raise roboearth.DBWriteErrorException("Can't write data to Robots table: " + err.__str__())
示例#19
0
def delete_row(table, rowKey):
    """
    deletes a complete row (rowKey) from a given table
    """

    hasBinaryFiles = ["Elements"]
    hasGraphData = ["Elements"]

    transport = roboearth.openDBTransport()
    client = transport['client']
    try:
        client.deleteAllRow(table, rowKey)
        roboearth.closeDBTransport(transport)
        if table in hasBinaryFiles:
            hdfs.rm_dir(os.path.join(roboearth.UPLOAD_DIR, table.lower(), rowKey.replace('.', '/')))
        if table in hasGraphData:
            sesame.rm(rowKey, table)
            
    except IOError, err:
        roboearth.closeDBTransport(transport)
        print table, rowKey
        raise roboearth.DBWriteErrorException("Can't delete data: " + err.__str__())
示例#20
0
def upload(request, identifier):
    """ this function receives a POST request
        'http://roboearth.informatik.uni-stuttgart.de/api/0.1/binary/<identifier>',

        identifier: the complete UID of the corresponding data set
        
        Content-Type: application/json which contains:

        type : identifier of the file (content)

        api_key: authenticates the user

        file: the binary file
    """
    if request.method == "POST":
        try:
            api_key = request.POST["api_key"]
            api_key = api_keys.objects.get(key__exact=api_key)
            if not User.objects.get(username__exact=api_key.username).is_active:
                raise
        except Exception, e:
            return rc.BAD_REQUEST
        transport = roboearth.openDBTransport()
        client = transport["client"]
        scanner = client.scannerOpenWithPrefix("Elements", identifier.lower(), [])
        res = client.scannerGet(scanner)
        client.scannerClose(scanner)
        if res:
            result = objects.upload(request, identifier.lower(), author=api_key.username)
        else:
            roboearth.closeDBTransport(transport)
            return rc.BAD_REQUEST

        roboearth.closeDBTransport(transport)
        if result:
            return rc.ALL_OK
        else:
            return rc.BAD_REQUEST
示例#21
0
def delete_column(table, rowKey, column):
    """
    deletes a column from a given row (rowKey) in a specific table
    """
    transport = roboearth.openDBTransport()
    client = transport['client']
    
    scanner = client.scannerOpenWithPrefix(table, rowKey, [ ])
    res = client.scannerGet(scanner)

    for i in res[0].columns:
        if i == column:
            break

    client.scannerClose(scanner)

    try:
        client.deleteAll(table, rowKey, column)
        roboearth.closeDBTransport(transport)
            
    except IOError, err:
        roboearth.closeDBTransport(transport)
        raise roboearth.DBWriteErrorException("Can't delete column: " + err.__str__())
示例#22
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
示例#23
0
def set(id_, class_, description, object_description, author, files=None):
    """
    write a object description to the database

    id_ : object identifier

    class_: object class

    description: human readable description

    object_description: owl description

    author: author of the description

    files: dictionary of binary files (file identifier : file)
    """
    logging.basicConfig(filename='example.log',level=logging.DEBUG)
    transport = roboearth.openDBTransport()
    client = transport['client']

    identifier = class_.replace(' ', '').lower().strip('.') + '.' + id_.replace(' ', '').lower().strip('.')

    #create paths
    path = class_.replace(' ', '').lower().strip('.') +  '.' +id_.replace(' ', '').lower().strip('.')
    wwwPath = roboearth.DOMAIN + os.path.join("data/", 'elements/', path.replace('.', '/'))
    path = os.path.join(roboearth.UPLOAD_DIR, 'elements/', path.replace('.', '/'))

    try:
        # object already exists
        if get(query=identifier, exact=True):
            return None
        
        # upload files and build file mutation list for hbase operation
        file_mutation_list = [ ]
        if files:
            for file_ID, file_ in files.items():
                hdfs.upload_file(file_, path)
                file_mutation_list.append(Mutation(column="file:"+file_ID, value=wwwPath+"/"+file_.name))

        # now write to hbase
        client.mutateRow("Elements", identifier,
                         [Mutation(column="info:description", value=description),
                          Mutation(column="info:author", value=author),
                          Mutation(column="info:rating", value="1"),
                          Mutation(column="info:type", value="object"),
                          Mutation(column="owl:description", value=object_description)]+
                         file_mutation_list)
        
        #write data to sesame
        sesame_ret = sesame.set(object_description, identifier, "elements")
        if sesame_ret != "0":
            hbase_op.delete_row("Elements", identifier)
            print 'raising shit'
            raise IllegalArgument(sesame_ret)

        client.mutateRow("Users", author,
                         [Mutation(column="element:"+identifier, value="")])

        roboearth.closeDBTransport(transport)

        #if not roboearth.local_installation:
        #    roboearth.send_twitter_message("upload", "Object", identifier, author)

        return {'id' : identifier, 'description' : description, 'object_description' : object_description}

    except (IOError, IllegalArgument), err:
        try: # try clean up
            hdfs.rm_dir(path)
            hbase_op.delete_row("Elements", identifier)
            sesame.rm(identifier, "Elements")
        except:
            sys.exc_clear()
        import traceback
        logging.info(traceback.format_exc())
        raise roboearth.DBWriteErrorException("Can't write data to Object table: HEREEEEE " + err.__str__())
示例#24
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