~sir-rainbow/+junk/scribes-on-win

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
# Copyright © 2005 Lateef Alabi-Oki
#
# This file is part of Scribes.
#
# Scribes 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.
#
# Scribes 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 Scribes; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

"""
This module documents functions to store and get a list of encodings to
display in the open/save dialog.

@author: Lateef Alabi-Oki
@organization: The Scribes Project
@copyright: Copyright © 2005 Lateef Alabi-Oki
@license: GNU GPLv2 or Later
@contact: mystilleef@gmail.com
"""

def open_database(flag="c"):
	"""
	Open encoding database.

	@return: A database object representing the encoding database.
	@rtype: A database Shelve object.
	"""
	from SCRIBES.info import metadata_folder
	from os.path import exists, join
	preference_folder = join(metadata_folder, "Preferences")
	if not exists(preference_folder):
		from os import makedirs
		makedirs(preference_folder)
	database_file = join(preference_folder, "EncodedFiles.gdb")
	from shelve import open
	from anydbm import error
	try:
		database = open(database_file, flag=flag, writeback=False)
	except error:
		database = open(database_file, flag="n", writeback=False)
	return database

def get_value(uri):
	"""
	Get encoding of a file.

	@return: Encoding of a file.
	@rtype: A String object.
	"""
	try:
		value = None
		database = open_database("r")
		value = database[str(uri)]
	except KeyError:
		pass
	finally:
		database.close()
	return value

def set_value(uri, encoding):
	"""
	Set encoding of a file.

	@param uri: Reference to a file..
	@type value: A string.

	@param encoding: Encoding of a file.
	@type encoding: A string object.
	"""
	try:
		database = open_database("w")
		database[str(uri)] = encoding
	finally:
		database.close()
	return

def remove_value(uri):
	"""
	Remove information about encoding of a file.

	@param uri: Reference to a file.
	@type uri: A String object.
	"""
	try:
		database = open_database("w")
		del database[str(uri)]
	except KeyError:
		pass
	finally:
		database.close()
	return