Examples#
The most useful features to the end-user is located in the parser.py module.
Parse single diff with changes in multiple files:#
from diffparser.parser import parse_diff
multi_file_diff = "--- a/source/org/jfree/data/DefaultKeyedValues.java\n+++ b/source/org/jfree/data/DefaultKeyedValues.java\n@@ -315,30 +315,29 @@ private void rebuildIndex () {\n public void removeValue(int index) {\n this.keys.remove(index);\n this.values.remove(index);\n- if (index < this.keys.size()) {\n rebuildIndex();\n- }\n }\n \n /**\n * Removes a value from the collection.\n *\n * @param key the item key (<code>null</code> not permitted).\n * \n * @throws IllegalArgumentException if <code>key</code> is \n * <code>null</code>.\n * @throws UnknownKeyException if <code>key</code> is not recognised.\n */\n public void removeValue(Comparable key) {\n int index = getIndex(key);\n if (index < 0) {\n-\t\t\treturn;\n+ throw new UnknownKeyException(\"The key (\" + key \n+ + \") is not recognised.\");\n }\n removeValue(index);\n }\n \n /**\n * Clears all values from the collection.\n * \n * @since 1.0.2\n */\n--- a/source/org/jfree/data/DefaultKeyedValues2D.java\n+++ b/source/org/jfree/data/DefaultKeyedValues2D.java\n@@ -454,12 +454,21 @@ public void removeColumn(int columnIndex) {\n public void removeColumn(Comparable columnKey) {\r\n+ \tif (columnKey == null) {\r\n+ \t\tthrow new IllegalArgumentException(\"Null 'columnKey' argument.\");\r\n+ \t}\r\n+ \tif (!this.columnKeys.contains(columnKey)) {\r\n+ \t\tthrow new UnknownKeyException(\"Unknown key: \" + columnKey);\r\n+ \t}\r\n Iterator iterator = this.rows.iterator();\r\n while (iterator.hasNext()) {\r\n DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next();\r\n+ int index = rowData.getIndex(columnKey);\r\n+ if (index >= 0) {\r\n rowData.removeValue(columnKey);\r\n+ }\r\n }\r\n this.columnKeys.remove(columnKey);\r\n }\r\n \r\n /**\r\n * Clears all the data and associated keys.\r\n */\r\n"
print(parse_diff(multi_file_diff, path_included=True))
Parse single diff with changes in a single file:#
from diffparser.parser import parse_diff
single_file_diff = "@@ -330,7 +330,7 @@ class DialsScaler(Scaler):\n Debug.write('X1698: %s: %s' % (pointgroup, reindex_op))\n \n if ntr:\n- integrater.integrater_reset_reindex_operator()\n+ intgr.integrater_reset_reindex_operator()\n need_to_return = True\n \n if pt and not probably_twinned:\n"
print(parse_diff(single_file_diff, path_included=False))
Parse list of diffs with changes in a single file:#
from diffparser.parser import parse_diff
# This is an example
From Json to parsed txt-files:#
This next example will show how to take a json file, parse it and then store the parsed code to two files. In this example, with the output names set to ‘’, we will get the output files src.txt and tgt.txt
from diffparser.parser import read_json, parse_list_of_commits, parsed_to_txt
data_set = '../../data/sample.json'
unparsed_list = read_json(data_set)
parsed = parse_list_of_commits(unparsed_list, path_included=True)
parsed_to_txt(parsed, '', changed_key='changedFiles')