From eeee79c787b5d2f7fbcb9af95ecea01c5a96da97 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 4 Jun 2020 15:27:48 -0400 Subject: [PATCH 1/3] more resilliant cmd.py oerisAdapter parsing --- lib/adapter/cmd.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index 5d2ffa6..1654b74 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -130,9 +130,10 @@ class AdapterOeis(CommandAdapter): cmd[0] = _get_abspath(cmd[0]) # cut oeis/ off - # Space delimiter for args to oeis.sh + # Replace all (non numeric and non '-')'s with Spaces to delimit args to oeis.sh if topic.startswith("oeis/"): - topic = topic[5:].replace('+',' ') + topic = topic[5:] + topic = re.sub('[^0-9-]', ' ', topic) return cmd + [topic] From 078411223d0a29bc592f30a0c3b03e17b2adeb6f Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 5 Jun 2020 10:04:35 -0400 Subject: [PATCH 2/3] Fixed chmod arg parse issue, formating/comments better --- lib/adapter/cmd.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index 1654b74..9d01e22 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -130,10 +130,10 @@ class AdapterOeis(CommandAdapter): cmd[0] = _get_abspath(cmd[0]) # cut oeis/ off - # Replace all (non numeric and non '-')'s with Spaces to delimit args to oeis.sh + # Replace all non (numeric, '-') chars with Spaces to delimit args to oeis.sh if topic.startswith("oeis/"): - topic = topic[5:] - topic = re.sub('[^0-9-]', ' ', topic) + topic = topic[5:] + topic = re.sub('[^0-9-]', ' ', topic) return cmd + [topic] @@ -155,10 +155,13 @@ class AdapterChmod(CommandAdapter): cmd = self._command[:] # cut chmod/ off + # remove all non (alphanumeric, '-') chars if topic.startswith("chmod/"): topic = topic[6:] + topic = re.sub('[^a-z^A-Z^0-9-]', '', topic) + return cmd + [topic] def is_found(self, topic): - return True + return True From 9aba6cd4601413b9a32465f2710833b1bb3b3db3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 6 Jun 2020 23:03:00 -0400 Subject: [PATCH 3/3] variable name change LANG -> LANGUAGE --- share/adapters/oeis.sh | 45 +++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/share/adapters/oeis.sh b/share/adapters/oeis.sh index b727384..d57bf70 100755 --- a/share/adapters/oeis.sh +++ b/share/adapters/oeis.sh @@ -13,14 +13,17 @@ oeis() ( local DOC=/tmp/oeis/doc.html local MAX_TERMS=10 mkdir -p $TMP - # Get short description of a sequence + # -- get_desc -- + # @return print description of OEIS sequence get_desc() { grep -A 1 '' $DOC \ | sed '//d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \ | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</.*, .*[0-9]' $DOC \ @@ -30,7 +33,9 @@ oeis() ( | cut -d ',' -f 1-${MAX_TERMS} return $? } - # Sample code parser INPUT arg: is grep regex + # -- parse_code -- + # @param GREP_REGEX + # @return Code snippet that corresponds to GREP_REGEX parse_code() { local GREP_REGEX="${1}" cat $DOC \ @@ -41,27 +46,35 @@ oeis() ( | sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</ + # . oeis + # . oeis if [ $# -lt 3 ] then # Arg-Parse ID, Generate URL - echo $1 | grep -q -e [a-z] -e [B-Z] && ID=$2 || ID=$1 - echo $1 | grep -q -e [a-z] -e [B-Z] && LANG=$1 || LANG=$2 + if echo ${1^^} | grep -q '[B-Z]' + then + ID=$2 + LANGUAGE=$1 + else + ID=$1 + LANGUAGE=$2 + fi [[ ${ID:0:1} == 'A' ]] && ID=${ID:1} ID=$(bc <<< "$ID") ID="A$(printf '%06d' ${ID})" URL+="/${ID}" curl $URL 2>/dev/null > $DOC - # Print ID + # Print ID, description, and sequence printf "ID: ${ID}\n" - # Print Description get_desc printf "\n" - # Print Sequence sample limited by $MAX_TERMS get_seq ${MAX_TERMS} printf "\n" # Print Code Sample - if [[ ${LANG^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC + if [[ ${LANGUAGE^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC then GREP_REGEX='MAPLE.*CROSSREFS' grep -q 'PROG' $DOC && GREP_REGEX='MAPLE.*PROG' @@ -69,7 +82,7 @@ oeis() ( parse_code "${GREP_REGEX}" \ | sed 's/MAPLE/(MAPLE)/; /MATHEMATICA/d; /PROG/d; /CROSSREFS/d' fi - if [[ ${LANG^^} == 'MATHEMATICA' ]] && grep -q 'MATHEMATICA' $DOC + if [[ ${LANGUAGE^^} == 'MATHEMATICA' ]] && grep -q 'MATHEMATICA' $DOC then GREP_REGEX='MATHEMATICA.*CROSSREFS' grep -q 'PROG' $DOC && GREP_REGEX='MATHEMATICA.*PROG' @@ -81,11 +94,11 @@ oeis() ( | sed '/PROG/d; /CROSSREFS/d' > ${TMP}/prog # Print out code sample for specified language rm -f ${TMP}/code_snippet - awk -v tgt="${LANG^^}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet - L="${LANG:0:1}" - LANG="${LANG:1}" - LANG="${L^^}${LANG,,}" - [ $(wc -c < $TMP/code_snippet) -eq 0 ] && awk -v tgt="${LANG}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet + awk -v tgt="${LANGUAGE^^}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet + L="${LANGUAGE:0:1}" + LANGUAGE="${LANGUAGE:1}" + LANGUAGE="${L^^}${LANGUAGE,,}" + [ $(wc -c < $TMP/code_snippet) -eq 0 ] && awk -v tgt="${LANGUAGE}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet cat ${TMP}/code_snippet # Search unknown sequence else