Skip to content

Shell

check_membership(importer, name, record_id)

A convenience function which checks if the given record ID is a member of the given view. If it isn't, the reason why is printed.

Parameters:

Name Type Description Default
importer DataImporter

a DataImporter instance

required
name str

name of the view

required
record_id Union[str, int]

record ID (can be int or str, we deal with it)

required
Source code in dataimporter/cli/shell.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def check_membership(importer: DataImporter, name: str, record_id: Union[str, int]):
    """
    A convenience function which checks if the given record ID is a member of the given
    view. If it isn't, the reason why is printed.

    :param importer: a DataImporter instance
    :param name: name of the view
    :param record_id: record ID (can be int or str, we deal with it)
    """
    view = importer.get_view(name)
    if view is None:
        console.print('view not found', style='red')
        return
    record = view.store.get_record(str(record_id))
    if record is None:
        console.print('record not found', style='red')
        return
    member_result = view.is_member(record)
    publish_result = view.is_publishable(record)
    if member_result and publish_result:
        console.print(f'{record_id} is a currently published member of {name}')
    elif member_result and not publish_result:
        console.print(
            f'{record_id} is a member of {name}, but is not published due to '
            f'{publish_result.reason}'
        )
    else:
        console.print(
            f"{record_id} is not a member of {name} due to '{member_result.reason}'"
        )

find_unsuitable_records(importer, view_name, skip_deleted=True)

Finds and returns lists of records in a view that are no longer members or no longer match the publishing rules.Effectively a dry run of purge_unsuitable_records.

Parameters:

Name Type Description Default
importer DataImporter

a DataImporter instance

required
view_name str

name of the view

required
skip_deleted bool

whether to skip deleted records when iterating over mongo records (True, default), or return a report on these too (False)

True

Returns:

Type Description
Dict[str, List[Tuple[str, str]]]

a dict

Source code in dataimporter/cli/shell.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def find_unsuitable_records(
    importer: DataImporter, view_name: str, skip_deleted: bool = True
) -> Dict[str, List[Tuple[str, str]]]:
    """
    Finds and returns lists of records in a view that are no longer members or no longer
    match the publishing rules.Effectively a dry run of purge_unsuitable_records.

    :param importer: a DataImporter instance
    :param view_name: name of the view
    :param skip_deleted: whether to skip deleted records when iterating over mongo
        records (True, default), or return a report on these too (False)
    :return: a dict
    """
    non_publishable_records = []
    non_member_records = []
    view = importer.get_view(view_name)
    db = importer.get_database(view_name)
    total = 0
    deleted = 0
    missing_source = 0
    not_member = 0
    not_publishable = 0
    acceptable = 0
    if skip_deleted:
        filter_kwargs = {'filter': {'data._id': {'$exists': True}}}
    else:
        filter_kwargs = {}
    for mongo_record in db.iter_records(**filter_kwargs):
        total += 1
        if mongo_record.is_deleted:
            deleted += 1
            continue
        source_record = view.store.get_record(mongo_record.id)
        if not source_record:
            missing_source += 1
            continue
        is_member = view.is_member(source_record)
        if not is_member:
            not_member += 1
            non_member_records.append([source_record.id, is_member.reason])
            continue
        is_publishable = view.is_publishable(source_record)
        if not is_publishable:
            not_publishable += 1
            non_publishable_records.append([source_record.id, is_publishable.reason])
            continue
        acceptable += 1
    console.print(
        f'total: {total} | deleted: {deleted} | missing: {missing_source} | not '
        f'members: {not_member} | not publishable: {not_publishable} | acceptable: '
        f'{acceptable}'
    )
    return {
        'not_publishable': non_publishable_records,
        'not_member': non_member_records,
    }

print_record_data(importer, name, record_id)

A convenience function for printing record data from the given data store.

Parameters:

Name Type Description Default
importer DataImporter

a DataImporter instance

required
name str

the store to look up the record ID in

required
record_id Union[str, int]

a record ID (can be int or str, we deal with it)

required
Source code in dataimporter/cli/shell.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def print_record_data(importer: DataImporter, name: str, record_id: Union[str, int]):
    """
    A convenience function for printing record data from the given data store.

    :param importer: a DataImporter instance
    :param name: the store to look up the record ID in
    :param record_id: a record ID (can be int or str, we deal with it)
    """
    store = importer.get_store(name)
    if store is None:
        console.print('store not found', style='red')
        return
    record = store.get_record(str(record_id))
    if record is None:
        console.print('record not found', style='red')
        return
    console.print(record.data)

setup_env(importer)

Returns a dict of variables to made available in the maintenance shell.

Parameters:

Name Type Description Default
importer DataImporter

a DataImporter instance

required

Returns:

Type Description
dict

a dict

Source code in dataimporter/cli/shell.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def setup_env(importer: DataImporter) -> dict:
    """
    Returns a dict of variables to made available in the maintenance shell.

    :param importer: a DataImporter instance
    :return: a dict
    """
    return {
        'importer': importer,
        'console': console,
        # some convenience functions for printing data
        'pcd': partial(print_record_data, importer, 'ecatalogue'),
        'ptd': partial(print_record_data, importer, 'etaxonomy'),
        'pmd': partial(print_record_data, importer, 'emultimedia'),
        'pgd': partial(print_record_data, importer, 'gbif'),
        'cm': partial(check_membership, importer),
        'find_unsuitable': partial(find_unsuitable_records, importer),
    }