From af36b437b67a420e9675c7c8331fc4f6ca358436 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 14 Nov 2012 10:43:32 -0500 Subject: [PATCH] log_event: Use 'with' for files (imported from commit d49b46ecd8aa74679ade3820004d10cb42940ad3) --- zephyr/models.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/zephyr/models.py b/zephyr/models.py index 63a73d1066..6d33e93ac2 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -474,14 +474,14 @@ def get_user_profile_by_id(uid): def log_event(event): assert("timestamp" in event) if not os.path.exists(settings.MESSAGE_LOG + '.lock'): - file(settings.MESSAGE_LOG + '.lock', "w").write("0") - lock = open(settings.MESSAGE_LOG + '.lock', 'r') - fcntl.flock(lock, fcntl.LOCK_EX) - f = open(settings.MESSAGE_LOG, "a") - f.write(simplejson.dumps(event) + "\n") - f.close() - fcntl.flock(lock, fcntl.LOCK_UN) - lock.close() + with open(settings.MESSAGE_LOG + '.lock', 'w') as lock: + lock.write('0') + + with open(settings.MESSAGE_LOG + '.lock', 'r') as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + with open(settings.MESSAGE_LOG, 'a') as log: + log.write(simplejson.dumps(event) + '\n') + fcntl.flock(lock, fcntl.LOCK_UN) def log_message(message): if not message.sending_client.name.startswith("test:"):