From 49a7771336ce09f6d42c7699ef32aecea0e83182 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 7 Dec 2009 20:07:55 -0500 Subject: Initial directory restructuring to clarify dependencies --- libbe/util/encoding.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 libbe/util/encoding.py (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py new file mode 100644 index 0000000..d09117f --- /dev/null +++ b/libbe/util/encoding.py @@ -0,0 +1,66 @@ +# Bugs Everywhere, a distributed bugtracker +# Copyright (C) 2008-2009 Gianluca Montecchi +# W. Trevor King +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" +Support input/output/filesystem encodings (e.g. UTF-8). +""" + +import codecs +import locale +import sys + +import libbe +if libbe.TESTING == True: + import doctest + + +ENCODING = None # override get_encoding() output by setting this + +def get_encoding(): + """ + Guess a useful input/output/filesystem encoding... Maybe we need + seperate encodings for input/output and filesystem? Hmm... + """ + if ENCODING != None: + return ENCODING + encoding = locale.getpreferredencoding() or sys.getdefaultencoding() + if sys.platform != 'win32' or sys.version_info[:2] > (2, 3): + encoding = locale.getlocale(locale.LC_TIME)[1] or encoding + # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ' + return encoding + +def known_encoding(encoding): + """ + >>> known_encoding("highly-unlikely-encoding") + False + >>> known_encoding(get_encoding()) + True + """ + try: + codecs.lookup(encoding) + return True + except LookupError: + return False + +def set_IO_stream_encodings(encoding): + sys.stdin = codecs.getreader(encoding)(sys.__stdin__) + sys.stdout = codecs.getwriter(encoding)(sys.__stdout__) + sys.stderr = codecs.getwriter(encoding)(sys.__stderr__) + +if libbe.TESTING == True: + suite = doctest.DocTestSuite() -- cgit From 44b4e3f8b6405d0e1e0ebf6cb526ab62cdbbdb25 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 8 Dec 2009 08:54:50 -0500 Subject: Transitioned bugdir.py to new storage format. --- libbe/util/encoding.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index d09117f..21e40cf 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -1,4 +1,3 @@ -# Bugs Everywhere, a distributed bugtracker # Copyright (C) 2008-2009 Gianluca Montecchi # W. Trevor King # @@ -62,5 +61,27 @@ def set_IO_stream_encodings(encoding): sys.stdout = codecs.getwriter(encoding)(sys.__stdout__) sys.stderr = codecs.getwriter(encoding)(sys.__stderr__) + + def _guess_encoding(self): + return encoding.get_encoding() + def _check_encoding(value): + if value != None: + return encoding.known_encoding(value) + def _setup_encoding(self, new_encoding): + # change hook called before generator. + if new_encoding not in [None, settings_object.EMPTY]: + if self._manipulate_encodings == True: + encoding.set_IO_stream_encodings(new_encoding) + def _set_encoding(self, old_encoding, new_encoding): + self._setup_encoding(new_encoding) + self._prop_save_settings(old_encoding, new_encoding) + + @_versioned_property(name="encoding", + doc="""The default input/output encoding to use (e.g. "utf-8").""", + change_hook=_set_encoding, + generator=_guess_encoding, + check_fn=_check_encoding) + def encoding(): return {} + if libbe.TESTING == True: suite = doctest.DocTestSuite() -- cgit From f8a498f76d7bbcb42cf7bbc80164d98bfe57f8ab Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 12 Dec 2009 01:43:20 -0500 Subject: Added libbe.ui.util.user for managing user ids. --- libbe/util/encoding.py | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index 21e40cf..67131bf 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -43,6 +43,15 @@ def get_encoding(): # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ' return encoding +def get_input_encoding(): + return get_encoding() + +def get_output_encoding(): + return get_encoding(): + +def get_filesystem_encoding(): + return get_encoding() + def known_encoding(encoding): """ >>> known_encoding("highly-unlikely-encoding") @@ -56,32 +65,5 @@ def known_encoding(encoding): except LookupError: return False -def set_IO_stream_encodings(encoding): - sys.stdin = codecs.getreader(encoding)(sys.__stdin__) - sys.stdout = codecs.getwriter(encoding)(sys.__stdout__) - sys.stderr = codecs.getwriter(encoding)(sys.__stderr__) - - - def _guess_encoding(self): - return encoding.get_encoding() - def _check_encoding(value): - if value != None: - return encoding.known_encoding(value) - def _setup_encoding(self, new_encoding): - # change hook called before generator. - if new_encoding not in [None, settings_object.EMPTY]: - if self._manipulate_encodings == True: - encoding.set_IO_stream_encodings(new_encoding) - def _set_encoding(self, old_encoding, new_encoding): - self._setup_encoding(new_encoding) - self._prop_save_settings(old_encoding, new_encoding) - - @_versioned_property(name="encoding", - doc="""The default input/output encoding to use (e.g. "utf-8").""", - change_hook=_set_encoding, - generator=_guess_encoding, - check_fn=_check_encoding) - def encoding(): return {} - if libbe.TESTING == True: suite = doctest.DocTestSuite() -- cgit From dff6bd9bf89ca80e2265696a478e540476718c9c Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 12 Dec 2009 20:57:59 -0500 Subject: Moved be to libbe.ui.command_line and transitioned to Command format. --- libbe/util/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index 67131bf..af312c1 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -47,7 +47,7 @@ def get_input_encoding(): return get_encoding() def get_output_encoding(): - return get_encoding(): + return get_encoding() def get_filesystem_encoding(): return get_encoding() -- cgit From 19fe0817ba7c2cd04caea3adfa82d4490288a548 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 07:37:51 -0500 Subject: Transitioned comment to Command format --- libbe/util/encoding.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index af312c1..434bae7 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -22,6 +22,7 @@ Support input/output/filesystem encodings (e.g. UTF-8). import codecs import locale import sys +import types import libbe if libbe.TESTING == True: @@ -65,5 +66,26 @@ def known_encoding(encoding): except LookupError: return False +def get_file_contents(path, mode='r', encoding=None, decode=False): + if decode == True: + if encoding == None: + encoding = get_filesystem_encoding() + f = codecs.open(path, mode, encoding) + else: + f = open(path, mode) + contents = f.read() + f.close() + return contents + +def set_file_contents(path, contents, mode='w', encoding=None): + if type(value) == types.UnicodeType: + if encoding == None: + encoding = get_filesystem_encoding() + f = codecs.open(path, mode, encoding) + else: + f = open(path, mode) + f.write(contents) + f.close() + if libbe.TESTING == True: suite = doctest.DocTestSuite() -- cgit From 595e4efee8b736e97c31eb0810ffeea508257413 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 21:03:47 -0500 Subject: Transitioned html to Command-format --- libbe/util/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index 434bae7..dcc41f8 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -78,7 +78,7 @@ def get_file_contents(path, mode='r', encoding=None, decode=False): return contents def set_file_contents(path, contents, mode='w', encoding=None): - if type(value) == types.UnicodeType: + if type(contents) == types.UnicodeType: if encoding == None: encoding = get_filesystem_encoding() f = codecs.open(path, mode, encoding) -- cgit From 89b7a1411e4658e831f5d635534b24355dbb941d Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 15 Dec 2009 06:44:20 -0500 Subject: Fixed libbe.command.diff + ugly BugDir.duplicate_bugdir implementation duplicate_bugdir() works, but for the vcs backends, it could require shelling out for _every_ file read. This could, and probably will, be horribly slow. Still it works ;). I'm not sure what a better implementation would be. The old implementation checked out the entire earlier state into a temporary directory pros: single shell out, simple upgrade implementation cons: wouldn't work well for HTTP backens I think a good solution would run along the lines of the currently commented out code in duplicate_bugdir(), where a VersionedStorage.changed_since(revision) call would give you a list of changed files. diff could work off of that directly, without the need to generate a whole duplicate bugdir. I'm stuck on how to handle upgrades though... Also removed trailing whitespace from all python files. --- libbe/util/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index dcc41f8..7706105 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -72,7 +72,7 @@ def get_file_contents(path, mode='r', encoding=None, decode=False): encoding = get_filesystem_encoding() f = codecs.open(path, mode, encoding) else: - f = open(path, mode) + f = open(path, mode) contents = f.read() f.close() return contents -- cgit