JSON and the Blarghonauts, or, Firefox and Pretty-Printing FAIL
There must be a better way of viewing pretty-printed JSON from Firefox than this. (EDIT: Hail, JSONovich!)
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 | #!/usr/bin/env python # ~/bin/jsonhandler.py # Take some JSON from a file or stdin, format it, output to a tempfile, # open in EDITOR from __future__ import with_statement import os import sys import simplejson import tempfile EDITOR = "/usr/bin/gedit" if __name__ == "__main__": if len(sys.argv) == 2: # if invoked as jsonhandler.py {FILE} json = open(sys.argv[1]) else: # if JSON is piped in (e.g., from Firefox, # or cat {FILE} | jsonhandler.py) json = sys.stdin json = simplejson.load(json) # the with_statement is kind of gratuitous but I like it with open(tempfile.mktemp('.json'), 'w') as jsonfile: simplejson.dump(json, jsonfile, indent=4) # all of that and gedit doesn't even highlight JSON # I have emacs highlighting JSON but this generates a "stdin is not # a tty" error, so EDITOR is not set to emacs # xemacs works a little better, but I need to click: # "Options > Syntax Highlighting > In this buffer" every time, despite # saving to custom.el, so EDITOR is not set to xemacs # Very annoying! os.system("%s %s" % (EDITOR, jsonfile.name)) |
And then I set ~/bin/jsonhandler.py as the action for application/json in Edit | Preferences | Applications.
Yuck. Help?
Convert Windows shortcuts into Ubuntu shortcuts
[Update: Feel free to grab the code via bzr with bzr branch http://lackoftalent.org/bzr/shortcut_converter.]
Here's another entry in the "dumb little scripts that work for me and may or may not be helpful to other folks" department…
I use both Windows and Ubuntu at home, gradually transitioning from the former to the latter. I've accumulated a bunch of Windows URL shortcuts, mostly things I wanted to read once so instead of bookmarking them, I dragged their links to my desktop. This creates .URL files which are simple little plain-text two-liners. It turns out that on Ubuntu, and probably similar *nix systems, web shortcuts are also simple little plain-text files. These files have the .desktop extension (though you won't see the extension by looking at the desktop).
I wanted a way to convert my .URL files to .desktop files so that I can just toss them on my Ubuntu desktop and double-click them the same way I would if I were on Windows. This cruddy little Python script does the trick.
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 | #!/usr/bin/env python # shortcut_converter.py from __future__ import with_statement import os.path import sys TEMPLATE = """[Desktop Entry] Version=1.0 Encoding=UTF-8 Name=%(basename)s Type=Link URL=%(url)s Icon=gnome-fs-bookmark """ def convert(f): """ Takes a full filepath to a .URL file, converts it to a .desktop file in the same directory """ print "Converting %s" % f (filepath, filename) = os.path.split(f) (basename, extension) = os.path.splitext(filename) with open(f) as urlfile: lines = [line.strip() for line in urlfile.readlines()] url = lines[1].split('URL=')[1] dtfname = os.path.join(filepath, '%s.desktop' % basename) with open(dtfname, 'w') as dtfile: print "Writing %s" % dtfile.name dtfile.write(TEMPLATE % locals()) if __name__ == '__main__': for arg in sys.argv[1:]: if os.path.isfile(arg) and arg[-3:].lower() == 'url': convert(arg) else: print "*** %s is not a URL file" % arg |
I used scp to pull over all my .URL files and then invoked the script thusly:
python shortcut_converter.py *.URL
worksforme!
Unescaping HTML in Python
Dear Future Me,
You've forgotten how to decode (or unescape) HTML or XML in Python again, haven't you? My, my, that old age does catch up with you.
Well, it turns out that xml.sax.saxutils.unescape() works like a charm. I'm certain that edge cases lurk here and there, so caveat, um, coder.
UPDATE: Edge case found. Note that unescape() will not work on ' or ", and so there is:
xml.sax.saxutils.unescape("<p>This is "markup"</p>", {"'": "'", """: '"'})
Jython scp
In spite of some open questions, I've been making some progress on my Jython-based transport tool. Right now it's pretty dumb and simple: it copies files to and fro via scp.
Being a newb at both Java and Jython made finding the right libraries a bit of a challenge, and so I'm posting some code here for folks in the same boat. It's not particularly pretty due to 1) wanting to get something working very quickly, and 2) weird errors when I try to make things prettier (such as getting rid of the hard-coded bits), but I'll resolve these soon. Continue reading…
Jythons and Javas and bears, oh my!
It's hard to believe but I've been at the new job for six months already, a full half-year come the 29th. Some days it seems like I've been here forever; others like I'm still a rank newb. I haven't written terribly much about what I've been up to (but I assure you I've been busy). Let me rectify that.
The Transfer Problem
Two of the projects I've been working on relate to a fairly general problem that we like to call "transfer," which revolves around, well, transferring files to and fro. Sounds simple. Is simple. That is, until you start thinking about preservation and accounting for a highly heterogeneous network with idiosyncratic nodes, esoteric storage software, and differential firewall rules. And that's where it gets interesting (and problematic). Continue reading…
