#!/usr/bin/env python2
"""
Copyright (c) 2008-2011, Anthony Garcia <lagg@lavabit.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""

pool_size = 5

import os, sys
import threading, Queue

try:
    sys.path += sys.argv[1:]
except IndexError:
    pass

import steam
from optf2.backend import database, config

steam.set_api_key(config.ini.get("steam", "api-key"))

inq = Queue.Queue()

class DumpThread(threading.Thread):
    def __init__(self, iq):
        self.iq = iq
        threading.Thread.__init__(self)

    def run(self):
        while True:
            input, name = self.iq.get()

            input.dump()

            self.iq.task_done()

for i in range(pool_size):
    t = DumpThread(inq)
    t.setDaemon(True) # How many people still run pythons older than 2.6 anyway?
    t.start()

if __name__ == "__main__":
    updatepairs = set()

    # Iterate cache dir and gather names to dump (not a perfect method, I know)
    for cachefile in os.listdir(database.CACHE_DIR):
        if not cachefile.startswith("schema-"):
            continue

        toks = cachefile.split('-')

        try:
            updatepairs.add((toks[-2], toks[-1]))
        except:
            pass

    # Update loop
    for scope, lang in updatepairs:
        dbcache = database.cache(mode = scope, language = lang)
        schema = database.schema(dbcache)

        inq.put((schema, scope + '-' + lang))

    inq.join()

@@ -786,23 +786,8 @@
 %22%22%0A%0A
-pool_size = 5%0A%0A
 impo
@@ -817,15 +817,8 @@
 ding
-, Queue
 %0A%0Atr
@@ -994,29 +994,8 @@
 ))%0A%0A
-inq = Queue.Queue()%0A%0A
 clas
@@ -1053,33 +1053,25 @@
 lf, 
-iq):%0A        self.iq = iq
+scope, language):
 %0A   
@@ -1111,112 +1111,206 @@
 lf)%0A
-%0A
+   
     
-def run(self):%0A        while True:%0A            input, name = self.iq.get()%0A%0A            input.dump(
+ self.scope = scope%0A        self.language = language%0A%0A    def run(self):%0A        dbcache = database.cache(mode = self.scope, language = self.language)%0A        schema = database.schema(dbcache
 )%0A%0A 
@@ -1320,177 +1320,89 @@
     
- 
-   self.iq.task_done()%0A%0Afor i in range(pool_size):%0A    t = DumpThread(inq)%0A    t.setDaemon(True) # How many people still run pythons older than 2.6 anyway?%0A    t.start(
+schema.dump()%0A        print(%22%7B0%7D-%7B1%7D: Finished%22.format(self.scope, self.language)
 )%0A%0Ai
@@ -1855,165 +1855,106 @@
     
-dbcache = database.cache(mode = scope, language = lang)%0A        schema = database.schema(dbcache)%0A%0A        inq.put((schema, scope + '-' + lang))%0A%0A    inq.join
+t = DumpThread(scope, lang)%0A%0A        print(%22%7B0%7D-%7B1%7D: Starting%22.format(scope, lang))%0A        t.start
 ()%0A

Use non-queue threading in schema updater