-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathjson.coffee
More file actions
executable file
·154 lines (129 loc) · 3.57 KB
/
Copy pathjson.coffee
File metadata and controls
executable file
·154 lines (129 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /usr/bin/env coffee
##
#
# DICOM Json Model
#
# http://medical.nema.org/dicom/2013/output/chtml/part18/sect_F.2.html
#
##
fs = require("fs")
stream = require("stream")
zlib = require("zlib")
printf = require("printf")
ConcatStream = require("concat-stream")
minimist = require("minimist")
tags = require("../lib/tags")
decoder = require("../lib/decoder")
log = require("./logger")("json")
JsonEncoder = require("./json/encoder")
JsonSink = require("./json/sink")
JsonSource = require("./json/source")
# remain compatible with old, all-in-one json.coffee
_COMPATIBILITY = true
if _COMPATIBILITY
exports.JsonEncoder = JsonEncoder
exports.JsonSink = JsonSink
exports.JsonSource = JsonSource
# helper functions
# path elements may be anything that can be
# tags.for_tag-ed except NUBMERS - they
# represent sequence item access
get_element = (json, path...) ->
lookup = []
must_pop = false
for p in path
if (typeof p) == 'number'
lookup.push p
must_pop = false
else
lookup.push tags.for_tag(p).tag_str
lookup.push "Value"
must_pop = true
if must_pop
lookup.pop()
result = json
for x in lookup
result = result?[x]
return result
get_values = (json, path...) ->
return get_element(json, path...)?.Value
get_value = (json, path...) ->
return get_values(json, path...)?[0]
get_vr = (json, path...) ->
return get_element(json, path...)?.vr
_get_filename = (obj_or_fn) ->
if typeof(obj_or_fn) == 'string'
obj_or_fn
else
obj_or_fn.filename
_get_bulkdata_uri = (obj_or_fn) ->
if typeof(obj_or_fn) == 'string'
obj_or_fn
else
obj_or_fn.bulkdata_uri ? obj_or_fn.filename
file2jsonstream = (fn, cb) ->
fs.createReadStream _get_filename(fn)
.on 'error', cb
.pipe decoder {guess_header: true}
.on 'error', cb
.pipe new JsonEncoder({bulkdata_uri: _get_bulkdata_uri(fn)})
.on 'error', cb
file2json = (fn, cb) ->
file2jsonstream(fn, cb)
.pipe new JsonSink(cb)
.on 'error', cb
# cb is called for errors
gunzip2jsonstream = (fn, cb) ->
fs.createReadStream _get_filename(fn)
.on 'error', cb
.pipe zlib.createGunzip()
.on 'error', cb
.pipe decoder {guess_header: true}
.on 'error', cb
.pipe new JsonEncoder({bulkdata_uri: _get_bulkdata_uri(fn)})
.on 'error', cb
gunzip2json = (fn, cb) ->
gunzip2jsonstream(fn, cb)
.pipe new JsonSink(cb)
.on 'error', cb
# make a decoder piping into json sink
# errors are correctly chained,
# returns the DECODER
# options: transfer_syntax (for decoder), bulkdata_uri for encoder
decoder2json = (opts, cb) ->
_dec = new decoder(opts)
_dec.on 'error', cb
.pipe new JsonEncoder(opts)
.on 'error', cb
.pipe new JsonSink(cb)
.on 'error', cb
return _dec
exports.get_element = get_element
exports.get_values = get_values
exports.get_value = get_value
exports.get_vr = get_vr
exports.file2jsonstream = file2jsonstream
exports.gunzip2jsonstream = gunzip2jsonstream
exports.file2json = file2json
exports.gunzip2json = gunzip2json
exports.decoder2json = decoder2json
_err_cb = (err) ->
console.error "Error:", err.stack
process.exit 1
if require.main is module
options = minimist(process.argv.slice(2),
{boolean: ['gunzip', 'emit'], alias: {z: 'gunzip', 'e': 'emit'}})
filename = options._[0]
if options.gunzip
input = gunzip2jsonstream(filename, _err_cb)
else
input = file2jsonstream(filename, _err_cb)
if options.emit
sink = new JsonSink (err, data) ->
throw err if err
log.info "setting up json source"
source = new JsonSource(data)
source.pipe process.stdout
input.pipe sink
else
input.pipe process.stdout