From de0e61426c5b2b3643cde7417e78dfbc402b5b14 Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Fri, 22 Jun 2018 15:00:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B7=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=84=D1=80=D0=B5=D0=B9=D0=BC=D0=B1=D1=83=D1=84=D1=84?= =?UTF-8?q?=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Так же добавлена функция для подбора наиболее подходящего среди доступных разрешений --- pym/calculate/lib/utils/device.py | 40 +++++++++++++++++++++++++++++++ pym/calculate/lib/utils/tools.py | 20 +++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/pym/calculate/lib/utils/device.py b/pym/calculate/lib/utils/device.py index 85b1cd0..b381759 100644 --- a/pym/calculate/lib/utils/device.py +++ b/pym/calculate/lib/utils/device.py @@ -720,3 +720,43 @@ devfs = Devfs() udev = Udev() lvm = Lvm(LvmCommand()) raid = Raid(MdadmCommand()) + +class HwinfoError(Exception): + pass + +class HwinfoTimeout(HwinfoError): + pass + +class HwinfoNotExists(HwinfoError): + pass + +class Hwinfo(object): + """ + Объект получения информации из hwinfo + """ + command_timeout = 20 + @property + def hwinfo_cmd(self): + return files.getProgPath("/usr/sbin/hwinfo") + + def framebuffer(self): + """ + Получить вывод hwinfo --framebuffer + """ + return self.run("--framebuffer") + + def run(self, *command): + if not self.hwinfo_cmd: + raise HwinfoNotExists() + try: + return files.process(*[self.hwinfo_cmd] + list(command), + **{'timeout':self.command_timeout}).read() + except files.ProcessTimeout: + raise HwinfoTimeout() + + def resolutions(self): + """ + Получить допустимые разрешения фреймбуффера + """ + re_modes = re.compile("^\s+Mode 0x[0-9a-f]+:\s+(\d+x\d+)\s",re.M) + return tuple(sorted(unique(re_modes.findall(self.framebuffer())))) diff --git a/pym/calculate/lib/utils/tools.py b/pym/calculate/lib/utils/tools.py index e4621ed..8028fac 100644 --- a/pym/calculate/lib/utils/tools.py +++ b/pym/calculate/lib/utils/tools.py @@ -27,7 +27,7 @@ from os import path import time from abc import ABCMeta, abstractmethod from types import GeneratorType - +from math import sqrt try: @@ -602,3 +602,21 @@ class Brokenable(object): return f(self, *args, **kw) return wrapper return decor + + +def get_best_nearest_resolution(preferred_res, support_res, aspect_only=False): + """ + Получить наилучшее ближайшее к preferred_res разрешенение из support_res. + """ + width, height = map(int, preferred_res.split('x')) + gep = sqrt(height ** 2 + width ** 2) + k = float(width) / float(height) + support_res_int = (tuple(map(int, x.split("x"))) for x in support_res) + if not aspect_only: + support_res_int = [(x,y) for x,y in support_res_int if x <= width and y <= height] + if not support_res_int: + return None + bestRes = min(support_res_int, + key=lambda x: (abs(x[0] / float(x[1]) - k), + abs(gep - sqrt(x[0] ** 2 + x[1] ** 2)))) + return "%sx%s" % bestRes