Skip to content

Instantly share code, notes, and snippets.

@polymorphm
Created June 23, 2013 16:39
Show Gist options
  • Save polymorphm/5845639 to your computer and use it in GitHub Desktop.
Save polymorphm/5845639 to your computer and use it in GitHub Desktop.
``siemens-qu-pr-decode`` is utility for decode Quoted-Printable (old Siemens non-standard dialect)
#!/usr/bin/bash
out_file="decode-out.txt"
decode_filter="$(dirname -- "$0")/siemens-qu-pr-decode"
echo "# $(date)" >"$out_file"
for arg
do
echo "--------------------------------------------------">>"$out_file"
cat -- "$arg" | "$decode_filter" >>"$out_file"
done
#!/usr/bin/python3
# -*- mode: python; coding: utf-8 -*-
#
# Copyright 2013 Andrej A Antonov <[email protected]>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
assert str is not bytes
import argparse, sys, re
magic_str = b'CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:'
regex = re.compile(re.escape(magic_str) + br'|=(?P<char>[0-9A-Fa-f]{2}|\r\n|\n)|\r\n', re.S)
def decode_func(matchobj):
if matchobj.group('char') is not None:
if matchobj.group('char') in (b'\r\n', b'\n'):
return
return bytes((int(matchobj.group('char'), base=16),))
if matchobj.group() == magic_str:
return
if matchobj.group() == b'\r\n':
return b'\n'
def decode(in_text):
out_text = regex.sub(decode_func, in_text)
return out_text
def main():
parser = argparse.ArgumentParser(
description="utility for decode Quoted-Printable (old Siemens non-standard dialect)")
args = parser.parse_args()
ifd = sys.stdin.buffer
ofd = sys.stdout.buffer
in_text = ifd.read()
out_text = decode(in_text)
ofd.write(out_text)
ofd.flush()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment