Saturday, January 9, 2021

Mozart Mud and cmud mapping

 

If you play Mozart MUD and use cmud client, the following will capture room descriptions to the map.  It's not elegant code wise in that it requires a special color combination for room descriptions to enable the scripts to only see the room lines, build up the description and then update on the description termination Terrain.

Both Mozart and cmud have configurations so your results may vary and require tweaking, 

Variables

flag to turn description capture on/off
#var roomdesc 1

holds the captured text of a description
#var description_store ""

a placeholder to capture result of function so as not to display on mud
#var result

Aliases

<alias name="roomson" id="136">
 <value>#var roomdesc 1;#show you have turned on update room desc.</value>
</alias>

<alias name="roomsoff" id="137">
  <value>#var roomdesc 0;#show you have turned off update room desc.</value>
</alias>

Triggers

Creates a trigger that matches any text that matches a foreground/background color.  The description color has to be a unique combo. each line is captured and adds it to the description_store variable and then when the 2nd trigger "Terrain:" hits, it updates the map room description

<trigger priority="1310" regex="true" linecolor="112" id="131">
 <pattern>(.*)</pattern>
<value>#var description_store %concat(@description_store,%1)</value>
</trigger>

<trigger priority="1330" id="133">
<pattern>Terrain:</pattern>
 <value>
#if (@roomdesc == 1) {
#var foo %roomdesc(%roomvnum,@description_store);
}
#var description_store "";
</value>
</trigger>

Monday, December 7, 2020

Bash: Script to archive files into YYYY/MM based on creation date

 I had over 10 years of photos that I wanted to organize by YYYY/MM.  Created this Bash script to do it.

#!/bin/sh
#script:       archiver.sh
#
#

sourceDir="/f/from_kims_ext/"
#sourceDir="/f/test/"
archiveDir="/f/image_archive/"
ARCHIVEFILE="/f/move_archive.txt"

function getfilelist {
`ls -Rp $sourceDir | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }' | sed "/\/$/d" > $ARCHIVEFILE`
}

function parsefilename {
  sourcefilename=`basename $1`
  extension="${sourcefilename##*.}"
  filename="${sourcefilename%.*}"
}

function updatefilename {
  typeset counter
  counter=0
  
  #check if ok to use original filename
  updatecheck="$archiveDir$datepath/$filename.$extension"
  echo "checking if target $updatecheck exists"
 # if [[ -f $updatecheck ]]; then
   if [ ! -f ${updatecheck} ]; then
    echo "$updatecheck does not exist"
    return
  else
    echo "$updatecheck exists"
  fi

  while [[ $counter -lt 200 ]]
  do
    counter=$((counter+1))
    updatecheckinc="$archiveDir$datepath/$filename"
    updatecheckinc+="_"
    updatecheckinc+="$counter.$extension"
    echo "checking for new $updatecheckinc"
    if [[ ! -f "${updatecheckinc}" ]]; then
      break
    fi
  done
 # changing name because it already exists, so increment to new file name
  filename+="_"
  filename+="$counter"
  echo "updating filename to $filename"
  return
}

set -u
#LOG_TIME=`date |cut -c12-13,15-16,21-23| sed s/" "//g`
LOG_TIME=`date`
 thisyear=`date +"%Y"`
 thismonth=`date +"%m"`
 
echo "archive starting..."
echo "Starting archive at $LOG_TIME"

getfilelist

# Setting delimiter to an empty string since we want to read the entire line of the file
# and be able to handle files with spaces since the default is a space delimiter
SAVEIFS=$IFS
IFS=$''

((archived_moved=0))
while read file
do
  parsefilename $file
  echo "processing $filename with extension $extension"
  fDate=`stat -c %y "${file}"`
  fDate=$(echo "$fDate" | cut -d' ' -f1)
  #example  2018-01-12
    year=$(echo ${fDate} | cut -d'-' -f1 )
    month=$(echo ${fDate} | cut -d'-' -f2)
    day=$(echo ${fDate} | cut -d'-' -f3)
    filedate=${year}${month}
   if [ $(echo "$filedate" | grep -c '^[0-9]\{6\}$') -eq 1 ]
    then
        if [ ! -d ${archiveDir}${year}/${month} ]
        then
          echo "echo making directory $archiveDir$year/$month"
          mkdir -p ${archiveDir}${year}/${month}
        fi
        datepath="${year}/${month}"
        updatefilename
        echo "Archiving ${file} to ${archiveDir}${datepath}/$filename.$extension"
        mv "${file}" "${archiveDir}${datepath}/${filename}.${extension}"
        ((archived_moved=archived_moved+1))
        
    else
        echo "Couldn't get stat on ${reportBakDir}${file}"
    fi
