Skip to content

Commit

Permalink
Merge pull request #7 from nmouhammad/feature/6-add-import-via-regex
Browse files Browse the repository at this point in the history
#6 -  Add import via regex
  • Loading branch information
reckart authored Mar 19, 2024
2 parents 98804ef + 4f53859 commit 9b9f328
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
12 changes: 8 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Options:

- "-u", "--url" INCEpTION instance URL
- "-U", "--user" User name of the previously created user, you will be prompted to enter the password
- "--projects" Names of the zip-files which should be imported
- "--regex" (default=False) Whether to interpret the project name as a regular expression
- "--projects" Names / regular expression of the zip-files which should be imported


list
Expand Down Expand Up @@ -142,7 +143,10 @@ Use inception-cli to easily migrate from WebAnno to INCEpTION
- assign at least the roles ``ROLE_ADMIN``, ``ROLE_USER`` and ``ROLE_REMOTE``
- make sure that the new user is enabled
- click on ``Save``
#. Import all exported projects to INCEpTION using inception-cli (replace WEBANNO_URL and WEBANNO_REMOTE_API_USERNAME with the url of your WebAnno instance and the name of the user created in step 2 and replace PATH_TO_EXPORTED_PROJECT_1 etc. with the paths to each of the zip-files created in step 3)
.. code:: shell
#. Import all exported projects to INCEpTION using inception-cli
- go to the directory containing all exported projects
- make sure the folder does not contain any zip-files which are no exported WebAnno-projects
- import the exported projects (replace WEBANNO_URL and WEBANNO_REMOTE_API_USERNAME with the url of your WebAnno instance and the name of the user created in step 2):
.. code:: shell
$ python inception project export -u INCEPTION_URL -U INCEPTION_REMOTE_API_USERNAME PATH_TO_EXPORTED_PROJECT_1, PATH_TO_EXPORTED_PROJECT_2, PATH_TO_EXPORTED_PROJECT_3
$ python inception project export -u INCEPTION_URL -U INCEPTION_REMOTE_API_USERNAME --regex *.zip
30 changes: 25 additions & 5 deletions inception/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@

import click

from inception.utils import make_client, list_matching_projects, get_project_from_name
from inception.utils import make_client, list_matching_projects, get_project_from_name, list_matching_zip_files


class FileIfNotRegex(click.ParamType):

def convert(self, value, param, ctx):
if ctx.params["regex"]:
return click.STRING.convert(value, param, ctx)
else:
return click.File("rb")


_log = logging.getLogger("inception")

Expand Down Expand Up @@ -47,15 +57,25 @@ def export_projects(url: str, user: Optional[str], regex: bool, dry_run: bool, o
@click.command(name="import")
@click.option("-u", "--url", help="INCEpTION instance URL")
@click.option("-U", "--user", help="User name")
@click.argument("projects", type=click.File("rb"), nargs=-1)
def import_projects(url: str, user: Optional[str], projects: List[str]):
@click.option(
"--regex", is_flag=True, default=False, help="Whether to interpret the project name as a regular expression"
)
@click.argument("projects", type=FileIfNotRegex(), nargs=-1)
def import_projects(url: str, user: Optional[str], regex: bool, projects: List[str]):
"""Imports the given projects."""

client = make_client(url, user=user)

if regex:
projects = list_matching_zip_files(projects)

for project in projects:
_log.info("Importing project [%s] ...", project.name)
imported_project = client.api.import_project(project)
_log.info("Importing project [%s] ...", project if regex else project.name)
if regex:
with open(project, "rb") as project_file:
imported_project = client.api.import_project(project_file)
else:
imported_project = client.api.import_project(project)
_log.info("Imported project as [%s]", imported_project.project_name)


Expand Down
13 changes: 13 additions & 0 deletions inception/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
import re
from typing import Optional, List

Expand Down Expand Up @@ -42,3 +43,15 @@ def get_project_from_name(client: Pycaprio, name: str) -> List[str]:
projects.append(accessible_project)

return projects


def list_matching_zip_files(patterns: List[str]) -> List[str]:
"""
Scans the filesystem for any projects matching the given patterns and returns their names.
"""
projects = []
for pattern in patterns:
projects += glob.glob(pattern)

projects = [filepath for filepath in projects if filepath.endswith(".zip")]
return projects

0 comments on commit 9b9f328

Please sign in to comment.