log_event: Use 'with' for files

(imported from commit d49b46ecd8aa74679ade3820004d10cb42940ad3)
This commit is contained in:
Keegan McAllister 2012-11-14 10:43:32 -05:00
parent ccf14b89c2
commit af36b437b6
1 changed files with 8 additions and 8 deletions

View File

@ -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')
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)
f = open(settings.MESSAGE_LOG, "a")
f.write(simplejson.dumps(event) + "\n")
f.close()
with open(settings.MESSAGE_LOG, 'a') as log:
log.write(simplejson.dumps(event) + '\n')
fcntl.flock(lock, fcntl.LOCK_UN)
lock.close()
def log_message(message):
if not message.sending_client.name.startswith("test:"):