[MediaWiki-commits] [Gerrit] add. handle instances for FileBroker where file not exists o... - change (analytics/user-metrics)

2013-07-14 Thread Rfaulk (Code Review)
Rfaulk has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/73729


Change subject: add. handle instances for FileBroker where file not exists on 
read.
..

add. handle instances for FileBroker where file not exists on read.

Change-Id: I21870e9aecd59012a44ae647748c01d74efd7034
---
M user_metrics/api/broker.py
1 file changed, 54 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/user-metrics 
refs/changes/29/73729/1

diff --git a/user_metrics/api/broker.py b/user_metrics/api/broker.py
index a91a11e..3bce31f 100644
--- a/user_metrics/api/broker.py
+++ b/user_metrics/api/broker.py
@@ -81,13 +81,19 @@
 
 Remove element with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-del lines[idx]
-break
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+del lines[idx]
+break
+except IOError:
+lines = []
+with open(target, 'w'):
+pass
+
 with open(target, 'w') as f:
 for line in lines:
 f.write(line)
@@ -96,13 +102,19 @@
 
 Update element with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-lines[idx] = json.dumps({key: value}) + '\n'
-break
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+lines[idx] = json.dumps({key: value}) + '\n'
+break
+except IOError:
+lines = []
+with open(target, 'w'):
+pass
+
 with open(target, 'w') as f:
 for line in lines:
 f.write(line)
@@ -111,28 +123,37 @@
 
 Retrieve a value with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-return item[key]
-return None
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+return item[key]
+except IOError:
+with open(target, 'w'):
+pass
+
+return None
 
 def pop(self, target):
 
 Pop the top value from the list
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-if not len(lines):
-try:
-item = json.loads(lines[0])
-key = item.keys()[0]
-except (KeyError, ValueError):
-logging.error(__name__ + ' :: FileBroker.pop - '
- 'Could not parse key.')
-return None
-self.remove(target, key)
-return item[key]
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+if not len(lines):
+try:
+item = json.loads(lines[0])
+key = item.keys()[0]
+except (KeyError, ValueError):
+logging.error(__name__ + ' :: FileBroker.pop - '
+ 'Could not parse key.')
+return None
+self.remove(target, key)
+return item[key]
+except IOError:
+with open(target, 'w'):
+pass
 return None

-- 
To view, visit https://gerrit.wikimedia.org/r/73729
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I21870e9aecd59012a44ae647748c01d74efd7034
Gerrit-PatchSet: 1
Gerrit-Project: analytics/user-metrics
Gerrit-Branch: repair_runtime
Gerrit-Owner: Rfaulk rfaulk...@wikimedia.org

___

[MediaWiki-commits] [Gerrit] add. handle instances for FileBroker where file not exists o... - change (analytics/user-metrics)

2013-07-14 Thread Rfaulk (Code Review)
Rfaulk has submitted this change and it was merged.

Change subject: add. handle instances for FileBroker where file not exists on 
read.
..


add. handle instances for FileBroker where file not exists on read.

Change-Id: I21870e9aecd59012a44ae647748c01d74efd7034
---
M user_metrics/api/broker.py
1 file changed, 54 insertions(+), 33 deletions(-)

Approvals:
  Rfaulk: Verified; Looks good to me, approved



diff --git a/user_metrics/api/broker.py b/user_metrics/api/broker.py
index a91a11e..3bce31f 100644
--- a/user_metrics/api/broker.py
+++ b/user_metrics/api/broker.py
@@ -81,13 +81,19 @@
 
 Remove element with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-del lines[idx]
-break
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+del lines[idx]
+break
+except IOError:
+lines = []
+with open(target, 'w'):
+pass
+
 with open(target, 'w') as f:
 for line in lines:
 f.write(line)
@@ -96,13 +102,19 @@
 
 Update element with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-lines[idx] = json.dumps({key: value}) + '\n'
-break
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+lines[idx] = json.dumps({key: value}) + '\n'
+break
+except IOError:
+lines = []
+with open(target, 'w'):
+pass
+
 with open(target, 'w') as f:
 for line in lines:
 f.write(line)
@@ -111,28 +123,37 @@
 
 Retrieve a value with the given key
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-for idx, line in enumerate(lines):
-item = json.loads(line)
-if item.keys()[0] == key:
-return item[key]
-return None
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+for idx, line in enumerate(lines):
+item = json.loads(line)
+if item.keys()[0] == key:
+return item[key]
+except IOError:
+with open(target, 'w'):
+pass
+
+return None
 
 def pop(self, target):
 
 Pop the top value from the list
 
-with open(target, 'r') as f:
-lines = f.read().split('\n')
-if not len(lines):
-try:
-item = json.loads(lines[0])
-key = item.keys()[0]
-except (KeyError, ValueError):
-logging.error(__name__ + ' :: FileBroker.pop - '
- 'Could not parse key.')
-return None
-self.remove(target, key)
-return item[key]
+try:
+with open(target, 'r') as f:
+lines = f.read().split('\n')
+if not len(lines):
+try:
+item = json.loads(lines[0])
+key = item.keys()[0]
+except (KeyError, ValueError):
+logging.error(__name__ + ' :: FileBroker.pop - '
+ 'Could not parse key.')
+return None
+self.remove(target, key)
+return item[key]
+except IOError:
+with open(target, 'w'):
+pass
 return None

-- 
To view, visit https://gerrit.wikimedia.org/r/73729
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I21870e9aecd59012a44ae647748c01d74efd7034
Gerrit-PatchSet: 1
Gerrit-Project: analytics/user-metrics
Gerrit-Branch: repair_runtime
Gerrit-Owner: Rfaulk rfaulk...@wikimedia.org
Gerrit-Reviewer: Rfaulk rfaulk...@wikimedia.org

___
MediaWiki-commits mailing