Skip to content

Commit

Permalink
Quick cleanup of edscupdate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Feb 14, 2015
1 parent b845e8a commit aee5cfc
Showing 1 changed file with 154 additions and 123 deletions.
277 changes: 154 additions & 123 deletions edscupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@
or "q" to stop recording.
"""


import tradedb
import argparse
import math
import misc.clipboard
import misc.edsc
import os
import re
import sys
import tradedb


# Systems we know are bad.
ignore = []


class UsageError(Exception):
""" Raised when command line usage is invalid. """
pass


def get_cmdr(tdb):
""" Look up the commander name """
try:
return os.environ['CMDR']
except KeyError:
Expand All @@ -60,158 +66,183 @@ def get_cmdr(tdb):
)


if 'DEBUG' in os.environ or 'TEST' in os.environ:
testMode = True
else:
testMode = False
def is_change(tdb, sysinfo):
""" Check if a system's EDSC data is different than TDs """
name = sysinfo['name'] = sysinfo['name'].upper()
if name.startswith("argetl"):
return False
if name in ignore:
return False
x, y, z = sysinfo['coord']
try:
place = tdb.systemByName[name]
if place.posX == x and place.posY == y and place.posZ == z:
return False
except KeyError:
place = None
sysinfo['place'] = place
return True

if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: {} <origin system> [date]".format(sys.argv[0]))
sys.exit(1)

tdb = tradedb.TradeDB()
date = tdb.query("SELECT MAX(modified) FROM System").fetchone()[0]
def has_position_changed(sysinfo):
place = sysinfo['place']
if not place:
return False

cmdr = get_cmdr(tdb)
print("! @{} [{},{},{}] vs @{} [{},{},{}]".format(
name, x, y, z,
place.dbname, place.posX, place.posY, place.posZ
))

startSys = tdb.lookupPlace(sys.argv[1])
ox, oy, oz = startSys.posX, startSys.posY, startSys.posZ
return True

if len(sys.argv) > 2:
date = sys.argv[2]
if not date.startswith("201"):
print("ERROR: Invalid date {}".format(date))
sys.exit(2)

print("start date: {}".format(date), file=sys.stderr)
def check_database(tdb, name, x, y, z):
# is it in the database?
cur = tdb.query("""
SELECT name, pos_x, pos_y, pos_z
FROM System
WHERE pos_x BETWEEN ? and ?
AND pos_y BETWEEN ? and ?
AND pos_z BETWEEN ? and ?
""", [
x - 0.5, x + 0.5,
y - 0.5, y + 0.5,
z - 0.5, z + 0.5,
])
for mname, mx, my, mz in cur:
print(
"! @{} [{},{},{}] matches coords for "
"@{} [{},{},{}]".format(
name, x, y, z,
mname, mx, my, mz
), file=sys.stderr)

confidence = os.environ.get("CONF", 2)

