Skip to content

Commit

Permalink
Add class SafenameIncrementalEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
batlock666 committed Jul 13, 2013
1 parent 25875c5 commit 4328bb2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/fnord/safename/codec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import codecs
import sys

from fnord.safename.handler import Handler, HandlerChain

Expand Down Expand Up @@ -71,3 +72,27 @@ def decode(self, string, errors="strict"):
"""Decode a string with codec ``safename``.
"""
return safename_decode(string, errors=errors)

if sys.version >= "2.5":

class SafenameIncrementalEncoder(codecs.IncrementalEncoder):
"""Incremental encoder for codec ``safename``.
"""

def __init__(self, errors="strict"):
"""Constructor.
"""
if errors != "strict":
raise UnicodeError(u"Unsupported error handling: %s" % errors)

codecs.IncrementalEncoder.__init__(self, errors)

def encode(self, string, final=False):
"""Encode a string with codec ``safename``.
"""
self.buffer += string

if final:
return safename_encode(self.buffer, errors=self.errors)
else:
return ""

0 comments on commit 4328bb2

Please sign in to comment.