annotate
Annotate existing dataset (that is, patch files in subdirectories corresponding to bugs), or selected subset of commits (of changes in selectes subset of commits) in a given repository.
The result of annotation is saved in JSON files, one per patch / commit.
This script provides the following subcommands:
diff-annotate patch [OPTIONS] PATCH_FILE RESULT_JSON
: annotate a single PATCH_FILE, writing results to RESULT_JSON,diff-annotate dataset [OPTIONS] DATASETS...
: annotate all bugs in provided DATASETS,diff-annotate from-repo [OPTIONS] REPO_PATH [REVISION_RANGE...]
: create annotation data for commits from local Git repository (withREVISION_RANGE...
passed as arguments to thegit log
command);
Example (after installing the 'patchscope' package): diff-annotate --help
diff-annotate --use-pylinguist patch tests/test_dataset/tqdm-1/c0dcf39b046d1b4ff6de14ac99ad9a1b10487512.diff c0dcf39b046d1b4ff6de14ac99ad9a1b10487512.json
diff-annotate dataset --output-prefix ~/example_annotations/ /mnt/data/HaPy-Bug/raw_data/bugsinpy-dataset/
diff-annotate from-repo --output-dir=~/example_annotations/tensorflow/yong.tang/ ~/example_repositories/tensorflow/ --author=yong.tang.github@outlook.com
PURPOSE_TO_ANNOTATION = {'documentation': 'documentation'}
module-attribute
¶
Defines when purpose of the file is propagated to line annotation, without parsing
AnnotatedHunk
¶
Annotations for diff for a single hunk in a patch
It parses pre-image and post-image of a hunk using Pygments, and assigns the type of "code" or "documentation" for each changed line.
Note that major part of the annotation process is performed on demand,
during the process()
method call.
:ivar patched_file: AnnotatedPatchedFile
this AnnotatedHunk
belongs to
:ivar hunk: source unidiff.Hunk
(modified blocks of a file) to annotate
:ivar patch_data: place to gather annotated hunk data
Source code in src/diffannotator/annotate.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 |
|
__init__(patched_file, hunk, hunk_idx)
¶
Initialize AnnotatedHunk with AnnotatedPatchedFile and Hunk
The patched_file
is used to examine file purpose, and possibly
annotate lines according to PURPOSE_TO_ANNOTATION
mapping.
For example each changed line in a changed file which purpose is
"documentation" is also marked as having "documentation" type.
:param patched_file: changed file the hunk belongs to :param hunk: diff hunk to annotate :param hunk_idx: index of this hunk in the patched file (0-based hunk number)
Source code in src/diffannotator/annotate.py
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
|
add_line_annotation(line_no, hunk_idx, in_hunk, file_line_no, source_file, target_file, change_type, line_annotation, purpose, tokens)
¶
Add line annotations for a given line in a hunk
:param line_no: line number (line index) in a diff hunk body, 0-based
:param hunk_idx: hunk number (hunk index) in a diff patched file, 0-based
:param in_hunk: line number (line index) of changed line in a diff hunk body;
only '-' lines and '+' lines counts, 0-based
:param file_line_no: line number in a file the line came from, 1-based
:param source_file: name of changed file in pre-image of diff,
before changes
:param target_file: name of changed file in post-image of diff,
after changes
:param change_type: one of LINE_TYPE_*
constants from unidiff.constants
:param line_annotation: type of line ("code", "documentation",...)
:param purpose: purpose of file ("project", "programming", "documentation",
"data", "markup", "other",...)
:param tokens: result of pygments.lexer.Lexer.get_tokens_unprocessed()
Source code in src/diffannotator/annotate.py
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 |
|
compute_sizes_and_spreads()
¶
Compute hunk sizes and inner-hunk spread
Computes the following metrics:
-
hunk sizes:
-
number of hunks (in the unified diff meaning), as 'n_hunks'
- number of modified, added and removed lines, counting a pair of adjacent removed and added line as single modified line, as 'n_mod', 'n_rem', and 'n_add'
- number of changed lines: sum of number of modified, added, and removed, as 'patch_size'
- number of '+' and '-' lines in hunk (without extracting modified lines), as 'n_lines_added', 'n_lines_removed'
-
number of all lines in hunk, including context lines, but excluding headers 'n_lines_all'
-
hunk spread
-
number of groups, i.e. spans of removed and added lines, not interrupted by context line (also called "chunks"), as 'n_groups'
-
sum of distance in context lines between groups (chunks) inside hunk, as 'spread_inner'
-
patched file spread helpers
-
start and end if hunk (pre-image and post-image) as 'hunk_start' and 'hunk_end' - both values are tuple of source file (pre-image) line number and target file (post-image) line number
- start of first group and end of first group (pre-/post-image) as 'groups_start' and 'groups_end'
- type of line that started first group, and that ended last group of changed lines, as 'type_first' and 'type_last'
:return: (Counter with different sizes and different spreads of the given hunk, dict with data needed to compute inter-hunk spread)
Source code in src/diffannotator/annotate.py
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 |
|
file_line_no(line)
staticmethod
¶
Line number in source file (for '-') or target file (for '+' and ' ')
This line number is 1-based (first line in file has file line no equal 1, not 0), and is not unique. For example, for changed line there might be added line with the same file line no in target as removed line in source.
Similar code is used in AnnotatedPatchedFile.hunk_tokens_for_type() method.
NOTE: Might be made into a function, instead of static method
(it does not use self
), or method or property monkey-patched onto PatchLine.
:param line: PatchLine from Hunk from PatchedFile from PatchSet (unidiff) :return: 1-based line number of changed line in source or target file, respectively
Source code in src/diffannotator/annotate.py
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
|
process()
¶
Process associated patch hunk, annotating changes
Returns single-element mapping from filename to pre- and post-image line annotations. The pre-image line annotations use "-" as key, while post-image use "+". For each line, there is currently gathered the following data:
- "id": line number in the hunk itself (it is not line number in pre-image for "-" lines, or line image in post-image for "+" lines); this numbering counts context lines, which are currently ignored, 0-based.
- "type": "documentation" or "code", or the value mapped from the file purpose
by the
PURPOSE_TO_ANNOTATION
global variable, or the value provided by theAnnotatedPatchedFile.line_callback
function; comments and docstrings counts as "documentation", and so do every line of documentation file - "purpose": file purpose
- "tokens": list of tokens from Pygments lexer (
get_tokens_unprocessed()
)
If file purpose is in PURPOSE_TO_ANNOTATION
, then line annotation that
corresponds to that file purpose in this mapping is used for all lines
of the hunk as "type".
Updates and returns the self.patch_data
field.
:return: annotated patch data, mapping from changed file name to '+'/'-', to annotated line info (from post-image or pre-image) :rtype: dict[str, dict[str, dict]]
Source code in src/diffannotator/annotate.py
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 |
|
tokens_for_type(line_type)
¶
Lexing results for removed ('-')/added ('+') lines in hunk, if possible
Passes work to AnnotatedPatchedFile.hunk_tokens_for_type
method
for a patched file this hunk belongs to.
:param line_type: line_type: denotes line type, e.g. line.line_type from unidiff; must be one of '-' (unidiff.LINE_TYPE_REMOVED) or '+' (unidiff.LINE_TYPE_ADDED). :return: post-processed result of lexing, split into lines, if there is pre-/post-image file contents available; None if there is no pre-/post-image contents attached.
Source code in src/diffannotator/annotate.py
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
|
AnnotatedPatchSet
¶
Annotations for whole patch / diff
:ivar patch_set: original unidiff.PatchSet or diffannotator.git.ChangeSet :ivar repo: optionally, the repository diffannotator.git.ChangeSet came from
Source code in src/diffannotator/annotate.py
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 |
|
__init__(patch_set, repo=None)
¶
Initialize AnnotatedPatchSet with unidiff.PatchSet (or derived class)
:param patch_set: parsed unified diff (if unidiff.PatchSet),
or parsed commit changes and parsed commit metadata (if ChangeSet)
:param repo: the Git repository the patch_set
(ChangeSet)
came from
Source code in src/diffannotator/annotate.py
346 347 348 349 350 351 352 353 354 355 356 357 |
|
add_repo(repo)
¶
Add the Git repository the patch (supposedly) came from
NOTE: Modifies self, and returns modified object.
:param repo: the Git repository connected to self / the patchset :return: changed object, to enable flow/builder pattern
Source code in src/diffannotator/annotate.py
360 361 362 363 364 365 366 367 368 369 |
|
compute_sizes_and_spreads()
¶
Compute patch set sizes and spread
See the detailed description of returned metrics in docstring
for AnnotatedPatchedFile.compute_sizes_and_spreads
.
:return: Counter with different sizes and different spreads of the given patch set (unified diff object, or diff file)
Source code in src/diffannotator/annotate.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
from_filename(filename, encoding=unidiff.DEFAULT_ENCODING, errors=None, newline=None, missing_ok=False, ignore_diff_parse_errors=True)
classmethod
¶
Return a AnnotatedPatchSet instance given a diff filename
:param filename: path to the patch file (diff file) to try to parse
(absolute or relative to the current working directory)
:param encoding: name of the encoding used to decode the file,
defaults to "UTF-8"
:param errors: optional string that specifies how decoding errors
are to be handled; see documentation of open
function for list
of possible values, see: https://docs.python.org/3/library/functions.html#open
:param newline: determines how to parse newline characters from the stream;
see documentation of open
function for possible values
:param missing_ok: if false (the default), FileNotFoundError
is raised
if the path does not exist, and PermissionError
is raised if file
exists but cannot be read because of path permissions; if missing_ok
is true,
return None on missing file, or file with wrong permissions
:param ignore_diff_parse_errors: if false (the default), unidiff.UnidiffParseError
is raised if there was error parsing the unified diff; if true, return None
on parse errors
:return: wrapped result of parsing patch file filename
Source code in src/diffannotator/annotate.py
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 |
|
process(sizes_and_spreads=False, ignore_annotation_errors=True)
¶
Process wrapped patch set, annotating changes for patched files
Returns mapping from filename to pre- and post-image line annotations. The pre-image line annotations use "-" as key, while post-image use "+".
The format of returned values is described in more detail
in AnnotatedHunk.process()
documentation.
TODO: Update and returns the self.patch_set_data
field (caching results).
:param sizes_and_spreads: if true, compute also various metrics
for patch size and for patch spread with compute_sizes_and_spreads
:param ignore_annotation_errors: if true (the default), ignore errors during
patch annotation process
:return: annotated patch data, mapping from changed file names
to '+'/'-', to annotated line info (from post-image or pre-image)
:rtype: dict[str, dict[str, dict | list | str]]
Source code in src/diffannotator/annotate.py
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 |
|
AnnotatedPatchedFile
¶
Annotations for diff for a single file in a patch
It includes metadata about the programming language associated with the changed/patched file.
Note that major part of the annotation process is performed on demand,
during the process()
method call.
Fixes some problems with unidiff.PatchedFile
:ivar patched_file: original unidiff.PatchedFile
to be annotated
:ivar source_file: name of source file (pre-image name),
without the "a/" prefix from diff / patch
:ivar target_file: name of target file (post-image name),
without the "b/" prefix from diff / patch
:ivar patch_data: gathers patch files and changed patch lines
annotations; mapping from file name to gathered data
Source code in src/diffannotator/annotate.py
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 1001 1002 1003 1004 1005 |
|
__init__(patched_file)
¶
Initialize AnnotatedPatchedFile with PatchedFile
Retrieve pre-image and post-image names of the changed file (cleaning them up by removing the "a/" or "B/" prefixes, if needed; unidiff does that for .path getter, if it is modern enough).
TODO: handle c-quoted filenames, e.g. '"przykład"' for 'przykład'.
Retrieves information about programming language and purpose
of the file based solely on the pathname of a source and of
a target file, using the :mod:languages
module.
:param patched_file: patched file data parsed from unified diff
Source code in src/diffannotator/annotate.py
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 |
|
add_sources(src, dst)
¶
Add pre-image and post-image of a file at given diff
NOTE: Modifies self, and returns modified object.
Example:
from diffannotator.annotate import AnnotatedPatchedFile import unidiff patch_path = 'tests/test_dataset_structured/keras-10/patches/c1c4afe60b1355a6c0e83577791a0423f37a3324.diff' patch_set = unidiff.PatchSet.from_filename(patch_path, encoding="utf-8") patched_file = AnnotatedPatchedFile(patch_set[0]).add_sources("a", "b") patched_file.source 'a' patched_file.target 'b'
:param src: pre-image contents of patched file :param dst: post-image contents of patched file :return: changed object, to enable flow/builder pattern
Source code in src/diffannotator/annotate.py
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 |
|
add_sources_from_files(src_file, dst_file)
¶
Read pre-image and post-image for patched file at given diff
NOTE: Modifies self, adding contents of files, and returns modified object.
Example:
from diffannotator.annotate import AnnotatedPatchedFile import unidiff from pathlib import Path patch_path = 'tests/test_dataset_structured/keras-10/patches/c1c4afe60b1355a6c0e83577791a0423f37a3324.diff' patch_set = unidiff.PatchSet.from_filename(patch_path, encoding="utf-8") patched_file = AnnotatedPatchedFile(patch_set[0]) files_path = Path('tests/test_dataset_structured/keras-10/files') src_path = files_path / 'a' / Path(patched_file.source_file).name dst_path = files_path / 'b' / Path(patched_file.target_file).name patched_file_with_sources = patched_file.add_sources_from_files(src_file=src_path, dst_file=dst_path) patched_file_with_sources.source.splitlines()[2] 'from future import absolute_import'
:param src_file: path to pre-image contents of patched file :param dst_file: path to post-image contents of patched file :return: changed object
Source code in src/diffannotator/annotate.py
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 |
|
compute_sizes_and_spreads()
¶
Compute sizes and spread for patched file in diff/patch
Computes the following metrics:
-
patched file sizes:
-
total number of hunks (in the unified diff meaning), as 'n_hunks'
- total number of modified, added and removed lines for patched file, counting a pair of adjacent removed and added line as single modified line, as 'n_mod', 'n_rem', and 'n_add'
- total number of changed lines: sum of number of modified, added, and removed, as 'patch_size'
- total number of '+' and '-' lines in hunks of patched file (without extracting modified lines), as 'n_lines_added', 'n_lines_removed'
-
number of all lines in all hunks of patched file, including context lines, but excluding hunk headers and patched file headers, as 'n_lines_all'
-
patched file spread
-
total number of groups, i.e. spans of removed and added lines, not interrupted by context line (also called "chunks"), as 'n_groups'
- number of modified files, as 'n_files' (always 1)
- number of modified binary files, as 'n_binary_files' (either 0 or 1); for those files there cannot beno information about "lines", like the number of hunks, groups (chunks), etc.
- sum of distances in context lines between groups (chunks) inside hunk, for all hunks in patched file, as 'spread_inner'
- sum of distances in lines between groups (chunks) for a single changed patched file, measuring how wide across file contents the patch spreads, as 'groups_spread'
:return: Counter with different sizes and different spreads of the given changed file
Source code in src/diffannotator/annotate.py
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 |
|
hunk_tokens_for_type(line_type, hunk)
¶
Lexing results for removed ('-')/added ('+') lines in hunk, if possible
The pre-image and post-image contents of patched file should / can
be provided with the help of add_sources()
or add_sources_from_files()
methods. If this contents is not provided, this method returns None.
The result is mapping from line number of the pre- or post-image contents, counting from 1 (the same as diff and unidiff), to the list of tokens corresponding to the line in question.
:param line_type: denotes line type, e.g. line.line_type from unidiff; must be one of '-' (unidiff.LINE_TYPE_REMOVED) or '+' (unidiff.LINE_TYPE_ADDED). :param hunk: block of changes in fragment of diff corresponding to changed file, either unidiff.Hunk or annotate.AnnotatedHunk :return: post-processed result of lexing, split into lines, if there is pre-/post-image file contents available; None if there is no pre-/post-image contents attached.
Source code in src/diffannotator/annotate.py
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 |
|
image_for_type(line_type)
¶
Return pre-image for '-', post-image for '+', if available
:param line_type: denotes line type, e.g. line.line_type from unidiff :return: pre-image or post-image, or None if pre/post-images are not set
Source code in src/diffannotator/annotate.py
716 717 718 719 720 721 722 723 724 725 726 727 |
|
make_line_callback(code_str)
staticmethod
¶
Create line callback function from text of its body
Example of creating a no-op callback:
AnnotatedPatchedFile.line_callback = AnnotatedPatchedFile.make_line_callback("return None")
:param code_str: text of the function body code :return: callback function or None
Source code in src/diffannotator/annotate.py
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 |
|
process()
¶
Process hunks in patched file, annotating changes
Returns single-element mapping from filename to pre- and post-image line annotations. The pre-image line annotations use "-" as key, while post-image use "+".
The format of returned values is described in more detail
in AnnotatedHunk.process()
documentation.
Updates and returns the self.patch_data
field.
:return: annotated patch data, mapping from changed file name to '+'/'-', to annotated line info (from post-image or pre-image) :rtype: dict[str, dict[str, dict]]
Source code in src/diffannotator/annotate.py
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
tokens_for_type(line_type)
¶
Run lexer on a pre-image or post-image contents, if available
Returns (cached) result of lexing pre-image for line_type
'-',
and of post-image for line type '+'.
The pre-image and post-image contents of patched file should / can
be provided with the help of add_sources()
or add_sources_from_files()
methods.
:param line_type: denotes line type, e.g. line.line_type from unidiff; must be one of '+' or '-'. :return: post-processed result of lexing, split into lines, if there is pre-/post-image file contents available.
Source code in src/diffannotator/annotate.py
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 |
|
tokens_range_for_type(line_type, start_line, length)
¶
Lexing results for given range of lines, or None if no pre-/post-image
The pre-image and post-image contents of patched file should / can
be provided with the help of add_sources()
or add_sources_from_files()
methods.
The result is mapping from line number of the pre- or post-image contents, counting from 1 (the same as diff and unidiff), to the list of tokens corresponding to the line in question.
:param line_type: denotes line type, e.g. line.line_type from unidiff;
must be one of '-' (unidiff.LINE_TYPE_REMOVED) or '+' (unidiff.LINE_TYPE_ADDED).
:param start_line: starting line number in file, counting from 1
:param length: number of lines to return results for,
starting from start_line
:return: post-processed result of lexing, split into lines,
if there is pre-/post-image file contents available;
None if there is no pre-/post-image contents attached.
Source code in src/diffannotator/annotate.py
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 |
|
Bug
¶
Represents a single bug in a dataset, or a set of related patches
:ivar patches: mapping from some kind of identifiers to annotated patches;
the identifier might be the pathname of patch file, or the commit id
:vartype patches: dict[str, dict]
:cvar DEFAULT_PATCHES_DIR: default value for patches_dir
parameter
in Bug.from_dataset()
static method (class property)
:cvar DEFAULT_ANNOTATIONS_DIR: default value for annotations_dir
parameter
in Bug.from_dataset()
static method (class property)
:ivar read_dir: path to the directory patches were read from, or None
:ivar save_dir: path to default directory where annotated data should
be saved (if save()
method is called without annotate_dir
), or None
:ivar relative_save_dir: bug_id / annotations_dir, i.e. subdirectory
where to save annotation data, relative to annotate_dir
parameter
in save()
method; available only if the Bug object was created
with from_dataset()
Source code in src/diffannotator/annotate.py
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 |
|
__init__(patches_data, *, read_dir=None, save_dir=None)
¶
Constructor for class representing a single Bug
You better use alternative constructors instead:
Bug.from_dataset
- from patch files in a directory (a dataset)Bug.from_patchset
- from patch id and unidiff.PatchSet
:param patches_data: annotation data, from annotating a patch
or a series of patches (e.g. from annotate_single_diff()
);
a mapping from patch id (e.g. filename of a patch file)
to the result of annotating said patch
:param read_dir: path to the directory patches were read from, or None
:param save_dir: path to default directory where annotated data should
be saved, or None
Source code in src/diffannotator/annotate.py
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 |
|
from_dataset(dataset_dir, bug_id, *, patches_dir=DEFAULT_PATCHES_DIR, annotations_dir=DEFAULT_ANNOTATIONS_DIR, sizes_and_spreads=False, fan_out=False)
classmethod
¶
Create Bug object from patch files for given bug in given dataset
Assumes that patch files have '*.diff' extension, and that they are
in the dataset_dir
/ bug_id
/ patches_dir
subdirectory (if patches_dir
is an empty string, this is just dataset_dir
/ bug_id
).
:param dataset_dir: path to the dataset (parent directory to
the directory with patch files)
:param bug_id: bug id (name of directory with patch files)
:param patches_dir: name of subdirectory with patch files, if any;
patches are assumed to be in dataset_dir / bug_id / patches_dir directory;
use empty string ("") to not use subdirectory
:param annotations_dir: name of subdirectory where annotated data will be saved;
in case the save()
method is invoked without providing annotate_path
parameter, the data is saved in dataset_dir / bug_id / annotations_dir
subdirectory; use empty string ("") to not use subdirectory
:param sizes_and_spreads: if true, compute also various metrics
for patch size and for patch spread
:param fan_out: the dataset uses stores patches in fan-out subdirectories,
like the ones generated by 'diff-generate --use-fanout', that is patches
are assumed to be in dataset_dir / bug_id / patches_dir / fanout_subdir
:return: Bug object instance
Source code in src/diffannotator/annotate.py
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
|
from_patchset(patch_id, patch_set, sizes_and_spreads=False, repo=None)
classmethod
¶
Create Bug object from unidiff.PatchSet
If patch_id
is None, then it tries to use the 'commit_id' attribute
of patch_set
; if this attribute does not exist, it constructs artificial
patch_id
(currently based on repr(patch_set), but that might change).
:param patch_id: identifies source of the patch_set
:param patch_set: changes to annotate
:param sizes_and_spreads: if true, compute also various metrics
for patch size and for patch spread with compute_sizes_and_spreads
:param repo: the git repository patch comes from; to be able to use
it, patch_set
should be changes in repo for commit patch_id
:return: Bug object instance
Source code in src/diffannotator/annotate.py
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 |
|
save(annotate_dir=None, fan_out=False, output_format_ext=JSONFormatExt.V2)
¶
Save annotated patches in JSON format
:param annotate_dir: Separate dir to save annotations, optional.
If not set, self.save_dir
is used as a base path.
:param fan_out: Save annotated data in a fan-out directory,
named after first 2 hexdigits of patch_id; the rest is used
for the basename; splits patch_id.
:param output_format_ext: Extension used when saving the data;
should look like JSON, e.g. '.json', '.v2.json', etc.
Source code in src/diffannotator/annotate.py
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 |
|
BugDataset
¶
Bugs dataset class
:ivar bug_ids: list of bug identifiers (directories with patch files)
contained in a given dataset_dir
, or list of PatchSet extracted
from Git repo - that can be turned into annotated patch data with
get_bug()
method.
:ivar _dataset_path: path to the dataset directory (with directories with patch files);
present only when creating BugDataset
object from dataset directory.
:ivar _patches: mapping from patch id to unidiff.PatchSet
(unparsed);
present only when creating BugDataset
object from Git repo commits.
Source code in src/diffannotator/annotate.py
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 |
|
__contains__(item)
¶
Is bug with given id contained in the dataset?
Source code in src/diffannotator/annotate.py
1880 1881 1882 |
|
__getitem__(idx)
¶
Get identifier of idx-th bug in the dataset
Source code in src/diffannotator/annotate.py
1876 1877 1878 |
|
__init__(bug_ids, dataset_path=None, patches_dict=None, patches_dir=Bug.DEFAULT_PATCHES_DIR, annotations_dir=Bug.DEFAULT_ANNOTATIONS_DIR, repo=None, fan_out=False)
¶
Constructor of bug dataset.
You better use alternative constructors instead:
BugDataset.from_directory
- from patch files in subdirectories (bugs) of a given directory (a dataset)BugDataset.from_repo
- from selected commits in a Git repo
:param bug_ids: set of bug ids
:param dataset_path: path to the dataset, if BugDataset was created
from dataset directory via BugDataset.from_directory
:param patches_dict: mapping from patch id to patch / patchset
:param patches_dir: name of subdirectory with patch files, if any;
patches are assumed to be in dataset_dir / bug_id / patches_dir directory;
use empty string ("") to not use subdirectory;
makes sense only if dataset_path
is not None
:param annotations_dir: name of subdirectory where annotated data will be saved;
in case the save()
method is invoked without providing annotate_path
parameter, the data is saved in dataset_dir / bug_id / annotations_dir
subdirectory; use empty string ("") to not use subdirectory;
makes sense only if dataset_path
is not None
:param fan_out: assume that patches are stored in fan-out subdirectories,
like the ones generated by 'diff-generate --use-fanout', that is patches
are assumed to be in dataset_dir / bug_id / patches_dir / fanout_subdir;
makes sense only if dataset_path
is not None
Source code in src/diffannotator/annotate.py
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 |
|
__iter__()
¶
Iterate over bugs ids in the dataset
Source code in src/diffannotator/annotate.py
1868 1869 1870 |
|
__len__()
¶
Number of bugs in the dataset
Source code in src/diffannotator/annotate.py
1872 1873 1874 |
|
from_directory(dataset_dir, patches_dir=Bug.DEFAULT_PATCHES_DIR, annotations_dir=Bug.DEFAULT_ANNOTATIONS_DIR, fan_out=False)
classmethod
¶
Create BugDataset object from directory with directories with patch files
:param dataset_dir: path to the dataset
:param patches_dir: name of subdirectory with patch files, if any;
patches are assumed to be in dataset_dir / bug_id / patches_dir directory;
use empty string ("") to not use subdirectory
:param annotations_dir: name of subdirectory where annotated data will be saved;
in case the save()
method is invoked without providing annotate_path
parameter, the data is saved in dataset_dir / bug_id / annotations_dir
subdirectory; use empty string ("") to not use subdirectory
:param fan_out: assume that patches are stored in fan-out subdirectories,
like the ones generated by 'diff-generate --use-fanout', that is patches
are assumed to be in dataset_dir / bug_id / patches_dir / fanout_subdir
:return: BugDataset object instance
Source code in src/diffannotator/annotate.py
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 |
|
from_repo(repo, revision_range='HEAD')
classmethod
¶
Create BugDataset object from selected commits in a Git repo
:param repo: GitRepo, or path to Git repository
:param revision_range: arguments to pass to git log --patch
, see
https://git-scm.com/docs/git-log; by default generates patches
for all commits from the HEAD
:return: BugDataset object instance
Source code in src/diffannotator/annotate.py
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 |
|
get_bug(bug_id, sizes_and_spreads=False, use_repo=True)
¶
Return specified bug
:param bug_id: identifier of a bug in this dataset :param sizes_and_spreads: if true, compute also various metrics for patch size and for patch spread :param use_repo: whether to retrieve pre-/post-image contents from self._git_repo, if available (makes difference only for datasets created from repository, for example with BugDataset.from_repo()) :returns: Bug instance
Source code in src/diffannotator/annotate.py
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 |
|
iter_bugs(sizes_and_spreads=False)
¶
Generate all bugs in the dataset, in annotated form
Generator function, returning Bug after Bug from iteration to iteration.
:param sizes_and_spreads: if true, compute also various metrics for patch size and for patch spread :return: bugs in the dataset
Source code in src/diffannotator/annotate.py
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 |
|
LanguagesFromLinguist
¶
Source code in src/diffannotator/annotate.py
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 |
|
annotate(path)
staticmethod
¶
Annotate file with its primary / first language metadata
:param path: file path in the repository :return: metadata about language, file type, and purpose of file
Source code in src/diffannotator/annotate.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
annotate_single_diff(diff_path, sizes_and_spreads=False, missing_ok=False, ignore_diff_parse_errors=True, ignore_annotation_errors=True)
¶
Annotate single unified diff patch file at given path
:param diff_path: patch filename
:param sizes_and_spreads: if true, compute also various metrics
for patch size and for patch spread with AnnotatedPatchSet.compute_sizes_and_spreads()
:param missing_ok: if false (the default), raise exception if diff_path
does not exist, or cannot be read.
:param ignore_diff_parse_errors: if true (the default), ignore parse errors
(malformed patches), otherwise re-raise the exception
:param ignore_annotation_errors: if true (the default), ignore errors during
patch annotation process
:return: annotation data
Source code in src/diffannotator/annotate.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 |
|
dataset(datasets, output_prefix=None, patches_dir=Bug.DEFAULT_PATCHES_DIR, annotations_dir=Bug.DEFAULT_ANNOTATIONS_DIR, uses_fanout=False)
¶
Annotate all bugs in provided DATASETS
Each DATASET is expected to be existing directory with the following structure, by default:
<dataset_directory>/<bug_directory>/patches/<patch_file>.diff
You can change the /patches/
part with --patches-dir option.
For example with --patches-dir='' the script would expect data
to have the following structure:
<dataset_directory>/<bug_directory>/<patch_file>.diff
Each DATASET can consist of many BUGs, each BUG should include patch to annotate as *.diff file in 'patches/' subdirectory (or in subdirectory you provide via --patches-dir option).
Source code in src/diffannotator/annotate.py
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 |
|
deep_update(d, u)
¶
Update nested dictionary of varying depth
Update dict d
with the contents of dict u
, without overwriting
deeply nested levels in input dictionary d
. Note that this
would also extend d
with new keys from u
.
:param d: dict to update :param u: data to update with :return: updated input dict
Source code in src/diffannotator/annotate.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
extension_to_language_callback(ctx, param, values)
¶
Update extension to language mapping with '
Source code in src/diffannotator/annotate.py
2008 2009 2010 2011 2012 |
|
filename_to_language_callback(ctx, param, values)
¶
Update filename to language mapping with '
Source code in src/diffannotator/annotate.py
2015 2016 2017 2018 2019 |
|
from_repo(repo_path, output_dir, log_args, use_fanout=False, bugsinpy_layout=False, annotations_dir=Bug.DEFAULT_ANNOTATIONS_DIR, use_repo=True, n_jobs=0)
¶
Create annotation data for commits from local Git repository
You can add additional options and parameters, which will be passed to
the git log -p
command. With those options and arguments you
can specify which commits to operate on (defaults to all commits).
See https://git-scm.com/docs/git-log or man git-log
(or git log -help
).
When no
https://git-scm.com/docs/gitrevisions#_specifying_revisions
Note that --use-fanout and --bugsinpy-layout are mutually exclusive.
Source code in src/diffannotator/annotate.py
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 |
|
front_fill_gaps(data)
¶
Fill any gaps in data
keys with previous value
front_fill_gaps({1: '1', 3: '3'})
:param data: Input data - dictionary with int keys :return: Front filled input data
Source code in src/diffannotator/annotate.py
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 |
|
group_tokens_by_line(code, tokens)
¶
Group tokens by line in code
For each line in the source code
, find all tokens
that belong
to that line, and group tokens by line. Note that tokens
must
be result of parsing code
.
:param code: Source code text that was parsed into tokens
:param tokens: An iterable of (index, token_type, value) tuples,
preferably with value
split into individual lines with the
help of split_multiline_lex_tokens
function.
:return: mapping from line number in code
to list of tokens
in that line
Source code in src/diffannotator/annotate.py
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 |
|
line_ends_idx(text)
¶
Return position+1 for each newline in text
This way each line can be extracted with text[pos[i-1]:pos[i]].
example_text = "123\n56\n" line_ends_idx(example_text) [4, 7] example_text[0:4] '123\n' example_text[4:7] '56\n'
:param text: str to process :return: list of positions after end of line characters
Source code in src/diffannotator/annotate.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
line_is_comment(tokens_list)
¶
Given results of parsing line, find if it is comment
:param tokens_list: An iterable of (index, token_type, text_fragment) tuples,
supposedly from parsing some line of source code text
:return: Whether set of tokens in tokens_list
can be all
considered to be a comment
Source code in src/diffannotator/annotate.py
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 |
|
line_is_empty(tokens_list)
¶
Given results of parsing a line, find if it is empty
:param tokens_list: An iterable of (index, token_type, text_fragment) tuples,
supposedly created by parsing some line of source code text
:return: Whether set of tokens in tokens_list
can be all
considered to come from empty line
Source code in src/diffannotator/annotate.py
279 280 281 282 283 284 285 286 287 288 |
|
line_is_whitespace(tokens_list)
¶
Given results of parsing a line, find if it consists only of whitespace tokens
:param tokens_list: An iterable of (index, token_type, text_fragment) tuples,
supposedly created by parsing some line of source code text
:return: Whether set of tokens in tokens_list
are all
whitespace tokens
Source code in src/diffannotator/annotate.py
291 292 293 294 295 296 297 298 299 300 301 |
|
patch(patch_file, result_json)
¶
Annotate a single PATCH_FILE, writing results to RESULT_JSON
Source code in src/diffannotator/annotate.py
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 |
|
pattern_to_purpose_callback(ctx, param, values)
¶
Update pattern to purpose mapping with '
Source code in src/diffannotator/annotate.py
1955 1956 1957 1958 1959 1960 |
|
process_single_bug(bugs, bug_id, output_dir, annotations_dir, bugsinpy_layout, use_fanout, use_repo)
¶
The workhorse of the from_repo
command, processing a single bug / commit
Uses the value of he global variable compute_patch_sizes_and_spreads
.
:param bugs: bug dataset the bug is from
:param bug_id: identifies the bug to process
:param output_dir: where to save annotation data
:param annotations_dir: whether to save annotations data in specific
subdirectory of the bug / commit directory
:param bugsinpy_layout: whether to use BugsInPy-like layout, with
annotations data in annotations_dir
subdirectories
:param use_fanout: whether to save commits under fan-out directories,
that is with subdirectory composed out of two first hex digits
of commit SHA-1 identifier
:param use_repo: whether to use repository to retrieve pre-image
and post-immage version of the file for more accurate lexing
Source code in src/diffannotator/annotate.py
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 |
|
purpose_to_annotation_callback(ctx, param, values)
¶
Update purpose to annotation mapping with '
Source code in src/diffannotator/annotate.py
1947 1948 1949 1950 1951 1952 |
|
purpose_to_default_annotation(file_purpose)
¶
Mapping from file purpose to default line annotation
Source code in src/diffannotator/annotate.py
335 336 337 |
|
split_multiline_lex_tokens(tokens_unprocessed)
¶
Split multiline tokens into individual lines
:param tokens_unprocessed: Result of calling get_tokens_unprocessed(text)
method on a pygments.lexer.Lexer
instance. This is an iterable
of (index, token_type, value) tuples, where index is the starting
position of the token within the input text.
:return: An iterable of (index, token_type, value) tuples, where index
is the starting position of value
in the input text, and each
value
contains at most one newline.
Source code in src/diffannotator/annotate.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
to_language_mapping_callback(ctx, param, values, mapping)
¶
To create callback for providing to language mapping with '
If there is no ':' (colon) separating key from value, it ignores the value.
On empty string it resets the whole mapping.
Assumes that values in mapping are lists (following GitHub Linguist's languages.yml), and that getting value for a key that exists in the mapping replaces the whole list.
:param ctx: Context object with additional data about the current execution of your program :param param: the specific Click Parameter object with information about the current parameter (argument or option) :param values: list of values to parse :param mapping: mapping to change
Source code in src/diffannotator/annotate.py
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 |
|
to_simple_mapping_callback(ctx, param, values, mapping, allow_simplified=False)
¶
Update given to simple mapping
with '
If allow_simplified
is true, and there is no ':' (colon) separating
key from value, add the original both as key and as value. This means
that using '
If allow_simplified
is false, and there is no ':' (colon) separating
key from value, it ignores the value (with warning).
On empty string it resets the whole mapping.
:param ctx: Context object with additional data about the current
execution of your program
:param param: the specific Click Parameter object with information
about the current parameter (argument or option)
:param values: list of values to parse
:param mapping: mapping to change
:param allow_simplified: whether
Source code in src/diffannotator/annotate.py
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 |
|