Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/atheris/utils.py: 34%

35 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# Copyright 2021 Google LLC 

2# Copyright 2021 Fraunhofer FKIE 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15 

16import sys 

17import os 

18 

19 

20def path(): 

21 dir, _ = os.path.split(sys.modules["atheris"].__file__) 

22 dir, _ = os.path.split(dir) 

23 return dir 

24 

25 

26class ProgressRenderer: 

27 """Displays an updating progress meter in the terminal.""" 

28 

29 def __init__(self, stream, total_count): 

30 assert stream.isatty() 

31 self.stream = stream 

32 

33 self._count = 0 

34 self.total_count = total_count 

35 

36 self._current_width = 0 

37 self.render() 

38 

39 def render(self): 

40 self.erase() 

41 done_percent = int(100 * self._count / self.total_count) 

42 message = f"[{self._count}/{self.total_count}] {done_percent}%" 

43 self.stream.write(message) 

44 self.stream.flush() 

45 self._current_width = len(message) 

46 

47 def erase(self): 

48 self.stream.write(("\b" * self._current_width) + 

49 (" " * self._current_width) + 

50 ("\b" * self._current_width)) 

51 self.stream.flush() 

52 self._current_width = 0 

53 

54 def drop(self): 

55 self._current_width = 0 

56 sys.stderr.write("\n") 

57 

58 @property 

59 def count(self): 

60 return self._count 

61 

62 @count.setter 

63 def count(self, new_count): 

64 self._count = new_count 

65 self.render()