コード例 #1
0
def GenFromDb(dbdir='~/.frackbar', dbfile='frackbar.sqlite', imdir='barcodes'):
    """ Generates barcodes in .png format. It uses manually defined 'special'
  codes and appends all barcodes present in the database to this list of codes
  it has to parse. Additionally generate a HTML page with the images..."""
    if dbdir.startswith('~'):
        dbdir = os.path.expanduser(dbdir)
    if not dbdir.endswith('/'):
        dbdir += '/'
    if not imdir.endswith('/'):
        imdir += '/'
    dbfile = os.path.join(dbdir, dbfile)
    imdir = os.path.join(dbdir, imdir)

    # Check if the dbfile exists, stops the operation if it doesnt exist.
    if not os.path.isfile(dbfile):
        print u"\r\n*** Database %s not found. Nothing to do...\r\n" % dbfile
        return
    # Only need to check the imdir as makedirs recursivly creates dirs.
    if not os.path.isdir(imdir):
        os.makedirs(imdir)

    # All tests evaluated propperly, let's connect things up and prepare to work!
    dbase = sqlite.Connection(dbfile)
    font = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Bold.ttf"
    imager = bci.BarcodeImage(code128.Code128(),
                              font_file=font,
                              font_size=20,
                              barwidth=2,
                              dpi=192)

    # Define the 'special' barcodes in below dictionary ...
    barcodes = [{
        'barcode': 'FRACKMEMBER',
        'text': 'Cash mode member toggle'
    }, {
        'barcode': 'FCC',
        'text': 'No creditcard'
    }]

    # Now get the barcodes from the database!
    with dbase as cursor:
        barcodes += cursor.execute(
            """SELECT DISTINCT `barcode`, `name` AS `text`
                               FROM `product`""")
        barcodes += cursor.execute(
            """SELECT DISTINCT `barcode`, `barcode` AS `text`
                               FROM `creditcard`""")

    # With all the gathered resources, start generating images...
    htmlimg = ""
    print u"Generating %d barcodes ..." % len(barcodes)
    for items in barcodes:
        filename = imdir + items['barcode'] + '.png'
        imager(items['barcode'], alt_text=items['text'], output=filename)
        htmlimg += """<tr>
                    <td height='100px'><img src='%s' border=0></td>
                    <td>%s</td>
               </tr>""" % (items['barcode'] + '.png', items['text'])
    WriteHtml(htmlimg, imdir + 'barcodes.html')
    print u"Done!\r\nBarcodes can be found in %s" % imdir
コード例 #2
0
 def _CreateTable(db_file):
     """ Create the database if it does not exist """
     if not os.path.exists(db_file):
         logging.info("Database not found, creating it...")
         with sqlite.Connection(db_file) as cursor:
             cursor.executescript(file('oxbar.schema').read())
             cursor.executescript(file('oxbar.data').read())
コード例 #3
0
 def __init__(self, dbfile='frackbar.sqlite'):
     """ Initiate the DB Connection """
     dbdir = os.path.expanduser("~/.frackbar/")
     dbfile = os.path.join(dbdir, dbfile)
     if not os.path.isdir(dbdir):
         os.makedirs(dbdir)
     if not os.path.isfile(dbfile):
         self._CreateTable(dbfile)
     self.connection = sqlite.Connection(dbfile)
コード例 #4
0
ファイル: db.py プロジェクト: cashweaver-zz/yahoostockdb
 def run(self):
     cnx = lite.Connection(self.db_path)
     cursor = cnx.cursor()
     while True:
         req, arg, res = self.reqs.get()
         if req == '--close--': break
         cursor.execute(req, arg)
         if res:
             for rec in cursor:
                 res.put(rec)
             res.put('--no more--')
     cnx.close()