done <"$ARCHIVEFILE"
IFS=$SAVEIFS
#END_TIME=`date |cut -c12-13,15-16,21-23| /usr/bin/sed s/" "//g`
END_TIME=`date`
echo "Completed archive at $END_TIME"
exit 0

Tuesday, August 25, 2020

Python 3: RegEx Back References

Regual Expressions is one of those things that isn't completely regular when switching between languages. This is how you use a back reference in Python.

 
import re

print( re.sub(r'(foo)(bar)',r'\g<2>\g<1>','foobar'))


More info here https://www.regular-expressions.info/replacebackref.html

Monday, August 24, 2020

Python 3: solving 'utf-8' codec can't decode byte 0x92

In Python 3, UTF-8 is the default encoding. In windows you may run into issues where characters will not decode such as 'utf-8' codec can't decode byte 0x92. Use a header with either cp1252 or windows-1252

 

# coding=CP1252
import re

text = """
LADY CAPULET
    Alack the day. She’s dead, she’s dead, she’s dead!
CAPULET
    Ha! Let me see her. Out, alas! She’s cold.
    Her blood is settled, and her joints are stiff.
    Life and these lips have long been separated.
    Death lies on her like an untimely frost
    Upon the sweetest flower of all the field.
Nurse
    O lamentable day!
"""

Thursday, August 6, 2020

Autosys: Changing output of autosys -j JOB_NAME into Markdown

When generating output using autosys -j command line, it produces an output using space delimited typical console output.  The following command will output the result into a markdown table.


autorep -j YOUR_JOB_OR_BOX_NAME | sed -e 's/ \{2,\}/|/g'| sed -e 's/_\{2,\}/ /g' | sed 's/\(.*\)/|\1/g' | sed -e 's/\(Job Name\)/|\1/g' | sed -e 's/\(Last Start\)/|\1/g' | sed -e 's/\(Last End\)/|\1|/g'| sed -e 's/ST Run/ST||Run/g' | sed -e 's/Ntry Pri\/Xit/Ntry||Pri\/Xit||/g'| sed -e 's/\(.*\)\([0-9]\{8\}\/[0-9]*\)/\1|\2|/g'

Wednesday, July 29, 2020

Python: Performing XSLT recursively on a directory of XML files

I've been using https://github.com/ajryan/RptToXml to extract out xml information from Crystal Reports. I then rip through the created xml files with xlst to provide detail information on how the reports are created.  This little python 3.5+ script uses https://www.saxonica.com/download/download_page.xml and will create html files from a directory tree containing xml files.  

TODO: Write an xxl file to convert Crystal Reports to Jasper Reports.


import os
import subprocess
import glob
from pathlib import Path
root_dir = r"C:\crystalxml"
xslFile = r"C:\Saxon\pretty.xsl"

for filename in glob.iglob(root_dir + '/**/*.xml', recursive=True):

 my_parts = filename.split("\\")
 justname = my_parts[-1].split('.')[0]
 my_dir = os.path.dirname(filename)
 newname = fr'{my_dir}\{justname}.html'
 cmd = fr'java -cp C:\Saxon\saxon-he-10.1.jar net.sf.saxon.Transform -t -s:"{filename}" -xsl:{xslFile} -o:"{newname}"'
 subprocess.call(cmd)

Tuesday, May 12, 2020

Python: Learning by starting with the basics.

Just a simple mind exercise in learning a language.  Created a console Hangman game that uses a word api with a hint command that fills in the first available blank.

hangman.py

Guess a Letter:hint
------
|     |
|     0
|    /|\
|    /
t y p h u _ e _ 
Wrong Guesses:lmaio
Guess a Letter:s
------
|     |
|     0
|    /|\
|    /
t y p h u s e s 
Wrong Guesses:lmaio
stay of execution