File: Open.py

package info (click to toggle)
pybliographer 1.2.12-4squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,560 kB
  • ctags: 1,413
  • sloc: python: 9,817; xml: 2,560; sh: 813; makefile: 621
file content (230 lines) | stat: -rw-r--r-- 5,996 bytes parent folder | download | duplicates (4)
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
# 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.
# 
# 


from types import *

from Pyblio import Autoload, Config, Exceptions, Fields, Help

import os, string, sys, tempfile, traceback, urllib, urlparse

def url_to_local (url):

    (file, headers) = urllib.urlretrieve (url.get_url ())
    return file


Help.register ('bibopen', """
Syntax: database = bibopen (source)

bibopen  tries several  method  to open  `source'  as a  bibliographic
entry. `source'  can be  a simple file  or even  an URL. FTP  and HTTP
files are automatically fetched. One can even create a specific method
for client/server access for example.

One can apply the following commands on the output of bibopen :

    - database.keys () : lists the available entries
    - database ['key'] : returns a given entry
    - del database ['key'] : removes an entry from the file
    - database.where (...) : searches the base (see also `searching')
""")


def get_by_name (entity, method):
    ''' returns a specific field of the given entity '''

    meth = Autoload.get_by_name ("format", entity)

    if meth and meth.data.has_key (method):
	return meth.data [method]

    return None

def get_by_regexp (entity, method):
    ''' returns a specific field of the given entity '''

    meth = Autoload.get_by_regexp ("format", entity)

    if meth and meth.data.has_key (method):
	return meth.data [method]

    return None


def bibopen (entity, how = None):
    ''' Generic function to open a bibliographic database '''

    def simple_try (url, how):
	# url is Fields.URL instance, only to be passed to opener
	base = None

	if how == None:
	    listedmethods = Autoload.available ("format")

	    for method in listedmethods:
		opener = get_by_name (method, 'open')
		if opener:
		    base = opener (url, 1)
		    if base is not None:
			return base
	    return None

	opener = get_by_name (how, 'open')

	if opener:
	    base = opener (url, 0)
	else:
	    raise Exceptions.FormatError (_("method `%s' provides no opener") % how)

	return base

    # Consider the reference as an URL: url is an Fields.URL instance
    url = Fields.URL (entity)

    if url.url [0] == 'file' and not os.path.exists (url.url [2]):
	raise Exceptions.FileError (_("File `%s' does not exist") % url.get_url ())

    # eventually load a new module
    if how is None:
	handler = Autoload.get_by_regexp ("format", url.get_url ())
	if handler:
	    how = handler.name

    base = simple_try (url, how)

    if base is None:
	raise Exceptions.FormatError (_("don't know how to open `%s'") % entity)

    return base


def bibiter (entity, how = None):
    ''' Generic function to iterate on a bibliographic database '''

    def simple_try (url, how):
	# url is Fields.URL instance, only to be passed to opener
	
	base = None
	if how == None:
	    listedmethods = Autoload.available ('format')

	    for method in listedmethods:
		opener = get_by_name (method, 'iter')
		if opener:
		    base = opener (url, 1)
		    if base is not None:
			return base
	    return None

	opener = get_by_name (how, 'iter')

	if opener:
	    base = opener (url, 0)
	else:
	    raise Exceptions.FormatError (_("method `%s' provides no iterator") % how)

	return base

    # Consider the reference as an URL
    url = Fields.URL (entity)

    if url.url [0] == 'file' and not os.path.exists (url.url [2]):
	raise Exceptions.FileError (_("File `%s' does not exist") % str (url))

    # eventually load a new module
    if how is None:
	handler = Autoload.get_by_regexp ('format', url.get_url ())
	if handler:
	    how = handler.name

    base = simple_try (url, how)

    if base is None:
	raise Exceptions.FormatError (_("don't know how to open `%s'") % entity)

    return base


Help.register ('bibwrite', """
Syntax: bibwrite (iterator, output, how)

This function sends an entry description to the specified output
(stdout by default), formatted as specified by the third argument. By
default, this formatting is the same as the one used by `more'.
""")


def bibwrite (iter, out = None, how = None, database=None):
    ''' writes a descriptions of a list of entries '''

    if database and Config.get ('bibtex/keep-preamble').data:
	preamble = database.get_metadata ('bibtex-preamble', [])
    else:
	preamble = []
	
    # default output
    out = out or sys.stdout

    if how == None:
	entry = iter.first ()
	while entry:
	    out.write (str (entry) + "\n")
	    entry = iter.next ()

	return

    writer = get_by_name (how, 'write')

    if writer is None:
	raise IOError, "type `%s' does not specify write method" % how
    writer (iter, out, preamble=preamble)
    return


Help.register ('bibnew', """
Syntax: bib = bibnew (name, type)

Creates a new bibliographic database of a given type.
""")

def bibnew (name, type = None):

    opener = get_by_name (type, 'new')

    if opener is None:
	if os.path.exists (name):
	    raise IOError, "file `%s' exists" % name

	file = open (name, 'w')
	file.close ()

	return bibopen (name, type)

    # Consider the reference as an URL
    url = list (urlparse.urlparse (name))

    if url [0] == '':
	# Consider we handle a local file
	url [0] = 'file'
	url [2] = os.path.expanduser (url [2])

    return opener (Fields.URL(url))