[~] Refactor

This commit is contained in:
Siarhei Siniak 2022-12-30 22:49:03 +03:00
parent f48a329886
commit cb315f452c
3 changed files with 156 additions and 62 deletions

@ -5,6 +5,7 @@ cp dotfiles/.local/bin/commands ~/.local/bin/commands
mkdir -p ~/.sway mkdir -p ~/.sway
cp dotfiles/.sway/config ~/.sway/config cp dotfiles/.sway/config ~/.sway/config
cp dotfiles/.zshenv ~/.zshenv cp dotfiles/.zshenv ~/.zshenv
cp dotfiles/.zshrc ~/.zshrc
cp dotfiles/.vimrc ~/.vimrc cp dotfiles/.vimrc ~/.vimrc
cp dotfiles/.tmux.conf ~/.tmux.conf cp dotfiles/.tmux.conf ~/.tmux.conf
cp -rp \ cp -rp \

@ -57,7 +57,10 @@ def intercept_output(
current_subprocess, current_subprocess,
return_aggregated=None, return_aggregated=None,
transform_callback=None, transform_callback=None,
timeout=None,
): ):
start_timestamp = datetime.datetime.now()
if not return_aggregated: if not return_aggregated:
return_aggregated = False return_aggregated = False
@ -72,6 +75,10 @@ def intercept_output(
not current_subprocess.poll() is None and \ not current_subprocess.poll() is None and \
not last_data is None and len(last_data) == 0 not last_data is None and len(last_data) == 0
): ):
if not timeout is None and \
(datetime.datetime.now() - start_timestamp).total_seconds() > timeout:
break
t2 = t1.poll(100) t2 = t1.poll(100)
if len(t2) == 1 and (t2[0][1] & select.POLLIN) > 0 and \ if len(t2) == 1 and (t2[0][1] & select.POLLIN) > 0 and \
not (isinstance(last_data, bytes) and len(last_data) == 0): not (isinstance(last_data, bytes) and len(last_data) == 0):
@ -917,15 +924,29 @@ def pass_ssh_osx(argv):
default=[], default=[],
type=str, type=str,
) )
parser.add_option(
'--clipboard_copy',
dest='clipboard_copy',
default=None,
type=str,
)
parser.add_option( parser.add_option(
'--debug', '--debug',
dest='debug', dest='debug',
action='store_true', action='store_true',
default=False, default=False,
) )
assert sys.platform == 'darwin' assert sys.platform in ['darwin', 'linux']
options, args = parser.parse_args(argv) options, args = parser.parse_args(argv)
if options.clipboard_copy is None:
if sys.platform == 'linux':
options.clipboard_copy = 'wl-copy'
elif sys.platform == 'darwin':
options.clipboard_copy = 'pbcopy'
else:
raise NotImplementedError
if len(args) == 0: if len(args) == 0:
raise RuntimeError('ssh_command is required') raise RuntimeError('ssh_command is required')
@ -933,8 +954,6 @@ def pass_ssh_osx(argv):
print(options.pass_option) print(options.pass_option)
pprint.pprint(args) pprint.pprint(args)
t1 = options.pass_option
assert len(t1) > 0
reset_gpg_agent = r''' reset_gpg_agent = r'''
@ -943,6 +962,9 @@ def pass_ssh_osx(argv):
''' '''
if not options.list: if not options.list:
t1 = options.pass_option
assert len(t1) > 0
print( print(
'select on of pass names\n%s' % '\n'.join([ 'select on of pass names\n%s' % '\n'.join([
'%d: %s' % (k, v) '%d: %s' % (k, v)
@ -995,9 +1017,9 @@ def pass_ssh_osx(argv):
if options.list: if options.list:
subprocess.check_call(ssh_command) subprocess.check_call(ssh_command)
else: else:
def cliboard_set(text): def clipboard_set(text):
with subprocess.Popen([ with subprocess.Popen([
'pbcopy', options.clipboard_copy,
], stdin=subprocess.PIPE) as p: ], stdin=subprocess.PIPE) as p:
p.stdin.write(text.encode('utf-8')) p.stdin.write(text.encode('utf-8'))
p.stdin.flush() p.stdin.flush()
@ -1044,6 +1066,7 @@ def pass_ssh_osx(argv):
current_subprocess=p, current_subprocess=p,
return_aggregated=True, return_aggregated=True,
transform_callback=transform_callback, transform_callback=transform_callback,
timeout=10,
): ):
if chunk['aggregated']: if chunk['aggregated']:
last_chunk = chunk last_chunk = chunk
@ -1078,7 +1101,7 @@ def pass_ssh_osx(argv):
password = last_line2.decode('utf-8').rstrip('\r\n') password = last_line2.decode('utf-8').rstrip('\r\n')
assert not password is None assert not password is None
cliboard_set(password) clipboard_set(password)
get_time = lambda : datetime.datetime.now().timestamp() get_time = lambda : datetime.datetime.now().timestamp()
start = get_time() start = get_time()
@ -1091,7 +1114,7 @@ def pass_ssh_osx(argv):
print('\r%5.2fs remains' % remains, end='') print('\r%5.2fs remains' % remains, end='')
time.sleep(0.1) time.sleep(0.1)
cliboard_set('') clipboard_set('')
print('\rcleared cliboard\n', end='') print('\rcleared cliboard\n', end='')
def player_v1(folder_url, item_id): def player_v1(folder_url, item_id):
@ -1126,6 +1149,76 @@ def player_v1(folder_url, item_id):
assert p.returncode == 0 assert p.returncode == 0
progress_bar.update(1) progress_bar.update(1)
def desktop_services(argv):
parser = optparse.OptionParser()
parser.add_option(
'--background_image',
dest='background_image',
default=None,
type=str,
)
options, args = parser.parse_args(argv)
assert all([
env_name in os.environ
for env_name in [
'GTK_IM_MODULE',
'XMODIFIERS',
'QT_IM_MODULE',
'I3SOCK',
'SWAYSOCK',
'WAYLAND_DISPLAY',
]
])
services = []
try:
swaylock_cmd = [
'swaylock', '-f', '-d',
]
if not options.background_image is None:
swaylock_cmd.extend(
[
'-i',
'"%s"' % options.background_image,
]
)
subprocess.check_call([
'swaymsg',
'--',
'output',
'*',
'bg',
options.background_image,
'fit',
])
services.extend([
subprocess.Popen(['ibus-daemon']),
subprocess.Popen(r'''
exec swayidle -d -w \
timeout 300 'echo t1; swaymsg "output * dpms off"' \
lock 'echo t6; pkill --signal SIGUSR1 swayidle;' \
unlock 'echo t7; pkill --signal SIGINT swaylock; swaymsg "output * dpms on";' \
timeout 900 'echo t5; {swaylock_cmd};' \
resume 'echo t2; swaymsg "output * dpms on"' \
before-sleep 'echo t3; loginctl lock-session;' \
after-resume 'echo t4; pkill --signal SIGUSR1 swayidle;'
'''.format(
swaylock_cmd=' '.join(swaylock_cmd),
), shell=True),
])
for o in services:
o.wait()
finally:
for o in services:
try:
o.terminate(timeout=10)
except:
logging.error('killed %s' % str(o.__dict__))
o.kill()
def suspend_timer(argv): def suspend_timer(argv):
import datetime; import datetime;
import subprocess; import subprocess;
@ -1276,8 +1369,39 @@ def share_wifi(argv):
finally: finally:
time.sleep(1) time.sleep(1)
def status(): def status(argv):
return ' | '.join([ assert isinstance(argv, list) and all([isinstance(o, str) for o in argv])
parser = optparse.OptionParser()
parser.add_option(
'--sh',
dest='sh',
default=[],
action='append',
type=str,
)
parser.add_option(
'--config',
dest='config',
default=None,
type=str,
)
options, args = parser.parse_args(argv)
config = dict()
try:
with io.open(options.config, 'r') as f:
config.update(
json.load(f)
)
except:
logging.error(traceback.format_exc())
pass
options.sh.extend(
config.get('sh', [])
)
t1 = [
subprocess.check_output(o, shell=True).decode('utf-8').strip() subprocess.check_output(o, shell=True).decode('utf-8').strip()
for o in [ for o in [
r''' r'''
@ -1288,19 +1412,21 @@ def status():
sensors | \ sensors | \
grep -Po '[\\\+\\\-\\\w][^\\\s]+C ' | head -n 5 | xargs echo -n grep -Po '[\\\+\\\-\\\w][^\\\s]+C ' | head -n 5 | xargs echo -n
''', ''',
r'''
ssh nartes@pizcool3070 free -h | \
grep -P Mem: | grep -Po '[\w\.\d]+' | tail -n +2 | head -n 3 | xargs echo -n;
''',
r'''
ssh nartes@pizcool3070 sensors | \
grep -Po '[\\\+\\\-\.0-9]+\s+C ' | head -n 1
''',
r''' r'''
date +'%Y-%m-%d %l:%M:%S %p'; date +'%Y-%m-%d %l:%M:%S %p';
''', ''',
] ]
]).replace('\n\r', '') ]
t2 = [
subprocess.check_output(o, shell=True).decode('utf-8').strip()
for o in options.sh
]
t3 = ' | '.join(t2 + t1).replace('\n\r', '')
sys.stdout.write(t3)
sys.stdout.flush()
def custom_translate(current_string, check, none_char=None,): def custom_translate(current_string, check, none_char=None,):
if none_char is None: if none_char is None:
@ -1363,8 +1489,7 @@ def commands_cli():
'@DEFAULT_SINK@' '@DEFAULT_SINK@'
]).decode('utf-8').strip() ]).decode('utf-8').strip()
elif sys.argv[1] == 'status': elif sys.argv[1] == 'status':
sys.stdout.write(status()) status(sys.argv[2:])
sys.stdout.flush()
elif sys.argv[1] == 'http-server': elif sys.argv[1] == 'http-server':
http_server(sys.argv[2:]) http_server(sys.argv[2:])
elif sys.argv[1] == 'pass-ssh-osx': elif sys.argv[1] == 'pass-ssh-osx':
@ -1400,42 +1525,7 @@ def commands_cli():
elif sys.argv[1] == 'suspend-timer': elif sys.argv[1] == 'suspend-timer':
suspend_timer(sys.argv[2:]) suspend_timer(sys.argv[2:])
elif sys.argv[1] == 'desktop-services': elif sys.argv[1] == 'desktop-services':
assert all([ desktop_services(sys.argv[2:])
env_name in os.environ
for env_name in [
'GTK_IM_MODULE',
'XMODIFIERS',
'QT_IM_MODULE',
'I3SOCK',
'SWAYSOCK',
'WAYLAND_DISPLAY',
]
])
services = []
try:
services.extend([
subprocess.Popen(['ibus-daemon']),
subprocess.Popen(r'''
exec swayidle -d -w \
timeout 300 'echo t1; swaymsg "output * dpms off"' \
lock 'echo t6; pkill --signal SIGUSR1 swayidle;' \
unlock 'echo t7; pkill --signal SIGINT swaylock; swaymsg "output * dpms on";' \
timeout 900 'echo t5; swaylock -f -d;' \
resume 'echo t2; swaymsg "output * dpms on"' \
before-sleep 'echo t3; loginctl lock-session;' \
after-resume 'echo t4; pkill --signal SIGUSR1 swayidle;'
''', shell=True),
])
for o in services:
o.wait()
finally:
for o in services:
try:
o.terminate(timeout=10)
except:
logging.error('killed %s' % str(o.__dict__))
o.kill()
else: else:
raise NotImplementedError raise NotImplementedError
except SystemExit: except SystemExit:

