Skip to content

Instantly share code, notes, and snippets.

@an-dr
Last active July 30, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save an-dr/9f3256f4ccd97bc6e04751b542364bff to your computer and use it in GitHub Desktop.
Save an-dr/9f3256f4ccd97bc6e04751b542364bff to your computer and use it in GitHub Desktop.
Python template for multi-platform script writing
"""
The Script : https://gist.github.com/an-dr/9f3256f4ccd97bc6e04751b542364bff
"""
class X:
"""
The Script's Class: https://gist.github.com/an-dr/9f3256f4ccd97bc6e04751b542364bff
"""
from os.path import realpath, basename, dirname, join
from os import system as x
from psutil import MACOS, WINDOWS, LINUX
from subprocess import Popen as shell
from sys import stdout
def __init__(self, cmd, script_name="Script"):
self.cmd, self.__script_dir, self.__script_name = self.__cfg(cmd)
#
self.__add_name_window_cmd(script_name)
self.__add_cd_to_start_dir()
try:
self.__run()
except KeyboardInterrupt:
pass
self.__press_enter(script_name)
def __cfg(self, cmd):
script_dir, script_name = X.dirname(X.realpath(__file__)),\
X.basename(__file__)
if isinstance(cmd, list):
self.cmd = cmd
else:
self.cmd = [cmd]
return cmd, script_dir, script_name
def __add_name_window_cmd(self, script_name):
if X.WINDOWS:
CH_WIN_NAME = "$host.ui.RawUI.WindowTitle = \"%s\"" % script_name
elif X.MACOS or X.LINUX:
CH_WIN_NAME = "echo -ne \"\\033]0;%s\\007\"" % script_name
self.cmd.insert(0, CH_WIN_NAME) # add window rename cmd
def __add_cd_to_start_dir(self):
self.cmd.insert(0, "cd " + self.__script_dir) # add window rename cmd
def __press_enter(self, script_name):
input("%s is finished. Press Enter to close..." % script_name)
def __run(self):
if X.WINDOWS:
SHELL, NEXT_CMD = "powershell", ";"
elif X.MACOS or X.LINUX:
SHELL, NEXT_CMDCMD = "sh", ";"
#
X.shell([SHELL, NEXT_CMD.join(self.cmd)],
stdout=X.stdout).communicate()
if __name__ == "__main__":
X([
"ls",
# add commands here
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment