def compute_usable_quota(self):
        global credentials_path

        if self.user_id_ == '':
            return 0

        store = credentials_mgr.get_storage(self.user_id_)
        if store == None:
            print(self.user_id_ + ' compute_usable_quota() error : no storage')
            return 0

        try:
            service = credentials_mgr.get_service(store)       

            about = service.about().get().execute()

            total_quota = int(about['quotaBytesTotal'])
            used_quota  = int(about['quotaBytesUsed'])
            return total_quota - used_quota

        except Exception as e:
            print(self.user_id_ + ' compute_usalble_quota() error : ')
            print(e)

            try:
                os.remove(credentials_path + self.get_credentials_name())
                self.set_id_credentials_name('','')
                self.set_recovery_wait_state()
            except Exception as er:
                print(er)
                pass
            return 0
def delete_all():
    
    credentials_files = os.listdir(datas.credentials_path) 

    print(str(len(credentials_files)))
    for cre in credentials_files:
        try:
            id = cre.split('_')[1].split('.')[0]
            store = credentials_mgr.get_storage(id)
            service = credentials_mgr.get_service(store)
            credentials_mgr.delete_all_files(service)
        except Exception as e:
            print(e)
            pass
            

    return 'del all'
def recover():

    while True:

        time.sleep(5)

        lines = []
        with open('./recover_list.txt', 'r') as f:
            lines = f.readlines()

        if len(lines) <= 0:
            continue

        print('recovering start...')

        credentials = lines[0].strip()
        group_name = util.extract_grouping_name(credentials)
        group_alphabet = group_name[:-1]

        group_info = None
        for g in datas.credentials_list:
            if group_alphabet == g.get_group_alphabet():
                group_info = g
                break
        
        origin_cre_info = None
        recover_cre_info = None
        if group_info:
    
            for cre in group_info.credentials_list:
                print(cre.get_credentials_name())
                if cre.get_state() == datas.CredentialsInfo.STATE_USABLE:
                    origin_cre_info = cre
                if cre.get_credentials_name() == credentials:
                    recover_cre_info = cre

        if (origin_cre_info == None) or (recover_cre_info == None):
            continue

        store = credentials_mgr.get_storage(origin_cre_info.get_user_id())
        service_o = credentials_mgr.get_service(store)
        store = credentials_mgr.get_storage(recover_cre_info.get_user_id())
        service_r = credentials_mgr.get_service(store)
        
        all_origin_files = credentials_mgr.retrieve_all_files(service_o)
        recover_files = credentials_mgr.retrieve_all_files(service_r)

        origin_jigsaw_folder_id = ''
        for file in all_origin_files:
            if file['title'] == 'jigsaw':
                origin_jigsaw_folder_id = file['id']
                break

        origin_children_folder_dic = credentials_mgr.get_children_folder(all_origin_files, origin_jigsaw_folder_id)

        recover_jigsaw_folder_info = None        
        for file in recover_files:
            if file['title'] == 'jigsaw':
                recover_jigsaw_folder_info = file
                break
        if recover_jigsaw_folder_info == None:
            recover_jigsaw_folder_info = credentials_mgr.create_public_folder(service_r, 'jigsaw')

        recover_children_folder_dic = {}
        for folder_name in origin_children_folder_dic.values():
            folder_info = credentials_mgr.create_public_folder(service_r, folder_name, recover_jigsaw_folder_info['id'])
            recover_children_folder_dic[folder_info['title']] = folder_info['id']

        for file in all_origin_files:
        
           parent_folder_id = file['parents'][0]['id']
           if parent_folder_id in origin_children_folder_dic:
                folder_name = origin_children_folder_dic[parent_folder_id]
                file_name = folder_name + '_' + file['title']
                file_path_name = './recover_files/'+ file_name
                #download
                credentials_mgr.download_file(service_o, file['id'],file_path_name ) 
                #upload
                credentials_mgr.upload_file(service_r, recover_children_folder_dic[folder_name], file_name, file['mimeType'], file_path_name)

        recover_cre_info.set_usable_state()
        group_info.compute_group_state()        

        lines.pop(0)
        with open('./recover_list.txt', 'w') as f:
            f.write('')
            for str in lines:
                f.write(str)

        git_manager.recover_add()
def credentials_delete():

    try:
        id = request.args.get('id', None)
        if id == None:
            id = 'anyone'

        store  = credentials_mgr.get_storage(id) 
        if store == None:
            return 'credentials not exist'

        credentials = store.get()
        if credentials == None:
            return 'already revoked'
        
        credentials.revoke(httplib2.Http())
    except Exception as e:
        return 'revoke fail : ' + e

    try:
        file_name = store._filename.split('/')[-1]
        group_name = util.extract_grouping_name(file_name)
        group_alphabet = group_name[:-1]
                
        group_info = None
        for g_info in datas.credentials_list:
            if g_info.group_alphabet_ == group_alphabet:
                group_info = g_info
        
        if group_info == None:
            return 'revoked.'

        for cre in group_info.credentials_list:
            if cre.get_group_name() == group_name:
                cre.set_recovery_wait_state()
                cre.set_id_credentials_name('','')

        group_info.compute_group_state()

        end_group_info = datas.credentials_list[-1]
        if group_alphabet == end_group_info.group_alphabet_:
            state = datas.CredentialsInfo.STATE_RECOVERY_WAIT
            for cre in end_group_info.credentials_list:
                if cre.get_state() == datas.CredentialsInfo.STATE_USABLE:
                    state = cre.get_state()
                    break
            if state != datas.CredentialsInfo.STATE_USABLE:

                for cre in end_group_info.credentials_list:
                    try:
                        os.remove(datas.credentials_path + cre.get_credentials_name())
                    except:
                        pass

                datas.credentials_list.pop()

        datas.credential_dic.pop(id)

    except Exception as e:
        print(e)
        return 'revoked..'

    return 'revoked'