From aa6d1277c3f990d411821e5791dbdb3380117eb2 Mon Sep 17 00:00:00 2001 From: C. Scott Ananian Date: Mon, 03 Nov 2008 21:50:50 +0000 Subject: Tweak utf-8 encoding patch so that 'coding:' lines don't accumulate over time. Hide the 'coding:' lines from the user when possible; add coding: lines only if they don't already exist. --- (limited to 'pippy_app.py') diff --git a/pippy_app.py b/pippy_app.py index d4fba88..6c0b3e9 100644 --- a/pippy_app.py +++ b/pippy_app.py @@ -1,3 +1,5 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright 2007 Chris Ball, based on Collabora's "hellomesh" demo. # # This program is free software; you can redistribute it and/or modify @@ -43,6 +45,10 @@ IFACE = SERVICE PATH = "/org/laptop/Pippy" text_buffer = None +# magic prefix to use utf-8 source encoding +PYTHON_PREFIX="""#!/usr/bin/python +# -*- coding: utf-8 -*- +""" class PippyActivity(ViewSourceActivity): """Pippy Activity as specified in activity.info""" @@ -268,8 +274,10 @@ class PippyActivity(ViewSourceActivity): text = text_buffer.get_text(start, end) with open(filename, 'w') as f: - f.write('#!/usr/bin/python\n') - f.write('# -*- coding: utf-8 -*-\n') + # write utf-8 coding prefix if there's not already one + if re.match(r'coding[:=]\s*([-\w.]+)', + '\n'.join(text.splitlines()[:2])) is None: + f.write(PYTHON_PREFIX) for line in text: f.write(line) def _reset_vte(self): @@ -403,6 +411,8 @@ class PippyActivity(ViewSourceActivity): def read_file(self, file_path): text = open(file_path).read() + # discard the '#!/usr/bin/python' and 'coding: utf-8' lines, if present + text = re.sub(r'^' + re.escape(PYTHON_PREFIX), '', text) global text_buffer text_buffer.set_text(text) -- cgit v0.9.1