Wednesday, May 11, 2022

openCSV in ColdFusion and Lucee

 The opencsv project at http://opencsv.sourceforge.net/  is a nice overall java based csv parser library that can automatically support qualified strings that contain comma's.  The below code works in ColdFusion 2021 as is with the opencsv.jar file but needs an additional jar with Lucee 5.3.9.

ColdFusion and Lucee: 

Grab the source from opencsv.sourceforge.net and run mvn to produce the jar.  I used version 5.6.

Lucee:

Download the apache commons lang3 source from https://commons.apache.org/proper/commons-lang/download_lang.cgi and compile with maven.

opencsv-5.6.1-SNAPSHOT.jar
commons-lang3-3.12.0.jar

Put the jar(s) in the lib folder of the server or use a jar loader.  start up the server and run the following test (adjust the path to your sample csv data file). which should produce the data below (excluding the other dumps)



<cfscript>

    csvFile ="#expandPath('.')#/csv_data/addresses.csv";
    csvData = [];

    fileReader = createobject("java","java.io.FileReader").init(csvFile);

    writeDump(fileReader);

    csvReader = createObject("java","com.opencsv.CSVReader").init(fileReader);
    writeDump(csvReader);
    csvData = csvReader.readAll();
    writeDump(csvData);

    csvReader.close();
 
</cfscript>

Monday, June 28, 2021

CMUD: Experiment in creating a visible color map

The createColorMap alias in CMUD will generate a pattern of rooms showing you each room color used in mapping.

 

<alias name="createColorMap" language="Lua" id="179">
  <value>local mapcolors ={"aliceblue",  "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", 
 "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", 
 "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", 
 "darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", 
 "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", 
 "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dodgerblue", "firebrick", 
 "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray",
 "green", "greenyellow", "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", 
 "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", 
 "lightcyan", "lightgoldenrodyellow", "lightgreen", "lightgrey", "lightpink", "lightsalmon", 
 "lightseagreen", "lightskyblue", "lightslategray", "lightsteelblue", "lightyellow", "lime", 
 "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", 
 "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", 
 "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", 
 "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", 
 "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", 
 "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", 
 "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "snow", "springgreen", 
 "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white"
 , "whitesmoke", "yellow", "yellowgreen"}
 
 function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

local maxcolumns = 10
local colorcount = tablelength(mapcolors)
local mapdir
local c
local targetcolor = 0
local r = 0
local numrows =  colorcount/maxcolumns + 1

for r = 1, numrows do
  if r % 2 == 0 then 
    mapdir = "e"
  else
    mapdir = "w"
  end
  -- always drop down for each row
   targetcolor = targetcolor + 1
   if targetcolor &gt; colorcount then
      break;
    end
    zs.make("s",mapcolors[targetcolor])
    zs.func.roomcol(null,mapcolors[targetcolor])

  for c = 1,maxcolumns - 1 do 
    targetcolor = targetcolor + 1
    if targetcolor &gt; colorcount then
      break;
    end
    zs.make(mapdir,mapcolors[targetcolor])
    zs.func.roomcol(null,mapcolors[targetcolor])
  end
end
</value>
</alias>

Saturday, January 9, 2021

Mud and cmud mapping

 

If you 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.

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'