Skip to content

Commit

Permalink
cleanup python2 six and imp libraries
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <kobi.zamir@gmail.com>
  • Loading branch information
yaacov authored and nirs committed Aug 27, 2023
1 parent 91606ad commit 7c1a9d1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 30 deletions.
7 changes: 1 addition & 6 deletions rose-admin
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import sys
import os
import logging

import six

if six.PY2:
import xmlrpclib
else:
from xmlrpc import client as xmlrpclib
from xmlrpc import client as xmlrpclib

from rose.common import config

Expand Down
3 changes: 1 addition & 2 deletions rose/client/game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import time

import six

from twisted.internet import reactor

Expand Down Expand Up @@ -34,7 +33,7 @@ def __init__(self, client, name, drive_func):
def update(self, info):
self.track.update(info)
self.players = {p["name"]: p for p in info['players']}
for player in six.itervalues(self.players):
for player in self.players.values():
self.cars[player['car']].update(player)
if info['started']:
self.drive()
Expand Down
21 changes: 8 additions & 13 deletions rose/client/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import argparse
import imp
import importlib
import logging
import os.path
import sys

from twisted.internet import reactor, protocol
Expand Down Expand Up @@ -73,7 +72,7 @@ def send_message(self, msg):

def load_driver_module(file_path):
"""
Load the driver module from file
Load the driver module from the specified path.
Arguments:
file_path (str): The path to the driver module
Expand All @@ -82,16 +81,12 @@ def load_driver_module(file_path):
Driver module (module)
Raises:
ImportError if the module cannot be loaded
Exception if the module cannot be loaded
"""
module_dir, module_name = os.path.split(os.path.splitext(file_path)[0])
fp, pathname, description = imp.find_module(module_name, [module_dir])
try:
return imp.load_module(module_name, fp, pathname, description)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
spec = importlib.util.spec_from_file_location("driver_module", file_path)
driver_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(driver_module)
return driver_module


def main():
Expand All @@ -109,7 +104,7 @@ def main():

try:
driver_mod = load_driver_module(args.driver_file)
except ImportError as e:
except Exception as e:
log.error("error loading driver module %r: %s", args.driver_file, e)
sys.exit(2)

Expand Down
8 changes: 3 additions & 5 deletions rose/server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import logging
import os

import six

from twisted.internet import reactor, task

from rose.common import actions, config, error, message, obstacles # NOQA
Expand Down Expand Up @@ -51,7 +49,7 @@ def start(self):
if not self.players:
raise error.ActionForbidden("start a game with no players.")
self.track.reset()
for p in six.itervalues(self.players):
for p in self.players.values():
p.reset()
self.timeleft = config.game_duration
self.started = True
Expand Down Expand Up @@ -106,7 +104,7 @@ def drive_player(self, name, info):

def print_stats(self):
lines = ['Stats:']
top_scorers = sorted(six.itervalues(self.players), reverse=True)
top_scorers = sorted(self.players.values(), reverse=True)
for i, p in enumerate(top_scorers):
line = '%d %10s row:%d score:%d' % (i + 1, p.name, p.y, p.score)
lines.append(line)
Expand All @@ -128,6 +126,6 @@ def update_clients(self):
def state(self):
return {'started': self.started,
'track': self.track.state(),
'players': [p.state() for p in six.itervalues(self.players)],
'players': [p.state() for p in self.players.values()],
'timeleft': self.timeleft,
'rate': self.rate}
6 changes: 2 additions & 4 deletions rose/server/score.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
""" Score logic """
import logging

import six

from rose.common import actions, config, obstacles

log = logging.getLogger('score')
Expand All @@ -19,7 +17,7 @@ def process(players, track):
# First handle right and left actions, since they may change in_lane
# status, used for resolving collisions.

for player in six.itervalues(players):
for player in players.values():
if player.action == actions.LEFT:
if player.x > 0:
player.x -= 1
Expand All @@ -31,7 +29,7 @@ def process(players, track):
# the ones out of lane, this ensure the car in lane will have
# priority when picking pinguins and in case of collisions.

sorted_players = sorted(six.itervalues(players),
sorted_players = sorted(players.values(),
key=lambda p: 0 if p.in_lane() else 1)
positions = set()

Expand Down

0 comments on commit 7c1a9d1

Please sign in to comment.