from pysqlite2 import dbapi2 as sqlite
import sys, os.path

class Statistics:
    def __init__(self):
        self.total = 0
        self.passed = 0
        self.failed = 0

    def register(self, manager, parser):
        manager.register(instance=self, event='input', keyword='stats', callback=self.input, order=65535)
        manager.register(instance=self, event='exit', keyword='stats', callback=self.exit)

    def init(self, con):
        create = """
        CREATE TABLE IF NOT EXISTS statistics
        (
           timestamp TIMESTAMP,
           feed varchar(255),
           success integer,
           failure integer
        );"""
        cur = con.cursor()
        cur.execute(create)
        con.commit()
        
    def input(self, feed):
        self.total = len(feed.entries)

    def exit(self, feed):
        self.passed = len(feed.entries)
        self.failed = self.total - self.passed

        dbname = os.path.join(sys.path[0], feed.manager.configname+".db")
        con = sqlite.connect(dbname)
        self.init(con)
        cur = con.cursor()

        cur.execute("insert into statistics (timestamp, feed, success, failure) values (date('now'), '%s', %d, %d);" % (feed.name, self.passed, self.failed))

        con.commit()
        con.close()

import sys, os.path

has_sqlite = True
try:
    from pysqlite2 import dbapi2 as sqlite
except:
    has_sqlite = False

class Statistics:
    def __init__(self):
        self.total = 0
        self.passed = 0
        self.failed = 0

    def register(self, manager, parser):
        manager.register(instance=self, event='input', keyword='stats', callback=self.input, order=65535)
        manager.register(instance=self, event='exit', keyword='stats', callback=self.exit)

    def init(self, con):
        create = """
        CREATE TABLE IF NOT EXISTS statistics
        (
           timestamp TIMESTAMP,
           feed varchar(255),
           success integer,
           failure integer
        );"""
        cur = con.cursor()
        cur.execute(create)
        con.commit()
        
    def input(self, feed):
        if not has_sqlite:
            raise Exception('module statistics requires python-sqlite2 (Sqlite v3) library.')
        self.total = len(feed.entries)

    def exit(self, feed):
        self.passed = len(feed.entries)
        self.failed = self.total - self.passed

        dbname = os.path.join(sys.path[0], feed.manager.configname+".db")
        con = sqlite.connect(dbname)
        self.init(con)
        cur = con.cursor()

        cur.execute("insert into statistics (timestamp, feed, success, failure) values (date('now'), '%s', %d, %d);" % (feed.name, self.passed, self.failed))

        con.commit()
        con.close()

Check that python-sqlite2 is installed
Check that python-sqlite2 is installed



git-svn-id: ad91b9aa7ba7638d69f912c9f5d012e3326e9f74@147 3942dd89-8c5d-46d7-aeed-044bccf3e60c

- from pysqlite2 import dbapi2 as sqlite
  import sys, os.path
+ 
+ has_sqlite = True
+ try:
+     from pysqlite2 import dbapi2 as sqlite
+ except:
+     has_sqlite = False
  
  class Statistics:
      def __init__(self):
          self.total = 0
          self.passed = 0
          self.failed = 0
  
      def register(self, manager, parser):
          manager.register(instance=self, event='input', keyword='stats', callback=self.input, order=65535)
          manager.register(instance=self, event='exit', keyword='stats', callback=self.exit)
  
      def init(self, con):
          create = """
          CREATE TABLE IF NOT EXISTS statistics
          (
             timestamp TIMESTAMP,
             feed varchar(255),
             success integer,
             failure integer
          );"""
          cur = con.cursor()
          cur.execute(create)
          con.commit()
          
      def input(self, feed):
+         if not has_sqlite:
+             raise Exception('module statistics requires python-sqlite2 (Sqlite v3) library.')
          self.total = len(feed.entries)
  
      def exit(self, feed):
          self.passed = len(feed.entries)
          self.failed = self.total - self.passed
  
          dbname = os.path.join(sys.path[0], feed.manager.configname+".db")
          con = sqlite.connect(dbname)
          self.init(con)
          cur = con.cursor()
  
          cur.execute("insert into statistics (timestamp, feed, success, failure) values (date('now'), '%s', %d, %d);" % (feed.name, self.passed, self.failed))
  
          con.commit()
          con.close()
  
Check that python-sqlite2 is installed
## Code Before:
from pysqlite2 import dbapi2 as sqlite
import sys, os.path

class Statistics:
    def __init__(self):
        self.total = 0
        self.passed = 0
        self.failed = 0

    def register(self, manager, parser):
        manager.register(instance=self, event='input', keyword='stats', callback=self.input, order=65535)
        manager.register(instance=self, event='exit', keyword='stats', callback=self.exit)

    def init(self, con):
        create = """
        CREATE TABLE IF NOT EXISTS statistics
        (
           timestamp TIMESTAMP,
           feed varchar(255),
           success integer,
           failure integer
        );"""
        cur = con.cursor()
        cur.execute(create)
        con.commit()
        
    def input(self, feed):
        self.total = len(feed.entries)

    def exit(self, feed):
        self.passed = len(feed.entries)
        self.failed = self.total - self.passed

        dbname = os.path.join(sys.path[0], feed.manager.configname+".db")
        con = sqlite.connect(dbname)
        self.init(con)
        cur = con.cursor()

        cur.execute("insert into statistics (timestamp, feed, success, failure) values (date('now'), '%s', %d, %d);" % (feed.name, self.passed, self.failed))

        con.commit()
        con.close()

## Instruction:
Check that python-sqlite2 is installed
## Code After:
import sys, os.path

has_sqlite = True
try:
    from pysqlite2 import dbapi2 as sqlite
except:
    has_sqlite = False

class Statistics:
    def __init__(self):
        self.total = 0
        self.passed = 0
        self.failed = 0

    def register(self, manager, parser):
        manager.register(instance=self, event='input', keyword='stats', callback=self.input, order=65535)
        manager.register(instance=self, event='exit', keyword='stats', callback=self.exit)

    def init(self, con):
        create = """
        CREATE TABLE IF NOT EXISTS statistics
        (
           timestamp TIMESTAMP,
           feed varchar(255),
           success integer,
           failure integer
        );"""
        cur = con.cursor()
        cur.execute(create)
        con.commit()
        
    def input(self, feed):
        if not has_sqlite:
            raise Exception('module statistics requires python-sqlite2 (Sqlite v3) library.')
        self.total = len(feed.entries)

    def exit(self, feed):
        self.passed = len(feed.entries)
        self.failed = self.total - self.passed

        dbname = os.path.join(sys.path[0], feed.manager.configname+".db")
        con = sqlite.connect(dbname)
        self.init(con)
        cur = con.cursor()

        cur.execute("insert into statistics (timestamp, feed, success, failure) values (date('now'), '%s', %d, %d);" % (feed.name, self.passed, self.failed))

        con.commit()
        con.close()