@ -1,4 +1,4 @@
# Default config for sway
# #
# Copy this to ~/.config/sway/config and edit it to your liking. # Copy this to ~/.config/sway/config and edit it to your liking.
# #
@ -21,12 +21,12 @@ set $term weston-terminal
#for_window [app_id="^launcher$"] floating enable, sticky enable, resize set 30 ppt 60 ppt, border pixel 10 #for_window [app_id="^launcher$"] floating enable, sticky enable, resize set 30 ppt 60 ppt, border pixel 10
#set $menu exec $term --class=launcher -e /usr/bin/sway-launcher-desktop #set $menu exec $term --class=launcher -e /usr/bin/sway-launcher-desktop
set $dmenu_path /usr/bin/bemenu-run set $dmenu_path /usr/bin/bemenu-run
set $menu dmenu_path | dmenu | xargs swaymsg exec -- set $menu $dmenu_path | xargs swaymsg exec --
### Output configuration ### Output configuration
# #
# Default wallpaper (more resolutions are available in /usr/share/backgrounds/sway/) # Default wallpaper (more resolutions are available in /usr/share/backgrounds/sway/)
output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill #output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
# #
# Example configuration: # Example configuration:
# #
@ -217,12 +217,15 @@ bar {
# When the status_command prints a new line to stdout, swaybar updates. # When the status_command prints a new line to stdout, swaybar updates.
# The default just shows the current date and time. # The default just shows the current date and time.
status_command while true; do commands status; sleep 1; done status_command while true; \
do commands status --config ~/.config/commands-status.json; \
sleep 1; \
done
colors { colors {
statusline #ffffff statusline #ffffff
background #323232 background #323232aa
inactive_workspace #32323200 #32323200 #5c5c5c inactive_workspace #323232aa #323232aa #ffffff
} }
} }