Rework application layout.

This commit is contained in:
2013-10-14 00:12:29 +01:00
parent cbc7d1eb9f
commit bca47e6331
7 changed files with 123 additions and 119 deletions

3
.gitignore vendored
View File

@@ -2,3 +2,6 @@ env/
test.py test.py
build/ build/
*.pyc *.pyc
.idea/
dist
*.egg-info

View File

@@ -1,4 +1,4 @@
-e git+https://github.com/nikdoof/eveapi.git#egg=eveapi eveapi
ordereddict ordereddict
jinja2 jinja2
argparse argparse

View File

@@ -1,21 +1,24 @@
#!/usr/bin/env python #!/usr/bin/env python
from distutils.core import setup from setuptools import setup
from standings import __version__ from standings import __version__
setup(name = "standings", setup(name="standings",
version = __version__, version=__version__,
description = "EVE API Standings Page Generator", description="EVE API Standings Page Generator",
author = "Andrew Willaims", author="Andrew Williams",
author_email = "andy@tensixtyone.com", author_email="andy@tensixtyone.com",
url = "https://dev.pleaseignore.com/", url="https://dev.pleaseignore.com/",
keywords = "eveapi", keywords="eveapi",
packages = ['standings',], packages=['standings'],
scripts = ['scripts/evestandings'], package_data={'standings': ['templates/*.html']},
package_data={'standings': ['templates/*.html']}, entry_points={
'console_scripts': [
classifiers = [ 'evestandings = standings.cli:main',
'License :: OSI Approved :: BSD License', ]
'Development Status :: 3 - Alpha', },
] classifiers=[
'License :: OSI Approved :: BSD License',
'Development Status :: 3 - Alpha',
]
) )

View File

@@ -1,94 +1,3 @@
# -*- coding: utf-8 -*- from .core import Standings
import sys __version__ = '0.2'
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from eveapi import EVEAPIConnection, Error
from jinja2 import Environment, PackageLoader
from standings.cache import DbCacheHandler
__version__ = '0.1'
STANDINGS_ALLIANCE = 0
STANDINGS_CORPORATION = 1
class Standings:
"""
Grabs the latest Standings from the EVE API and outputs them into
a nice template format
"""
def __init__(self, keyid, vcode, characterid, dbpath='/tmp/standingscache.sqlite3', type=STANDINGS_ALLIANCE):
self.eveapi = EVEAPIConnection(cacheHandler=DbCacheHandler(dbpath)).auth(keyID=keyid, vCode=vcode)
self.character = characterid
self.standings_type = type
@property
def _get_alliance_id_list(self):
if not hasattr(self, '_allianceids'):
self._allianceids = set([x.allianceID for x in EVEAPIConnection().eve.AllianceList().alliances])
return self._allianceids
def _check_if_corp(self, corpid):
try:
res = EVEAPIConnection().corp.CorporationSheet(corporationID=corpid)
except Error:
return False
return True
def _get_standings(self):
res = self.eveapi.corp.ContactList(characterID=self.character)
standings = OrderedDict()
for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']: standings[x] = []
def parse_list(list, output):
for row in list:
level = float(row['standing'])
if level > 5 and level <= 10:
type = 'excellent'
elif level > 0 and level <= 5:
type = 'good'
elif level < 0 and level >= -5:
type = 'bad'
elif level < -5 and level >= -10:
type = 'terrible'
else:
# Neutral?
type = 'neutral'
if int(row['contactID']) in self._get_alliance_id_list:
rowtype = 'alli'
elif self._check_if_corp(int(row['contactID'])):
rowtype = 'corp'
else:
rowtype = 'char'
output[type].append((rowtype, row['contactID'], row['contactName'], row['standing']))
# Order standings for each group
for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']:
standings[x] = sorted(standings[x], key=lambda v: -int(v[3]))
if self.standings_type == STANDINGS_ALLIANCE:
parse_list(res.allianceContactList, standings)
else:
parse_list(res.corporateContactList, standings)
return standings
def _get_name(self):
res = self.eveapi.corp.CorporationSheet()
if hasattr(res, 'allianceName'): return res.allianceName
return res.corporationName
def _get_html(self, template='standings_list.html'):
if not template: template = 'standings_list.html'
env = Environment(loader=PackageLoader('standings', 'templates'))
template = env.get_template(template)
return template.render(standings=self._get_standings(), name=self._get_name())

13
scripts/evestandings → standings/cli.py Executable file → Normal file
View File

