def import_project_file(file_name): """Import .asreview project file""" try: # Unzip the project file with zipfile.ZipFile(file_name, "r") as zip_obj: zip_filenames = zip_obj.namelist() # raise error if no ASReview project file if "project.json" not in zip_filenames: raise ValueError("File doesn't contain valid project format.") # extract all files to a temporary folder tmpdir = tempfile.mkdtemp() zip_obj.extractall(path=tmpdir) except zipfile.BadZipFile: raise ValueError("File is not an ASReview file.") try: # Open the project file and check the id. The id needs to be # unique, otherwise it is exended with -copy. import_project = None fp = Path(tmpdir, "project.json") with open(fp, "r+") as f: # load the project info in scope of function import_project = json.load(f) # If the uploaded project already exists, # then overwrite project.json with a copy suffix. while is_project(import_project["id"]): # project update import_project["id"] = f"{import_project['id']}-copy" import_project["name"] = f"{import_project['name']} copy" else: # write to file f.seek(0) json.dump(import_project, f) f.truncate() # location to copy file to fp_copy = get_project_path(import_project["id"]) # Move the project from the temp folder to the projects folder. os.replace(tmpdir, fp_copy) except Exception: # Unknown error. raise ValueError( "Failed to import project " f"'{file_name.filename}'." ) return import_project["id"]
def update_project_info(project_id, project_name=None, project_description=None, project_authors=None): '''Update project info''' project_id_new = re.sub('[^A-Za-z0-9]+', '-', project_name).lower() if not project_id_new and not isinstance(project_id_new, str) \ and len(project_id_new) >= 3: raise ValueError("Project name should be at least 3 characters.") if (project_id != project_id_new) & is_project(project_id_new): raise ValueError("Project name already exists.") try: # read the file with project info with open(get_project_file_path(project_id), "r") as fp: project_info = json.load(fp) project_info["id"] = project_id_new project_info["name"] = project_name project_info["authors"] = project_authors project_info["description"] = project_description # # backwards support <0.10 # if "projectInitReady" not in project_info: # project_info["projectInitReady"] = True # update the file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_info, fp) # rename the folder get_project_path(project_id) \ .rename(Path(asreview_path(), project_id_new)) except Exception as err: raise err return project_info["id"]
def init_project(project_id, project_name=None, project_description=None, project_authors=None): """Initialize the necessary files specific to the web app.""" if not project_id and not isinstance(project_id, str) \ and len(project_id) >= 3: raise ValueError("Project name should be at least 3 characters.") if is_project(project_id): raise ValueError("Project already exists.") try: get_project_path(project_id).mkdir() get_data_path(project_id).mkdir() project_config = { 'version': asreview_version, # todo: Fail without git? 'id': project_id, 'name': project_name, 'description': project_description, 'authors': project_authors, 'created_at_unix': int(time.time()), # project related variables 'projectInitReady': False, 'reviewFinished': False, } # create a file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_config, fp) return project_config except Exception as err: # remove all generated folders and raise error shutil.rmtree(get_project_path()) raise err
def update_project_info(project_id, project_name=None, project_description=None, project_authors=None): '''Update project info''' project_id_new = create_project_id(project_name) if (project_id != project_id_new) & is_project(project_id_new): raise ValueError("Project name already exists.") try: # read the file with project info with open(get_project_file_path(project_id), "r") as fp: project_info = json.load(fp) project_info["id"] = project_id_new project_info["name"] = project_name project_info["authors"] = project_authors project_info["description"] = project_description # # backwards support <0.10 # if "projectInitReady" not in project_info: # project_info["projectInitReady"] = True # update the file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_info, fp) # rename the folder get_project_path(project_id) \ .rename(Path(asreview_path(), project_id_new)) except Exception as err: raise err return project_info["id"]