Пример #1
0
def repo_items():
    """ 
    get list of items in a repository.
    param required: repo_id: id of repository
    """
    # repo_id = request.args.get('repo_id')
    # repo_name = request.args.get('repo_name')
    client = custom_seafileapi.connect(server,
                                       token=session['seafile_token'],
                                       verify_ssl=False)
    list_repos = client.repos.list_repos(type="mine")
    if len(list_repos) < 1:
        return "You dont have any repository"
    data = []
    repo_name = ''
    for repo in list_repos:
        repo_item = client.repos.get_repo(repo.id)
        #return array of SeafDir objects
        items = repo.get_items(recursive=0, type='d')
        for item in items:
            if item.name == 'FTICR_DATA':
                data.append(item)
                repo_name = repo.name

    return render_template('seafile_client/repo_items.html',
                           data=data,
                           repo_name=repo_name)
Пример #2
0
def list_repositories():
    """
    get list of all repositories in seafile server

    """
    client = custom_seafileapi.connect(server,
                                       token=session['seafile_token'],
                                       verify_ssl=False)
    list_repos = client.repos.list_repos(type="mine")

    if len(list_repos) < 1:
        return "You dont have any repository"
    return render_template('seafile_client/list_repositories.html',
                           data=list_repos)
Пример #3
0
def login():
    """
    Login page
    param required: username: user email
    param required: password
    return login token from seafile server
    """
    if 'seafile_token' in session and session['seafile_token'] != '':
        return redirect(url_for('seafile_client.dir_items'))

    if request.method == 'POST':
        email = request.form.get('email')
        _password = request.form.get('password')
        request_token_url = server + "/api2/auth-token/"

        _data = {"username": email, "password": _password}
        _headers = {
            "Accept-Encoding": "gzip",
            "Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
        }

        response = requests.post(url=request_token_url,
                                 data=_data,
                                 headers=_headers,
                                 verify=False)
        if response.status_code == 200:
            content = json.loads(response.text)
            session["seafile_token"] = content['token']
            session["current_user"] = email

            # get client username
            client = custom_seafileapi.connect(server,
                                               token=session['seafile_token'],
                                               verify_ssl=False)
            client_prof = client.get_user_profile()
            session["username"] = client_prof['name']

            # get user profile here

            # create user temp folder
            tmp_folder_path = os.path.join(seafile_client.root_path,
                                           'static/tmp',
                                           session['current_user'])
            if not os.path.exists(tmp_folder_path):
                os.makedirs(tmp_folder_path)
            return redirect(url_for('seafile_client.dir_items'))
        else:
            flash(message="Login Fail", category="error")
    return render_template('seafile_client/login.html')
Пример #4
0
def download_mscf_file(repo_id, file_full_path, parent_dir):
    """
    Download mscf file to a temporary local directory
    """
    # create temp local dir path
    tmp_folder_path = os.path.join(seafile_client.root_path, 'static/tmp',
                                   session['current_user'])
    if not os.path.exists(tmp_folder_path):
        os.makedirs(tmp_folder_path)
    # if file doesnt exist in seafile server, file_full_path is None, then we create a default mscf file in temp local dir
    if file_full_path == '':
        tmp_file_path = os.path.join(tmp_folder_path, 'new.mscf')
        default_conf_file = os.path.join(seafile_client.root_path, "static",
                                         "files", "process2D.default.mscf")
        with open(tmp_file_path, "w") as f:
            with open(default_conf_file, 'r') as data:
                f.write(data.read())
    # else, mscf file exists in seafile server
    else:
        # connect to seafile server
        client = custom_seafileapi.connect(server,
                                           token=session['seafile_token'],
                                           verify_ssl=False)
        # get repo
        repo = client.repos.get_repo(repo_id)
        # check if temp local folder existed or not. if not, creat temp local folder

        # get mscf file
        file = repo.get_file(file_full_path, parent_dir)
        # get mscf file download link
        download_file_link = file._get_download_link()
        # return download_file_link

        # in dev: fix bug can not connect to host:
        # urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='seafile.fticr-ms.eu', port=443)
        download_path = urlparse(download_file_link).path
        re_download_file_link = server + download_path

        file_name = file.name
        tmp_file_path = os.path.join(tmp_folder_path, file_name)
        file_content = requests.get(url=re_download_file_link,
                                    stream=True,
                                    verify=False)
        # write mscf file into local file
        with open(tmp_file_path, "wb") as f:
            for chunk in file_content.iter_lines():
                f.write(chunk)
                f.write(("\n").encode())
    return tmp_file_path
Пример #5
0
def dir_items():
    """
    get list of .d downstream directories in a FTICR_DATA directory
    """
    # return session['seafile_token']
    client = custom_seafileapi.connect(server,
                                       token=session['seafile_token'],
                                       verify_ssl=False)
    list_repos = client.repos.list_repos(type="mine",
                                         nameContains='My Library')
    if len(list_repos) != 1:
        return "You dont have any repository named'My Library'"

    my_library_repo = list_repos[0]
    repo_id = my_library_repo.id

    dir_name = 'FTICR_DATA'

    # # create FTICR_DATA dir object. It is a upstream directory of My Library, so parent_dir is '/' and we no need to declare in object
    # FTICR_DATA_dir = SeafDir(repo=my_library_repo, name=dir_name, type='dir')
    # # load dir items
    # FTICR_DATA_dir.load_entries(recursive=1, type='d')
    directories = my_library_repo.get_items(type="d", recursive=True)
    data = []
    # for item in FTICR_DATA_dir.entries:
    for item in directories:
        # if item['name'].endswith('.d'):
        if item.name.endswith('.d'):
            # sub_dir = SeafDir(repo=my_library_repo, name=item['name'], type='dir', parent_dir=item['parent_dir'])
            # check if in .d folder has ser file or not
            try:
                # check if sub directory of FTICR_DATA has ser file or not
                # re = my_library_repo.get_file(path=sub_dir.full_path+'ser', parent_dir=sub_dir.full_path)
                re = my_library_repo.get_file(path=item.full_path + 'ser',
                                              parent_dir=item.full_path)
                # if 'FTICR_DATA' in sub_dir.full_path:
                #     data.append(sub_dir)
                data.append(item)
            except:
                # return render_template("errors/general_error.html", message="There is not a project in which has a ser file.")
                pass

    return render_template('seafile_client/dir_items.html',
                           data=data,
                           dir_name=dir_name,
                           repo_id=repo_id)
Пример #6
0
def sub_dir():
    repo_id = request.args.get('repo_id')
    dir_name = request.args.get('dir_name')

    parent_dir = request.args.get('parent_dir')
    dir_fullpath = request.args.get('dir_fullpath')

    # connect to seafile server
    client = custom_seafileapi.connect(server,
                                       token=session['seafile_token'],
                                       verify_ssl=False)
    # get repository data
    repo = client.repos.get_repo(repo_id)

    dir = SeafDir(repo=repo, name=dir_name, type='dir', parent_dir=parent_dir)

    dir.load_entries(recursive=1, type='f')
    data = []
    for item in dir.entries:
        if item['name'].endswith(
                '.mscf') and item['parent_dir'] in dir_fullpath:
            mscf_file = SeafFile(repo=repo,
                                 name=item['name'],
                                 type='file',
                                 parent_dir=item['parent_dir'])
            if dir_name in mscf_file.full_path:
                data.append(mscf_file)
    # for go back button or go back link in edit_mscf page
    # session['prev_url'] = request.referrer
    # return session['prev_url']
    return render_template('seafile_client/sub_dir.html',
                           data=data,
                           dir_name=dir_name,
                           repo_id=repo_id,
                           dir_fullpath=dir_fullpath,
                           job_email=session['current_user'],
                           job_username=session['username'])
Пример #7
0
def load_corresponse_files(repo_id, parent_dir):
    """
    Get .method, ExciteSweep and scan.xml files in a directory
    """
    # create temporary local folder to contain method file. Method local file should be in same folder with mscf file
    tmp_folder_path = os.path.join(seafile_client.root_path, 'static/tmp',
                                   session['current_user'], parent_dir)
    if not os.path.exists(tmp_folder_path):
        os.makedirs(tmp_folder_path)

    # check if .method file has already existed in temp local folder
    met = glob.glob(os.path.join(tmp_folder_path, "*", ".method"))
    exc = glob.glob(os.path.join(tmp_folder_path, "*", "ExciteSweep"))
    scanxml = glob.glob(os.path.join(tmp_folder_path, "*", "scan.xml"))
    corresponse_files = {}
    if len(met) > 1 or len(exc) > 1 or len(scanxml) > 1:
        # if there are more than 1 file, raise exeption
        return render_template(
            "errors/general_error.html",
            message=
            "You have more than 1 apexAcquisition.method or ExciteSweep or scan.xml file in the %s folder, using the first one"
            % parent_dir)
    elif len(met) == 1 and len(exc) == 1 and len(scanxml) == 1:
        corresponse_files["method"] = met
        corresponse_files["excitesweep"] = exc
        corresponse_files["scanxml"] = scanxml
        # if there is just one file, return file path
        return corresponse_files
    else:
        # if there is not a file, then we load it from seafile server
        client = custom_seafileapi.connect(server,
                                           token=session['seafile_token'],
                                           verify_ssl=False)
        repo = client.repos.get_repo(repo_id)
        method_files = repo.get_items(type='f', recursive=1)
        data = []
        # check number of method files in seafile server
        for file in method_files:
            if parent_dir in file.full_path:
                # return (file.full_path, parent_dir, file.path + '\n')

                if (file.name.endswith('.method') or file.name == 'ExciteSweep'
                        or file.name == 'scan.xml'
                    ) and file.type == 'file' and (parent_dir
                                                   in file.full_path):
                    data.append(file)
        # return data
        # if len(data) > 3:
        #     return render_template("errors/400.html", message="You have more than 1 apexAcquisition.method or ExciteSweep or scan.xml file in the %s folder, using the first one"%parent_dir)
        # if len(data) < 3:
        #     return render_template("errors/400.html", message="You don't have any apexAcquisition.method or ExciteSweep or scan.xml file in the %s folder"%parent_dir)
        # else:

        for item in data:
            download_file_link = item._get_download_link()

            # in dev: fix bug can not connect to host:
            # urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='seafile.fticr-ms.eu', port=443)
            download_path = urlparse(download_file_link).path
            re_download_file_link = server + download_path

            file_name = item.name
            tmp_file_path = os.path.join(tmp_folder_path, file_name)
            file_content = requests.get(url=re_download_file_link,
                                        stream=True,
                                        verify=False)
            with open(tmp_file_path, "wb") as f:
                for chunk in file_content.iter_lines():
                    f.write(chunk)
                    f.write(("\n").encode())
            corresponse_files[item.name] = tmp_file_path
            # return path of temporary method file in local folder
        return corresponse_files