コード例 #5
0
ファイル: parsexml.py プロジェクト: germank/junkyard
def main():
    parser = optparse.OptionParser(usage='Usage: %prog <data directory>')
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_usage()
        sys.exit(1)

    datadir = args[0]

    temp_file = os.path.join(datadir, 'index.sqlite.temp')
    final_file = os.path.join(datadir, 'index.sqlite')
    if os.path.exists(temp_file):
        log('Removing %s\n' % temp_file)
        os.unlink(temp_file)
    conn = sqlite.Connection(temp_file)

    c = conn.cursor()
    c.execute(
        'CREATE TABLE files (name TEXT, name_low TEXT, ext TEXT, size INTEGER, userid INTEGER, dirid INTEGER)'
    )
    c.execute(
        'CREATE TABLE users (userid INTEGER PRIMARY KEY, name TEXT, name_low TEXT, seen INTEGER)'
    )
    c.execute(
        'CREATE TABLE dirs (dirid INTEGER PRIMARY KEY, parent INTEGER, name TEXT)'
    )
    # dirid=-1 means no parent directory

    c.execute('CREATE INDEX idx1 ON files (name_low)')
    c.execute('CREATE INDEX idx2 ON files (ext)')
    c.execute('CREATE INDEX idx3 ON files (size)')
    c.execute('CREATE INDEX idx4 ON users (name_low)')

    next_dir_id = [0]

    def alloc_dir_id():
        next_dir_id[0] += 1
        return next_dir_id[0] - 1

    user_list = [
        s.strip().split('\t') for s in file(os.path.join(datadir, 'index'))
    ]
    for userid, (username, seentime, filename) in enumerate(user_list):
        try:
            username.decode('utf-8')
        except:
            username = username.decode('cp1251').encode('utf-8')

        seentime = int(seentime)
        username_u = username.decode('utf-8')
        c.execute(
            'INSERT INTO users VALUES(%s,%s,%s,%s)',
            (userid, username, username_u.lower().encode('utf-8'), seentime))

        def callback(filename_u, ext_u, size, dirid):
            c.execute('INSERT INTO files VALUES(%s,%s,%s,%s,%s,%s)',
                      (filename_u.encode('utf-8'),
                       filename_u.lower().encode('utf-8'),
                       ext_u.lower().encode('utf-8'), size, userid, dirid))

        try:
            log('Parsing %s\n' % filename)
            bzfile = bz2.BZ2File(os.path.join(datadir, filename), 'r')
            parser = make_parser()
            parser.setContentHandler(DcXmlFilesHandler(callback, alloc_dir_id))
            parser.parse(bzfile)
        except KeyboardInterrupt:
            raise
        except:
            traceback.print_exc()

        conn.commit()

    c = None
    conn.close()

    os.rename(temp_file, final_file)
    log('Parsing completed\n')
コード例 #6
0
async def run(opts):
    mapping = config.json.load(opts["config"])
    preferences = config.from_mapping(mapping)
    logger = log.new_logger("avatar", log.Verbosity.DEBUG,
                            log.SIMPLE_TEXT_FORMAT)

    logger.info("Starting avatar process with interval %d.",
                preferences.avatar_interval)

    container = di.default_container

    container.register(config.Config, preferences)
    container.register(logging.Logger, logger)
    container.register(avatar.Connection,
                       sqlite.Connection(preferences.database_filename))
    container.register(avatar.Reader, avatar.sqlite.Reader())
    container.register(
        avatar.Writer,
        avatar.sqlite.Writer(preferences.avatar_reload_timeout,
                             preferences.avatar_retry_timeout,
                             preferences.avatar_max_errors,
                             preferences.avatar_error_timeout))
    container.register(
        avatar.Storage,
        avatar.fs.AsciiFiles(preferences.avatar_directory,
                             preferences.avatar_ascii_width,
                             preferences.avatar_ascii_height))

    client = ipc.Client(preferences.server_ipc_binding)

    download = Download()

    conn_f = await client.connect()
    msg_f = asyncio.ensure_future(client.read())
    timeout_f = asyncio.ensure_future(asyncio.sleep(1))
    clean_f = asyncio.ensure_future(
        asyncio.sleep(preferences.avatar_cleanup_interval))

    if os.name == "posix":
        loop = asyncio.get_event_loop()

        logger.debug("Registerung SIGINT handler.")

        loop.add_signal_handler(signal.SIGINT, lambda: None)

    quit = False

    class Action(Enum):
        NONE = 0
        FETCH = 1
        CLEANUP = 2
        QUIT = 3

    while not quit:
        done, _ = await asyncio.wait([conn_f, msg_f, timeout_f, clean_f],
                                     return_when=asyncio.FIRST_COMPLETED)

        action = Action.NONE

        for f in done:
            if f is msg_f:
                receiver, message = msg_f.result()

                if receiver == "avatar":
                    logger.debug("Message received: '%s'", message)

                    if message == "put":
                        action = Action.FETCH
            elif f is conn_f:
                action = Action.QUIT
            elif f is timeout_f:
                action = Action.FETCH
            elif f is clean_f:
                action = Action.CLEANUP

        if action == Action.QUIT:
            quit = True
        elif action == Action.FETCH:
            download.fetch()
        elif action == Action.CLEANUP:
            download.cleanup()

        for f in done:
            if f is msg_f:
                msg_f = asyncio.ensure_future(client.read())
            elif f is timeout_f:
                timeout_f = asyncio.ensure_future(
                    asyncio.sleep(preferences.avatar_interval))
            elif f is clean_f:
                clean_f = asyncio.ensure_future(
                    asyncio.sleep(preferences.avatar_cleanup_interval))

    logger.info("Stopped.")
