1#!/usr/bin/env python3

2# -*- coding: utf-8 -*-

3#

4# fresh_rate_info.py

5#

6# This file is part of Checkbox.

7#

8# Copyright 2012 Canonical Ltd.

9#

10# Authors: Shawn Wang <shawn.wang@canonical.com>

11#

12# Checkbox is free software: you can redistribute it and/or modify

13# it under the terms of the GNU General Public License version 3,

14# as published by the Free Software Foundation.

15

16#

17# Checkbox is distributed in the hope that it will be useful,

18# but WITHOUT ANY WARRANTY; without even the implied warranty of

19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

20# GNU General Public License for more details.

21#

22# You should have received a copy of the GNU General Public License

23# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

24

25"""

26 The fresh_rate_info.py got information from xrandr

27"""

28

29 import re

30 import sys

31 import subprocess

32

33

34 def xrandr_paser(data=None):

35 """return an array(xrandrs)"""

36

37 resolution = None

38 xrandrs = list()

39 for line in str(data).split("\n"):

40 for match in re.finditer(r"(.+) connected (\d+x\d+)\+", line):

41 connector = match.group(1)

42 resolution = match.group(2)

43 break

44 if resolution is None:

45 continue

46 for match in re.finditer(r"{0}\s+(.+)\*".format(resolution), line):

47 refresh_rate = match.group(1)

48 xrandr = {

49 "connector": connector,

50 "resolution": resolution,

51 "refresh_rate": refresh_rate,

52 }

53 xrandrs.append(xrandr)

54

55 return xrandrs

56

57

58 def main():

59 """main function"""

60

61 try:

62 data = subprocess.check_output(

63 ["xrandr", "--current"], universal_newlines=True

64 )

65 except subprocess.CalledProcessError as exc:

66 return exc.returncode

67

68 xrandrs = xrandr_paser(data)

69 for xrandr in xrandrs:

70 output_str = "Connector({0}):\t Resolution: {1} \t RefreshRate: {2}"

71 print(

72 output_str.format(

73 xrandr["connector"],

74 xrandr["resolution"],

75 xrandr["refresh_rate"],

76 )

77 )

78

79

80 if __name__ == "__main__":

81 sys.exit(main())