Ejemplo n.º 1
0
def create_label(env, start_response, args):
    method = env['REQUEST_METHOD']
    #if method != 'POST':
    #    start_response('501 Not Implemented', [])
    #    return ['Unsupported']
    user = _validate_fb(env)
    if user is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_SESSION)
    if ('obj_id' not in args) or ('parent' not in args) or ('name' not in args) or ('nick' not in args) or ('rule' not in args) or ('color' not in args):
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER)
    try:
        obj_id = str(int(args['obj_id']))
    except:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'obj_id is not valid.')
    admin = _is_admin(obj_id, user)
    if admin is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_PERMISSION, 'access to the object is denied or the object is not supported.')
    obj_type, id = _detect_obj_type(obj_id, user)
    if admin:
        if obj_type == 'profile':
            shared = 'personal'
            if ('shared' in args) and (str(args['shared']) == '1'):
                shared = 'shared'
        else:
            shared = 'global'
    elif ('personal' not in args) or (str(args['personal']) != '1'):
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'the personal view should be selected.')
    if ('personal' in args) and (str(args['personal']) == '1') and ((not admin) or (obj_type != 'profile')):
        obj_id = str(obj_id) + ':' + str(user['uid'])
        shared = 'personal'
        if ('shared' in args) and (str(args['shared']) == '1'):
            shared = 'shared'
    sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
    #sdb = SimpleDBWRTY(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY, retry_limit = RETRY_LIMIT)
    try:
        if not str(args['parent']) == '0':
            parent = sdb.get_attributes(AWS_SDB_LABELS_DOMAIN, obj_id, [args['parent']])
            if not parent.has_key(args['parent']) or parent[args['parent']] is None:
                return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER,'parent label_id is not passed.')
        label_id = hashlib.sha1(str(args['name']) + str(time.time())).hexdigest()
        if admin and (shared == 'gloabl'):
            owned = 'admin'
        elif admin:
            owned = 'owner'
        else:
            owned = user['uid']
        sdb.put_attributes(AWS_SDB_LABELS_DOMAIN, obj_id, [(label_id, SHARE_DEL + shared, True), 
                                                        (label_id, NAME_DEL + args['name'], True), 
                                                        (label_id, NICK_DEL + args['nick'], True), 
                                                        (label_id, PARENT_DEL + args['parent'], True), 
                                                        (label_id, RULE_DEL + args['rule'], True),
                                                        ('owned', owned, True),
                                                        ('obj_id', obj_id.split(':')[0], True),
                                                        (label_id, COL_DEL + args['color'], True)])
    except:
        raise
        start_response('503 Service Unavailable',[])
        return ['Temporarily not available']
    return json_ok(env, start_response, label_id)
Ejemplo n.º 2
0
def set_exception(env, start_response, args):
    method = env['REQUEST_METHOD']
    #if method != 'GET':
    #    start_response('501 Not Implemented', [])
    #    return ['Unsupported']
    user = _validate_fb(env)
    if user is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_SESSION)
    if ('post_id' not in args) or ('label_id' not in args) or ('excluded' not in args):
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER)
    try:
        from_id, to_id, created_time, updated_time = _get_post_info(args['post_id'], user)
    except:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'post_id is not valid or not accessible.')
    if from_id is None:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'the post does not have a from_id field.')
    try:
        sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
        labels = sdb.select(AWS_SDB_LABELS_DOMAIN, "select `%s` from %s where `%s` is not null" %(args['label_id'], AWS_SDB_LABELS_DOMAIN, args['label_id']))
        if len(labels) == 0:    
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'the label_id is not a valid label or is not public.')
        if labels[0].name.count(':') == 0:
            owner = 'admin'
        else:
            owner = labels[0].name.split(':')[1]
        obj_id = labels[0].name.split(':')[0]
        shared_status, name, nick, parent, rule, color = _decode_label(labels[0].values()[0])
        if str(to_id) != obj_id:
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'the label is not defined for this object.')
        is_admin = _is_admin(to_id, user)
        if (shared_status == 'global') and (str(user['uid']) != str(from_id)) and (not is_admin):
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'only the admin or the author can assign global labels.')
        if (shared_status == 'shared') and (str(user['uid']) != str(from_id)) and (creator_status != str(user['uid'])):
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'only the label creator or the author can assign shared labels.')
        itm = args['label_id'] + ':' + args['post_id']
        if args['excluded'] == '1':
            excluded = '1'
        else:
            excluded = '0'
        if is_admin and (shared_status == 'global'):
            creator = 'admin'
        else:
            creator = str(user['uid'])
        sdb.put_attributes(AWS_SDB_EXCEPTIONS_DOMAIN, itm, [('excluded', excluded, True),
                                                      ('label_id', args['label_id'], True),
                                                      ('post_id', args['post_id'], True),
                                                      ('creator', creator, True)])
        return json_ok(env, start_response, {})
    except:
        raise
        start_response('503 Service Unavailable',[])
        return ['Temporarily not available']
Ejemplo n.º 3
0
    def __init__(self):
        self.state = 'pre'
        # init pygame
        pygame.init()
        pygame.mixer.init(44100, 16, 2, 1024*4)
        pygame.display.set_caption("MR. TRAPEZIUS ")
        

        try:
            self.screen = pygame.display.set_mode((640, 480), 
                    HWSURFACE | SRCALPHA, 32)
        except:
            self.screen = pygame.display.set_mode((640, 480), 
                    SRCALPHA, 32)
        
        try:
            pygame.display.set_icon(pygame.image.load(
                util.file_path("Inky.png")).convert_alpha())
        except:
            # some platfom do not allow change icon after shown
            pass

        # init fonts and music lists
        util.init()

        # init sub states objects
        self.pre = Pre(self.screen)
        self.go = MainGame(self.screen)
        self.level_info = Level(self.screen)
        self.re = Retry(self.screen)
        self.next = NextLevel(self.screen)
        sound.load()
Ejemplo n.º 4
0
def get_labels(env, start_response, args):
    method = env['REQUEST_METHOD']
    #if method != 'GET':
    #    start_response('501 Not Implemented', [])
    #    return ['Unsupported']
    user = _validate_fb(env)
    if user is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_SESSION)
    if 'obj_id' not in args:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'obj_id is not passed.')
    try:
        obj_id = str(int(args['obj_id']))
    except:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'obj_id is not valid.')
    admin = _is_admin(obj_id, user)
    if admin is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_PERMISSION, 'access to the object is denied or the object is not supported.')
    shared = 'global'
    if 'uid' in args:
        try:
            uid = str(int(args['uid']))
            if uid != str(user['uid']):
                shared = 'shared'
            else:
                shared = 'personal'
        except:
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'uid is not valid.')
        obj_id = obj_id + ':' + str(user['uid'])
    else:
        uid = str(user['uid'])
    try:
        sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
        labels = sdb.select(AWS_SDB_LABELS_DOMAIN, "select * from %s where obj_id in ('%s', '%s') and owned in ('%s', '%s', 'admin', 'owner')" %(AWS_SDB_LABELS_DOMAIN, obj_id.split(':')[0], user['uid'], user['uid'], uid))
        easy_labels = {}
        for i in labels:
            for j in i:
                if j not in ['obj_id', 'owned']:
                    shared_status, name, nick, parent, rule, color = _decode_label(i[j])
                    if (i['owned']=='admin') or (i['owned'] == user['uid']) or (shared_status == 'shared') or ((i['owned'] == 'owner') and admin) or (i['obj_id'] == user['uid']):
                        easy_labels[j] = {'parent' : parent, 'name' : name, 'owner' : i['owned'], 'obj_id' : i.name,
                                      'nick': nick, 'shared' : shared_status, 'rule' : rule, 'color' : color}
                        
        return json_ok(env, start_response, easy_labels)
    except:
        raise
        start_response('503 Service Unavailable',[])
        return ['Temporarily not available']
Ejemplo n.º 5
0
def get_exceptions(env, start_response, args):
    method = env['REQUEST_METHOD']
    #if method != 'GET':
    #    start_response('501 Not Implemented', [])
    #    return ['Unsupported']
    user = _validate_fb(env)
    if user is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_SESSION)
    if 'uid' in args:
        try:
            uid = str(int(args['uid']))
        except:
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'not a valid uid.')
    else:
        uid = user['uid']
    if 'label_id' in args:
        sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
        try:
            exceptions = sdb.select(AWS_SDB_EXCEPTIONS_DOMAIN, 'select excluded, post_id from %s where label_id="%s" and creator in ("%s", "%s", "admin")' %(AWS_SDB_EXCEPTIONS_DOMAIN, args['label_id'], user['uid'], uid) )
            post_list = {}
            for i in exceptions:
                post_list[i['post_id']] = i['excluded']
            return json_ok(env, start_response, post_list)
        except:
            raise
            start_response('503 Service Unavailable',[])
            return ['Temporarily not available']
    if 'post_id' not in args:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'post_id or label_id should be passed.')
    sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
    try:
        post_list = {}
        post_id = ''
        for i in args['post_id'].split(','):
            post_id = post_id + ',"%s"' %i
        post_id = post_id.strip(',')
        exceptions = sdb.select(AWS_SDB_EXCEPTIONS_DOMAIN, 'select excluded, post_id, label_id from %s where post_id in (%s) and creator in ("%s", "%s", "admin")' %(AWS_SDB_EXCEPTIONS_DOMAIN, post_id, user['uid'], uid) )
        for i in exceptions:
            post_list[i['post_id']] = {'excluded' : i['excluded'], 'label_id' : i['label_id']}
        return json_ok(env, start_response, post_list)
    except:
        raise
        start_response('503 Service Unavailable',[])
        return ['Temporarily not available']
