-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathcore.py
More file actions
1696 lines (1360 loc) · 64 KB
/
core.py
File metadata and controls
1696 lines (1360 loc) · 64 KB
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
from __future__ import annotations
import os
import sys
from contextlib import suppress
from copy import deepcopy
from typing import TYPE_CHECKING, Any, Generic, cast
from hatchling.metadata.utils import (
format_dependency,
is_valid_project_name,
normalize_project_name,
normalize_requirement,
)
from hatchling.plugin.manager import PluginManagerBound
from hatchling.utils.constants import DEFAULT_CONFIG_FILE
from hatchling.utils.fs import locate_file
if TYPE_CHECKING:
from packaging.requirements import Requirement
from packaging.specifiers import SpecifierSet
from hatchling.metadata.plugin.interface import MetadataHookInterface
from hatchling.utils.context import Context
from hatchling.version.scheme.plugin.interface import VersionSchemeInterface
from hatchling.version.source.plugin.interface import VersionSourceInterface
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
def load_toml(path: str) -> dict[str, Any]:
with open(path, encoding="utf-8") as f:
return tomllib.loads(f.read())
class ProjectMetadata(Generic[PluginManagerBound]):
def __init__(
self,
root: str,
plugin_manager: PluginManagerBound | None,
config: dict[str, Any] | None = None,
) -> None:
self.root = root
self.plugin_manager = plugin_manager
self._config = config
self._context: Context | None = None
self._build: BuildMetadata | None = None
self._core: CoreMetadata | None = None
self._hatch: HatchMetadata | None = None
self._core_raw_metadata: dict[str, Any] | None = None
self._dynamic: list[str] | None = None
self._name: str | None = None
self._version: str | None = None
self._project_file: str | None = None
# App already loaded config
if config is not None and root is not None:
self._project_file = os.path.join(root, "pyproject.toml")
def has_project_file(self) -> bool:
_ = self.config
if self._project_file is None:
return False
return os.path.isfile(self._project_file)
@property
def context(self) -> Context:
if self._context is None:
from hatchling.utils.context import Context
self._context = Context(self.root)
return self._context
@property
def core_raw_metadata(self) -> dict[str, Any]:
if self._core_raw_metadata is None:
if "project" not in self.config:
message = "Missing `project` metadata table in configuration"
raise ValueError(message)
core_raw_metadata = self.config["project"]
if not isinstance(core_raw_metadata, dict):
message = "The `project` configuration must be a table"
raise TypeError(message)
core_raw_metadata = deepcopy(core_raw_metadata)
pkg_info = os.path.join(self.root, "PKG-INFO")
if os.path.isfile(pkg_info):
from hatchling.metadata.spec import PROJECT_CORE_METADATA_FIELDS, project_metadata_from_core_metadata
with open(pkg_info, encoding="utf-8") as f:
pkg_info_contents = f.read()
base_metadata = project_metadata_from_core_metadata(pkg_info_contents)
defined_dynamic = core_raw_metadata.get("dynamic", [])
for field in list(defined_dynamic):
if field in PROJECT_CORE_METADATA_FIELDS and field in base_metadata:
core_raw_metadata[field] = base_metadata[field]
defined_dynamic.remove(field)
self._core_raw_metadata = core_raw_metadata
return self._core_raw_metadata
@property
def dynamic(self) -> list[str]:
# Keep track of the original dynamic fields before depopulation
if self._dynamic is None:
dynamic = self.core_raw_metadata.get("dynamic", [])
if not isinstance(dynamic, list):
message = "Field `project.dynamic` must be an array"
raise TypeError(message)
for i, field in enumerate(dynamic, 1):
if not isinstance(field, str):
message = f"Field #{i} of field `project.dynamic` must be a string"
raise TypeError(message)
self._dynamic = list(dynamic)
return self._dynamic
@property
def name(self) -> str:
# Duplicate the name parsing here for situations where it's
# needed but metadata plugins might not be available
if self._name is None:
name = self.core_raw_metadata.get("name", "")
if not name:
message = "Missing required field `project.name`"
raise ValueError(message)
self._name = normalize_project_name(name)
return self._name
@property
def version(self) -> str:
"""
https://peps.python.org/pep-0621/#version
"""
if self._version is None:
self._version = self._get_version()
with suppress(ValueError):
self.core.dynamic.remove("version")
return self._version
@property
def config(self) -> dict[str, Any]:
if self._config is None:
project_file = locate_file(self.root, "pyproject.toml")
if project_file is None:
self._config = {}
else:
self._project_file = project_file
self._config = load_toml(project_file)
return self._config
@property
def build(self) -> BuildMetadata:
if self._build is None:
build_metadata = self.config.get("build-system", {})
if not isinstance(build_metadata, dict):
message = "The `build-system` configuration must be a table"
raise TypeError(message)
self._build = BuildMetadata(self.root, build_metadata)
return self._build
@property
def core(self) -> CoreMetadata:
if self._core is None:
metadata = CoreMetadata(self.root, self.core_raw_metadata, self.hatch.metadata, self.context)
# Save the fields
_ = self.dynamic
metadata_hooks = self.hatch.metadata.hooks
if metadata_hooks:
static_fields = set(self.core_raw_metadata)
if "version" in self.hatch.config:
self._version = self._get_version(metadata)
self.core_raw_metadata["version"] = self.version
if metadata.dynamic:
for metadata_hook in metadata_hooks.values():
metadata_hook.update(self.core_raw_metadata)
metadata.add_known_classifiers(metadata_hook.get_known_classifiers())
new_fields = set(self.core_raw_metadata) - static_fields
for new_field in new_fields:
if new_field in metadata.dynamic:
metadata.dynamic.remove(new_field)
else:
message = (
f"The field `{new_field}` was set dynamically and therefore must be "
f"listed in `project.dynamic`"
)
raise ValueError(message)
self._core = metadata
return self._core
@property
def hatch(self) -> HatchMetadata:
if self._hatch is None:
tool_config = self.config.get("tool", {})
if not isinstance(tool_config, dict):
message = "The `tool` configuration must be a table"
raise TypeError(message)
hatch_config = tool_config.get("hatch", {})
if not isinstance(hatch_config, dict):
message = "The `tool.hatch` configuration must be a table"
raise TypeError(message)
hatch_file = (
os.path.join(os.path.dirname(self._project_file), DEFAULT_CONFIG_FILE)
if self._project_file is not None
else locate_file(self.root, DEFAULT_CONFIG_FILE) or ""
)
if hatch_file and os.path.isfile(hatch_file):
config = load_toml(hatch_file)
hatch_config = hatch_config.copy()
hatch_config.update(config)
self._hatch = HatchMetadata(self.root, hatch_config, self.plugin_manager)
return self._hatch
def _get_version(self, core_metadata: CoreMetadata | None = None) -> str:
if core_metadata is None:
core_metadata = self.core
version = core_metadata.version
if version is None:
version = self.hatch.version.cached
source = f"source `{self.hatch.version.source_name}`"
core_metadata._version_set = True # noqa: SLF001
else:
source = "field `project.version`"
from packaging.version import InvalidVersion, Version
try:
normalized_version = str(Version(version))
except InvalidVersion:
message = f"Invalid version `{version}` from {source}, see https://peps.python.org/pep-0440/"
raise ValueError(message) from None
else:
return normalized_version
def validate_fields(self) -> None:
_ = self.version
self.core.validate_fields()
class BuildMetadata:
"""
https://peps.python.org/pep-0517/
"""
def __init__(self, root: str, config: dict[str, str | list[str]]) -> None:
self.root = root
self.config = config
self._requires: list[str] | None = None
self._requires_complex: list[Requirement] | None = None
self._build_backend: str | None = None
self._backend_path: list[str] | None = None
@property
def requires_complex(self) -> list[Requirement]:
if self._requires_complex is None:
from packaging.requirements import InvalidRequirement, Requirement
requires = self.config.get("requires", [])
if not isinstance(requires, list):
message = "Field `build-system.requires` must be an array"
raise TypeError(message)
requires_complex = []
for i, entry in enumerate(requires, 1):
if not isinstance(entry, str):
message = f"Dependency #{i} of field `build-system.requires` must be a string"
raise TypeError(message)
try:
requires_complex.append(Requirement(entry))
except InvalidRequirement as e:
message = f"Dependency #{i} of field `build-system.requires` is invalid: {e}"
raise ValueError(message) from None
self._requires_complex = requires_complex
return self._requires_complex
@property
def requires(self) -> list[str]:
if self._requires is None:
self._requires = [str(r) for r in self.requires_complex]
return self._requires
@property
def build_backend(self) -> str:
if self._build_backend is None:
build_backend = self.config.get("build-backend", "")
if not isinstance(build_backend, str):
message = "Field `build-system.build-backend` must be a string"
raise TypeError(message)
self._build_backend = build_backend
return self._build_backend
@property
def backend_path(self) -> list[str]:
if self._backend_path is None:
backend_path = self.config.get("backend-path", [])
if not isinstance(backend_path, list):
message = "Field `build-system.backend-path` must be an array"
raise TypeError(message)
for i, entry in enumerate(backend_path, 1):
if not isinstance(entry, str):
message = f"Entry #{i} of field `build-system.backend-path` must be a string"
raise TypeError(message)
self._backend_path = backend_path
return self._backend_path
class CoreMetadata:
"""
https://peps.python.org/pep-0621/
"""
def __init__(
self,
root: str,
config: dict[str, Any],
hatch_metadata: HatchMetadataSettings,
context: Context,
) -> None:
self.root = root
self.config = config
self.hatch_metadata = hatch_metadata
self.context = context
self._raw_name: str | None = None
self._name: str | None = None
self._version: str | None = None
self._description: str | None = None
self._readme: str | None = None
self._readme_content_type: str | None = None
self._readme_path: str | None = None
self._requires_python: str | None = None
self._python_constraint: SpecifierSet | None = None
self._license: str | None = None
self._license_expression: str | None = None
self._license_files: list[str] | None = None
self._authors: list[str] | None = None
self._authors_data: dict[str, list[str]] | None = None
self._maintainers: list[str] | None = None
self._maintainers_data: dict[str, list[str]] | None = None
self._keywords: list[str] | None = None
self._classifiers: list[str] | None = None
self._extra_classifiers: set[str] = set()
self._urls: dict[str, str] | None = None
self._scripts: dict[str, str] | None = None
self._gui_scripts: dict[str, str] | None = None
self._entry_points: dict[str, dict[str, str]] | None = None
self._dependencies_complex: dict[str, Requirement] | None = None
self._dependencies: list[str] | None = None
self._optional_dependencies_complex: dict[str, dict[str, Requirement]] | None = None
self._optional_dependencies: dict[str, list[str]] | None = None
self._dynamic: list[str] | None = None
self._import_names: list[str] | None = None
self._import_namespaces: list[str] | None = None
# Indicates that the version has been successfully set dynamically
self._version_set: bool = False
@property
def raw_name(self) -> str:
"""
https://peps.python.org/pep-0621/#name
"""
if self._raw_name is None:
if "name" in self.dynamic:
message = "Static metadata field `name` cannot be present in field `project.dynamic`"
raise ValueError(message)
raw_name = self.config.get("name", "")
if not raw_name:
message = "Missing required field `project.name`"
raise ValueError(message)
if not isinstance(raw_name, str):
message = "Field `project.name` must be a string"
raise TypeError(message)
if not is_valid_project_name(raw_name):
message = (
"Required field `project.name` must only contain ASCII letters/digits, underscores, "
"hyphens, and periods, and must begin and end with ASCII letters/digits."
)
raise ValueError(message)
self._raw_name = raw_name
return self._raw_name
@property
def name(self) -> str:
"""
https://peps.python.org/pep-0621/#name
"""
if self._name is None:
self._name = normalize_project_name(self.raw_name)
return self._name
@property
def version(self) -> str:
"""
https://peps.python.org/pep-0621/#version
"""
version: str
if self._version is None:
if "version" not in self.config:
if not self._version_set and "version" not in self.dynamic:
message = (
"Field `project.version` can only be resolved dynamically "
"if `version` is in field `project.dynamic`"
)
raise ValueError(message)
else:
if "version" in self.dynamic:
message = (
"Metadata field `version` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
version = self.config["version"]
if not isinstance(version, str):
message = "Field `project.version` must be a string"
raise TypeError(message)
self._version = version
return cast(str, self._version)
@property
def description(self) -> str:
"""
https://peps.python.org/pep-0621/#description
"""
if self._description is None:
if "description" in self.config:
description = self.config["description"]
if "description" in self.dynamic:
message = (
"Metadata field `description` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
description = ""
if not isinstance(description, str):
message = "Field `project.description` must be a string"
raise TypeError(message)
self._description = " ".join(description.splitlines())
return self._description
@property
def readme(self) -> str:
"""
https://peps.python.org/pep-0621/#readme
"""
readme: str | dict[str, str] | None
content_type: str | None
if self._readme is None:
if "readme" in self.config:
readme = self.config["readme"]
if "readme" in self.dynamic:
message = (
"Metadata field `readme` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
readme = None
if readme is None:
self._readme = ""
self._readme_content_type = "text/markdown"
self._readme_path = ""
elif isinstance(readme, str):
normalized_path = readme.lower()
if normalized_path.endswith(".md"):
content_type = "text/markdown"
elif normalized_path.endswith(".rst"):
content_type = "text/x-rst"
elif normalized_path.endswith(".txt"):
content_type = "text/plain"
else:
message = f"Unable to determine the content-type based on the extension of readme file: {readme}"
raise TypeError(message)
readme_path = os.path.normpath(os.path.join(self.root, readme))
if not os.path.isfile(readme_path):
message = f"Readme file does not exist: {readme}"
raise OSError(message)
with open(readme_path, encoding="utf-8") as f:
self._readme = f.read()
self._readme_content_type = content_type
self._readme_path = readme
elif isinstance(readme, dict):
content_type = readme.get("content-type")
if content_type is None:
message = "Field `content-type` is required in the `project.readme` table"
raise ValueError(message)
if not isinstance(content_type, str):
message = "Field `content-type` in the `project.readme` table must be a string"
raise TypeError(message)
if content_type not in {"text/markdown", "text/x-rst", "text/plain"}:
message = (
"Field `content-type` in the `project.readme` table must be one of the following: "
"text/markdown, text/x-rst, text/plain"
)
raise ValueError(message)
if "file" in readme and "text" in readme:
message = "Cannot specify both `file` and `text` in the `project.readme` table"
raise ValueError(message)
if "file" in readme:
relative_path = readme["file"]
if not isinstance(relative_path, str):
message = "Field `file` in the `project.readme` table must be a string"
raise TypeError(message)
path = os.path.normpath(os.path.join(self.root, relative_path))
if not os.path.isfile(path):
message = f"Readme file does not exist: {relative_path}"
raise OSError(message)
with open(path, encoding=readme.get("charset", "utf-8")) as f:
contents = f.read()
readme_path = relative_path
elif "text" in readme:
contents = readme["text"]
if not isinstance(contents, str):
message = "Field `text` in the `project.readme` table must be a string"
raise TypeError(message)
readme_path = ""
else:
message = "Must specify either `file` or `text` in the `project.readme` table"
raise ValueError(message)
self._readme = contents
self._readme_content_type = content_type
self._readme_path = readme_path
else:
message = "Field `project.readme` must be a string or a table"
raise TypeError(message)
return self._readme
@property
def readme_content_type(self) -> str:
"""
https://peps.python.org/pep-0621/#readme
"""
if self._readme_content_type is None:
_ = self.readme
return cast(str, self._readme_content_type)
@property
def readme_path(self) -> str:
"""
https://peps.python.org/pep-0621/#readme
"""
if self._readme_path is None:
_ = self.readme
return cast(str, self._readme_path)
@property
def requires_python(self) -> str:
"""
https://peps.python.org/pep-0621/#requires-python
"""
if self._requires_python is None:
from packaging.specifiers import InvalidSpecifier, SpecifierSet
if "requires-python" in self.config:
requires_python = self.config["requires-python"]
if "requires-python" in self.dynamic:
message = (
"Metadata field `requires-python` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
requires_python = ""
if not isinstance(requires_python, str):
message = "Field `project.requires-python` must be a string"
raise TypeError(message)
try:
self._python_constraint = SpecifierSet(requires_python)
except InvalidSpecifier as e:
message = f"Field `project.requires-python` is invalid: {e}"
raise ValueError(message) from None
self._requires_python = str(self._python_constraint)
return self._requires_python
@property
def python_constraint(self) -> SpecifierSet:
from packaging.specifiers import SpecifierSet
if self._python_constraint is None:
_ = self.requires_python
return cast(SpecifierSet, self._python_constraint)
@property
def license(self) -> str:
"""
https://peps.python.org/pep-0621/#license
"""
if self._license is None:
if "license" in self.config:
data = self.config["license"]
if "license" in self.dynamic:
message = (
"Metadata field `license` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
data = None
if data is None:
self._license = ""
self._license_expression = ""
elif isinstance(data, str):
from packaging.licenses import canonicalize_license_expression
try:
self._license_expression = str(canonicalize_license_expression(data))
except ValueError as e:
message = f"Error parsing field `project.license` - {e}"
raise ValueError(message) from None
self._license = ""
elif isinstance(data, dict):
if "file" in data and "text" in data:
message = "Cannot specify both `file` and `text` in the `project.license` table"
raise ValueError(message)
if "file" in data:
relative_path = data["file"]
if not isinstance(relative_path, str):
message = "Field `file` in the `project.license` table must be a string"
raise TypeError(message)
path = os.path.normpath(os.path.join(self.root, relative_path))
if not os.path.isfile(path):
message = f"License file does not exist: {relative_path}"
raise OSError(message)
with open(path, encoding="utf-8") as f:
contents = f.read()
elif "text" in data:
contents = data["text"]
if not isinstance(contents, str):
message = "Field `text` in the `project.license` table must be a string"
raise TypeError(message)
else:
message = "Must specify either `file` or `text` in the `project.license` table"
raise ValueError(message)
self._license = contents
self._license_expression = ""
else:
message = "Field `project.license` must be a string or a table"
raise TypeError(message)
return self._license
@property
def license_expression(self) -> str:
"""
https://peps.python.org/pep-0639/
"""
if self._license_expression is None:
_ = self.license
return cast(str, self._license_expression)
@property
def license_files(self) -> list[str]:
"""
https://peps.python.org/pep-0639/
"""
if self._license_files is None:
if "license-files" in self.config:
globs = self.config["license-files"]
if "license-files" in self.dynamic:
message = (
"Metadata field `license-files` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
if isinstance(globs, dict):
globs = globs.get("globs", globs.get("paths", []))
else:
globs = ["LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*"]
from glob import glob
license_files: list[str] = []
if not isinstance(globs, list):
message = "Field `project.license-files` must be an array"
raise TypeError(message)
for i, pattern in enumerate(globs, 1):
if not isinstance(pattern, str):
message = f"Entry #{i} of field `project.license-files` must be a string"
raise TypeError(message)
full_pattern = os.path.normpath(os.path.join(self.root, pattern))
license_files.extend(
os.path.relpath(path, self.root).replace("\\", "/")
for path in glob(full_pattern)
if os.path.isfile(path)
)
self._license_files = sorted(license_files)
return self._license_files
@property
def authors(self) -> list[str]:
"""
https://peps.python.org/pep-0621/#authors-maintainers
"""
authors: list[str]
authors_data: dict[str, list[str]]
if self._authors is None:
if "authors" in self.config:
authors = self.config["authors"]
if "authors" in self.dynamic:
message = (
"Metadata field `authors` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
authors = []
if not isinstance(authors, list):
message = "Field `project.authors` must be an array"
raise TypeError(message)
from email.headerregistry import Address
authors = deepcopy(authors)
authors_data = {"name": [], "email": []}
for i, data in enumerate(authors, 1):
if not isinstance(data, dict):
message = f"Author #{i} of field `project.authors` must be an inline table"
raise TypeError(message)
name = data.get("name", "")
if not isinstance(name, str):
message = f"Name of author #{i} of field `project.authors` must be a string"
raise TypeError(message)
email = data.get("email", "")
if not isinstance(email, str):
message = f"Email of author #{i} of field `project.authors` must be a string"
raise TypeError(message)
if name and email:
authors_data["email"].append(str(Address(display_name=name, addr_spec=email)))
elif email:
authors_data["email"].append(str(Address(addr_spec=email)))
elif name:
authors_data["name"].append(name)
else:
message = f"Author #{i} of field `project.authors` must specify either `name` or `email`"
raise ValueError(message)
self._authors = authors
self._authors_data = authors_data
return self._authors
@property
def authors_data(self) -> dict[str, list[str]]:
"""
https://peps.python.org/pep-0621/#authors-maintainers
"""
if self._authors_data is None:
_ = self.authors
return cast(dict, self._authors_data)
@property
def maintainers(self) -> list[str]:
"""
https://peps.python.org/pep-0621/#authors-maintainers
"""
maintainers: list[str]
if self._maintainers is None:
if "maintainers" in self.config:
maintainers = self.config["maintainers"]
if "maintainers" in self.dynamic:
message = (
"Metadata field `maintainers` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
maintainers = []
if not isinstance(maintainers, list):
message = "Field `project.maintainers` must be an array"
raise TypeError(message)
from email.headerregistry import Address
maintainers = deepcopy(maintainers)
maintainers_data: dict[str, list[str]] = {"name": [], "email": []}
for i, data in enumerate(maintainers, 1):
if not isinstance(data, dict):
message = f"Maintainer #{i} of field `project.maintainers` must be an inline table"
raise TypeError(message)
name = data.get("name", "")
if not isinstance(name, str):
message = f"Name of maintainer #{i} of field `project.maintainers` must be a string"
raise TypeError(message)
email = data.get("email", "")
if not isinstance(email, str):
message = f"Email of maintainer #{i} of field `project.maintainers` must be a string"
raise TypeError(message)
if name and email:
maintainers_data["email"].append(str(Address(display_name=name, addr_spec=email)))
elif email:
maintainers_data["email"].append(str(Address(addr_spec=email)))
elif name:
maintainers_data["name"].append(name)
else:
message = f"Maintainer #{i} of field `project.maintainers` must specify either `name` or `email`"
raise ValueError(message)
self._maintainers = maintainers
self._maintainers_data = maintainers_data
return self._maintainers
@property
def maintainers_data(self) -> dict[str, list[str]]:
"""
https://peps.python.org/pep-0621/#authors-maintainers
"""
if self._maintainers_data is None:
_ = self.maintainers
return cast(dict, self._maintainers_data)
@property
def keywords(self) -> list[str]:
"""
https://peps.python.org/pep-0621/#keywords
"""
if self._keywords is None:
if "keywords" in self.config:
keywords = self.config["keywords"]
if "keywords" in self.dynamic:
message = (
"Metadata field `keywords` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
keywords = []
if not isinstance(keywords, list):
message = "Field `project.keywords` must be an array"
raise TypeError(message)
unique_keywords = set()
for i, keyword in enumerate(keywords, 1):
if not isinstance(keyword, str):
message = f"Keyword #{i} of field `project.keywords` must be a string"
raise TypeError(message)
unique_keywords.add(keyword)
self._keywords = sorted(unique_keywords)
return self._keywords
@property
def classifiers(self) -> list[str]:
"""
https://peps.python.org/pep-0621/#classifiers
"""
if self._classifiers is None:
import bisect
if "classifiers" in self.config:
classifiers = self.config["classifiers"]
if "classifiers" in self.dynamic:
message = (
"Metadata field `classifiers` cannot be both statically defined and "
"listed in field `project.dynamic`"
)
raise ValueError(message)
else:
classifiers = []
if not isinstance(classifiers, list):
message = "Field `project.classifiers` must be an array"
raise TypeError(message)
verify_classifiers = not os.environ.get("HATCH_METADATA_CLASSIFIERS_NO_VERIFY")
if verify_classifiers:
import trove_classifiers
known_classifiers = trove_classifiers.classifiers | self._extra_classifiers
sorted_classifiers = list(trove_classifiers.sorted_classifiers)
for classifier in sorted(self._extra_classifiers - trove_classifiers.classifiers):
bisect.insort(sorted_classifiers, classifier)
unique_classifiers = set()
for i, classifier in enumerate(classifiers, 1):
if not isinstance(classifier, str):
message = f"Classifier #{i} of field `project.classifiers` must be a string"
raise TypeError(message)
if (
not self.__classifier_is_private(classifier)
and verify_classifiers
and classifier not in known_classifiers
):
message = f"Unknown classifier in field `project.classifiers`: {classifier}"
raise ValueError(message)
unique_classifiers.add(classifier)
if not verify_classifiers:
import re
# combined text-numeric sort that ensures that Python versions sort correctly