This file is indexed.

/usr/lib/python2.7/dist-packages/pySesame.py is in python-sesame 0.24-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#
#	 pySesame 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.
#
#	 pySesame 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.
#
#	 A copy of the full General Public License is available at
#	 http://www.gnu.org/copyleft/gpl.html and from the Free Software
#	 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
#
# 20050606 *pike 
# 	This version was modified to work with python 2>, which has
#	notable differences in the httplib module. I have kept the
#	interface the same.
#


import logging
from string import split
from urllib2 import urlopen
from urllib2 import Request
from urllib import urlencode
from xml.sax.handler import ContentHandler
from xml.sax import make_parser

query_paths = {'login':'servlets/login',
			   'logout':'servlets/logout',
			   'repositories':'servlets/listRepositories',
			   'query':'servlets/evaluateTableQuery',
			   'construct':'servlets/evaluateGraphQuery',
			   'extract':'servlets/extractRDF',
			   'uploadData':'servlets/uploadData',
			   'uploadURL':'servlets/uploadURL',
			   'clear':'servlets/clearRepository',
			   'remove':'servlets/removeStatements?',
			   }

class SesameConnection:
	"The main class for communicating with a Sesame Server."
	def __init__(self, url, path, logLevel="warning", logPath=""):
		self.hdlr = None
		self.setLogger(logLevel, logPath)
		self.url = url
		self.path = path
		self.cookie = ""
		self.resetQuery()
		self.logger.info('SesameConnection starting up...')

	def close(self):
		self.logout()
		self.logger.info('...SesameConnection closing down.')
		self.logger.removeHandler(self.hdlr)

	def setLogger(self, level, path):	 
		if self.hdlr: self.logger.removeHandler(self.hdlr)
		levelDict = {'debug':logging.DEBUG,
					 'info':logging.INFO,
					 'warning':logging.WARNING,
					 'error':logging.ERROR,
					 'critical':logging.CRITICAL}
		self.logger = logging.getLogger()
		self.hdlr = logging.FileHandler(path + 'pySesame.log')
		formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
		self.hdlr.setFormatter(formatter)
		self.logger.addHandler(self.hdlr)
		self.logger.setLevel(levelDict[level])

	def resetQuery(self):
		self.query = {}

	def doRequest(self, request, handler=None, post=None):
		# Sesame uses the text/plain content type for XML responses
		request.add_header("Accept", "text/plain")
		request.add_header("User-Agent", "PySesame 0.2")
		
		if self.cookie != "":
			cookie_string = "sesame_sid=%s" % self.cookie
			request.add_header("Cookie", cookie_string)

		if post != None:
			request.add_data(post)
		
		self.logger.debug(request.get_method()+":" + request.get_full_url())
			
		try:
			response = urlopen(request)
			# debug alls ok. i'd like to print 
			# the Status header, but urllib2 has already eaten it
			# and thrown its appropriate errors or followed 
			# any redirects etc
			if response.headers.has_key('Content-Length'): 
				self.logger.debug("OK: "+response.headers['Content-Length']+" bytes")
			else:
				self.logger.debug("OK")
		except Exception, error:
			self.logger.exception(error)
			raise SesameServerError(error)

		if response:
			if handler == "login":
				if not self.CookieHandler(response): 
					raise SesameServerError("No SessionID in cookie")
				# returns None on succes ....
			elif handler:
				saxparser = make_parser()
				saxparser.setContentHandler(handler)
				parsed = saxparser.parse(response)
				response.close()
				return handler.results
			else:
				results = response.readlines()
				resultString = ""
				resultString = resultString.join(results)
				return resultString

	def login(self, user, password):
		"""
		Gets your cookie.
		Returns None if successful.
		Throws an error if not
		"""
		self.resetQuery()
		self.query['user'] = user
		self.query['password'] = password
		query = urlencode(self.query)
		base = self.path + query_paths['login']
		request = Request(self.url+base)
		return self.doRequest(request, "login",query)

	def logout(self):
		"""
		Logs you out.
		Returns 'Logged out successfully' if successful.
		"""
		self.resetQuery()
		base = self.path + query_paths['logout']
		request = Request(self.url+base)
		return self.doRequest(request,None,"post")

	def repositories(self):
		"""
		Returns a list of dictionaries about available repositories.
		The dictionary keys are "id", "title", "readable" and "writeable"
		"""
		self.resetQuery()
		base = self.path + query_paths['repositories']
		query = urlencode(self.query)
		request = Request(self.url + base + query)
		handler = RepositoriesHandler()
		return self.doRequest(request, handler)
		
	def tableQuery(self, repository, queryLanguage, query,
				resultFormat="xml", serialization = "rdfxml"):
		"""
		xml format results are returned as a data structure as follows:
		* a dictionary with two keys, "header" and "tuples"
		* header is a list of string values
		* tuples is a list of lists.
		* each sub-list is like a row in the table
		* each sub-list is made up of dictionaries, wherein:
		* the key a string made from the text of the node
		* the value is the type ('uri', 'literal' or 'bNode')

		All other formats are returned as a string.		   
		"""
		self.resetQuery()
		self.query['repository'] = repository
		self.query['queryLanguage'] = queryLanguage
		self.query['resultFormat'] = resultFormat
		self.query['query'] = query
		if resultFormat == 'rdf':
			self.query['serialization'] = serialization
			handler = None
		elif resultFormat == 'html':
			handler = None
		else:
			handler = SelectQueryHandler()
		base = self.path + query_paths['query']
		request = Request(self.url+base)
		query = urlencode(self.query)
		return self.doRequest(request, handler, query)

	def graphQuery(self, repository, queryLanguage, query,
				serialization = "rdfxml"):
		"""
		Returns a string.
		"""
		self.resetQuery()
		self.query['repository'] = repository
		self.query['queryLanguage'] = queryLanguage
		self.query['query'] = query
		self.query['serialization'] = serialization
		handler = None
		# query_path *was* 'query' .. seemed wrong ..
		base = self.path + query_paths['construct']
		request = Request(self.url+base)
		query = urlencode(self.query)
		return self.doRequest(request, handler, query)
	
	def extract(self, repository, schema="no", data="no", explicitOnly="no",
				niceOutput="no", serialization="rdfxml"):
		"""
		Returns a string.
		"""
		self.resetQuery()
		self.query['repository'] = repository
		self.query['schema'] = schema
		self.query['data'] = data
		self.query['explicitOnly'] = explicitOnly
		self.query['niceOutput'] = niceOutput
		self.query['serialization'] = serialization

		base = self.path + query_paths['extract']
		query = urlencode(self.query)
		request = Request(self.url + base + '?' + query)
		return self.doRequest(request)

	def uploadData(self, repository, data = "", dataFormat="rdfxml",
				   baseURL="", format="xml", verifyData="yes"):
		"""
		Adds a string ("data") of RDF data to the repository.
		Returns 1 if successful.
		"""
		self.resetQuery()
		self.query['data'] = data
		self.query['repository'] = repository
		self.query['dataFormat'] = dataFormat
		self.query['baseURL'] = baseURL
		self.query['format'] = format
		base = self.path + query_paths['uploadData']
		request = Request(self.url+base)
		query = urlencode(self.query)
		handler = PostHandler()
		return self.doRequest(request, handler, query)

	def uploadURL(self, repository, dataURL, dataFormat="rdfxml",
				  baseURL="", format="xml", verifyData="yes"):
		"""
		Attempts to add whatever is at dataURL to the repository.
		Returns 1 if successful.
		"""
		self.resetQuery()
		self.query['url'] = dataURL
		self.query['repository'] = repository
		self.query['dataFormat'] = dataFormat
		self.query['baseURL'] = baseURL
		self.query['format'] = format
		base = self.path + query_paths['uploadURL']
		request = Request(self.url+base)
		query = urlencode(self.query)
		handler = PostHandler()
		return self.doRequest(request, handler, query)
		
	def clear(self, repository, format="xml"):
		"""
		Clears a repository.
		Returns 1 if successful.
		"""
		self.resetQuery()
		self.query['repository'] = repository
		base = self.path + query_paths['clear']
		request = Request(self.url+base)
		query = urlencode(self.query)
		handler = PostHandler()
		return self.doRequest(request, handler, query)

	def remove(self, repository, subject, predicate, object,
			   resultFormat="xml"):
		"""
		Removes statements from repository.
		Returns 1 if successful.
		"""
		self.resetQuery()
		self.query['repository'] = repository
	
		base = self.path + query_paths['remove']
		request = Request(self.url+base)
		if subject:
			self.query['subject'] = subject
		if predicate:
			self.query['predicate'] = predicate
		if object:
			self.query['object'] = object
		self.query['resultFormat'] = resultFormat
		query = urlencode(self.query)
		handler = PostHandler()
		return self.doRequest(request, handler, query)

	def CookieHandler(self, response):
		value = False
		info = response.info()
		cookies = info.getheaders("Set-Cookie")
		for cookie in cookies:
			if 'sesame_sid' in cookie:
				half = split(cookie,';')[0]
				value = split(half,'=')[-1]
				self.logger.debug('Using cookie %s' % value)
				self.cookie = value
		return value
				

