-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanage.py
executable file
·1501 lines (1307 loc) · 61.5 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
77
78
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import csv
import filecmp
import json
import re
import shutil
import sys
from collections import defaultdict
from copy import deepcopy
from io import StringIO
from operator import itemgetter
from pathlib import Path
from textwrap import dedent, indent
from urllib.parse import urlsplit
import click
import json_merge_patch
import lxml.etree
import lxml.html
import mdformat
import numpy as np
import pandas as pd
import requests
import yaml
from jsonpointer import set_pointer
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator
from ocdsextensionregistry import ProfileBuilder
basedir = Path(__file__).resolve().parent
sourcedir = basedir / "source"
outputdir = basedir / "output"
mappingdir = outputdir / "mapping"
eformsdir = mappingdir / "eforms"
docsdir = outputdir / "content" / "eforms"
staticdir = docsdir / "_static" / "svg"
# From https://github.com/OP-TED/eForms-SDK/tree/main/examples
# See https://docs.ted.europa.eu/eforms/latest/schema/schemas.html
xmlhead = (
'<ContractNotice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" '
'xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" '
'xmlns="urn:oasis:names:specification:ubl:schema:xsd:ContractNotice-2" '
'xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" '
'xmlns:efac="http://data.europa.eu/p27/eforms-ubl-extension-aggregate-components/1" '
'xmlns:efext="http://data.europa.eu/p27/eforms-ubl-extensions/1" '
'xmlns:efbc="http://data.europa.eu/p27/eforms-ubl-extension-basic-components/1" '
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
)
xmltail = "</ContractNotice>"
class Dumper(yaml.SafeDumper):
def ignore_aliases(self, data): # noqa: ARG002
return True
def na_representer(dumper, data): # noqa: ARG001
return dumper.represent_data(None)
def ndarray_representer(dumper, data):
return dumper.represent_data(data.tolist())
def float_representer(dumper, data):
if np.isnan(data):
return dumper.represent_data(None)
return dumper.represent_float(data)
def str_representer(dumper, data):
# Use the literal style on multiline strings to reduce quoting, instead of the single-quoted style (default).
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|" if "\n" in data else None)
Dumper.add_representer(pd._libs.missing.NAType, na_representer) # noqa: SLF001
Dumper.add_representer(np.ndarray, ndarray_representer)
Dumper.add_representer(float, float_representer)
Dumper.add_representer(str, str_representer)
def get(url):
"""GET a URL and return the response."""
response = requests.get(url, timeout=10)
response.raise_for_status()
return response
def get_html(url):
"""GET a URL and return the parsed HTML content."""
return lxml.html.fromstring(get(url).content)
def unique(series):
"""Return the unique values of a series."""
series = series.dropna()
# Write "null" not "[]".
if series.empty:
return None
# Lists of lists are not supported by `Series.unique()` ("TED guidance").
if isinstance(series.iloc[0], np.ndarray):
return sorted({item for array in series for item in array})
return series.unique()
def check(actual, expected, noun):
"""Assert that ``actual`` equals ``expected``, with a templated error message."""
if actual != expected:
raise AssertionError(f"expected {expected} {noun}, got {actual}")
def get_column_order(df, drop=()):
# Pandas uses the order of appearance, within rows and *across* rows. Since we remove null values from the output,
# the order of appearance across rows can differ from the order in a given row. A hardcoded list is needed to have
# a consistent order.
column_order = [
"id",
"parentNodeId",
"name",
"btId",
"xpathAbsolute",
"type",
"schemeName",
"idSchemes",
"repeatable",
"mandatory",
"codeList",
"pattern",
]
for column in column_order:
if column not in df.columns:
column_order.remove(column)
for column in df.columns.format():
if column not in column_order:
column_order.append(column)
for column in drop:
if column in column_order:
column_order.remove(column)
return column_order
def write_yaml_file(filename, data):
with filename.open("w") as f:
# Make it easier to see indentation. Avoid line wrapping. sort_keys is True by default.
yaml.dump(data, f, Dumper=Dumper, indent=4, width=1000, sort_keys=False)
def report_unmerged_rows(df, columns, series=None, unformatted=()):
"""If the data frame (or the ``series`` within it) is non-empty, print the data frame's ``columns``."""
if series is not None:
df = df[series]
if not df.empty:
# "Why, pandas?" https://stackoverflow.com/a/67202912/244258
formatters = {
col: f"{{:<{df[col].str.len().max()}s}}".format
for col in df.columns[df.dtypes == "object"]
if col not in unformatted
}
click.echo("These rows were not included in the update:")
click.echo(f"{df[columns].to_string(index=False, formatters=formatters)}\nRows unmerged: {df.shape[0]}")
# From standard-maintenance-scripts/tests/test_readme.py
def set_additional_properties_and_remove_pattern_properties(data, additional_properties):
if isinstance(data, list):
for item in data:
set_additional_properties_and_remove_pattern_properties(item, additional_properties)
elif isinstance(data, dict):
if "properties" in data:
data["additionalProperties"] = additional_properties
if "patternProperties" in data:
del data["patternProperties"]
for value in data.values():
set_additional_properties_and_remove_pattern_properties(value, additional_properties)
def write(
filename, df, overwrite=None, explode=None, compare=None, compare_override=None, how="left", drop=(), **kwargs
):
"""
Read the data frame from the file (if it exists) and merge it with ``df`` according to ``how`` and ``kwargs``,
overwriting only the ``overwrite`` columns.
If ``explode`` is a list, explode the data frame before merging, then group the data frame by the "id" column
after merging. (This option allows merging against a list.)
If ``compare`` is a dict, print any rows in which the cells that match the dict's key and value don't match.
"""
if not compare_override:
compare_override = {}
df_unmerged = pd.DataFrame()
# Default to the data frame's columns.
column_order = get_column_order(df, drop)
if filename.exists():
with filename.open() as f:
df_old = pd.DataFrame.from_records(yaml.safe_load(f))
# Maintain the column order.
column_order = get_column_order(df_old, drop)
for column in overwrite:
if column not in column_order and column not in drop:
column_order.append(column)
# Pandas has no option to overwrite cells, so we drop first. Protect "id" from being overwritten.
df_old = df_old.drop(columns=[column for column in overwrite if column != "id"], errors="ignore")
if explode:
df_old = df_old.explode(explode)
df_outer = df_old.merge(df, how="outer", indicator=True, **kwargs)
# To return the unmerged rows.
df_unmerged = df_outer[df_outer["_merge"] == "right_only"]
if compare:
compared = 0
for _, row in df_outer[df_outer["_merge"] == "both"].iterrows():
for a, b in compare.items():
if row[a] != row[b] and compare_override[a].get(row["id"]) != row[b]:
if not compared:
click.echo(
f"{'id: name'.ljust(75)} | SDK field : Annex col | "
f"{'SDK value'.ljust(50)} : Annex value"
)
compared += 1
field = f'{row["id"]}: {row["name"]}'
click.echo(f"{field.ljust(75)} | {a} : {b} | {str(row[a]).ljust(50)} : {row[b]}")
if compared:
click.echo(f"{compared} value differences")
untouched = [column for column in df.columns if column not in overwrite]
# Merge all the columns, then drop the non-overwritten columns.
df = df_old.merge(df, how=how, **kwargs).drop(columns=untouched)
if drop:
df = df.drop(columns=drop, errors="ignore")
if explode:
df = df.groupby("id").agg(
{column: unique if column in explode or column in overwrite else "first" for column in column_order}
)
# Stop pandas from writing ints as floats.
for column in ("maxLength",):
if column in df.columns:
df[column] = df[column].astype("Int64")
# Initialize, fill in, and order the manually-edited columns.
for column in ("eForms guidance", "eForms example", "OCDS example", "sdk"):
if column not in df.columns:
df[column] = pd.Series(dtype="object")
else:
column_order.remove(column)
df[column] = df[column].fillna("")
column_order.append(column)
write_yaml_file(filename, [row.dropna().to_dict() for label, row in df[column_order].iterrows()])
click.echo(f"{df.shape[0]} rows written")
return df_unmerged
# https://github.com/pallets/click/issues/486
@click.group(context_settings={"max_content_width": 150})
def cli():
pass
@cli.command()
@click.argument("filename", type=click.Path(dir_okay=False, path_type=Path))
@click.option("-v", "--verbose", is_flag=True, help="Print verbose output")
def update_with_sdk(filename, verbose):
"""Create or update FILE with fields metadata from the eForms SDK."""
with (sourcedir / "fields.json").open() as f:
data = json.load(f)
df = pd.DataFrame.from_dict(data["fields"]).set_index("id")
xml = pd.DataFrame.from_dict(data["xmlStructure"]).set_index("id")
subtypes = {}
with (sourcedir / "notice-types.json").open() as f:
data = json.load(f)
for subtype in data["noticeSubTypes"]:
sub_type_id = subtype["subTypeId"]
if sub_type_id.isdigit():
document_type = subtype["documentType"]
match (document_type, subtype["formType"]):
case ("PIN", "competition"):
name = "PIN(CN)"
case ("CAN", "dir-awa-pre"):
name = "CAN(VEAT)"
case ("CAN", "cont-modif"):
name = "CAN(MOD)"
case ("PIN", "planning") | ("CN", "competition") | ("CAN", "result"):
name = document_type
case _:
raise NotImplementedError
subtypes[sub_type_id] = name
labels_to_drop = set()
no_supported_types = {}
supported_notice_types = {str(i) for i in range(1, 41)} | {"CEI", "T01", "T02"}
expected = {"value": False, "severity": "ERROR", "constraints": [{"value": True, "severity": "ERROR"}]}
for label, row in df.iterrows():
# Remove OPA- fields. "Those fields can be ignored when generating the XML notice"
# https://docs.ted.europa.eu/eforms/latest/fields/index.html#_fields_other_than_bt
if label.startswith("OPA-"):
labels_to_drop.add(label)
# Remove a field's own attribute fields, as we documented these together.
if row["attributeOf"] is not np.nan and row["attributeOf"] in label:
labels_to_drop.add(label)
# Remove fields that are forbidden on all supported notice types.
forbidden = row["forbidden"]
# Short-circuit the logic that follows.
if forbidden is np.nan:
continue
# Abbreviate the constraints (there is sometimes a conditional constraint.).
for constraint in forbidden["constraints"][1:]:
if not constraint["condition"].startswith("{ND-"):
raise AssertionError
forbidden["constraints"] = forbidden["constraints"][:1]
# If a field's forbidden types are a superset of all supported types, drop it.
if (
set(forbidden["constraints"][0].pop("noticeTypes")) >= supported_notice_types
and "condition" not in forbidden["constraints"][0]
):
# Ensure the forbidden property's structure is as expected.
if forbidden != expected:
raise AssertionError(f"{label} {forbidden} !=\n{expected}")
labels_to_drop.add(label)
no_supported_types[label] = row["name"]
df = df.drop(index=labels_to_drop)
non_privacy_xml = xml[~xml["xpathRelative"].str.startswith("efac:FieldsPrivacy")]
for label, row in df.iterrows():
# If a repeatable business term is implemented as a field that is the only child of a parent,
# then the SDK marks the parent as repeatable, rather than the child.
# eForms creates a XML structure to repeat terms together.
if row["parentNodeId"] in {
# BT-06-Lot (Strategic Procurement) with BT-777-Lot (Strategic Procurement Description).
"ND-StrategicProcurementType",
# BT-26(a)-* (Classification Type (e.g. CPV)) with BT-263-* (Additional Classification Code).
"ND-LotAdditionalClassification",
"ND-PartAdditionalClassification",
"ND-ProcedureAdditionalCommodityClassification",
}:
df.at[label, "repeatable"] = xml.loc[row["parentNodeId"], "repeatable"]
# eForms moves repeatable to the parent node, if its single child is a field.
elif row["parentNodeId"].startswith("ND-"):
cell = row["repeatable"]
if (
# `severity` is not the only other top-level key.
not (cell["value"] if isinstance(cell, dict) and len(cell) == 2 else cell)
and xml.loc[row["parentNodeId"], "repeatable"]
and len(df[df["parentNodeId"] == row["parentNodeId"]]) == 1 # run after drop()
and len(non_privacy_xml[non_privacy_xml["parentId"] == row["parentNodeId"]]) == 0
):
df.at[label, "repeatable"] = True
mandatory = row["mandatory"]
if mandatory is not np.nan:
values = []
for constraint in mandatory["constraints"]:
value = " ".join({subtypes.get(t, t): None for t in constraint.pop("noticeTypes")})
if constraint.pop("condition", None):
value += " (conditional)"
values.append(value)
# Ensure the mandatory property's structure is as expected.
if any(c != {"value": True, "severity": "ERROR"} for c in mandatory.pop("constraints")):
raise AssertionError
if mandatory != {"value": False, "severity": "ERROR"}:
raise AssertionError
df.at[label, "mandatory"] = " & ".join(values)
if verbose:
click.echo(
f"{df.shape[0]} kept, {len(labels_to_drop)} dropped "
"(OPA- fields and BT- attributes in white, fields with no supported types in yellow)"
)
for label in sorted(labels_to_drop):
click.echo(f"- {label.ljust(45)}", nl=False)
click.secho(no_supported_types.get(label, ""), fg="yellow")
# Remove or abbreviate columns that do not assist the mapping process and that lengthen the JSON file. See README.
drop = [
# https://docs.ted.europa.eu/eforms/latest/fields/index.html#_static_properties
"xpathRelative",
"xsdSequenceOrder",
"attributeOf",
"attributeName",
"attributes",
"presetValue",
"legalType",
"maxLength",
"idScheme",
# https://docs.ted.europa.eu/eforms/latest/fields/index.html#_withheld_publication_mechanism
"privacy",
# https://docs.ted.europa.eu/eforms/latest/fields/index.html#_dynamic_properties
"forbidden",
"assert",
"inChangeNotice",
# 1.12.0
"businessEntityId",
"referencedBusinessEntityIds",
]
# Simplify these columns if `severity` is the only other top-level key.
for column in ("repeatable", "pattern"):
df[column] = [cell["value"] if isinstance(cell, dict) and len(cell) == 2 else cell for cell in df[column]]
# Simplify `codelist` if `severity` and `constraints` are the only other top-level keys, and if `type` and
# `parentId` (optional) are the only other second-level keys.
df["codeList"] = [
(
cell["value"]["id"]
if (
isinstance(cell, dict)
and not (set(cell) - {"value", "severity", "constraints"})
and not (set(cell["value"]) - {"id", "type", "parentId"})
)
else cell
)
for cell in df["codeList"]
]
write(filename, df, df.columns, how="outer", drop=drop, on="id", validate="1:1")
@cli.command()
@click.argument("filename", type=click.Path(exists=True, dir_okay=False, path_type=Path))
def update_with_annex(filename):
"""
Update FILE with details from the 2019 regulation's annex.
\f
Add the columns:
- Description
- Business groups
"""
nonrepeatable = {
# https://github.com/open-contracting/european-union-support/issues/188
"BT-124",
"BT-1251",
"BT-1252",
"BT-136",
"BT-556",
"BT-65",
# https://docs.ted.europa.eu/eforms/latest/schema/procedure-lot-part-information.html#accessibilitySection
"BT-754",
}
# A warning is issued, because the Excel file has an unsupported extension.
df = pd.read_excel(sourcedir / "CELEX_32019R1780_EN_ANNEX_TABLE2_Extended.xlsx", "Annex")
# 0:Level, 1:ID, 2:Name, 3:Data type, 4:Repeatable, 5:Description, 6-50:Legal Status, 51:Fields not included in...
check(df.shape[1], 52, "columns")
# Remove extra header rows.
check(df["ID"].isna().sum(), 3, "extra header rows")
df = df[df["ID"].notna()]
# !!! BG-714 now identifies "CVD Information" in addition to "Review".
if df.at[295, "ID"] == "BG-714":
df.at[295, "ID"] = "BG-7140"
else:
click.echo("BG-714 has moved, or isn't repeated?")
# Ensure there are no duplicates.
df.set_index("ID", verify_integrity=True)
# Normalize whitespace (used in "Business groups").
df["Name"] = df["Name"].str.strip()
# Normalize whitespace.
df["Description"] = df["Description"].str.replace("\xa0", " ", regex=False) # non-breaking space
# Add "Business groups" column, to assist mapping by providing context.
df["Business groups"] = pd.Series(dtype="object")
# !!! The 2024 Annex adds business terms.
annex_2024 = {
"BT-681": ( # Lot
"Foreign Subsidies Regulation",
"The Foreign Subsidies Regulation (FSR) (EU) 2022/2560, in line with Article 28 thereof, "
"is applicable to this procurement procedure.",
"No",
("BG-705", "Other Requirements"),
),
"BT-682": ( # Tender
"Foreign Subsidies Measures",
"Measures applied under the Foreign Subsidies Regulation (EU) 2022/2560.",
"No",
("BG-7", "Notice Result"),
("BG-320", "Tender"),
),
"BT-806": ( # Procedure
"Exclusion Grounds Source",
"Where the exclusions grounds are defined, for example, the procurement documents or in ESPD.",
"Yes",
("BG-700", "Exclusion Grounds and Selection Criteria"),
),
"BT-809": ( # Lot
"Selection Criteria",
"The criteria (or criterion).",
"No",
("BG-700", "Exclusion Grounds and Selection Criteria"),
("BG-702", "Selection Criteria"),
),
"BT-821": ( # Lot
"Selection Criteria Source",
"Where the selection criteria are defined, for example, the procurement documents or in ESPD.",
"Yes",
("BG-700", "Exclusion Grounds and Selection Criteria"),
),
}
for identifier, (name, description, repeatable, *business_groups) in annex_2024.items():
df.loc[-1] = ["", identifier, name, repeatable, None, description] + [None] * 46 + [dict(business_groups)]
df.index = df.index + 1
line = []
previous_level = 0
for label, row in df.iterrows():
# !!! Fix typo, to not be at same level as immediately preceding BG-714 (deduplicated to BG-7140, above).
if row["ID"] == "BT-735" and row["Level"] == "++":
row["Level"] = "+++"
current_level = len(row["Level"])
if current_level:
# Adjust the size of this line of the "tree", then update the head.
if current_level > previous_level:
line.append((None, None))
elif current_level < previous_level:
line = line[:current_level]
previous_level = current_level
line[-1] = [row["ID"], row["Name"]]
if row["ID"] in nonrepeatable:
df.at[label, "Repeatable"] = "No"
if row["ID"] not in annex_2024:
if len(line) > 1:
business_groups = dict(line[:-1])
# !!! The 2024 Annex adds a business group.
if line[0][0] in {"BG-701", "BG-702"}:
business_groups = {"BG-700": "Exclusion Grounds and Selection Criteria", **business_groups}
else:
business_groups = None
df.at[label, "Business groups"] = business_groups
# We can now remove all rows for business groups.
df = df[~df["ID"].str.startswith("BG-")]
# Make the repeatable properties comparable.
df["Repeatable"] = df["Repeatable"].map({"Yes": True, "No": False})
# The fields metadata covers "Name", "Data type", "Repeatable" and "Legal Status" ("forbidden" and "mandatory").
# "Business groups" replaces "Level". "Fields not included in the legal text" isn't informative.
#
# Adding `compare={'name': 'Name'}` shows that the names agree, and differ mainly due to field:bt being m:1.
df = write(
filename,
df,
["Description", "Business groups"],
compare={"repeatable": "Repeatable"},
compare_override={
# For comparison, force the eForms value to the Annex value, if the eForms value is correct.
"repeatable": {
# https://github.com/open-contracting/european-union-support/issues/188#issuecomment-1664643319
"BT-1501(c)-Contract": True,
"BT-1501(n)-Contract": True,
# https://github.com/open-contracting/european-union-support/issues/188#issuecomment-1664720396
"BT-26(a)-Lot": False,
"BT-26(a)-Part": False,
"BT-26(a)-Procedure": False,
"BT-702(a)-notice": True,
"BT-735-Lot": False,
}
},
left_on="btId",
right_on="ID",
validate="m:1",
)
report_unmerged_rows(
df,
["ID", "Name"],
~df["ID"].isin(
{
# See OPT-155 and OPT-156.
# https://docs.ted.europa.eu/eforms/latest/schema/competition-results.html#lotResultComponentsTable
"BT-715",
"BT-725",
"BT-716",
# See Table 3: "Roles and subroles are conveyed by their dedicated element in a specific context from
# where a reference to the Company or TouchPoint exist, linking role/subrole to the appropriate contact
# information of the organization"
# https://docs.ted.europa.eu/eforms/latest/schema/parties.html#mappingOrganizationBTsSchemaComponentsTable
"BT-08",
"BT-770",
# See Table 4: "Pointless Business Terms due to design"
# https://docs.ted.europa.eu/eforms/latest/schema/identifiers.html#pointlessDueToDesignSection
"BT-557", # BT-137
"BT-1371", # BT-137
"BT-1372", # BT-137
"BT-1373", # BT-137
"BT-1374", # BT-137
"BT-1376", # BT-137
"BT-1377", # BT-137
"BT-1378", # BT-137
"BT-1379", # BT-137
"BT-13717", # BT-137
"BT-13710", # BT-137
"BT-13711", # BT-137
"BT-13712", # BT-137
"BT-13718", # BT-137
"BT-13719", # BT-137
"BT-13720",
"BT-13721", # BT-137
"BT-13722", # BT-137
"BT-13715", # BT-137
"BT-53", # BT-54
"BT-724", # BT-124
"BT-778", # BT-113
"BT-5561", # BT-556
# "deletion of fields for BT-747, BT-748, BT-749"
# https://github.com/OP-TED/eForms-SDK/releases/tag/1.12.0
"BT-747",
"BT-748",
"BT-749",
# Forbidden on all supported types in update-with-sdk.
"BT-779",
"BT-780",
"BT-781",
"BT-782",
"BT-783",
"BT-784",
"BT-785",
"BT-786",
"BT-787",
"BT-788",
"BT-789",
"BT-790",
"BT-791",
"BT-792",
"BT-793",
"BT-794",
"BT-795",
"BT-796",
"BT-797",
"BT-798",
"BT-799",
"BT-800",
}
),
["Repeatable"],
)
@cli.command()
@click.argument("filename", type=click.Path(exists=True, dir_okay=False, path_type=Path))
def update_with_xpath(filename):
"""
Update FILE with XPaths from TED XML.
\f
Add the columns:
- TED Xpath
"""
# See the "Legend" sheet for the data dictionary.
with pd.ExcelFile(sourcedir / "TED-XML-to-eForms-mapping-OP-public-20220404.xlsx") as xlsx:
df = pd.read_excel(xlsx, "tedxml_to_eforms_mapping.v0.4", na_values=["---", "no match", "no direct match"])
for a, b in (("Field ID", "eForms BT ID"), ("eForms BT ID", "Field ID")):
actual = df[df["TED Xpath"].notna() & df[a].isna() & df[b].notna()]
if not actual[b].empty:
# The BTs seem to be mapped for other forms, so the omissions seem to be accidental and inconsequential.
click.secho(f'Expected "{b}" to be N/A if "{a}" is N/A. Rows unmerged:', fg="yellow")
click.echo(actual[["eForms BT ID", "TED level", "TED Xpath"]].to_string(index=False))
# We assume that the "Field ID" and "TED Xpath" are correct. Otherwise, we could check the eForms columns for
# discrepancies: "eForms BT ID", "BT name", "Type", "Codelist", "Code", "eForms Xpath".
#
# "EC notes" (~100) and "OP comments" (~10) aren't informative. "Mapping ID" is an internal identifier.
#
# "TED level" and "TED element" can be added, but they might not add anything new.
df = df.groupby("Field ID").agg({"TED Xpath": unique})
write(filename, df, ["TED Xpath"], left_on="id", right_on="Field ID", validate="1:m")
# We don't report_unmerged_rows(), because rows are merged on field ID.
@cli.command()
@click.argument("filename", type=click.Path(exists=True, dir_okay=False, path_type=Path))
def update_with_ted_guidance(filename):
"""
Update FILE with guidance for TED XML.
\f
Add the columns:
- TED guidance
"""
# Collect the guidance.
dfs = []
for path in sorted(mappingdir.glob("*.csv")):
df = pd.read_csv(path)
# Ignore rows without guidance (like defence forms), or for which the guidance is to discard.
df = df[df["guidance"].notna()]
# Prefix the XPath to match the spreadsheet used in `update-with-xpath`.
df["xpath"] = f"TED_EXPORT/FORM_SECTION/{path.stem}" + df["xpath"]
# Add the form for more concise reporting.
df["form"] = path.stem.replace("_2014", "")
dfs.append(df)
# ignore_index is required, as each data frame repeats indices.
df = pd.concat(dfs, ignore_index=True).rename(columns={"guidance": "TED guidance"}, errors="raise")
# This drops "index" and "comment", which are of no assistance to mapping, and "label-key".
df = df.groupby("xpath").agg({"TED guidance": unique, "form": "first"})
# We need to promote the "xpath" index to a column for it to be returned by `write`.
df["index"] = df.index
df = write(filename, df, ["TED guidance"], explode=["TED Xpath"], left_on="TED Xpath", right_on="xpath")
# Ignore unmerged rows whose guidance is to discard.
df = df[~df["TED guidance"].astype(str).str.startswith(("['Discard", '["Discard'))]
# Reduce duplication in the unmerged rows.
df["index"] = df["index"].str.replace(r"TED_EXPORT/FORM_SECTION/[^/]+", "", regex=True)
df = df.groupby("index").agg({"form": unique})
df["xpath"] = df.index
# Some TED elements cannot be converted to eForms.
# https://github.com/OP-TED/ted-xml-data-converter/blob/main/ted-elements-not-convertible.md
url = "https://raw.githubusercontent.com/OP-TED/ted-xml-data-converter/main/ted-elements-not-convertible.md"
elements = [
match.group(1) for line in get(url).text.splitlines() if (match := re.search(r"^\| ([A-Z_]+) \|", line))
]
report_unmerged_rows(df, ["form", "xpath"], ~df["xpath"].str.endswith(tuple(elements)), unformatted=["form"])
@cli.command()
@click.argument("filename", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.option("-a", "--additional-properties", is_flag=True, help="Allow additional properties")
def lint(filename, additional_properties):
"""
Lint FILE (validate and format XML, JSON and Markdown, report unrecognized OCDS fields, update eForms SDK URLs).
"""
literal_strings = {
"COFOG", # BT-10, BT-610
"Restricted.", # BT-14
"Subcontracting", # BT-195
}
required_properties_base = {
"id",
"parentNodeId",
"name",
"btId",
"xpathAbsolute",
"type",
"repeatable",
"eForms guidance",
"eForms example",
"OCDS example",
"sdk",
}
required_properties_bt = {
"Description",
"Business groups",
}
optional_properties = {
"schemeName",
"idSchemes",
"codeList",
"mandatory",
"pattern",
# Only if update-with-xpath matches.
"TED Xpath",
# Only if update-with-ted-guidance matches.
"TED guidance",
}
# Similar to tests/fixtures/release_minimal.json in ocdskit.
minimal_release = {
"ocid": "ocds-213czf-1",
"id": "1",
"date": "2001-02-03T04:05:06Z",
"tag": ["planning"],
"initiationType": "tender",
"tender": {
"id": "1e86a664-ae3c-41eb-8529-0242ac130003",
},
}
set_missing_ids = {
("tender", "items"): ("id",),
("tender", "lots"): ("id",),
("tender", "lotGroups"): ("id",),
("awards",): ("id",),
("contracts",): ("id", "awardID"),
}
with filename.open() as f:
fields = yaml.safe_load(f)
url = "https://raw.githubusercontent.com/open-contracting-extensions/eforms/latest/docs/extension_versions.json"
schema = ProfileBuilder("1__1__5", get(url).json()).patched_release_schema(extension_field="extension")
# Remove `patternProperties` to clarify output.
set_additional_properties_and_remove_pattern_properties(schema, additional_properties)
# Remove required fields.
for definition in (
"Bid",
"Document",
"Finance",
"ParticipationFee",
"Person",
"Statistic",
"WithheldInformationItem",
):
set_pointer(schema, f"/definitions/{definition}/required", [])
validator = Draft4Validator(schema, format_checker=FormatChecker())
codes_txt = Path("codes.txt")
if codes_txt.is_file():
with codes_txt.open() as f:
known_codes = set(f.read().splitlines())
else:
known_codes = set()
url = "https://standard.open-contracting.org/profiles/eforms/latest/en/_static/patched/codelists/"
document = get_html(url)
document.make_links_absolute(url)
for url in document.xpath('//@href[contains(., ".csv")]'):
for row in csv.DictReader(StringIO(get(url).text)):
known_codes.add(row["Code"])
with codes_txt.open("w") as f:
f.write("\n".join(sorted(known_codes)))
codes_eforms_csv = Path("codes-eforms.csv")
if codes_eforms_csv.is_file():
with codes_eforms_csv.open() as f:
known_eforms_codes = {row["code"] for row in csv.DictReader(f)} | literal_strings
else:
known_eforms_codes = set()
sdk_documents = {}
unreviewed = 0
http_errors = set()
anchor_errors = set()
single_quoted = defaultdict(list)
double_quoted = defaultdict(list)
additional_fields = defaultdict(list)
for field in fields:
identifier = field["id"]
required_properties = required_properties_base.copy()
if identifier.startswith("BT-"):
required_properties |= required_properties_bt
if missing_properties := required_properties - set(field):
click.echo(f"{identifier}: missing properties: {missing_properties}")
if extra_properties := set(field) - required_properties_base - required_properties_bt - optional_properties:
click.echo(f"{identifier}: extra properties: {extra_properties}")
# Update and check SDK URLs.
if field["sdk"]:
parsed = urlsplit(field["sdk"])
fragment = parsed.fragment
base_url = parsed._replace(fragment="").geturl()
try:
if base_url not in sdk_documents:
sdk_documents[base_url] = get_html(base_url)
if not fragment:
if not field["id"].endswith("-Contract"):
anchor_errors.add(f"{identifier}: no anchor")
elif not sdk_documents[base_url].xpath(f'//@id="{fragment}"'):
anchor_errors.add(f"{identifier}: anchor not found: {fragment}")
except requests.HTTPError:
http_errors.add(base_url)
# Format Markdown.
field["eForms guidance"] = mdformat.text(field["eForms guidance"]).rstrip()
unreviewed += field["eForms guidance"].startswith("(UNREVIEWED)")
for match in re.finditer(r"'(\S+)'", field["eForms guidance"]):
single_quoted[match.group(1)].append([field["id"], field["name"]])
for match in re.finditer(r'"(\S+)"', re.sub(r"`[^`]+`", "", field["eForms guidance"])):
double_quoted[match.group(1)].append([field["id"], field["name"]])
# Format XML.
eforms_example = field["eForms example"]
if eforms_example and eforms_example != "N/A":
try:
element = lxml.etree.fromstring(f"{xmlhead}{eforms_example}{xmltail}") # noqa: S320 # our data
field["eForms example"] = lxml.etree.tostring(element).decode()[len(xmlhead) : -len(xmltail)]
# Note: The XML snippets are too short to validate against the eForms schema.
except lxml.etree.XMLSyntaxError as e:
click.echo(f"{identifier}: XML is invalid: {e}: {eforms_example}")
# Format and validate JSON.
ocds_example = field["OCDS example"]
if ocds_example and ocds_example != "N/A":
try:
data = json.loads(ocds_example)
field["OCDS example"] = json.dumps(data, separators=(",", ":")).replace("Infinity", "1e9999")
release = deepcopy(minimal_release)
json_merge_patch.merge(release, data)
for parents, keys in set_missing_ids.items():
obj = release
for parent in parents:
if parent in obj:
obj = obj[parent]
else:
break
else:
for key in keys:
if key not in obj[0]:
obj[0][key] = "1"
for e in validator.iter_errors(release):
if e.validator == "additionalProperties":
e.absolute_schema_path[-1] = "properties"
e.absolute_schema_path.append("")
for match in re.findall(r"'(\S+)'", e.message):
e.absolute_schema_path[-1] = match
additional_fields[
"/".join(e.absolute_schema_path)
.replace("items/properties/", "")
.replace("properties/", "")
].append([field["id"], field["name"]])
else:
click.echo(f"{identifier}: OCDS is invalid: {e.message} ({'/'.join(e.absolute_schema_path)})")
except json.decoder.JSONDecodeError as e:
click.echo(f"{identifier}: JSON is invalid: {e}: {ocds_example}")
unknown_codes = {code: v for code, v in single_quoted.items() if code not in known_codes}
if unknown_codes:
click.echo("\nOCDS codes (tokens in single quotes) that do not appear in any OCDS codelist:")
click.echo("code,id,title")
for code, occurrences in sorted(unknown_codes.items(), key=itemgetter(1)):
click.echo(f"{code}{''.join(f',{identifier},{title}' for identifier, title in occurrences)}")
unknown_eforms_codes = {code: v for code, v in double_quoted.items() if code not in known_eforms_codes}
if unknown_eforms_codes:
click.echo("\neForms codes (tokens in double quotes) that do not appear in any eForms codelist:")
click.echo("code,id,title")
for code, occurrences in sorted(unknown_eforms_codes.items(), key=itemgetter(1)):
click.echo(f"{code}{''.join(f',{identifier},{title}' for identifier, title in occurrences)}")
if unreviewed:
click.echo(f"\n{unreviewed} unreviewed eForms guidance")
for title, errors in (
("HTTP errors", http_errors),
("Anchor errors", anchor_errors),
):
if errors:
click.echo(f"\n{title} ({len(errors)}):")
click.echo("\n".join(sorted(errors)))
if additional_fields:
click.echo(f"\nAdditional fields ({len(additional_fields)}):")
click.echo("field,id,title")
for field, occurrences in sorted(additional_fields.items(), key=itemgetter(1)):
click.echo(f"{field}{''.join(f',{identifier},{title}' for identifier, title in occurrences)}")
write_yaml_file(filename, fields)
@cli.command()
@click.argument("directory", type=click.Path(exists=True, file_okay=False))
def build(directory):
def copy_if_changed(src, dst):
if not (dst / src.name).exists() or not filecmp.cmp(src, dst / src.name):
shutil.copy(src, dst)
def write_if_changed(path, new):
old = ""
new = dedent(new)
if path.exists():
with path.open() as f:
old = f.read()
if old != new: