refactoring for less duplicate code

This commit is contained in:
alterNERDtive 2020-01-06 17:20:37 +01:00
parent 0b32f1fb7f
commit d062d90307

View file

@ -10,46 +10,17 @@ from pyEDSM.edsm.models import System, Commander
# =========================================================================== # ===========================================================================
def distanceBetween(system1, system2): def distanceBetween(system1, system2):
try: coords1 = System(system1).coords
coords1 = System(system1).coords coords2 = System(system2).coords
coords2 = System(system2).coords return int(round(math.sqrt( (coords1['x']-coords2['x'])**2
except ServerError as e:
print(e)
sys.exit(1)
except NotFoundError as e:
print(e)
syst.exit(2)
else:
print(int(round(math.sqrt( (coords1['x']-coords2['x'])**2
+ (coords1['y']-coords2['y'])**2 + (coords1['y']-coords2['y'])**2
+ (coords1['z']-coords2['z'])**2 ),0))) + (coords1['z']-coords2['z'])**2 ),0))
sys.exit(0)
def findCommander(name): def findCommander(name):
try: return Commander(name).currentSystem
currentSystem = Commander(name).currentSystem
except ServerError as e:
print(e)
sys.exit(1)
except NotFoundError as e:
print(e)
sys.exit(2)
else:
print(currentSystem)
sys.exit(0)
def getBodyCount(system): def getBodyCount(system):
try: return System(system).bodyCount
bodyCount = System(system).bodyCount
except ServerError as e:
print(e)
sys.exit(1)
except NotFoundError as e:
print(e)
sys.exit(2)
else:
print(bodyCount)
sys.exit(0)
# =========================================================================== # ===========================================================================
@ -69,7 +40,7 @@ parser_distance = subparsers.add_parser("distancebetween",
+ "on EDSM.") + "on EDSM.")
parser_distance.add_argument("system", nargs=2) parser_distance.add_argument("system", nargs=2)
parser_find = subparsers.add_parser("findCommander", parser_find = subparsers.add_parser("findcommander",
help="Attempts to find a CMDRs last known position. Will exit with code 1 " help="Attempts to find a CMDRs last known position. Will exit with code 1 "
+ "on server error and code 2 if the CMDR could not be found on EDSM.") + "on server error and code 2 if the CMDR could not be found on EDSM.")
parser_find.add_argument("name") parser_find.add_argument("name")
@ -79,9 +50,19 @@ args = parser.parse_args()
# =========================================================================== # ===========================================================================
if args.subCommand == "bodycount": try:
getBodyCount(args.system[0]) if args.subCommand == "bodycount":
elif args.subCommand == "distancebetween": out = getBodyCount(args.system[0])
distanceBetween(args.system[0], args.system[1]) elif args.subCommand == "distancebetween":
elif args.subCommand == "findCommander": out = distanceBetween(args.system[0], args.system[1])
findCommander(args.name) elif args.subCommand == "findcommander":
out = findCommander(args.name)
except ServerError as e:
print(e)
sys.exit(1)
except NotFoundError as e:
print(e)
sys.exit(2)
else:
print(out)
sys.exit(0)