Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

git: added option for specify encoding input stream/file

Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
  • Loading branch information...
commit 2c1423c5644f3125d79ec532ddc0f30cebb5fb33 1 parent 40ee38c
@awieser awieser authored
Showing with 28 additions and 7 deletions.
  1. +28 −7 memacs/git.py
View
35 memacs/git.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-# Time-stamp: <2011-12-20 15:13:31 aw>
+# Time-stamp: <2012-04-16 18:24:28 armin>
import sys
import os
@@ -122,6 +122,13 @@ def _parser_add_arguments(self):
help="if you wanna parse only commit from a specific person. " + \
"format:<Forname Lastname> of user to grep")
+ self._parser.add_argument(
+ "-e", "--encoding", dest="encoding",
+ action="store",
+ help="default encoding utf-8, see " + \
+ "http://docs.python.org/library/codecs.html#standard-encodings" + \
+ "for possible encodings")
+
def _parser_parse_args(self):
"""
overwritten method of class Memacs
@@ -134,6 +141,18 @@ def _parser_parse_args(self):
os.access(self._args.gitrevfile, os.R_OK)):
self._parser.error("input file not found or not readable")
+ if not self._args.encoding:
+ self._args.encoding = "utf-8"
+
+ def get_line_from_stream(self, input_stream):
+ try:
+ return input_stream.readline()
+ except UnicodeError, e:
+ logging.error("Can't decode to encoding %s, " + \
+ "use argument -e or --encoding see help",
+ self._args.encoding)
+ sys.exit(1)
+
def _main(self):
"""
get's automatically called from Memacs class
@@ -142,12 +161,14 @@ def _main(self):
# read file
if self._args.gitrevfile:
- logging.debug("using as %s input_stream", self._args.gitrevfile)
- input_stream = codecs.open(self._args.gitrevfile, encoding='utf-8')
+ logging.debug("using as %s input_stream",
+ self._args.gitrevfile)
+ input_stream = codecs.open(self._args.gitrevfile,
+ encoding=self._args.encoding)
else:
logging.debug("using sys.stdin as input_stream")
- input_stream = codecs.getreader('utf-8')(sys.stdin)
-
+ input_stream = codecs.getreader(self._args.encoding)(sys.stdin)
+
# now go through the file
# Logic (see example commit below)
# first we are in an header and not in an body
@@ -168,7 +189,7 @@ def _main(self):
commit = Commit()
commits = []
- line = input_stream.readline()
+ line = self.get_line_from_stream(input_stream)
while line:
line = line.rstrip() # removing \n
@@ -183,7 +204,7 @@ def _main(self):
commit.add_header(line)
was_in_body = False
- line = input_stream.readline()
+ line = self.get_line_from_stream(input_stream)
# adding last commit
if not commit.is_empty():
Please sign in to comment.
Something went wrong with that request. Please try again.