Ejemplo n.º 6
0
def _get_labels(obj_id, uid, admin, view_uid=None):
    if view_uid is None:
        view_uid = uid
    elif view_uid == uid:
        shared = 'personal'
        obj_id = obj_id + ':' + str(uid)
    else:
        shared = 'shared'
        obj_id = obj_id + ':' + str(uid)
    try:
        sdb = Retry(SimpleDB, RETRY_LIMIT, ['select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
        labels = sdb.select(AWS_SDB_LABELS_DOMAIN, "select * from %s where obj_id in ('%s', '%s') and owned in ('%s', '%s', 'admin', 'owner')" %(AWS_SDB_LABELS_DOMAIN, obj_id.split(':')[0], uid, uid, view_uid))
        easy_labels = {}
        for i in labels:
            for j in i:
                if j not in ['obj_id', 'owned']:
                    shared_status, name, nick, parent, rule, color = _decode_label(i[j])
                    if (i['owned']=='admin') or (i['owned'] == uid) or (shared_status == 'shared') or ((i['owned'] == 'owner') and admin) or (i['obj_id'] == uid):
                        easy_labels[j] = {'parent' : parent, 'name' : name, 'owner' : i['owned'], 'obj_id' : i.name,
                                      'nick': nick, 'shared' : shared_status, 'rule' : rule, 'color' : color}
    except:
        raise
    return easy_labels
Ejemplo n.º 7
0
def update_label(env, start_response, args):
    method = env['REQUEST_METHOD']
    #if method != 'GET':
    #    start_response('501 Not Implemented', [])
    #    return ['Unsupported']
    user = _validate_fb(env)
    if user is None:
        return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_SESSION)
    if 'label_id' not in args:
        return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'label_id not passed.')
    sdb = Retry(SimpleDB, RETRY_LIMIT, ['delete_attributes', 'select', 'put_attributes', 'get_attributes'], AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
    try:
        label = sdb.select(AWS_SDB_LABELS_DOMAIN, 'select owned, `%s` from %s where `%s` in ("A:shared", "A:global", "A:personal")' %(args['label_id'], AWS_SDB_LABELS_DOMAIN, args['label_id']))
        if len(label) == 0:
            return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'label_id does not exist.')
        for i in label:
            owned = i['owned']
            con_obj_id = i.name
            obj_id = i.name.split(':')[0]
            if i.name.count(':') == 0:
                owner = 'admin'
            else:
                owner = i.name.split(':')[1]
            #original_label_value = dict([[args['label_id'],i[args['label_id']]]])
            shared_status, name, nick, parent, rule, color = _decode_label(i[args['label_id']])
            admin = _is_admin(obj_id, user)
            if admin is None:
                return json_error(env, start_response, ERROR_CODES.FACEBOOK_NO_PERMISSION, 'access to the object is denied or the object is not supported.')
            if (not admin) and (owner != str(user['uid'])):
                return json_error(env, start_response, ERROR_CODES.BAD_PARAMTER, 'the label does not belong to the user.')
        if 'name' in args:
             name = args['name']
        if 'nick' in args:
             nick = args['nick']
        if 'rule' in args:
             rule = args['rule']
        if 'color' in args:
             color = args['color']
        shared = shared_status
        obj_type, id = _detect_obj_type(con_obj_id.split(':')[0], user)
        if (obj_type == 'profile') and admin:
            if ('shared' in args) and (str(args['shared']) == '1'):
                shared = 'shared'
            elif ('shared' in args) and (str(args['shared']) == '0'):
                shared = 'personal'
                
        if (obj_type == 'profile') and (not admin):
            if ('shared' in args) and (str(args['shared']) == '1'):
                shared = 'shared'
            elif ('shared' in args) and (str(args['shared']) == '0'):
                shared = 'personal'
        if (obj_type != 'profile') and admin:
            if shared != 'global':
                if ('shared' in args) and (str(args['shared']) == '1'):
                    shared = 'shared'
                elif ('shared' in args) and (str(args['shared']) == '0'):
                    shared = 'personal'
        if (obj_type != 'profile') and (not admin):
            if ('shared' in args) and (str(args['shared']) == '1'):
                shared = 'shared'
            elif ('shared' in args) and (str(args['shared']) == '0'):
                shared = 'personal'

        original_label_value = dict([[args['label_id'], None]])
        #sdb.delete_attributes(AWS_SDB_LABELS_DOMAIN, con_obj_id, original_label_value)
        sdb.put_attributes(AWS_SDB_LABELS_DOMAIN, con_obj_id, [(args['label_id'], SHARE_DEL + shared, True),
                                                        (args['label_id'], NAME_DEL + name, True),
                                                        (args['label_id'], NICK_DEL + nick, True),
                                                        (args['label_id'], PARENT_DEL + parent, True),
                                                        (args['label_id'], RULE_DEL + rule, True),
                                                        ('owned', owned, True),
                                                        ('obj_id', con_obj_id.split(':')[0], True),
                                                        (args['label_id'], COL_DEL + color, True)])
        return json_ok(env, start_response, {})
    except:
        raise
        start_response('503 Service Unavailable',[])
        return ['Temporarily not available']
