From d8acb2a5980b7e60087baba75344a7df37253122 Mon Sep 17 00:00:00 2001 From: Kuninori Morimoto Date: Tue, 12 Feb 2019 14:23:17 +0900 Subject: add PeriJect viewer Very simple 1st verstion Signed-off-by: Kuninori Morimoto --- scripts/base.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 scripts/base.py (limited to 'scripts/base.py') diff --git a/scripts/base.py b/scripts/base.py new file mode 100755 index 0000000..1a6396e --- /dev/null +++ b/scripts/base.py @@ -0,0 +1,108 @@ +#! /usr/bin/env python3 +#=============================== +# +# base +# +# 2019/02/07 Kuninori Morimoto +#=============================== +import os +import re +import subprocess + +#==================================== +# +# base +# +# it supports do/run/run1 for using external command +# +#==================================== +class base: + __top = os.path.abspath(__file__ + "/../../"); + __key = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" + + #-------------------- + # chomp + #-------------------- + def chomp(self, text): + return re.sub(r"\n$", r"", text); + + #-------------------- + # top() + #-------------------- + def top(self): + return base.__top; + + #-------------------- + # is_key() + #-------------------- + def is_key(self, key): + return re.match(base.__key, key); + + #-------------------- + # color + #-------------------- + def color(self, color, text): + return "\033[{}m{}\033[0m".format(self.config(color), text) + + #-------------------- + # do() + # + # do command + #-------------------- + def do(self, command): + return subprocess.run(command, shell=True).returncode; + + #-------------------- + # tolist() + #-------------------- + def tolist(self, string): + if (len(string) > 0): + return string.split('\n'); + + return []; + + #-------------------- + # run() + # + # run command and get result as plane text + #-------------------- + def run(self, command): + + # Ughhhh + # I don't like python external command !! + # (ノ `Д´)ノ go away !! + return self.chomp(subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).\ + communicate()[0].decode("utf-8")); + + #-------------------- + # run1() + # + # run command and get result as list + #-------------------- + def runl(self, command): + + # call run() and exchange result as array + # + # "xxxxxxx + # yyyyyyy + # zzzzzzz" + # -> + # ["xxxxxxx", + # "yyyyyyy", + # "zzzzzzz"] + return self.tolist(self.run(command)); + + #-------------------- + # config() + # + # read settings from config + #-------------------- + def config(self, item): + if (not os.path.exists("{}/.config".format(self.top()))): + print("\nplease copy .config.sample to .config\n" + + "and edit it for your environment\n"); + exit(); + + config = self.run("grep {} {}/.config | cut -d : -f2".\ + format(item, self.top())); + return self.chomp(config); -- cgit v1.2.3