Permalink
Browse files

Merge pull request #4 from grierj/master

Some minor improvements to the python lib and cli
  • Loading branch information...
2 parents 882e26b + 8dcd343 commit 9c8ba83c355a13d62086239427b2c90fb9447ddd @erikwb erikwb committed Nov 28, 2011
Showing with 46 additions and 31 deletions.
  1. +27 −19 python_seco_range/root/usr/bin/eh
  2. +19 −12 python_seco_range/source/seco/range.py
@@ -19,26 +19,34 @@ parser.add_option('-e', '--expand', dest="expand",
(options, args) = parser.parse_args()
if len(args) == 0:
- print "Must provide a range to expand"
- sys.exit(1)
+ print "Must provide a range to expand (or - to read from stdin)"
+ sys.exit(1)
+
+input = args[0]
+if args[0] == '-':
+ try:
+ input = sys.stdin.readlines()
+ except KeyboardInterrupt:
+ sys.exit(0)
+ input = [item.rstrip() for item in input]
r = seco.range.Range(host=options.vip)
-if options.expand:
- try:
- expansion = r.expand(args[0])
- except seco.range.RangeException as e:
- print "Range server exception: %s" % e
- sys.exit(1)
+
+if options.count or options.expand:
+ try:
+ expansion = r.expand(input)
+ except seco.range.RangeException as e:
+ print "Range server exception: %s" % e
+ sys.exit(1)
+ if options.count:
+ print len(expansion)
+ else:
for e in expansion:
- print e
+ print e
else:
- try:
- expansion = r.expand(args[0], list=False)
- except seco.range.RangeException as e:
- print "Range server exception: %s" % e
- sys.exit(1)
- print expansion
-
-
-
-
+ try:
+ expansion = r.collapse(input)
+ except seco.range.RangeException as e:
+ print "Range server exception: %s" % e
+ sys.exit(1)
+ print expansion
@@ -1,6 +1,6 @@
"""
- Library for querying the range webservice - http://github.com/ytoolshed/range
- ebourget@linkedin.com
+Library for querying the range webservice - http://github.com/ytoolshed/range
+ebourget@linkedin.com
"""
import urllib2
@@ -16,30 +16,37 @@ class Range(object):
def __init__(self, host):
self.host = host
- def expand(self, expr, list=True):
- if list:
- url = 'http://%s/range/list?%s' % (self.host,
- urllib2.quote(expr))
+ def expand(self, expr, ret_list=True):
+ if isinstance(expr, list):
+ expr = ','.join(expr)
+ if ret_list:
+ url = 'http://%s/range/list?%s' % (self.host, urllib2.quote(expr))
else:
- url = 'http://%s/range/expand?%s' % (self.host,
- urllib2.quote(expr))
- req = urllib2.urlopen(url)
+ url = 'http://%s/range/expand?%s' % (self.host, urllib2.quote(expr))
+ req = None
+ try:
+ req = urllib2.urlopen(url)
+ except urllib2.URLError, e:
+ raise RangeException(e)
code = req.getcode()
if code != 200:
- raise RangeException("Got %d response code from %s" %
- (code, url))
+ raise RangeException("Got %d response code from %s" % (code, url))
reqinfo = req.info()
exception = reqinfo.getheader('RangeException')
if exception:
raise RangeException(exception)
- if list:
+ if ret_list:
expansion = []
for line in req.readlines():
expansion.append(line.rstrip())
+ expansion.sort()
return expansion
else:
return req.read()
+ def collapse(self, expr):
+ return self.expand(expr, ret_list=False)
+
if __name__ == '__main__':
try:
r = Range("localhost:80")

0 comments on commit 9c8ba83

Please sign in to comment.