コード例 #7
0
async def run_services(opts):
    data_dir = opts.get("data_dir")

    mapping = config.json.load(opts["config"])
    preferences = config.from_mapping(mapping)

    logger = log.new_logger("icbd", preferences.logging_verbosity)

    registry = log.Registry()

    registry.register(logger)

    logger.info("Starting server process with pid %d.", os.getpid())

    container = di.default_container

    connection = sqlite.Connection(preferences.database_filename)

    ipfilter_storage = ipfilter.sqlite.Storage()
    ipfilter_cached = ipfilter.cache.Storage(ipfilter_storage)

    container.register(logging.Logger, logger)
    container.register(log.Registry, registry)
    container.register(config.Config, preferences)
    container.register(ipc.Broadcast, ipc.Broadcast())
    container.register(shutdown.Shutdown, shutdown.Shutdown())
    container.register(ipfilter.Connection, connection)
    container.register(ipfilter.Storage, ipfilter_cached)
    container.register(broker.Broker, broker.memory.Broker())
    container.register(session.Store, session.memory.Store())
    container.register(session.AwayTimeoutTable, timer.TimeoutTable())
    container.register(session.NotificationTimeoutTable, timer.TimeoutTable())
    container.register(reputation.Reputation, reputation.memory.Reputation())
    container.register(group.Store, group.memory.Store())
    container.register(nickdb.Connection, connection)
    container.register(nickdb.NickDb, nickdb.sqlite.NickDb())
    container.register(statsdb.Connection, connection)
    container.register(statsdb.StatsDb, statsdb.sqlite.StatsDb())
    container.register(confirmation.Connection, connection)
    container.register(confirmation.Confirmation,
                       confirmation.sqlite.Confirmation())
    container.register(passwordreset.Connection, connection)
    container.register(passwordreset.PasswordReset,
                       passwordreset.sqlite.PasswordReset())
    container.register(motd.Motd,
                       motd.plaintext.Motd(os.path.join(data_dir, "motd")))
    container.register(manual.Manual,
                       manual.plaintext.Manual(os.path.join(data_dir, "help")))
    container.register(news.News,
                       news.plaintext.News(os.path.join(data_dir, "news")))
    container.register(
        template.Template,
        template.plaintext.Template(os.path.join(data_dir, "templates")))
    container.register(mail.Connection, connection)
    container.register(mail.Sink, mail.sqlite.Sink())
    container.register(avatar.Connection, connection)
    container.register(avatar.Reader, avatar.sqlite.Reader())
    container.register(
        avatar.Writer,
        avatar.sqlite.Writer(preferences.avatar_reload_timeout,
                             preferences.avatar_retry_timeout,
                             preferences.avatar_max_errors,
                             preferences.avatar_error_timeout))
    if avatar.is_available():
        container.register(
            avatar.Storage,
            avatar.fs.AsciiFiles(preferences.avatar_directory,
                                 preferences.avatar_ascii_width,
                                 preferences.avatar_ascii_height))
    else:
        logger.info("Avatar preview not available.")

        container.register(avatar.Storage, avatar.void.Storage())

    with connection.enter_scope() as scope:
        container.resolve(ipfilter.Storage).setup(scope)
        container.resolve(nickdb.NickDb).setup(scope)
        container.resolve(statsdb.StatsDb).setup(scope)
        container.resolve(confirmation.Confirmation).setup(scope)
        container.resolve(mail.Sink).setup(scope)
        container.resolve(avatar.Reader).setup(scope)
        container.resolve(avatar.Writer).setup(scope)
        container.resolve(passwordreset.PasswordReset).setup(scope)

        scope.complete()

    container.resolve(avatar.Storage).setup()

    bus = ipc.Bus()

    await bus.start()

    if os.name == "posix":
        loop = asyncio.get_event_loop()

        loop.add_signal_handler(signal.SIGINT, lambda: server.close())
        loop.add_signal_handler(signal.SIGTERM, lambda: server.close())

    processes = [MailProcess()]

    if avatar.is_available():
        processes.append(AvatarProcess())

    asyncio.gather(*[p.spawn(opts) for p in processes])

    failed = False

    try:
        server = network.Server()

        await server.run()
    except asyncio.CancelledError:
        pass
    except:
        logger.warning(traceback.format_exc())

        failed = True

    await bus.close()

    for p in processes:
        p.exit()

    logger.info("Server stopped.")

    with container.resolve(nickdb.Connection).enter_scope() as scope:
        container.resolve(nickdb.NickDb).set_signoff(scope, core.NICKSERV,
                                                     dateutils.now())

        scope.complete()

    sys.exit(server.exit_code if not failed else core.EXIT_FAILURE)