class RepositoriesHandler(ContentHandler):
	"""Creates a list of dictionaries describing available servers."""
	def __init__(self):
		self.inRepository = 0
		self.inTitle = 0
		self.repository = {}
		self.results = []
	def startElement(self, name, attrs):
		if self.inRepository and name == "title":
			self.inTitle = 1
			self.title = ""
		elif name == "repository":
			self.repository['id'] = attrs.get('id', '')
			self.repository['readable'] = attrs.get('readable', '')
			self.repository['writeable'] = attrs.get('writeable', '')
			self.inRepository = 1
	def characters(self, ch):
		if self.inTitle:
			self.title = ch
	def endElement(self, name):
		if name == 'title':
			self.repository['title'] = self.title
			self.inTitle = 0
		elif name == 'repository':
			self.results.append(self.repository)
			self.repository = {}
			self.inRepository = 0
		
class SelectQueryHandler(ContentHandler):
	"""."""
	# this doesn't handle the header yet, right?
	def __init__ (self):
		self.types = ['uri', 'literal', 'bNode','null']
		self.inTuple = 0
		self.inResult = 0
		self.inHeader = 0
		self.header = []
		self.tuple = []
		self.tuples = []
	def startElement(self, name, attrs):
		if self.inTuple and name in self.types:
			self.inResult = 1
			self.value = ""
			self.type = name
		elif name == 'tuple':
			self.inTuple = 1
			self.inResult = 0
		elif name == 'columnName':
			self.inHeader = 1
	def characters(self, ch):
		if self.inResult:
			self.value=ch
			self.tuple.append({'value':self.value,'type':self.type}) 
		if self.inHeader:
			self.header.append(ch)
	def endElement(self, name):
		if name == 'tuple':
			self.tuples.append(self.tuple)
			self.tuple = []
			self.inTuple = 0
		if name in self.types:
			self.inResult = 0
		elif name == 'columnName':
			self.inHeader = 0
		elif name == 'tableQueryResult':
			self.results = {'header':self.header,
							'tuples':self.tuples}


class PostHandler(ContentHandler):
	"""Returns 1 if successful.	 Throws an exception if not."""
	def __init__(self):
		self.results = 1
		self.inMsg = 0
		self.line = ""
		self.elements = ['msg', 'line', 'column']

	def startElement(self, name, attrs):
		if name == "error":
			self.results = 0
		if name in ['status', 'notification', 'warning', 'error']:
			self.tag = name
		if name in self.elements:
			self.inMsg = 1

	def characters(self, ch):
		if self.inMsg:
			self.line += "%s: %s,  " % (self.tag, ch)

	def endElement(self, name):
		if name == "error":
			raise SesameServerError(self.line)
			self.line = ""
		if name in self.elements:
			self.inMsg = 0
			
class SesameServerError(Exception):
	pass