-1

I have a string which contains svn unified diff. My PyGTK app need to show this diff to user, and I want to render it like other diff tools do, or at least have it colorized.

Do you have something to suggest, external tool, library, custom implementation...? I was loking at http://kafka.fr.free.fr/diff2html/ but I prefer to use some library or sth like that, so users don't need to install third party apps.

I want use this for git and mercurial diffs later as well.

umpirsky
  • 9,902
  • 13
  • 71
  • 96
  • Have you investigated the features provided by the standard [`difflib`](http://docs.python.org/library/difflib.html) module? – Greg Hewgill Dec 24 '11 at 18:38
  • Yes. If you mean difflib.HtmlDiff it looks good, but I don't see how to couple it with unified diff? – umpirsky Dec 24 '11 at 18:41

3 Answers3

4

You could use difflib to generate diffs, and pygtkscintilla for syntax-highlighting, line-numbering, code-folding, etc.

If you only want syntax-highlighting (as opposed to all the editor features offered by pygtkscintilla), then you could also look at pygments.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks. Pygments looks like killer package. I'm trying it now. Getting ImportError, after install, which is strange because package is installed and I can navigate from Eclipse to source code. Pygments is exactly what I was looking for, I hope I'll fix this error. – umpirsky Dec 24 '11 at 19:37
  • For some reason it's installed in /usr/local/lib/python2.7/dist-packages instead in /usr/local/lib... Any idea? – umpirsky Dec 24 '11 at 20:14
  • What command did you use to install it? I just tested it with the source tarball from [here](http://pypi.python.org/pypi/Pygments), using `sudo /usr/local/bin/python2.7 setup.py install`, and it worked fine. – ekhumoro Dec 24 '11 at 20:36
  • I used sudo python setup.py install as it says on http://pygments.org/docs/installation/. Used same tarball. – umpirsky Dec 24 '11 at 20:47
  • Your problem seems to be ubuntu/debian specific. [This bug report](https://bugs.launchpad.net/ubuntu/+source/python2.6/+bug/362570) suggests using `--prefix=/usr/local` to override the system default install directory. – ekhumoro Dec 24 '11 at 21:05
  • Thanks. But it still installs to wrong location, please check https://gist.github.com/1518400 – umpirsky Dec 24 '11 at 21:32
  • Sorry, I've only just noticed that you're trying to install it with your _system_ python. Don't do that. Install one of [ubuntu's pygments packages](http://packages.ubuntu.com/search?keywords=pygments&searchon=names&suite=all&section=all), instead. – ekhumoro Dec 24 '11 at 23:43
  • There is a package, great, thanks. But I get same ImportError after installing it. But now it's installed in /usr/share/pyshared/pygments and there are symlinks in /usr/lib/python2.7/dist-packages/pygments point to that location. So, it loooks better, but get same error. I am Python beginner, sorry if this is a stupid question. – umpirsky Dec 25 '11 at 09:01
  • I can't fix it, posted new question on http://askubuntu.com/questions/90362/installing-python-pygments-importerror – umpirsky Dec 25 '11 at 13:41
  • Well, I'm sorry that you've had so much difficulty with this - it should be really simple! There seems to be a problem with your python setup, but I don't really know enough about ubuntu to diagnose it. So, to avoid these comments drifting any further off-topic, I would suggest you ask about it on a ubuntu forum. – ekhumoro Dec 25 '11 at 13:56
  • Thanks for your valuable help. Once I install it, I will see how it renders diffs and accept your answer. ImportError problem is out of scope of this question anyway. – umpirsky Dec 25 '11 at 14:18
  • pygments works like a charm, problem was old pygments.pyc which was producing an error, I didn't delete it. Thanks. – umpirsky Dec 25 '11 at 15:00
0

The difflib.HtmlDiff class provides facilities for doing this. However, instead of starting with a unified diff file, HtmlDiff wants you to pass the complete "before" and "after" files. These files are easy to get with svn/git/mercurial commands without using the "diff" functionality of those VCS.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Yes, but I guess this will be slower then with native unified diff if you want complete diff for given revision (my case) since in one revision you can have many files changed, fetching all of them can take significant amount of time. Native diff does this in one step. But you gave me idea, maybe I can diff file by file on demand, still not sure how my app will work :) Thanks. – umpirsky Dec 24 '11 at 18:53
-1

GtkSourceView is a drop in replacement for pygtk's TextView that can syntax-highlight diff files, including unified diffs.

ptomato
  • 56,175
  • 13
  • 112
  • 165