edsq = misc.edsc.StarQuery(
test=testMode,
confidence=confidence,
date=date,
def get_distance(tdb, startSys, x, y, z):
distance = tdb.calculateDistance(
startSys.posX, startSys.posY, startSys.posZ,
x, y, z
)
data = edsq.fetch()
return float("{:.2f}".format(distance))

ignore = [
]

if edsq.status['statusnum'] != 0:
raise Exception("Query failed: {} ({})".format(
edsq.status['msg'],
edsq.status['statusnum'],
))
def main():
if 'DEBUG' in os.environ or 'TEST' in os.environ:
testMode = True
else:
testMode = False

date = data['date']
systems = data['systems']
clip = misc.clipboard.SystemNameClip()
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: {} <origin system> [date]".format(sys.argv[0]))
sys.exit(1)

tdb = tradedb.TradeDB()
date = tdb.query("SELECT MAX(modified) FROM System").fetchone()[0]

def dist(x, y, z):
return ((ox-x)**2 + (oy-y)**2 + (oz-z)**2) ** 0.5
cmdr = get_cmdr(tdb)

startSys = tdb.lookupPlace(sys.argv[1])

def ischange(sysinfo):
name = sysinfo['name'] = sysinfo['name'].upper()
if name.startswith("argetl"):
return False
if name in ignore:
return False
x, y, z = sysinfo['coord']
try:
place = tdb.systemByName[name]
if place.posX == x and place.posY == y and place.posZ == z:
return False
except KeyError:
place = None
sysinfo['place'] = place
return True
if len(sys.argv) > 2:
date = sys.argv[2]
if not date.startswith("201"):
print("ERROR: Invalid date {}".format(date))
sys.exit(2)

print("start date: {}".format(date), file=sys.stderr)

confidence = os.environ.get("CONF", 2)

edsq = misc.edsc.StarQuery(
test=testMode,
confidence=confidence,
date=date,
)
data = edsq.fetch()

if edsq.status['statusnum'] != 0:
raise Exception("Query failed: {} ({})".format(
edsq.status['msg'],
edsq.status['statusnum'],
))

systems = [
sysinfo for sysinfo in systems if ischange(sysinfo)
]
print("{} changes".format(len(systems)))
date = data['date']
systems = data['systems']
clip = misc.clipboard.SystemNameClip()

print("{} results".format(len(systems)))
# Filter out systems we already know that match the EDSC data.
systems = [
sysinfo for sysinfo in systems if is_change(tdb, sysinfo)
]
print("{} deltas".format(len(systems)))

if len(systems) <= 0:
return

if len(systems) > 0:
print("At the prompt enter y, n or q. Default is n")
print(
"To correct a typo'd name that has the correct distance, "
"use =correct name"
)
print()

total = len(systems)
current = 0
with open("tmp/new.systems.csv", "a") as output:
try:
total = len(systems)
current = 0
with open("tmp/new.systems.csv", "a") as output:
for sysinfo in systems:
current += 1
name = sysinfo['name']
x, y, z = sysinfo['coord']
place = sysinfo['place']
if place:
print("! @{} [{},{},{}] vs @{} [{},{},{}]".format(
name, x, y, z,
place.dbname, place.posX, place.posY, place.posZ
), file=sys.stderr)
else:
created = sysinfo['createdate']

# is it in the database?
cur = tdb.query("""
SELECT name, pos_x, pos_y, pos_z
FROM System
WHERE pos_x BETWEEN ? and ?
AND pos_y BETWEEN ? and ?
AND pos_z BETWEEN ? and ?
""", [
x - 0.5, x + 0.5,
y - 0.5, y + 0.5,
z - 0.5, z + 0.5,
])
for mname, mx, my, mz in cur:
print(
"! @{} [{},{},{}] matches coords for "
"@{} [{},{},{}]".format(
name, x, y, z,
mname, mx, my, mz
), file=sys.stderr)
distance = float("{:.2f}".format(dist(x, y, z)))
clip.copy_text(name.lower())
prompt = "{}/{}: '{}': {:.2f}ly? ".format(
current, total,
name,
distance,
)
ok = input(prompt)
if ok.lower() == 'q':
break
if ok.startswith('='):
name = ok[1:].strip().upper()
ok = 'y'
with open("data/extra-stars.txt", "a") as fh:
print(name, file=fh)
print("Added to data/extra-stars.txt")
if ok.lower() == 'y':
print("'{}',{},{},{},'Release 1.00-EDStar','{}'".format(
name, x, y, z, created,
), file=output)
sub = misc.edsc.StarSubmission(
star=name.upper(),
commander=cmdr,
distances={startSys.name(): distance},
test=testMode,
)
r = sub.submit()
result = misc.edsc.StarSubmissionResult(
star=name.upper(),
response=r,
)

print(str(result))

if has_position_changed(sysinfo):
continue

check_database(tdb, name, x, y, z)

created = sysinfo['createdate']

distance = get_distance(tdb, startSys, x, y, z)
clip.copy_text(name.lower())
prompt = "{}/{}: '{}': {:.2f}ly? ".format(
current, total,
name,
distance,
)
ok = input(prompt)
if ok.lower() == 'q':
break
if ok.startswith('='):
name = ok[1:].strip().upper()
ok = 'y'
with open("data/extra-stars.txt", "a") as fh:
print(name, file=fh)
print("Added to data/extra-stars.txt")
if ok.lower() != 'y':
continue

print("'{}',{},{},{},'Release 1.00-EDStar','{}'".format(
name, x, y, z, created,
), file=output)
sub = misc.edsc.StarSubmission(
star=name.upper(),
commander=cmdr,
distances={startSys.name(): distance},
test=testMode,
)
r = sub.submit()
result = misc.edsc.StarSubmissionResult(
star=name.upper(),
response=r,
)

print(str(result))


if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("^C")

0 comments on commit aee5cfc

Please sign in to comment.