X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_p2p%2Futil.py;h=345ab9afdc918b5945a759816bd28d09216103df;hb=9fc63fde5b46f6afcfb957d685f910d36535c67b;hp=c334d1db9bd07026c5031db8f96a92937800d0f6;hpb=7b1167d8ce780312d3689c9309c7e9c64060c085;p=quix0rs-apt-p2p.git diff --git a/apt_p2p/util.py b/apt_p2p/util.py index c334d1d..345ab9a 100644 --- a/apt_p2p/util.py +++ b/apt_p2p/util.py @@ -12,7 +12,9 @@ from twisted.trial import unittest isLocal = re.compile('^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|'+ '(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|'+ - '(172\.0?([1][6-9])|([2][0-9])|([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(172\.0?1[6-9]\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(172\.0?2[0-9]\.[0-9]{1,3}\.[0-9]{1,3})|'+ + '(172\.0?3[0-1]\.[0-9]{1,3}\.[0-9]{1,3})|'+ '(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$') def findMyIPAddr(addrs, intended_port, local_ok = False): @@ -117,7 +119,7 @@ def ipAddrFromChicken(): f.close() current_ip = ip_search.findall(data) return current_ip - except Exception: + except: return [] def uncompact(s): @@ -153,6 +155,40 @@ def compact(ip, port): raise ValueError return s +def byte_format(s): + """Format a byte size for reading by the user. + + @type s: C{long} + @param s: the number of bytes + @rtype: C{string} + @return: the formatted size with appropriate units + """ + if (s < 1): + r = str(int(s*1000.0)/1000.0) + 'B' + elif (s < 10): + r = str(int(s*100.0)/100.0) + 'B' + elif (s < 102): + r = str(int(s*10.0)/10.0) + 'B' + elif (s < 1024): + r = str(int(s)) + 'B' + elif (s < 10485): + r = str(int((s/1024.0)*100.0)/100.0) + 'KiB' + elif (s < 104857): + r = str(int((s/1024.0)*10.0)/10.0) + 'KiB' + elif (s < 1048576): + r = str(int(s/1024)) + 'KiB' + elif (s < 10737418L): + r = str(int((s/1048576.0)*100.0)/100.0) + 'MiB' + elif (s < 107374182L): + r = str(int((s/1048576.0)*10.0)/10.0) + 'MiB' + elif (s < 1073741824L): + r = str(int(s/1048576)) + 'MiB' + elif (s < 1099511627776L): + r = str(int((s/1073741824.0)*100.0)/100.0) + 'GiB' + else: + r = str(int((s/1099511627776.0)*100.0)/100.0) + 'TiB' + return(r) + class TestUtil(unittest.TestCase): """Tests for the utilities."""