From 490821c03967d279a77bf836a86e0f6eb5d15511 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 29 May 2020 12:35:29 -0400 Subject: [PATCH 1/2] added chmod.sh --- share/adapters/chmod.sh | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 share/adapters/chmod.sh diff --git a/share/adapters/chmod.sh b/share/adapters/chmod.sh new file mode 100644 index 0000000..13064d7 --- /dev/null +++ b/share/adapters/chmod.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +# Contributed by Erez Binyamin (github.com/ErezBinyamin) + +# Translate between chmod string and number +# Contrib to chubin - cheat.sh +chmod_calc(){ + p_s="" + p_n="" + R=() + W=() + X=() + # If permission number is given calc string + if [[ $1 =~ ^-?[0-9]+$ ]] + then + p_n=$1 + for (( i=0; i<${#1}; i++ )) + do + num=$(echo "obase=2;${1:$i:1}" | bc | xargs printf '%03d') + [ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-' + [ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-' + [ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-' + [ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ') + [ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ') + [ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ') + done + # If permission string is given calc number + else + p_s=$1 + for (( i=0; i<${#1}; i+=0 )) + do + num=0 + [[ ${1:$i:1} == 'r' ]] && R+=('X') || R+=(' ') + [[ ${1:$((i++)):1} == 'r' ]] && let num++ + num=$(( num << 1 )) + [[ ${1:$i:1} == 'w' ]] && W+=('X') || W+=(' ') + [[ ${1:$((i++)):1} == 'w' ]] && let num++ + num=$(( num << 1 )) + [[ ${1:$i:1} == 'x' ]] && X+=('X') || X+=(' ') + [[ ${1:$((i++)):1} == 'x' ]] && let num++ + p_n+="$num" + done + fi + printf " +Linux Permissions String:\t${p_s} +Linux Permissions Number:\t${p_n} + +Owner\t\tGroup\t\tPublic + +Read [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}] +Write [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}] +Execute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}] +" +} + +chmod_calc $@ From 6c14dc98dafd3b45628ca78f1fd6c654b5efb138 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 29 May 2020 12:39:16 -0400 Subject: [PATCH 2/2] Integrated chmod.sh --- lib/adapter/cmd.py | 25 +++++++++++++++++++++++++ lib/config.py | 2 ++ 2 files changed, 27 insertions(+) diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index 66699ea..db365e2 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -140,3 +140,28 @@ class AdapterOeis(CommandAdapter): def is_found(self, topic): return True + +class AdapterChmod(CommandAdapter): + """ + Show chmod numeric values and strings + Exported as: "/chmod/NUMBER" + """ + + _adapter_name = "chmod" + _output_format = "text" + _cache_needed = True + _command = ["share/adapters/chmod.sh"] + + def _get_command(self, topic, request_options=None): + cmd = self._command[:] + if not cmd[0].startswith("/"): + cmd[0] = _get_abspath(cmd[0]) + + # cut chmod/ off + if topic.startswith("chmod/"): + topic = topic[4:] + + return cmd + [topic] + + def is_found(self, topic): + return True diff --git a/lib/config.py b/lib/config.py index 4b51460..25c9785 100644 --- a/lib/config.py +++ b/lib/config.py @@ -86,6 +86,7 @@ _CONFIG = { "learnxiny", "rfc", "oeis", + "chmod", ], "adapters.mandatory": [ "search", @@ -116,6 +117,7 @@ _CONFIG = { ("^[^/]*/rosetta(/|$)", "rosetta"), ("^rfc/", "rfc"), ("^oeis/", "oeis"), + ("^chmod/", "chmod"), ("^:", "internal"), ("/:list$", "internal"), ("/$", "cheat.sheets dir"),