@@ -5,7 +5,8 @@ import os
from argparse import ArgumentParser from argparse import ArgumentParser
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
import standings from standings import Standings
def load_config(file='~/.evestandings.conf'): def load_config(file='~/.evestandings.conf'):
config = ConfigParser() config = ConfigParser()
@@ -19,11 +20,8 @@ def load_config(file='~/.evestandings.conf'):
else: else:
return {} return {}
def output_html(keyid, vcode, character, type):
sys.stdout.write(Standings(keyid, vcode, character, type=type)._get_html())
if __name__ == '__main__':
def main():
parser = ArgumentParser(prog='EVEStandings', description='Outputs a EVE corporation/alliance standings to a HTML page') parser = ArgumentParser(prog='EVEStandings', description='Outputs a EVE corporation/alliance standings to a HTML page')
parser.add_argument('-k', '--keyid', help='Key ID of the API key') parser.add_argument('-k', '--keyid', help='Key ID of the API key')
parser.add_argument('-v', '--vcode', help='vCode of the API key') parser.add_argument('-v', '--vcode', help='vCode of the API key')
@@ -32,7 +30,6 @@ if __name__ == '__main__':
parser.add_argument('-C', '--config', help='Path to your configuration file') parser.add_argument('-C', '--config', help='Path to your configuration file')
parser.add_argument('-f', '--output', help='Output the resulting HTML to a file') parser.add_argument('-f', '--output', help='Output the resulting HTML to a file')
parser.add_argument('--template', help='Location of a customized template to use instead of the default') parser.add_argument('--template', help='Location of a customized template to use instead of the default')
parser.add_argument('--version', action='version', version='%(prog)s ' + standings.__version__)
ns = parser.parse_args() ns = parser.parse_args()
@@ -48,10 +45,10 @@ if __name__ == '__main__':
if not conf.keyid or not conf.vcode: if not conf.keyid or not conf.vcode:
sys.stderr.write('Key ID or vCode is missing, please provide both on the command line or in the config file\n') sys.stderr.write('Key ID or vCode is missing, please provide both on the command line or in the config file\n')
parser.print_help() parser.print_help()
sys.exit(os.EX_USAGE) sys.exit(1)
print ns print ns
obj = standings.Standings(conf.keyid, conf.vcode, conf.character) obj = Standings(conf.keyid, conf.vcode, conf.character)
html = obj._get_html(conf.template) html = obj._get_html(conf.template)
if conf.output: if conf.output:

92
standings/core.py Normal file
View File

@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
import sys
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from eveapi import EVEAPIConnection, Error
from jinja2 import Environment, PackageLoader
from .cache import DbCacheHandler
STANDINGS_ALLIANCE = 0
STANDINGS_CORPORATION = 1
class Standings:
"""
Grabs the latest Standings from the EVE API and outputs them into
a nice template format
"""
def __init__(self, keyid, vcode, characterid, dbpath='/tmp/standingscache.sqlite3', type=STANDINGS_ALLIANCE):
self.eveapi = EVEAPIConnection(cacheHandler=DbCacheHandler(dbpath)).auth(keyID=keyid, vCode=vcode)
self.character = characterid
self.standings_type = type
@property
def _get_alliance_id_list(self):
if not hasattr(self, '_allianceids'):
self._allianceids = set([x.allianceID for x in EVEAPIConnection().eve.AllianceList().alliances])
return self._allianceids
def _check_if_corp(self, corpid):
try:
res = EVEAPIConnection().corp.CorporationSheet(corporationID=corpid)
except Error:
return False
return True
def _get_standings(self):
res = self.eveapi.corp.ContactList(characterID=self.character)
standings = OrderedDict()
for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']: standings[x] = []
def parse_list(list, output):
for row in list:
level = float(row['standing'])
if level > 5 and level <= 10:
type = 'excellent'
elif level > 0 and level <= 5:
type = 'good'
elif level < 0 and level >= -5:
type = 'bad'
elif level < -5 and level >= -10:
type = 'terrible'
else:
# Neutral?
type = 'neutral'
if int(row['contactID']) in self._get_alliance_id_list:
rowtype = 'alli'
elif self._check_if_corp(int(row['contactID'])):
rowtype = 'corp'
else:
rowtype = 'char'
output[type].append((rowtype, row['contactID'], row['contactName'], row['standing']))
# Order standings for each group
for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']:
standings[x] = sorted(standings[x], key=lambda v: -int(v[3]))
if self.standings_type == STANDINGS_ALLIANCE:
parse_list(res.allianceContactList, standings)
else:
parse_list(res.corporateContactList, standings)
return standings
def _get_name(self):
res = self.eveapi.corp.CorporationSheet()
if hasattr(res, 'allianceName'): return res.allianceName
return res.corporationName
def _get_html(self, template='standings_list.html'):
if not template: template = 'standings_list.html'
env = Environment(loader=PackageLoader('standings', 'templates'))
template = env.get_template(template)
return template.render(standings=self._get_standings(), name=self._get_name())

View File

@@ -3,7 +3,7 @@ try:
except ImportError: except ImportError:
raise Exception('Flask is not available, please install if you wish to use Standings as a webapp') raise Exception('Flask is not available, please install if you wish to use Standings as a webapp')
from standings import Standings from .core import Standings
app = Flask(__name__) app = Flask(__name__)
stdobj = Standings() stdobj = Standings()