pyprocessors-pseudonimizer 0.6.23__tar.gz → 0.7.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/.gitignore +2 -11
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/Dockerfile +5 -5
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/Jenkinsfile +31 -89
- pyprocessors_pseudonimizer-0.7.2/PKG-INFO +67 -0
- pyprocessors_pseudonimizer-0.7.2/README.md +23 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/pyprocessors_pseudonimizer/__init__.py +1 -2
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/pyprocessors_pseudonimizer/fakerop.py +2 -2
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/pyprocessors_pseudonimizer/pseudonimizer.py +54 -55
- pyprocessors_pseudonimizer-0.7.2/pyproject.toml +84 -0
- pyprocessors_pseudonimizer-0.7.2/setup.py +49 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/data/afp_ner_fr-document-test-anon.json +56 -56
- pyprocessors_pseudonimizer-0.7.2/tests/data/terrorisme_ner_v2-document-test-anon.json +204 -0
- pyprocessors_pseudonimizer-0.7.2/tests/test_pseudonimizer.py +97 -0
- pyprocessors_pseudonimizer-0.7.2/tox.ini +51 -0
- pyprocessors_pseudonimizer-0.6.23/.python-version +0 -1
- pyprocessors_pseudonimizer-0.6.23/MIGRATION.md +0 -170
- pyprocessors_pseudonimizer-0.6.23/PKG-INFO +0 -126
- pyprocessors_pseudonimizer-0.6.23/README.md +0 -85
- pyprocessors_pseudonimizer-0.6.23/pyproject.toml +0 -77
- pyprocessors_pseudonimizer-0.6.23/tests/data/terrorisme_ner_v2-document-test-anon.json +0 -204
- pyprocessors_pseudonimizer-0.6.23/tests/test_pseudonimizer.py +0 -193
- pyprocessors_pseudonimizer-0.6.23/trivy-html-template.tpl +0 -148
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/.dockerignore +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/bumpversion.py +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/__init__.py +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/data/afp_ner_fr-document-test.json +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/data/mindspeak_demo-document-test-anon.json +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/data/mindspeak_demo-document-test.json +0 -0
- {pyprocessors_pseudonimizer-0.6.23 → pyprocessors_pseudonimizer-0.7.2}/tests/data/terrorisme_ner_v2-document-test.json +0 -0
|
@@ -13,17 +13,8 @@ log
|
|
|
13
13
|
**/.passwd-pypi
|
|
14
14
|
**/.env
|
|
15
15
|
**/results.xml
|
|
16
|
-
|
|
17
|
-
# Unit test / coverage reports
|
|
18
|
-
.tox/
|
|
19
|
-
|
|
20
|
-
# Specific
|
|
21
|
-
.idea/
|
|
22
16
|
.groovylintrc.json
|
|
23
17
|
.emailNotif
|
|
24
|
-
uv.lock
|
|
25
18
|
|
|
26
|
-
#
|
|
27
|
-
|
|
28
|
-
**/trivy*.html
|
|
29
|
-
**/audit*.json
|
|
19
|
+
# Unit test / coverage reports
|
|
20
|
+
.tox/
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
FROM python:3.
|
|
2
|
-
|
|
3
|
-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
|
|
4
|
-
|
|
1
|
+
FROM python:3.8-slim-bookworm
|
|
5
2
|
# Install prerequisites
|
|
6
3
|
RUN apt-get update -y && \
|
|
7
4
|
apt-get install -y \
|
|
@@ -14,6 +11,9 @@ RUN apt-get update -y && \
|
|
|
14
11
|
apt-get update -y && \
|
|
15
12
|
apt-get clean all -y
|
|
16
13
|
|
|
17
|
-
#
|
|
14
|
+
# Enable Installing packages as root
|
|
15
|
+
ENV FLIT_ROOT_INSTALL=1
|
|
16
|
+
|
|
17
|
+
# Add pyproject.toml + README.md for flit install
|
|
18
18
|
ADD pyproject.toml pyproject.toml
|
|
19
19
|
ADD README.md README.md
|
|
@@ -6,6 +6,14 @@ pipeline {
|
|
|
6
6
|
|
|
7
7
|
agent none
|
|
8
8
|
|
|
9
|
+
options {
|
|
10
|
+
throttleJobProperty(
|
|
11
|
+
categories: ['pyprocessors'],
|
|
12
|
+
throttleEnabled: true,
|
|
13
|
+
throttleOption: 'category'
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
triggers {
|
|
10
18
|
upstream(upstreamProjects: 'pymultirole_plugins/' + BRANCH_NAME.replaceAll('/', '%2F'),\
|
|
11
19
|
threshold: hudson.model.Result.SUCCESS)
|
|
@@ -19,15 +27,15 @@ pipeline {
|
|
|
19
27
|
PYTHONDONTWRITEBYTECODE = '1'
|
|
20
28
|
JENKINS_UIDGID = '1004:1004'
|
|
21
29
|
|
|
22
|
-
MAJOR_VERSION =
|
|
23
|
-
MINOR_VERSION =
|
|
30
|
+
MAJOR_VERSION = "${MAJOR_VERSION_PY38}"
|
|
31
|
+
MINOR_VERSION = "${MINOR_VERSION_PY38_HOTFIX}"
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
stages {
|
|
27
35
|
stage('Catch build termination') {
|
|
28
36
|
agent {
|
|
29
37
|
node {
|
|
30
|
-
label '
|
|
38
|
+
label 'pre-build'
|
|
31
39
|
customWorkspace "${PATH_HOME}/${JOB_NAME}"
|
|
32
40
|
}
|
|
33
41
|
}
|
|
@@ -58,7 +66,7 @@ pipeline {
|
|
|
58
66
|
stage('Add credentials') {
|
|
59
67
|
steps {
|
|
60
68
|
script {
|
|
61
|
-
// Add password file for
|
|
69
|
+
// Add password file for flit publishing
|
|
62
70
|
sh "cp ${PATH_HOME}/.passwd-pypi .env"
|
|
63
71
|
}
|
|
64
72
|
}
|
|
@@ -73,7 +81,6 @@ pipeline {
|
|
|
73
81
|
withCredentials([gitUsernamePassword(credentialsId: 'bitbucket-user', gitToolName: 'git-tool')]) {
|
|
74
82
|
sh 'git pull'
|
|
75
83
|
sh "echo '\"\"\"Processor based on Presidio anonymizer\"\"\"' > pyprocessors_pseudonimizer/__init__.py"
|
|
76
|
-
sh "echo '' >> pyprocessors_pseudonimizer/__init__.py"
|
|
77
84
|
sh "echo '__version__ = \"${MAJOR_VERSION}.${MINOR_VERSION}.${BUILD_ID}\"' >> pyprocessors_pseudonimizer/__init__.py"
|
|
78
85
|
sh 'git commit pyprocessors_pseudonimizer/__init__.py -m "[Jenkins CI] Commit on version files" || echo "No changes to commit"'
|
|
79
86
|
sh 'git push'
|
|
@@ -96,7 +103,7 @@ pipeline {
|
|
|
96
103
|
// - docker: /root/test-reports
|
|
97
104
|
// - host : /tmp/_${JOB_NAME}/test-reports
|
|
98
105
|
dockerfile {
|
|
99
|
-
label '
|
|
106
|
+
label 'docker-build'
|
|
100
107
|
customWorkspace "${PATH_HOME}/${JOB_NAME}"
|
|
101
108
|
filename 'Dockerfile'
|
|
102
109
|
args "-u root --privileged -v /tmp/_${JOB_NAME}/test-reports:${TEST_REPORT_DIR}"
|
|
@@ -104,11 +111,13 @@ pipeline {
|
|
|
104
111
|
}
|
|
105
112
|
|
|
106
113
|
stages {
|
|
107
|
-
stage('Install
|
|
114
|
+
stage('Install flit & flake8') {
|
|
108
115
|
steps {
|
|
109
|
-
|
|
110
|
-
sh '
|
|
111
|
-
sh '
|
|
116
|
+
// remove any previous tox env
|
|
117
|
+
sh 'rm -rf .tox'
|
|
118
|
+
sh 'python -m pip install pip==22.0.3'
|
|
119
|
+
sh 'pip install --no-cache-dir flit==3.2.0 flake8==3.9.2 flakehell tox'
|
|
120
|
+
sh 'flit install'
|
|
112
121
|
}
|
|
113
122
|
}
|
|
114
123
|
|
|
@@ -116,20 +125,20 @@ pipeline {
|
|
|
116
125
|
steps {
|
|
117
126
|
// remove any previous results.xml file
|
|
118
127
|
sh "rm -f ${TEST_REPORT_DIR}/results.xml"
|
|
119
|
-
sh '
|
|
120
|
-
sh 'uv run ruff format --check .'
|
|
121
|
-
sh "uv run pytest --junit-xml=${TEST_REPORT_DIR}/results.xml"
|
|
128
|
+
sh 'tox'
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
131
|
|
|
125
132
|
stage('Publish on PyPI') {
|
|
126
133
|
environment {
|
|
127
|
-
|
|
128
|
-
|
|
134
|
+
FLIT_USERNAME = getUserName '.env'
|
|
135
|
+
FLIT_PASSWORD = getUserPass '.env'
|
|
129
136
|
}
|
|
130
137
|
steps {
|
|
131
138
|
// remove any previous folder dist
|
|
132
139
|
sh 'rm -rf dist'
|
|
140
|
+
// create (as root) folder dist
|
|
141
|
+
sh 'mkdir dist'
|
|
133
142
|
// pull recent updates of file __init__.py
|
|
134
143
|
withCredentials([gitUsernamePassword(credentialsId: 'bitbucket-user', gitToolName: 'git-tool')]) {
|
|
135
144
|
sh 'git config --global pull.rebase false'
|
|
@@ -142,85 +151,18 @@ pipeline {
|
|
|
142
151
|
sh "chown ${JENKINS_UIDGID} pyprocessors_pseudonimizer/__init__.py"
|
|
143
152
|
// get git status
|
|
144
153
|
sh 'git status'
|
|
145
|
-
//
|
|
154
|
+
// publish on PyPI
|
|
146
155
|
sh '''
|
|
147
156
|
export COMMIT_VERSION=$( cat pyprocessors_pseudonimizer/__init__.py|grep version|cut -d '"' -f2|tr -s '[:blank:]' )
|
|
148
157
|
export BUILD_VERSION="${MAJOR_VERSION}"."${MINOR_VERSION}"."${BUILD_ID}"
|
|
149
|
-
if [ "${COMMIT_VERSION}" = "${BUILD_VERSION}" ] ; then
|
|
158
|
+
if [ "${COMMIT_VERSION}" = "${BUILD_VERSION}" ] ; then flit publish ; fi
|
|
150
159
|
'''
|
|
151
160
|
// remove current folder dist
|
|
152
161
|
sh 'rm -rf dist'
|
|
153
162
|
// remove current folder .hypothesis
|
|
154
163
|
sh 'rm -rf .hypothesis'
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
stage('Prepare SBOM') {
|
|
161
|
-
when {
|
|
162
|
-
beforeAgent true
|
|
163
|
-
environment name: 'SKIP_JOB', value: '0'
|
|
164
|
-
}
|
|
165
|
-
options {
|
|
166
|
-
timeout(time: 120, unit: 'SECONDS')
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
agent {
|
|
170
|
-
docker {
|
|
171
|
-
image 'alpine:3.19'
|
|
172
|
-
label 'built-in'
|
|
173
|
-
customWorkspace "${PATH_HOME}/${JOB_NAME}"
|
|
174
|
-
args "-u 0"
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
stages {
|
|
179
|
-
stage('Generate SBOM') {
|
|
180
|
-
steps {
|
|
181
|
-
sh '''
|
|
182
|
-
apk add --no-cache curl
|
|
183
|
-
|
|
184
|
-
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b . v${SYFT_VERSION}
|
|
185
|
-
|
|
186
|
-
./syft . \
|
|
187
|
-
--exclude '**/syft' \
|
|
188
|
-
--exclude '**/.pytest_cache' \
|
|
189
|
-
--exclude '**/.ruff_cache' \
|
|
190
|
-
--exclude '**/sbom.cdx.json' \
|
|
191
|
-
--exclude '**/sbom.spdx.json' \
|
|
192
|
-
--source-name "${JOB_NAME}" \
|
|
193
|
-
--source-version "${BUILD_NUMBER}" \
|
|
194
|
-
-o cyclonedx-json=sbom.cdx.json \
|
|
195
|
-
-o spdx-json=sbom.spdx.json
|
|
196
|
-
|
|
197
|
-
rm -f ./syft
|
|
198
|
-
'''
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
stage('Analyse SBOM') {
|
|
203
|
-
steps {
|
|
204
|
-
sh '''
|
|
205
|
-
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b . v${TRIVY_VERSION}
|
|
206
|
-
curl -L https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/html.tpl -o trivy-html-template.tpl
|
|
207
|
-
|
|
208
|
-
./trivy sbom --download-db-only
|
|
209
|
-
|
|
210
|
-
./trivy sbom sbom.cdx.json \
|
|
211
|
-
--skip-db-update \
|
|
212
|
-
--format template \
|
|
213
|
-
--template "@trivy-html-template.tpl" \
|
|
214
|
-
-o trivy-report.html
|
|
215
|
-
|
|
216
|
-
rm -f ./trivy
|
|
217
|
-
'''
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
stage('Archive SBOM & analysis') {
|
|
222
|
-
steps {
|
|
223
|
-
archiveArtifacts artifacts: 'sbom*.json, trivy*.html', fingerprint: true
|
|
164
|
+
// remove current folder .tox
|
|
165
|
+
sh 'rm -rf .tox'
|
|
224
166
|
}
|
|
225
167
|
}
|
|
226
168
|
}
|
|
@@ -305,7 +247,7 @@ pipeline {
|
|
|
305
247
|
}
|
|
306
248
|
}
|
|
307
249
|
|
|
308
|
-
// return
|
|
250
|
+
// return FLIT_USERNAME from given file
|
|
309
251
|
def getUserName(path) {
|
|
310
252
|
def USERNAME = sh(
|
|
311
253
|
script: "grep FLIT_USERNAME ${path}|cut -d '=' -f2",
|
|
@@ -314,7 +256,7 @@ def getUserName(path) {
|
|
|
314
256
|
return USERNAME
|
|
315
257
|
}
|
|
316
258
|
|
|
317
|
-
// return
|
|
259
|
+
// return FLIT_PASSWORD from given file
|
|
318
260
|
def getUserPass(path) {
|
|
319
261
|
def USERPASS = sh(
|
|
320
262
|
script: "grep FLIT_PASSWORD ${path}|cut -d '=' -f2",
|
|
@@ -475,6 +417,6 @@ def analyseBuildCause() {
|
|
|
475
417
|
println 'Skipping build because last commit has been done by CI'
|
|
476
418
|
env.SKIP_JOB = '1'
|
|
477
419
|
switchEmailNotif(false, 0)
|
|
478
|
-
currentBuild.result = 'NOT_BUILT'
|
|
420
|
+
//currentBuild.result = 'NOT_BUILT'
|
|
479
421
|
}
|
|
480
422
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyprocessors-pseudonimizer
|
|
3
|
+
Version: 0.7.2
|
|
4
|
+
Summary: Processor based on Presidio anonymizer
|
|
5
|
+
Home-page: https://kairntech.com/
|
|
6
|
+
Author: Olivier Terrier
|
|
7
|
+
Author-email: olivier.terrier@kairntech.com
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Classifier: Intended Audience :: Information Technology
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
17
|
+
Classifier: Topic :: Software Development
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Development Status :: 4 - Beta
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Requires-Dist: pymultirole-plugins>=0.7.0,<0.8.0
|
|
22
|
+
Requires-Dist: presidio_anonymizer>=2.2.29
|
|
23
|
+
Requires-Dist: log-with-context
|
|
24
|
+
Requires-Dist: Faker
|
|
25
|
+
Requires-Dist: flit ; extra == "dev"
|
|
26
|
+
Requires-Dist: pre-commit ; extra == "dev"
|
|
27
|
+
Requires-Dist: bump2version ; extra == "dev"
|
|
28
|
+
Requires-Dist: sphinx ; extra == "docs"
|
|
29
|
+
Requires-Dist: sphinx-rtd-theme ; extra == "docs"
|
|
30
|
+
Requires-Dist: m2r2 ; extra == "docs"
|
|
31
|
+
Requires-Dist: sphinxcontrib.apidoc ; extra == "docs"
|
|
32
|
+
Requires-Dist: jupyter_sphinx ; extra == "docs"
|
|
33
|
+
Requires-Dist: pytest ; extra == "test"
|
|
34
|
+
Requires-Dist: pytest-cov ; extra == "test"
|
|
35
|
+
Requires-Dist: pytest-flake8 ; extra == "test"
|
|
36
|
+
Requires-Dist: pytest-black ; extra == "test"
|
|
37
|
+
Requires-Dist: flake8==3.9.2 ; extra == "test"
|
|
38
|
+
Requires-Dist: tox ; extra == "test"
|
|
39
|
+
Requires-Dist: dirty-equals ; extra == "test"
|
|
40
|
+
Provides-Extra: dev
|
|
41
|
+
Provides-Extra: docs
|
|
42
|
+
Provides-Extra: test
|
|
43
|
+
|
|
44
|
+
## Requirements
|
|
45
|
+
|
|
46
|
+
- Python 3.8+
|
|
47
|
+
- Flit to put Python packages and modules on PyPI
|
|
48
|
+
- Pydantic for the data parts.
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
```
|
|
52
|
+
pip install flit
|
|
53
|
+
pip install pymultirole-plugins
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Publish the Python Package to PyPI
|
|
57
|
+
- Increment the version of your package in the `__init__.py` file:
|
|
58
|
+
```
|
|
59
|
+
"""An amazing package!"""
|
|
60
|
+
|
|
61
|
+
__version__ = 'x.y.z'
|
|
62
|
+
```
|
|
63
|
+
- Publish
|
|
64
|
+
```
|
|
65
|
+
flit publish
|
|
66
|
+
```
|
|
67
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## Requirements
|
|
2
|
+
|
|
3
|
+
- Python 3.8+
|
|
4
|
+
- Flit to put Python packages and modules on PyPI
|
|
5
|
+
- Pydantic for the data parts.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
```
|
|
9
|
+
pip install flit
|
|
10
|
+
pip install pymultirole-plugins
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Publish the Python Package to PyPI
|
|
14
|
+
- Increment the version of your package in the `__init__.py` file:
|
|
15
|
+
```
|
|
16
|
+
"""An amazing package!"""
|
|
17
|
+
|
|
18
|
+
__version__ = 'x.y.z'
|
|
19
|
+
```
|
|
20
|
+
- Publish
|
|
21
|
+
```
|
|
22
|
+
flit publish
|
|
23
|
+
```
|
|
@@ -34,11 +34,11 @@ class FakerOp:
|
|
|
34
34
|
"""
|
|
35
35
|
|
|
36
36
|
def __init__(self, provider, **kwargs):
|
|
37
|
-
self.locale = kwargs.pop(
|
|
37
|
+
self.locale = kwargs.pop('locale', None)
|
|
38
38
|
self.provider = provider
|
|
39
39
|
self.extra = kwargs
|
|
40
40
|
|
|
41
|
-
def evaluate(self, val):
|
|
41
|
+
def evaluate(self, val):
|
|
42
42
|
subfaker = self._get_faker(self.locale)
|
|
43
43
|
result = subfaker.format(self.provider, **self.extra)
|
|
44
44
|
return str(result)
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import copy
|
|
2
2
|
import json
|
|
3
|
-
from functools import
|
|
3
|
+
from functools import lru_cache
|
|
4
4
|
from logging import Logger
|
|
5
|
-
from typing import cast
|
|
5
|
+
from typing import List, Type, cast, Dict
|
|
6
6
|
|
|
7
7
|
from log_with_context import add_logging_context
|
|
8
8
|
from presidio_anonymizer import AnonymizerEngine
|
|
9
|
-
from presidio_anonymizer.entities import
|
|
10
|
-
from pydantic import
|
|
11
|
-
from pymultirole_plugins.v1.processor import
|
|
12
|
-
from pymultirole_plugins.v1.schema import
|
|
9
|
+
from presidio_anonymizer.entities import RecognizerResult, OperatorConfig
|
|
10
|
+
from pydantic import Field, BaseModel
|
|
11
|
+
from pymultirole_plugins.v1.processor import ProcessorParameters, ProcessorBase
|
|
12
|
+
from pymultirole_plugins.v1.schema import Document, AltText
|
|
13
13
|
|
|
14
14
|
from .fakerop import FakerOp
|
|
15
15
|
|
|
@@ -23,57 +23,52 @@ OPERATOR_MASK = {
|
|
|
23
23
|
}
|
|
24
24
|
OPERATOR_MASK_STR = json.dumps(OPERATOR_MASK, indent=2)
|
|
25
25
|
|
|
26
|
-
OPERATOR_REPLACE = {
|
|
26
|
+
OPERATOR_REPLACE = {
|
|
27
|
+
"type": "replace",
|
|
28
|
+
"new_value": "_ANONYMIZED_"
|
|
29
|
+
}
|
|
27
30
|
OPERATOR_REPLACE_STR = json.dumps(OPERATOR_REPLACE, indent=2)
|
|
28
31
|
|
|
29
|
-
OPERATOR_REDACT = {
|
|
32
|
+
OPERATOR_REDACT = {
|
|
33
|
+
"type": "redact"
|
|
34
|
+
}
|
|
30
35
|
OPERATOR_REDACT_STR = json.dumps(OPERATOR_REDACT, indent=2)
|
|
31
36
|
|
|
32
|
-
OPERATOR_LABEL = {
|
|
37
|
+
OPERATOR_LABEL = {
|
|
38
|
+
"type": "label"
|
|
39
|
+
}
|
|
33
40
|
OPERATOR_LABEL_STR = json.dumps(OPERATOR_LABEL, indent=2)
|
|
34
41
|
|
|
35
|
-
OPERATOR_IDENTITY = {
|
|
42
|
+
OPERATOR_IDENTITY = {
|
|
43
|
+
"type": "identity"
|
|
44
|
+
}
|
|
36
45
|
OPERATOR_IDENTITY_STR = json.dumps(OPERATOR_IDENTITY, indent=2)
|
|
37
46
|
|
|
38
47
|
CUSTOM_OPERATORS = {
|
|
39
48
|
"identity": lambda x: x,
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
OPERATOR_FAKER = {
|
|
51
|
+
OPERATOR_FAKER = {
|
|
52
|
+
"type": "faker",
|
|
53
|
+
"provider": "name",
|
|
54
|
+
"locale": "fr_FR"
|
|
55
|
+
}
|
|
43
56
|
OPERATOR_FAKER_STR = json.dumps(OPERATOR_FAKER, indent=2)
|
|
44
57
|
|
|
45
58
|
|
|
46
59
|
class PseudonimizerParameters(ProcessorParameters):
|
|
47
|
-
mapping:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
+ "```"
|
|
60
|
-
+ "<li>identity: leaves the annotation as it is. ```"
|
|
61
|
-
+ OPERATOR_IDENTITY_STR
|
|
62
|
-
+ "```"
|
|
63
|
-
+ "<li>label: replaces the annotation with its label. ```"
|
|
64
|
-
+ OPERATOR_LABEL_STR
|
|
65
|
-
+ "```"
|
|
66
|
-
+ "<li>faker: replaces the annotation by a fake value generated by a [faker provider](https://faker.readthedocs.io/en/master/providers.html). ```" # noqa: E501
|
|
67
|
-
+ OPERATOR_FAKER_STR
|
|
68
|
-
+ "```" # noqa: E501
|
|
69
|
-
),
|
|
70
|
-
json_schema_extra={"extra": "key:label,val:json"},
|
|
71
|
-
)
|
|
72
|
-
default_operator: str = Field(
|
|
73
|
-
OPERATOR_LABEL_STR,
|
|
74
|
-
description="""Default anonymization/pseudonymization operator to use if no explicit mapping is provided""",
|
|
75
|
-
json_schema_extra={"extra": "json"},
|
|
76
|
-
)
|
|
60
|
+
mapping: Dict[str, str] = Field(None,
|
|
61
|
+
description="""List of anonymization/pseudonymization [operators](https://microsoft.github.io/presidio/tutorial/10_simple_anonymization/) like this ones:<br/>"""
|
|
62
|
+
+ "<li>mask: replaces the annotation with a sequence of a given character. ```" + OPERATOR_MASK_STR + "```"
|
|
63
|
+
+ "<li>replace: replaces the annotation with desired value. ```" + OPERATOR_REPLACE_STR + "```"
|
|
64
|
+
+ "<li>redact: redact: removes the annotation completely from text. ```" + OPERATOR_REDACT_STR + "```"
|
|
65
|
+
+ "<li>identity: leaves the annotation as it is. ```" + OPERATOR_IDENTITY_STR + "```"
|
|
66
|
+
+ "<li>label: replaces the annotation with its label. ```" + OPERATOR_LABEL_STR + "```"
|
|
67
|
+
+ "<li>faker: replaces the annotation by a fake value generated by a [faker provider](https://faker.readthedocs.io/en/master/providers.html). ```" + OPERATOR_FAKER_STR + "```",
|
|
68
|
+
extra="key:label,val:json")
|
|
69
|
+
default_operator: str = Field(OPERATOR_LABEL_STR,
|
|
70
|
+
description="""Default anonymization/pseudonymization operator to use if no explicit mapping is provided""",
|
|
71
|
+
extra="json")
|
|
77
72
|
as_altText: str = Field(
|
|
78
73
|
"pseudonimization",
|
|
79
74
|
description="""If defined generate the pseudonimization as an alternative text of the input document,
|
|
@@ -94,11 +89,12 @@ def operator_from_annotation(a, definitions):
|
|
|
94
89
|
|
|
95
90
|
def operator_from_definition(a, definition):
|
|
96
91
|
defi = copy.deepcopy(definition)
|
|
97
|
-
if
|
|
98
|
-
type = defi[
|
|
99
|
-
if type ==
|
|
92
|
+
if 'type' in defi:
|
|
93
|
+
type = defi['type']
|
|
94
|
+
if type == 'label':
|
|
100
95
|
replacement = f"<{a.label or a.labelName}>"
|
|
101
|
-
return OperatorConfig("replace",
|
|
96
|
+
return OperatorConfig("replace",
|
|
97
|
+
{"new_value": replacement})
|
|
102
98
|
elif type == "faker":
|
|
103
99
|
defi.pop("type")
|
|
104
100
|
provider = defi.pop("provider")
|
|
@@ -114,13 +110,15 @@ def operator_from_definition(a, definition):
|
|
|
114
110
|
class PseudonimizerProcessor(ProcessorBase):
|
|
115
111
|
__doc__ = """Pseudonimizer based on [Presidio](https://microsoft.github.io/presidio/anonymizer/)."""
|
|
116
112
|
|
|
117
|
-
def process(
|
|
113
|
+
def process(
|
|
114
|
+
self, documents: List[Document], parameters: ProcessorParameters
|
|
115
|
+
) -> List[Document]:
|
|
118
116
|
params: PseudonimizerParameters = cast(PseudonimizerParameters, parameters)
|
|
119
117
|
mapping = frozenset(params.mapping.items()) if params.mapping else None
|
|
120
118
|
definitions = get_definitions(mapping, params.default_operator)
|
|
121
119
|
# logger.warning(f"Anonymizing with: {str(definitions)}")
|
|
122
120
|
engine = get_engine()
|
|
123
|
-
operators:
|
|
121
|
+
operators: Dict[str, OperatorConfig] = {}
|
|
124
122
|
try:
|
|
125
123
|
for document in documents:
|
|
126
124
|
with add_logging_context(docid=document.identifier):
|
|
@@ -130,10 +128,7 @@ class PseudonimizerProcessor(ProcessorBase):
|
|
|
130
128
|
for a in document.annotations:
|
|
131
129
|
if a.status != "KO":
|
|
132
130
|
analyzer_results.append(
|
|
133
|
-
RecognizerResult(
|
|
134
|
-
entity_type=a.labelName, start=a.start, end=a.end, score=a.score or 1.0
|
|
135
|
-
)
|
|
136
|
-
)
|
|
131
|
+
RecognizerResult(entity_type=a.labelName, start=a.start, end=a.end, score=a.score or 1.0))
|
|
137
132
|
if a.labelName not in operators:
|
|
138
133
|
op = operator_from_annotation(a, definitions)
|
|
139
134
|
if op is not None:
|
|
@@ -147,7 +142,11 @@ class PseudonimizerProcessor(ProcessorBase):
|
|
|
147
142
|
if params.as_altText is not None and len(params.as_altText.strip()):
|
|
148
143
|
as_altText = params.as_altText.strip()
|
|
149
144
|
document.altTexts = document.altTexts or []
|
|
150
|
-
altTexts = [
|
|
145
|
+
altTexts = [
|
|
146
|
+
alt
|
|
147
|
+
for alt in document.altTexts
|
|
148
|
+
if alt.name != as_altText
|
|
149
|
+
]
|
|
151
150
|
altTexts.append(AltText(name=as_altText, text=result_text))
|
|
152
151
|
document.altTexts = altTexts
|
|
153
152
|
elif document.text != result_text:
|
|
@@ -159,16 +158,16 @@ class PseudonimizerProcessor(ProcessorBase):
|
|
|
159
158
|
return documents
|
|
160
159
|
|
|
161
160
|
@classmethod
|
|
162
|
-
def get_model(cls) ->
|
|
161
|
+
def get_model(cls) -> Type[BaseModel]:
|
|
163
162
|
return PseudonimizerParameters
|
|
164
163
|
|
|
165
164
|
|
|
166
|
-
@
|
|
165
|
+
@lru_cache(maxsize=None)
|
|
167
166
|
def get_engine():
|
|
168
167
|
return AnonymizerEngine()
|
|
169
168
|
|
|
170
169
|
|
|
171
|
-
@
|
|
170
|
+
@lru_cache(maxsize=None)
|
|
172
171
|
def get_definitions(mapping_items, default):
|
|
173
172
|
definitions = {None: None}
|
|
174
173
|
if default is not None and default.strip() != "":
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["flit_core >=3.2,<4"]
|
|
3
|
+
#requires = ["flit_core >=2,<3"]
|
|
4
|
+
build-backend = "flit_core.buildapi"
|
|
5
|
+
|
|
6
|
+
[tool.flit.metadata]
|
|
7
|
+
module = "pyprocessors_pseudonimizer"
|
|
8
|
+
author = "Olivier Terrier"
|
|
9
|
+
author-email = "olivier.terrier@kairntech.com"
|
|
10
|
+
home-page = "https://kairntech.com/"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Intended Audience :: Information Technology",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Intended Audience :: System Administrators",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
17
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
18
|
+
"Topic :: Software Development :: Libraries",
|
|
19
|
+
"Topic :: Software Development",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Development Status :: 4 - Beta",
|
|
22
|
+
"Programming Language :: Python :: 3.8",
|
|
23
|
+
]
|
|
24
|
+
requires = [
|
|
25
|
+
"pymultirole-plugins>=0.7.0,<0.8.0",
|
|
26
|
+
"presidio_anonymizer>=2.2.29",
|
|
27
|
+
"log-with-context",
|
|
28
|
+
"Faker"
|
|
29
|
+
]
|
|
30
|
+
dist-name = "pyprocessors-pseudonimizer"
|
|
31
|
+
description-file = "README.md"
|
|
32
|
+
requires-python = ">=3.8"
|
|
33
|
+
|
|
34
|
+
[tool.flit.entrypoints."pyprocessors.plugins"]
|
|
35
|
+
pseudonymizer = "pyprocessors_pseudonimizer.pseudonimizer:PseudonimizerProcessor"
|
|
36
|
+
[tool.flit.metadata.requires-extra]
|
|
37
|
+
test = [
|
|
38
|
+
"pytest",
|
|
39
|
+
"pytest-cov",
|
|
40
|
+
"pytest-flake8",
|
|
41
|
+
"pytest-black",
|
|
42
|
+
# "hypothesis",
|
|
43
|
+
"flake8==3.9.2",
|
|
44
|
+
"tox",
|
|
45
|
+
"dirty-equals"
|
|
46
|
+
]
|
|
47
|
+
docs = [
|
|
48
|
+
"sphinx",
|
|
49
|
+
"sphinx-rtd-theme",
|
|
50
|
+
"m2r2", # markdown support
|
|
51
|
+
"sphinxcontrib.apidoc", # run sphinx-apidoc when building docs
|
|
52
|
+
"jupyter_sphinx", # for execution of code snippets in the documentation
|
|
53
|
+
]
|
|
54
|
+
dev = [
|
|
55
|
+
"flit",
|
|
56
|
+
"pre-commit",
|
|
57
|
+
"bump2version",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
[tool.flakehell]
|
|
61
|
+
exclude = ["README.md"]
|
|
62
|
+
format = "colored"
|
|
63
|
+
#format = "junit-xml"
|
|
64
|
+
max_line_length = 120
|
|
65
|
+
show_source = true
|
|
66
|
+
#whitelist = "../../allowlist.txt"
|
|
67
|
+
|
|
68
|
+
[tool.flakehell.plugins]
|
|
69
|
+
flake8-bandit = ["+*", "-S322"]
|
|
70
|
+
flake8-bugbear = ["+*"]
|
|
71
|
+
flake8-builtins = ["+*"]
|
|
72
|
+
flake8-comprehensions = ["+*"]
|
|
73
|
+
#flake8-darglint = ["+*"]
|
|
74
|
+
flake8-docstrings = ["+*"]
|
|
75
|
+
flake8-eradicate = ["+*"]
|
|
76
|
+
flake8-isort = ["+*"]
|
|
77
|
+
flake8-mutable = ["+*"]
|
|
78
|
+
flake8-pytest-style = ["+*"]
|
|
79
|
+
flake8-spellcheck = ["+*"]
|
|
80
|
+
mccabe = ["+*"]
|
|
81
|
+
pep8-naming = ["+*"]
|
|
82
|
+
pycodestyle = ["+*"]
|
|
83
|
+
pyflakes = ["+*"]
|
|
84
|
+
pylint = ["+*"]
|