Skip to content

Commit

Permalink
Move all launch funcs to launch.py
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyCthulhu committed Oct 8, 2021
1 parent d8a6bb1 commit b7f191c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 174 deletions.
12 changes: 6 additions & 6 deletions card0_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from logging_settings import set_logger


class CARD0:
class Card0:

@staticmethod
def check_cable_status(fp):
Expand Down Expand Up @@ -36,7 +36,7 @@ def check_if_cable_was_disconnected(past_cables_conditions, cables_conditions_fi

# проходимся по списку из старых кабелей, отдельно получаем текущую информацию для кабеля, сравниваем
for port_path, port_past_condition in past_cables_conditions.copy().items():
current_cable_status = CARD0.check_cable_status(f'{port_path}/status').replace('\n', '')
current_cable_status = Card0.check_cable_status(f'{port_path}/status').replace('\n', '')

if current_cable_status != port_past_condition:
removed_cables.append(port_path.split("/")[-1])
Expand Down Expand Up @@ -76,19 +76,19 @@ def check_cable_conditions_until_it_change(cabels_conditions_file_path):
logger.debug(f'Файл конфигов был пуст или не существовал')

with open(cabels_conditions_file_path, 'w') as file:
json.dump(CARD0.get_cables_path_condition_from_card0(), file, indent=4)
json.dump(Card0.get_cables_path_condition_from_card0(), file, indent=4)
return True

else:
with open(cabels_conditions_file_path, 'r') as file:
past_cables_conditions = json.load(file)

current_cables_conditions_from_card0 = CARD0.get_cables_path_condition_from_card0()
current_cables_conditions_from_card0 = Card0.get_cables_path_condition_from_card0()

cables_changes = {
'new_cables': CARD0.check_if_cable_was_connected(past_cables_conditions,
'new_cables': Card0.check_if_cable_was_connected(past_cables_conditions,
current_cables_conditions_from_card0),
'removed_cables': CARD0.check_if_cable_was_disconnected(past_cables_conditions,
'removed_cables': Card0.check_if_cable_was_disconnected(past_cables_conditions,
cabels_conditions_file_path)
}

Expand Down
166 changes: 166 additions & 0 deletions launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import json
import os
import time
from pathlib import Path

from card0_manager import Card0
from logging_settings import set_logger
from monitors_manager import Monitors
from xrandrmanager import XrandrManager
class Launch:



@staticmethod
def monitoring_activity():
while True:
Card0.check_cable_conditions_until_it_change(CABLES_CONDITIONS_FILE_PATH)
xrandr = XrandrManager()

if len(xrandr.monitors_data) == 1:
execute_command = 'xrandr --auto'
logger.info(execute_command)

# при пустом файлу st_size возвращает 1, а не 0...
if not Path(MONITORS_LAYOUTS_FILE_PATH).is_file() or Path(MONITORS_LAYOUTS_FILE_PATH).stat().st_size < 2:
position_manually()

else:
with open(MONITORS_LAYOUTS_FILE_PATH, 'r') as file:
previously_saved_mons_pos = json.load(file)

location_of_monitors = xrandr.get_needed_monitors_layout_config_automatically(xrandr.monitors_data,
previously_saved_mons_pos)

if location_of_monitors:
execute_command = xrandr.create_string_for_execute(xrandr.monitors_data, location_of_monitors)
logger.debug(f'Позиционируем мониторы согласно сохраненным ранее настройкам: {location_of_monitors}')

logger.info(execute_command)

else:
logger.info(
'Config with previous monitor layouts doesnt contains current connected cables. Connecting monitors with sorting by monitors size. If you want to set monitors by yout self, use command: "moncotrol -s"')
monitors_sorted_by_size = Monitors.connect_monitors_automatically(xrandr.monitors_data)

with open(MONITORS_LAYOUTS_FILE_PATH, 'w') as file:
json.dump(monitors_sorted_by_size, file, indent=4)

execute_command = xrandr.create_string_for_execute(xrandr.monitors_data, monitors_sorted_by_size)

logger.info(execute_command)

time.sleep(3)

# os.system(execute_command)


@staticmethod
def position_manually():
monitors = Monitors()
xrandr = XrandrManager()

my_monitors_position = monitors.set_monitors_position_manually()

# TODO: сделать проверку на существование и пустоту файла

if not Path(MONITORS_LAYOUTS_FILE_PATH).is_file() or Path(MONITORS_LAYOUTS_FILE_PATH).stat().st_size < 2:

with open(MONITORS_LAYOUTS_FILE_PATH, 'w') as file:
json.dump([my_monitors_position], file)

execute_command = xrandr.create_string_for_execute(xrandr.monitors_data, my_monitors_position)

else:
with open(MONITORS_LAYOUTS_FILE_PATH, 'r') as file:
previously_saved_mons_pos = json.load(file)

new_collection_of_monitors_positions = xrandr.create_new_collections_of_mon_positions(my_monitors_position,
previously_saved_mons_pos)

with open(MONITORS_LAYOUTS_FILE_PATH, 'w') as file:
json.dump(new_collection_of_monitors_positions, file, indent=4)

execute_command = xrandr.create_string_for_execute(xrandr.monitors_data,
new_collection_of_monitors_positions[0])

logger.info('Settings was saved successfully')
logger.info(execute_command)


def wrong_input(mode, settings):
line_break = '\n'
logger.critical(f'\nСкрипт не имеет настройки: {mode}\n\n'
f'\nЗапустите скрит с одной из следующих настроек:{line_break}{line_break}\n'
f'{f"{line_break}".join(settings)}')


@staticmethod
def choose_one_of_saved_positions_of_monitors():
if not Path(MONITORS_LAYOUTS_FILE_PATH).is_file() or Path(MONITORS_LAYOUTS_FILE_PATH).stat().st_size == 0:
logger.info('Config file empty or doesnt exist')

else:
with open(MONITORS_LAYOUTS_FILE_PATH, 'r') as file:
previously_saved_mons_pos = json.load(file)

# TODO: возможно, стоит сделать отдельную функцию для принта конфигов
if not previously_saved_mons_pos:
logger.info('You did not save any monitors layouts yet')
logger.info('Next strings will show monitors_positions in format: {config_id: [cable_1, cable_2, etc]}\n')

saved_cable_position_with_id = XrandrManager.create_layout_id_layout_name_dict(previously_saved_mons_pos)

# TODO: здесь должен быть logger
for id, monitors_names in saved_cable_position_with_id.items():
print(f'\n{id}: {monitors_names}')

print('\n')

logger.info('Write number of config, that u need rn and press Enter button')
input_id = int(input())

xrandr = XrandrManager()
execute_command = xrandr.create_string_for_execute(xrandr.monitors_data, saved_cable_position_with_id[input_id])
logger.info(execute_command)


@staticmethod
def show_saved_monitors_positions():
if not Path(MONITORS_LAYOUTS_FILE_PATH).is_file() or Path(MONITORS_LAYOUTS_FILE_PATH).stat().st_size == 0:
logger.info('Config file empty or doesnt exist')

else:
with open(MONITORS_LAYOUTS_FILE_PATH, 'r') as file:
saved_positions = json.load(file)

saved_positions_with_position_id = XrandrManager.create_layout_id_layout_name_dict(saved_positions)

logger.info(
'Next strings will show monitors_positions in format: {monitors_position_id: [cabel_1, cabel_2, etc]}')

for id, monitors_names in saved_positions_with_position_id.items():
print(f'\n{id}: {monitors_names}')
print('\n')


@staticmethod
def delete_config():
if not Path(MONITORS_LAYOUTS_FILE_PATH).is_file() or Path(MONITORS_LAYOUTS_FILE_PATH).stat().st_size == 0:
logger.info('Config file empty or doesnt exist: saved layouts doesnt exits')

else:
with open(MONITORS_LAYOUTS_FILE_PATH, 'r') as file:
previously_saved_mons_pos = json.load(file)

new_configs = Monitors.delete_saved_config(previously_saved_mons_pos)

with open(MONITORS_LAYOUTS_FILE_PATH, 'w') as file:
json.dump(new_configs, file)

logger.info(
f'This configs was deleted successfully: {[config for config in previously_saved_mons_pos if config not in new_configs]}')

logger = set_logger()
MONITORS_LAYOUTS_FILE_PATH = 'my_monitor_layout.json'
CABLES_CONDITIONS_FILE_PATH = 'cables_conditions_from_card0.json'
Loading

0 comments on commit b7f191c

Please sign in to comment.