From 5baa0b40922dd427ad9712ece8b05f45cfdb4aa6 Mon Sep 17 00:00:00 2001 From: Alexander Lutsai Date: Sun, 25 Apr 2021 19:25:47 +0300 Subject: [PATCH] first --- .gitignore | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++ menu.py | 160 +++++++++++++++++++++++++++++++++++++++++++++ mounter.py | 20 ++++++ readme.md | 0 4 files changed, 368 insertions(+) create mode 100644 .gitignore create mode 100644 menu.py create mode 100644 mounter.py create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ed79d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,188 @@ +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ diff --git a/menu.py b/menu.py new file mode 100644 index 0000000..3e32f5a --- /dev/null +++ b/menu.py @@ -0,0 +1,160 @@ +#!/usr/bin/python3 +# coding: utf-8 +# License: The MIT License +# Author: Alexander Lutsai +# Year: 2021 +# Description: This script draws menu to choose, mount and unmount drives + +import curses +import subprocess +import json + + +class ChoosePartition: + blkinfo = None + screen = None + selected_partn = 1 + partn = 1 + + message = "" + + def __init__(self): + self.screen = curses.initscr() + curses.start_color() + curses.curs_set(0) + curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) + self.selected_partn = 1 + self._read_partitions() + + def _read_partitions(self): + r = subprocess.check_output(['lsblk', '--all', '--json', '-O']) + self.blkinfo = json.loads(r) + partn = 0 + for bd in self.blkinfo['blockdevices']: + if 'children' not in bd: + continue + for part in bd['children']: + partn += 1 + + self.partn = partn + if self.selected_partn > self.partn: + self.selected_partn = self.partn + if self.selected_partn <= 0: + self.selected_partn = 1 + + def _get_part_by_partn(self): + partn = 0 + for bd in self.blkinfo['blockdevices']: + if 'children' not in bd: + continue + for part in bd['children']: + partn += 1 + if self.selected_partn == partn: + return part + return None + + def _select_print(self, x): + self.screen.clear() + self.screen.border(0) + self.screen.addstr( + 2, 2, + "Press 'm' to mount and 'u' to unmount and 'e' to unmount whole drive") + + partn = 0 + i = 0 + for bd in self.blkinfo['blockdevices']: + i += 1 + model = bd['model'] if bd['model'] is not None else "" + size = bd['size'] if bd['size'] is not None else "" + self.screen.addstr(2 + i, 2, bd['name'] + " " + model + " " + size) + if 'children' not in bd: + continue + for part in bd['children']: + i += 1 + partn += 1 + is_selected = 0 if self.selected_partn != partn else 1 + lab = part['label'] if part['label'] is not None else part['partlabel'] + lab = lab if lab is not None else part['parttypename'] + mp = part['mountpoint'] if part['mountpoint'] is not None else "Not mounted" + + s = "{name:<12} {size:<8} {label:<16} {mp}".format( + name=part['name'] if part['name'] is not None else "None", + label=lab if lab is not None else "None", + size=part['size'] if part['size'] is not None else "None", + mp=mp + ) + self.screen.addstr(2 + i, 4, s, curses.color_pair(is_selected)) + self.screen.refresh() + + self.screen.addstr(2 + i + 2, 4, self.message) + + def _eject_all(self): + blk = None + partn = 0 + for bd in self.blkinfo['blockdevices']: + if 'children' not in bd: + continue + for part in bd['children']: + partn += 1 + if self.selected_partn == partn: + blk = bd + if blk is None: + return + for part in blk['children']: + self.unmount(part['path']) + + def select(self): + sel = None + x = 0 + while x != ord('q'): + self._select_print(x) + x = self.screen.getch() + + if x == ord('j') or x == 66 or x == 14: + self.selected_partn += 1 + if self.selected_partn > self.partn: + self.selected_partn = self.partn + elif x == ord('k') or x == 65 or x == 16: + self.selected_partn -= 1 + if self.selected_partn <= 0: + self.selected_partn = 1 + elif x == 10: + sel = self._get_part_by_partn() + break + elif x == ord('e'): + sel = self._eject_all() + elif x == ord('m'): + sel = self._get_part_by_partn() + if sel is not None: + self.mount(sel['path']) + elif x == ord('u'): + sel = self._get_part_by_partn() + if sel is not None: + self.unmount(sel['path']) + elif x == ord('g') or x == ord('r'): + self._read_partitions() + curses.endwin() + return sel + + def _udisk_mount_unmount(self, cmd, dev): + r = "" + try: + r = subprocess.run( + ['udisksctl', cmd, '-b', dev], capture_output=True) + r = (r.stdout.decode(encoding="utf-8") + + r.stderr.decode(encoding="utf-8")) + self.message = r + except Exception as e: + self.message = cmd + " error: " + r + str(e) + self._read_partitions() + + def unmount(self, dev): + self._udisk_mount_unmount("unmount", dev) + + def mount(self, dev): + self._udisk_mount_unmount("mount", dev) + + +if __name__ == "__main__": + cp = ChoosePartition() + cp.select() diff --git a/mounter.py b/mounter.py new file mode 100644 index 0000000..b0f422f --- /dev/null +++ b/mounter.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 +# coding: utf-8 +# License: The MIT License +# Author: Alexander Lutsai +# Year: 2021 +# Description: This launches script that draws menu to choose, mount and unmount drives from ranger file manager + +from ranger.api.commands import Command + + +class mount(Command): + """:mount + + Show menu to mount and unmount + """ + + def execute(self): + """ Show menu to mount and unmount """ + self.fm.execute_console( + "shell python3 ~/.config/ranger/ranger_udisk_menu/menu.py") diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e69de29