Skip to content

Instantly share code, notes, and snippets.

@swiatczak
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save swiatczak/ceb46f16ea24338b0fd9 to your computer and use it in GitHub Desktop.

Select an option

Save swiatczak/ceb46f16ea24338b0fd9 to your computer and use it in GitHub Desktop.
CoffeeScript - parse PeopleSoft project XML export file
path = require 'path'
fs = require 'fs'
Fiber = require 'fibers'
Future = require 'fibers/future'
wait = Future.wait
readFile = Future.wrap(fs.readFile)
#
# peoplesoft XML project file parser - similar to:
# python version: https://github.com/swiatczak/psprojectparser
#
# just playing with class construction and chaining of the methods
#
# References:
# http://coffeescriptcookbook.com/chapters/classes_and_objects/chaining
# http://arcturo.github.io/library/coffeescript/03_classes.html
# Fibers:
# https://github.com/laverdet/node-fibers
# http://competo.com/?p=290
# http://bjouhier.wordpress.com/2012/03/11/fibers-and-threads-in-node-js-what-for/
# http://stackoverflow.com/questions/14854346/how-to-run-code-using-node-js-fibers
# http://sstur.com/pres-fibers/#/
# http://blog.willconant.com/post/8583050398/why-im-using-node-fibers-for-my-current-project
class ProjectParser
# creates a chained accessor methods for each property defined
@chained: (obj, attr) ->
(newValues...) ->
if newValues.length == 0
obj.properties[attr]
else
obj.properties[attr] = newValues[0]
obj
#
constructor: ->
@properties=
prjfile: ''
wrkfldr: ''
outfldr: ''
startRE: ''
endRE: ''
this[a] = ProjectParser.chained(this, a) for a of @properties
# parse file
parseFile: (err, data) ->
console.log err if err
@lines = data.split('\n') if !err
section: (startRegex, endRegex) =>
fiber = Fiber(->
section = ""
inSection = false
sectType = ""
cntr = 1
while true
line = Fiber.yield()
if line
grp = startRegex.exec(line)
if grp
inSection = true
sectType = grp[1]
section = line
else
if inSection
if line.match(endRegex)
section = section + "\n" + line
console.log "------------- section ----------- " + sectType
console.log section
console.log "------------- ------- ----------- "
section = ""
inSection = false
else
section = section + "\n" + line
)
fiber.run.bind(fiber)
# read project file
sections: (target) =>
path = path.join(@wrkfldr(), @prjfile())
fut = readFile(path, 'utf8')
data = fut.wait()
lines = data.split('\n')
fiber = Fiber(->
t = target
index = 0
while (index < lines.length)
t(lines[index])
index += 1
)
fiber.run.bind(fiber)()
Fiber(->
pp = new ProjectParser().prjfile('file.XML').wrkfldr('data/fldr').outfldr('data/fldr/out')
#sss = pp.sections()
strtRe = /<instance class="(.*)">/ig
endRe = /<\/instance>/i
ss = pp.section(strtRe, endRe)
for s in pp.sections(ss)
console.log
).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment