This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
First run at everkeys.add_key #5
Open
zigdon
wants to merge
6
commits into
eve-val:master
Choose a base branch
from
zigdon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2701368
Untested, but general idea of add_key thread
zigdon 2377fd5
Don't block forever so that we keep checking the stop flag.
zigdon cde7ceb
Queue.Queue, not Queue.queue, silly me.
zigdon b3b9d85
Address review comments: import order and type; log messages
zigdon ae59381
Remove undeeded find_or_create, rewrite key saving code to actually w…
zigdon e5242dc
Explicitly get and pass the account value, making metadata a purly pa…
zigdon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import logging | ||
import Queue | ||
import threading | ||
import time | ||
|
||
import evelink | ||
import evelink.cache.sqlite | ||
from kitnirc.modular import Module | ||
|
||
import schema | ||
|
||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class EveKeysModule(Module): | ||
"""Module to look up details on an EVE API key.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(EveKeysModule, self).__init__(*args, **kwargs) | ||
|
||
self.cache = evelink.cache.sqlite.SqliteCache(".evecache.sqlite3") | ||
self.results = Queue.Queue() | ||
self.tasks = Queue.Queue() | ||
|
||
for i in range(5): | ||
t = threading.Thread(target=self._worker) | ||
t.daemon = True | ||
t.start() | ||
|
||
self._stop = False | ||
|
||
def start(self, *args, **kwargs): | ||
super(EveKeysModule, self).start(*args, **kwargs) | ||
self._stop = False | ||
|
||
def stop(self, *args, **kwargs): | ||
super(EveKeysModule, self).stop(*args, **kwargs) | ||
self.stop = True | ||
for _ in range(10): | ||
if self.tasks.empty(): | ||
break | ||
|
||
_log.info( | ||
"EveKeys shutting down - %d threads still outstanding." % | ||
self.tasks.qsize()) | ||
time.sleep(1) | ||
|
||
if not self.tasks.empty(): | ||
_log.warning( | ||
"EveKeys shutting down - giving up on %d outstanding threads." % | ||
self.tasks.qsize()) | ||
|
||
def _worker(): | ||
while not self.stop: | ||
try: | ||
request = self.tasks.get(True, 1) | ||
except Queue.Empty: | ||
continue | ||
|
||
_add_key(request) | ||
self.tasks.task_done() | ||
|
||
def _add_key(request): | ||
keyid = request['keyid'] | ||
vcode = request['vcode'] | ||
irc_account = request['account'] | ||
|
||
try: | ||
api = evelink.api.API(api_key(keyid, vcode), cache=self.cache) | ||
account = evelink.account.Account(api=api) | ||
result = account.key_info() | ||
except evelink.APIError as e: | ||
_log.warn("Error loading API key(%s): %s" % (keyid, e)) | ||
self.results.put((request, "Failed to load api key %s." % keyid)) | ||
return | ||
|
||
if result: | ||
_log.debug("key: %s, characters: %s" % (keyid, | ||
", ".join(char['name'] for char | ||
in result['characters'].itervalues()))) | ||
else: | ||
_log.warn("key: %s, invalid key.") | ||
self.results.put((request, "Invalid key.")) | ||
return | ||
|
||
try: | ||
summary = _save_key_info(keyid, | ||
vcode, | ||
irc_account, | ||
result['characters']) | ||
return | ||
except DatabaseError as e: | ||
_log.warn("Database error saving key(%s): %s" % (keyid, e)) | ||
self.results.put((request, "Database error, try again later.")) | ||
return | ||
|
||
self.results.put((request, summary)) | ||
|
||
def _save_key_info(keyid, vcode, irc_account, characters): | ||
session = schema.Session() | ||
|
||
# find or create account | ||
account = session.query(schema.Account).get(irc_account) | ||
if not account: | ||
account = schema.Account(irc_account, False) | ||
|
||
# find or create an associated key | ||
key = session.query(schema.ApiKey).get(keyid) | ||
if key: | ||
key.vcode = vcode | ||
else: | ||
key = schema.ApiKey(keyid, vcode) | ||
account.keys.add(key) | ||
|
||
# update/delete existing characters | ||
for character in key.characters: | ||
if character.name in characters: | ||
character.corp = characters[character.name]['corp'] | ||
del(characters[character.name]) | ||
else: | ||
session.delete(character) | ||
|
||
# add new characters | ||
for character in characters.itervalues(): | ||
key.characters.add(schema.Character(character['name'], | ||
character['corp'])) | ||
|
||
# save everything | ||
session.add(account) | ||
session.commit() | ||
return "%d characters added: %s" % ( | ||
len(characters), | ||
", ".join([char['name'] for char in characters])) | ||
|
||
def add_key(self, keyid, vcode, account, metadata): | ||
"""Look up a given API key, associate with account and characters. | ||
|
||
Args: | ||
keyid - API key id. | ||
vcode - API key verification. | ||
account - IRC account name. | ||
metadata - context object passed through | ||
""" | ||
request = { 'keyid': keyid, | ||
'vcode': vcode, | ||
'account': account, | ||
'metadata': metadata } | ||
self.tasks.put(request) | ||
|
||
|
||
# vim: set ts=4 sts=4 sw=4 et: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be grouped with other third party imports