コード例 #8
0
async def run(opts):
    mapping = config.json.load(opts["config"])
    preferences = config.from_mapping(mapping)
    logger = log.new_logger("mail", log.Verbosity.DEBUG, log.SIMPLE_TEXT_FORMAT)

    logger.info("Starting mail process with interval %.2f.", preferences.mail_interval)

    container = di.default_container

    container.register(config.Config, preferences)
    container.register(logging.Logger, logger)
    container.register(mail.Connection, sqlite.Connection(preferences.database_filename))
    container.register(mail.Queue, mail.sqlite.Queue(preferences.mail_ttl,
                                                     preferences.mail_max_errors,
                                                     preferences.mail_retry_timeout))
    container.register(mail.MTA, mail.smtp.MTA(preferences.smtp_hostname,
                                               preferences.smtp_port,
                                               preferences.smtp_ssl_enabled,
                                               preferences.smtp_start_tls,
                                               preferences.smtp_sender,
                                               preferences.smtp_username,
                                               preferences.smtp_password))

    client = ipc.Client(preferences.server_ipc_binding)

    mailer = Sendmail()

    conn_f = await client.connect()
    msg_f = asyncio.ensure_future(client.read())
    cleanup_f = asyncio.ensure_future(asyncio.sleep(0))
    timeout_f = asyncio.ensure_future(asyncio.sleep(1))

    if os.name == "posix":
        loop = asyncio.get_event_loop()

        logger.debug("Registerung SIGINT handler.")

        loop.add_signal_handler(signal.SIGINT, lambda: None)

    quit = False

    class Action(Enum):
        NONE = 0
        SEND = 1
        CLEANUP = 2
        QUIT = 3

    while not quit:
        done, _ = await asyncio.wait([conn_f, msg_f, cleanup_f, timeout_f], return_when=asyncio.FIRST_COMPLETED)

        action = Action.NONE

        for f in done:
            if f is msg_f:
                receiver, message = msg_f.result()

                if receiver == "mail":
                    logger.debug("Message received: '%s'", message)

                    if message == "put":
                        action = Action.SEND
            elif f is cleanup_f:
                action = Action.CLEANUP
            elif f is timeout_f:
                action = Action.SEND
            elif f is conn_f:
                action = Action.QUIT

        if action == Action.SEND:
            mailer.send()
        elif action == Action.CLEANUP:
            mailer.cleanup()
        elif action == Action.QUIT:
            quit = True

        for f in done:
            if f is msg_f:
                msg_f = asyncio.ensure_future(client.read())
            if f is cleanup_f:
                cleanup_f = asyncio.ensure_future(asyncio.sleep(preferences.mail_cleanup_interval))
            if f is timeout_f:
                timeout_f = asyncio.ensure_future(asyncio.sleep(preferences.mail_interval))

    logger.info("Stopped.")