download and upload measurements should be stored in SpeedtestResults in the units specified, defaulting to bits
This commit is contained in:
		
							parent
							
								
									d09ec27cb2
								
							
						
					
					
						commit
						a4cb217522
					
				
							
								
								
									
										71
									
								
								speedtest.py
								
								
								
								
							
							
						
						
									
										71
									
								
								speedtest.py
								
								
								
								
							|  | @ -207,6 +207,10 @@ class SpeedtestException(Exception): | ||||||
|     """Base exception for this module""" |     """Base exception for this module""" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class SpeedtestConfigError(SpeedtestException): | ||||||
|  |     """Configuration provided is invalid""" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class ConfigRetrievalError(SpeedtestException): | class ConfigRetrievalError(SpeedtestException): | ||||||
|     """Could not retrieve config.php""" |     """Could not retrieve config.php""" | ||||||
| 
 | 
 | ||||||
|  | @ -459,6 +463,25 @@ class HTTPUploader(threading.Thread): | ||||||
|         del self.data |         del self.data | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class UnitsDataDescriptor(object): | ||||||
|  |     """Data Descriptor class, that converts values from Bytes to specified | ||||||
|  |     units when setting the value. | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     def __init__(self, name, default=None): | ||||||
|  |         self.name = name | ||||||
|  |         self.default = default | ||||||
|  | 
 | ||||||
|  |     def __get__(self, obj, objtype=None): | ||||||
|  |         return getattr(obj, '_%s' % self.name, self.default) | ||||||
|  | 
 | ||||||
|  |     def __set__(self, obj, val): | ||||||
|  |         setattr(obj, '_%s' % self.name, val * obj.units[1]) | ||||||
|  | 
 | ||||||
|  |     def __delete__(self, obj): | ||||||
|  |         raise NotImplementedError() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class SpeedtestResults(object): | class SpeedtestResults(object): | ||||||
|     """Class for holding the results of a speedtest, including: |     """Class for holding the results of a speedtest, including: | ||||||
| 
 | 
 | ||||||
|  | @ -472,9 +495,13 @@ class SpeedtestResults(object): | ||||||
|     to get a share results image link. |     to get a share results image link. | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
|     def __init__(self, download=0, upload=0, ping=0, server=None): |     download = UnitsDataDescriptor('download', default=0) | ||||||
|         self.download = download |     upload = UnitsDataDescriptor('upload', default=0) | ||||||
|         self.upload = upload | 
 | ||||||
|  |     def __init__(self, download=0, upload=0, ping=0, server=None, | ||||||
|  |                  units=('bit', 8)): | ||||||
|  |         self._download = download | ||||||
|  |         self._upload = upload | ||||||
|         self.ping = ping |         self.ping = ping | ||||||
|         if server is None: |         if server is None: | ||||||
|             self.server = {} |             self.server = {} | ||||||
|  | @ -483,6 +510,8 @@ class SpeedtestResults(object): | ||||||
|         self._share = None |         self._share = None | ||||||
|         self.timestamp = datetime.datetime.utcnow().isoformat() |         self.timestamp = datetime.datetime.utcnow().isoformat() | ||||||
| 
 | 
 | ||||||
|  |         self.units = units | ||||||
|  | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return repr(self.dict()) |         return repr(self.dict()) | ||||||
| 
 | 
 | ||||||
|  | @ -494,9 +523,19 @@ class SpeedtestResults(object): | ||||||
|         if self._share: |         if self._share: | ||||||
|             return self._share |             return self._share | ||||||
| 
 | 
 | ||||||
|         download = int(round((self.download / 1000) * 8, 0)) |         if self.units[1] == 1: | ||||||
|  |             bit_download = self.download * 8 | ||||||
|  |             bit_upload = self.upload * 8 | ||||||
|  |         elif self.units[1] == 8: | ||||||
|  |             bit_download = self.download | ||||||
|  |             bit_upload = self.upload | ||||||
|  |         else: | ||||||
|  |             raise SpeedtestConfigError('Unknown units, valid configurations ' | ||||||
|  |                                        'are ("bit", 8) and ("byte", 1)') | ||||||
|  | 
 | ||||||
|  |         download = int(round(bit_download / 1000, 0)) | ||||||
|         ping = int(round(self.ping, 0)) |         ping = int(round(self.ping, 0)) | ||||||
|         upload = int(round((self.upload / 1000) * 8, 0)) |         upload = int(round(bit_upload / 1000, 0)) | ||||||
| 
 | 
 | ||||||
|         # Build the request to send results back to speedtest.net |         # Build the request to send results back to speedtest.net | ||||||
|         # We use a list instead of a dict because the API expects parameters |         # We use a list instead of a dict because the API expects parameters | ||||||
|  | @ -574,20 +613,20 @@ class SpeedtestResults(object): | ||||||
|             }) |             }) | ||||||
|         return json.dumps(self.dict(), **kwargs) |         return json.dumps(self.dict(), **kwargs) | ||||||
| 
 | 
 | ||||||
|     def simple(self, units=('bit', 8)): |     def simple(self): | ||||||
|         return """Ping: %s ms |         return """Ping: %s ms | ||||||
| Download: %0.2f M%s/s | Download: %0.2f M%s/s | ||||||
| Upload: %0.2f M%s/s""" % (self.ping, | Upload: %0.2f M%s/s""" % (self.ping, | ||||||
|                           (self.download / 1000 / 1000) * units[1], |                           (self.download / 1000 / 1000), | ||||||
|                           units[0], |                           self.units[0], | ||||||
|                           (self.upload / 1000 / 1000) * units[1], |                           (self.upload / 1000 / 1000), | ||||||
|                           units[0]) |                           self.units[0]) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Speedtest(object): | class Speedtest(object): | ||||||
|     """Class for performing standard speedtest.net testing operations""" |     """Class for performing standard speedtest.net testing operations""" | ||||||
| 
 | 
 | ||||||
|     def __init__(self, config=None): |     def __init__(self, config=None, units=('bit', 8)): | ||||||
|         self.config = {} |         self.config = {} | ||||||
|         self.get_config() |         self.get_config() | ||||||
|         if config is not None: |         if config is not None: | ||||||
|  | @ -597,7 +636,7 @@ class Speedtest(object): | ||||||
|         self.closest = [] |         self.closest = [] | ||||||
|         self.best = {} |         self.best = {} | ||||||
| 
 | 
 | ||||||
|         self.results = SpeedtestResults() |         self.results = SpeedtestResults(units=units) | ||||||
| 
 | 
 | ||||||
|     def get_config(self): |     def get_config(self): | ||||||
|         """Download the speedtest.net configuration and return only the data |         """Download the speedtest.net configuration and return only the data | ||||||
|  | @ -1170,7 +1209,7 @@ def shell(): | ||||||
| 
 | 
 | ||||||
|     printer('Retrieving speedtest.net configuration...', quiet) |     printer('Retrieving speedtest.net configuration...', quiet) | ||||||
|     try: |     try: | ||||||
|         speedtest = Speedtest() |         speedtest = Speedtest(units=args.units) | ||||||
|     except ConfigRetrievalError: |     except ConfigRetrievalError: | ||||||
|         printer('Cannot retrieve speedtest configuration') |         printer('Cannot retrieve speedtest configuration') | ||||||
|         sys.exit(1) |         sys.exit(1) | ||||||
|  | @ -1242,17 +1281,17 @@ def shell(): | ||||||
|     printer('Testing download speed', quiet, end=('\n', '')[bool(callback)]) |     printer('Testing download speed', quiet, end=('\n', '')[bool(callback)]) | ||||||
|     speedtest.download(callback=callback) |     speedtest.download(callback=callback) | ||||||
|     printer('Download: %0.2f M%s/s' % |     printer('Download: %0.2f M%s/s' % | ||||||
|             ((results.download / 1000 / 1000) * args.units[1], args.units[0]), |             ((results.download / 1000 / 1000), args.units[0]), | ||||||
|             quiet) |             quiet) | ||||||
| 
 | 
 | ||||||
|     printer('Testing upload speed', quiet, end=('\n', '')[bool(callback)]) |     printer('Testing upload speed', quiet, end=('\n', '')[bool(callback)]) | ||||||
|     speedtest.upload(callback=callback) |     speedtest.upload(callback=callback) | ||||||
|     printer('Upload: %0.2f M%s/s' % |     printer('Upload: %0.2f M%s/s' % | ||||||
|             ((results.upload / 1000 / 1000) * args.units[1], args.units[0]), |             ((results.upload / 1000 / 1000), args.units[0]), | ||||||
|             quiet) |             quiet) | ||||||
| 
 | 
 | ||||||
|     if args.simple: |     if args.simple: | ||||||
|         print_(results.simple(args.units)) |         print_(results.simple()) | ||||||
|     elif args.csv: |     elif args.csv: | ||||||
|         print_(results.csv(delimiter=args.csv_delimiter)) |         print_(results.csv(delimiter=args.csv_delimiter)) | ||||||
|     elif args.json: |     elif args.json: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue