Skip to content

Instantly share code, notes, and snippets.

class RapidSMSLexer(RegexLexer):
"""
Lexer for RapidSMS text messages.
"""
name = 'RapidSMS'
aliases = ['sms']
filenames = []
mimetypes = ['application/x-rapidsms']
@malthe
malthe / image.py
Created November 4, 2010 14:51
Traverse to image scales which are automatically created if missing!
class SomeArchetypeWithImages(...):
def __bobo_traverse__(self, REQUEST, name):
if name in self.image_field_ids:
field = self.getField(name)
image = field.getScale(self)
else:
# decode request
parts = name.split('_')
field_name = '_'.join(parts[:-1])
scale = len(parts) > 1 and parts[-1] or None
@malthe
malthe / fix-po.py
Created November 9, 2010 12:26
Fixes translation punctuation for a gettext .po file and writes it back to disk.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
import StringIO
f = open(sys.argv[1], 'r')
g = iter(f)
default = None
@malthe
malthe / migrate.py
Created November 18, 2010 12:20
Broken object migration with ZODB
import imp
import sys
def create_modules(module_path):
path = ""
module = None
for element in module_path.split('.'):
path += element
try:
@malthe
malthe / controlpanel.py
Created December 2, 2010 09:47
Rich text field on a property sheet in a control panel
from z3c.form import form
from z3c.form import field
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
from plone.app.textfield.value import RichTextValue
from plone.z3cform import layout
from Products.CMFCore.utils import getToolByName
class IMyFormSchema(Interface):
text = RichText(title=u"Text", required=False, default=u"")
@malthe
malthe / abstractrecordsproxy.py
Created December 12, 2010 16:38
Abstract records proxy for Plone's settings registry
from zope.component import getUtility
from zope.interface import Interface
from zope.interface import alsoProvides
from plone.registry.interfaces import IRegistry
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.z3cform import layout
@malthe
malthe / less.app
Created September 22, 2011 08:14
less.app in bash
#!/bin/bash
if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit -1
fi
while read line; do
filename="$(basename $line)"
if [ "${filename##*.}" == "less" ]; then
@malthe
malthe / hyphenation.py
Created September 22, 2011 13:11
Dictionary-based HTML hyphenation using soft hyphen
import os
import re
from htmlentitydefs import name2codepoint
# Must install the `hyphenator` library from PyPi!
from hyphenator import Hyphenator
# Firefox comes with an English hyphenation dictionary
path = os.popen('locate hyph_en_US.dic').readlines()[0].strip()
@malthe
malthe / nuke.el
Created January 20, 2012 11:09
Nuke dashes in Emacs
(defun nuke-dashes ()
(interactive) (save-excursion (goto-char (point-min)) (while (re-search-forward "^-" nil t) (replace-match ""))))
(global-set-key "\C-d" 'nuke-dashes)
@malthe
malthe / .emacs.el
Created February 24, 2012 11:06
Dot-emacs
;; .emacs
;; =========================================================================
;;
;; Malthe Borch
;;
;; Last updated: Fri Feb 24, 2012.
;; Add own packages to load path
(let* ((my-lisp-dir "~/.emacs.d/site-lisp")
(default-directory my-lisp-dir)