1 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 6 from autotest_lib.client.cros.video import method_logger 7 8 9 class ScreenShotFileNamer(object): 10 """ 11 Creates a filename of an image given instance in time it was captured. 12 13 Encapsulates the mapping from a timestamp to a filename. 14 15 There are two uses of this class. 16 17 When downloading golden images, we will have timestamps that we will want 18 the images for. Using this class, we will neatly get the filenames from the 19 timestamps. 20 21 When capturing test screenshots we will have timestamps also. Again using 22 this class we can SAVE the images in the appropriate names. This ensures 23 the golden image and test image that are from the same timestamp have 24 exactly the same name. 25 26 """ 27 28 29 @method_logger.log 30 def __init__(self, image_format): 31 self._image_format = image_format 32 33 34 @property 35 def image_format(self): 36 """ 37 Returns the format to use for the image. 38 39 """ 40 return self._image_format 41 42 43 @method_logger.log 44 def get_filename(self, time_delta_value): 45 """ 46 Gets required full filename for a screenshot image. 47 48 @param time_delta_value: time_delta, the time value at which 49 an image will be or has been captured. 50 51 @returns filename encoded based on the time value, as a string. 52 53 """ 54 hours, remainder = divmod(time_delta_value.total_seconds(), 3600) 55 56 minutes, seconds = divmod(remainder, 60) 57 58 # integer division, discard decimal milliseconds 59 milliseconds = time_delta_value.microseconds // 1000 60 61 filename = "%02d_%02d_%02d_%03d" % ( 62 hours, minutes, seconds, milliseconds) 63 64 return filename + '.' + self.image_format 65