Skip to content

Commit 4f31dc8

Browse files
author
Thomas Grainger
committed
update docs, and use doctests to ensure continuing accuracy
1 parent ed7f20a commit 4f31dc8

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

README.rst

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ What The Patch!?
44
.. image:: https://travis-ci.org/cscorley/whatthepatch.svg?style=flat
55
:target: https://travis-ci.org/cscorley/whatthepatch
66

7-
What The Patch!? is a library for parsing patch files. Its only purpose is to
8-
read a patch file and get it into some usable form by other programs.
7+
What The Patch!? is a library for both parsing and applying patch files.
98

109
Features
1110
---------
@@ -71,47 +70,41 @@ each diff in the patch:
7170
.. code-block:: python
7271
7372
>>> import whatthepatch
74-
>>> with open('somechanges.patch') as f:
73+
>>> import pprint
74+
>>> with open('tests/casefiles/diff-unified.diff') as f:
7575
... text = f.read()
7676
...
7777
>>> for diff in whatthepatch.parse_patch(text):
78-
... print(diff)
78+
... print(diff) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
7979
...
80-
diff(header=header(
81-
index_path=None,
82-
old_path='lao',
83-
old_version='2012-12-26 23:16:54.000000000 -0600',
84-
new_path='tzu',
85-
new_version='2012-12-26 23:16:50.000000000 -0600'
86-
),
87-
changes=[
88-
(1, None, 'The Way that can be told of is not the eternal Way;'),
89-
(2, None, 'The name that can be named is not the eternal name.'),
90-
(3, 1, 'The Nameless is the origin of Heaven and Earth;'),
91-
(4, None, 'The Named is the mother of all things.'),
92-
(None, 2, 'The named is the mother of all things.'),
93-
(None, 3, ''),
94-
(5, 4, 'Therefore let there always be non-being,'),
95-
(6, 5, ' so we may see their subtlety,'),
96-
(7, 6, 'And let there always be being,'),
97-
(9, 8, 'The two are the same,'),
98-
(10, 9, 'But after they are produced,'),
99-
(11, 10, ' they have different names.'),
100-
(None, 11, 'They both may be called deep and profound.'),
101-
(None, 12, 'Deeper and more profound,'),
102-
(None, 13, 'The door of all subtleties!')
103-
]
104-
)
105-
106-
*Edited to show structure of the results*
80+
diff(header=header(index_path=None,
81+
old_path='lao',
82+
old_version='2013-01-05 16:56:19.000000000 -0600',
83+
new_path='tzu',
84+
new_version='2013-01-05 16:56:35.000000000 -0600'),
85+
changes=[Change(old=1, new=None, hunk=1, line='The Way that can be told of is not the eternal Way;'),
86+
Change(old=2, new=None, hunk=1, line='The name that can be named is not the eternal name.'),
87+
Change(old=3, new=1, hunk=1, line='The Nameless is the origin of Heaven and Earth;'),
88+
Change(old=4, new=None, hunk=1, line='The Named is the mother of all things.'),
89+
Change(old=None, new=2, hunk=1, line='The named is the mother of all things.'),
90+
Change(old=None, new=3, hunk=1, line=''), Change(old=5, new=4, hunk=1, line='Therefore let there always be non-being,'),
91+
Change(old=6, new=5, hunk=1, line=' so we may see their subtlety,'),
92+
Change(old=7, new=6, hunk=1, line='And let there always be being,'),
93+
Change(old=9, new=8, hunk=2, line='The two are the same,'),
94+
Change(old=10, new=9, hunk=2, line='But after they are produced,'),
95+
Change(old=11, new=10, hunk=2, line=' they have different names.'),
96+
Change(old=None, new=11, hunk=2, line='They both may be called deep and profound.'),
97+
Change(old=None, new=12, hunk=2, line='Deeper and more profound,'),
98+
Change(old=None, new=13, hunk=2, line='The door of all subtleties!')],
99+
text='...')
107100
108101
The changes are listed as they are in the patch, but instead of the +/- syntax
109102
of the patch, we get a tuple of two numbers and the text of the line.
110103
What these numbers indicate are as follows:
111104

112-
#. ``( 1, None, ... )`` indicates line 1 of the file lao was **removed**.
113-
#. ``( None, 2, ... )`` indicates line 2 of the file tzu was **inserted**.
114-
#. ``( 5, 4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.
105+
#. ``( old=1, new=None, ... )`` indicates line 1 of the file lao was **removed**.
106+
#. ``( old=None, new=2, ... )`` indicates line 2 of the file tzu was **inserted**.
107+
#. ``( old=5, new=4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.
115108

116109
Please note that not all patch formats provide the actual lines modified, so some
117110
results will have the text portion of the tuple set to ``None``.
@@ -124,15 +117,29 @@ To apply a diff to some lines of text, first read the patch and parse it.
124117
.. code-block:: python
125118
126119
>>> import whatthepatch
127-
>>> with open('somechanges.patch') as f:
120+
>>> with open('tests/casefiles/diff-default.diff') as f:
128121
... text = f.read()
129122
...
130-
>>> with open('lao') as f:
123+
>>> with open('tests/casefiles/lao') as f:
131124
... lao = f.read()
132125
...
133126
>>> diff = [x for x in whatthepatch.parse_patch(text)]
134127
>>> diff = diff[0]
135128
>>> tzu = whatthepatch.apply_diff(diff, lao)
129+
>>> tzu # doctest: +NORMALIZE_WHITESPACE
130+
['The Nameless is the origin of Heaven and Earth;',
131+
'The named is the mother of all things.',
132+
'',
133+
'Therefore let there always be non-being,',
134+
' so we may see their subtlety,',
135+
'And let there always be being,',
136+
' so we may see their outcome.',
137+
'The two are the same,',
138+
'But after they are produced,',
139+
' they have different names.',
140+
'They both may be called deep and profound.',
141+
'Deeper and more profound,',
142+
'The door of all subtleties!']
136143
137144
138145
Contribute

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
envlist = py{27,33,34,35,36}, lint
33

44
[testenv]
5-
commands = nosetests {posargs}
5+
commands = nosetests --with-doctest --doctest-extension=rst {posargs}
66
deps =
77
nose==1.3.7
88

0 commit comments

Comments
 (0)