class ShaManager: def __init__(self, path='shas.db', app='futil'): self._logger = FutilLogger(app) self.connection = None self.path = path if (not os.path.exists(self.path)): self.createEmptyDB() def createEmptyDB(self): (con, cur) = self.connect() cur.execute("CREATE TABLE shas (uri TEXT, sha VARCHAR(40), PRIMARY KEY(uri, sha) )") con.commit() def insertUriSha(self, uri, sha): try: (con, cur) = self.connect() query = """ INSERT INTO shas(uri, sha) VALUES ('%s','%s') """ % (uri, sha) cur.execute(query) con.commit() except Exception, e: self._logger.error("Inserting in sha database " + str(e)) pass
class MySQLWrapper(DBWrapper): def __init__(self, app='futil', host='localhost', db='futil', user='******', passwd='futil', table='foafs'): self.data = { 'host':host, 'db':db, 'user':user, 'passwd':passwd, 'table':table} self.connection = None self.log = FutilLogger(app) self.pendingCache = [] def realConnect(self): try: return MySQLdb.connect(host=self.data['host'], db=self.data['db'], user=self.data['user'], passwd=self.data['passwd']) except MySQLdb.Error, e: self.log.error('conecting to db: ' + str(e[1])) sys.exit(-1)
class PTSW: def __init__(self, app='futil', config='.ptsw'): self.rest = "http://pingthesemanticweb.com/rest/?url=" self.log = FutilLogger(app) self.stats = {'pinged':0, 'sioc':0, 'foaf':0, 'doap':0, 'owl':0, 'rdfs':0, 'rdf':0, 'flerror':0} self.pathStats = config self.loadStats() def ping(self, uri): try: import socket socket.setdefaulttimeout(TIMEOUT) #TODO: # proxy: urllib2.ProxyHandler({}) url = self.rest + urllib.quote(uri) data = {} headers = { 'User-Agent' : futil.__agent__ } request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request).read() responseParsed = self.parseResponse(response) ok = (responseParsed['flerror'] == 0) self.setStats(responseParsed) if ok: self.stats['pinged'] += 1 self.log.info(uri+' pinged') else: self.log.error('error pinging ' + uri + ': ' + responseParsed['message']) return ok except Exception, details: print str(details) self.log.error('problem pinging ' + uri + ': ' + str(details)) return False