Ejemplo n.º 8
0
 def test_retry_or_exit_false(self):
     with self.assertRaises(SystemExit) as test:
         Retry.retry_or_exit()
     self.assertEqual(test.exception.code, 0)
Ejemplo n.º 9
0
 def test_retry_or_exit_true(self):
     test = Retry.retry_or_exit()
     self.assertFalse(test)
Ejemplo n.º 10
0
 def test_check_exit(self):
     with self.assertRaises(SystemExit) as test:
         Retry.check_if_exit(False)
     self.assertEqual(test.exception.code, 0)
Ejemplo n.º 11
0
 def test_check_true(self):
     test = Retry.check_if_exit(True)
     self.assertFalse(test)
Ejemplo n.º 12
0
 def test_retry_n(self):
     test = Retry.retry()
     self.assertFalse(test)
Ejemplo n.º 13
0
 def test_retry_y(self):
     test = Retry.retry()
     self.assertTrue(test)
Ejemplo n.º 14
0
    gr.setup()

    while True:
        UpdateDis = UpdateDisplay(word_check, tried)
        # called to show how many letters are in word object
        print(UpdateDis.update_ans_dis())
        print(f"you have tried: {UpdateDis.update_try_dis()}")
        guess = input("guess a letter: ")

        if guess == '':
            print("you need to put an answer\n")
            continue

        elif guess == "quit" or guess == "exit":
            Retry.check_if_exit(False)

        elif guess in tried:
            print(f"{guess} has allready been tried")

        elif len(guess) > 1:
            print("you must imput only one letter\n")
            continue  # jumps to begining of loop for user input

        elif guess in word:
            # holds the index of every instance that the letter is in the word
            letter_index = []
            count = 0  # used to represent the index

            # finds every location that letter is in the word
            for letter in word:
Ejemplo n.º 15
0
class Game(object):
    """main loop, switching from different states"""
    def __init__(self):
        self.state = 'pre'
        # init pygame
        pygame.init()
        pygame.mixer.init(44100, 16, 2, 1024*4)
        pygame.display.set_caption("MR. TRAPEZIUS ")
        

        try:
            self.screen = pygame.display.set_mode((640, 480), 
                    HWSURFACE | SRCALPHA, 32)
        except:
            self.screen = pygame.display.set_mode((640, 480), 
                    SRCALPHA, 32)
        
        try:
            pygame.display.set_icon(pygame.image.load(
                util.file_path("Inky.png")).convert_alpha())
        except:
            # some platfom do not allow change icon after shown
            pass

        # init fonts and music lists
        util.init()

        # init sub states objects
        self.pre = Pre(self.screen)
        self.go = MainGame(self.screen)
        self.level_info = Level(self.screen)
        self.re = Retry(self.screen)
        self.next = NextLevel(self.screen)
        sound.load()

    def loop(self):

        
        while self.state != 'quit':
            
            print self.state
            if self.state == 'pre':
                self.state = self.pre.run()
            elif self.state == 'pretending':
                self.state = self.pre.show_pretend_loading()
            elif self.state.startswith('level'):
                info_mode = int(self.state[-1])
                print self.state, info_mode
                self.state = self.level_info.run(info_mode)
            elif self.state.startswith('game'):
                mode = int(self.state[-1])
                self.state = self.go.run(mode)
            elif self.state.startswith('next'):
                nextlevel = int(self.state[-1])
                self.state = self.next.run(nextlevel)
            elif self.state.startswith('retry'):
                retrylevel = int(self.state[-1])
                self.state = self.re.run(retrylevel)
            # pygame.display.update()

        pygame.quit()
        exit()