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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
# This file is part of pybliographer
#
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
''' This Module contains the base classes one might want to inherit
from in order to provide a new database format '''
from string import *
import re, copy, os
import Pyblio.Help
from types import *
from Pyblio import Autoload, Config, Iterator, Key, Open, Selection, Utils
from shutil import copyfile
class Entry:
'''
A database entry. It behaves like a dictionnary, which
returns an instance of Description for each key. For example,
entry [\'author\'] is expected to return a Types.AuthorGroup
instance.
Each entry class must define an unique ID, which is used
during conversions.
The entry.key is an instance of Key, and has to be unique over
the whole application.
The entry.type is an instance of Types.EntryDescription. It
links the field names with their type.
'''
id = 'VirtualEntry'
def __init__ (self, key = None, type = None, dict = None):
self.type = type
self.dict = dict or {}
self.key = key
return
def keys (self):
''' returns all the keys for this entry '''
return self.dict.keys()
def has_key (self, key):
if self.dict.has_key(key):
return True
return False
def field_and_loss (self, key):
''' return field with indication of convertion loss '''
return self.dict[key], 0
def __getitem__ (self, key):
''' return text representation of a field '''
return self.field_and_loss(key)[0]
def get(self, key, default=None):
if self.has_key(key):
return self[key]
else:
return default
def __setitem__(self, name, value):
self.dict[name] = value
return
def __delitem__(self, name):
del self.dict[name]
return
def __add__(self, other):
''' Merges two entries, key by key '''
ret = Entry (self.key, self.type, {})
# Prendre ses propres entrees
for f in self.keys():
ret[f] = self[f]
# et ajouter celles qu'on n'a pas
for f in other.keys():
if not self.has_key(f):
ret[f] = other[f]
return ret
def __repr__(self):
''' Internal representation '''
return 'Entry (%s, %s, %s)' % (`self.key`, `self.type`, `self.dict`)
def __str__(self):
''' Nice standard entry '''
tp = self.type.name
fields = self.type.fields
try:
text = '%s [%s]\n' % (tp, self.key.key)
except AttributeError:
text = '%s [no key]\n' %(tp)
text = text + ('-' * 70) + '\n'
dico = self.keys ()
for f in fields:
name = f.name
lcname = lower(name)
if not self.has_key(lcname):
continue
text = text + ' %-14s ' % name
text = text + Utils.format(str(self[lcname]),
75, 17, 17) [17:]
text = text + '\n'
try:
dico.remove(lcname)
except ValueError:
raise ValueError, \
'multiple definitions of field `%s\' in `%s\'' \
% (name, tp)
for f in dico:
text = text + ' %-14s ' % f
text = text + Utils.format(str(self[f]),
75, 17, 17) [17:]
text = text + '\n'
return text
class DataBase:
'''This class represents a full bibliographic database. It
also looks like a dictionnary, each key being an instance of
class Key.
'''
properties = {}
filemeta = {}
id ='VirtualDB'
def __init__(self, url):
''' Open the database referenced by the URL '''
self.key = url
self.dict = {}
self.file_metadata = {}
return
def has_property(self, prop):
'''Indicates if the database has a given property.'''
if self.properties.has_key (prop):
return self.properties [prop]
return True
def generate_key(self, entry):
# call a key generator
keytype = Config.get('base/keyformat').data
return Autoload.get_by_name('key', keytype).data(entry, self)
def add(self, entry):
'''Adds an (eventually) anonymous entry.'''
if entry.key is None:
entry.key = self.generate_key(entry)
else:
entry.key.base = self.key
if self.has_key(entry.key):
prefix = entry.key.key
suffix = ord ('a')
while True:
key = Key.Key(self, prefix + '-' + chr(suffix))
if not self.has_key (key):
break
suffix += 1
entry.key = key
self[entry.key] = entry
return entry
def new_entry(self, type):
'''Creates a new entry of the native type of the database '''
return Entry(None, type)
def keys(self):
'''Returns a list of all the keys available for the database '''
return self.dict.keys()
def has_key(self, key):
'''Tests for a given key '''
return self.dict.has_key(key)
def would_have_key(self, key):
'''Test for a key that would be set on the database '''
return self.has_key(Key.Key(self, key.key))
def __getitem__(self, key):
'''Returns the Entry object associated with the key '''
return self.dict [key]
def __setitem__ (self, key, value):
'''Sets a key Entry '''
key.base = self.key
value.key = key
self.dict[key] = value
return
def __delitem__(self, key):
'''Removes an Entry from the database, by its key '''
del self.dict[key]
return
def __len__(self):
'''Number of entries in the database '''
return len(self.keys())
def __str__(self):
'''Database representation '''
return '<generic bibliographic database (' + `len(self)` + \
' entries)>'
def __repr__(self):
'''Database representation '''
return 'DataBase (%s)' % `self.key`
def iterator(self):
''' Returns an iterator for that database '''
return Iterator.DBIterator(self)
def update(self, sorting=None):
''' Updates the Entries stored in the database '''
if self.key.url [0] != 'file':
raise IOError, "can't update the remote database `%s'" % self.key
name = self.key.url[2]
if Config.get('base/directsave').data:
if Config.get('base/backup').data:
copyfile(name, name + '.bak')
namefile = open(name, 'w')
iterator = Selection.Selection(sort=sorting).iterator(self.iterator())
Open.bibwrite(iterator, out=namefile, how=self.id, database=self)
namefile.close ()
else:
# create a temporary file for the new version
tmp = os.path.join(os.path.dirname(name),
'.#' + os.path.basename(name))
tmpfile = open(tmp, 'w')
iterator = Selection.Selection(sort=sorting).iterator(self.iterator())
Open.bibwrite(iterator, out=tmpfile, how=self.id, database=self)
tmpfile.close()
# if we succeeded, and backup is set, backup file
if Config.get('base/backup').data:
os.rename(name, name + '.bak')
# ...and bring new version online
os.rename(tmp, name)
return
def get_metadata(self, key, default=None):
return self.file_metadata.get(key, default)
def set_metadata(self, key, value):
self.file_metadata[key] = value
|