-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.coffee
172 lines (152 loc) · 5.19 KB
/
main.coffee
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
fs = require 'fs'
md5 = require 'md5'
mkdirp = require 'mkdirp'
Record = require './record'
stdin = process.openStdin()
tty = require('tty')
#console.log(tty)
#tty.setRawMode(true)
gather_by = (record, key, cb, depth = 6) ->
new Record record, (_record) ->
if _record._attributes[key]
value = _record._attributes[key]
setImmediate ->
cb(value)
if value._isRecord and depth > 0
setImmediate ->
gather_by value, key, cb, depth-1
setImmediate ->
gather_by 'Erez', 'friend', (value) ->
console.log value.id.toString()
define_new_record = (record_name, cb) ->
return new Record record_name, (record) ->
cb(Object.keys(record._attributes).length is 0)
describe = (record_name) ->
return new Record record_name, (record) ->
console.log record_name
for key, value of record._attributes
if value is true
return console.log('has',key)
if value is false
return console.log('does not have',key)
unless key is '__name__'
console.log(key,'is',value)
new_input = (record_name, attrib, value, cb) ->
return new Record record_name, (record) ->
if typeof(value) is 'object'
value = value.map (v) ->
if /^[A-Z]/.test(v)
return new Record v
return v
record.set(attrib, value)
record.save()
return cb(null)
if /^[A-Z]/.test(value)
new Record value, (_value) ->
record.set(attrib, _value)
record.save()
if Object.keys(_value._attributes).length > 0
cb(_value._attributes)
else
cb(null)
else
if parseInt(value).toString() is value
value = parseInt(value)
record.set(attrib, value)
record.save()
process.stdin.resume()
wtf = ->
console.log """
"This is [record]" - Creates a new record
"[record] has (a|an) [attribute], [value] and [value]..." - Set attributes and relations
"[record] has (number) [attribute], [value] and [value]... - Set array of values
"track [index]" - Add new index
"report [index]" - Lists index
"describe [record]" - displays shallow record
"forget [record]" - removes all attributes and relations from object
"""
fs.readdir '/img/index/', (e, files) =>
console.log """
---------------------------------------------------------
Welcome to StupidDB, the dumbest DB created.
"wtf" displays the help, it is not very helpful.
---------------------------------------------------------
"""
console.log 'tracking', files
process.stdin.on 'data', (chunk) ->
chunk = chunk.toString()
if chunk.indexOf('wtf') is 0
return wtf()
if chunk.indexOf('This is') is 0
record_name = chunk.slice(8).trim()
return define_new_record record_name, (isNew) ->
unless isNew
process.stdout.write 'I already know '+chunk.slice(8)+'\n\r'
else
process.stdout.write 'now I know ' + chunk.slice(8)+'\n\r'
if chunk.indexOf('track') is 0
parts = chunk.split(' ').map (e) -> e.trim()
console.log 'tracking',parts[1]
location = '/img/index/'+parts[1]+'/'
return mkdirp location, (e) ->
console.error(e) if e
if chunk.indexOf('report') is 0
parts = chunk.split(' ').map (e) -> e.trim()
location = '/img/index/'+parts[1]+'/'
console.log 'reporting '+parts[1]
_data = {}
return fs.readdir location, (e, files) =>
files.map (file) ->
fs.readFile location+file+'/__name__', (e, data) ->
console.log '-=->',data.toString()
if chunk.indexOf('describe') is 0
parts = chunk.split(/\s/)
return describe parts[1]
if chunk.indexOf('forget') is 0
return new Record chunk.substring(7).trim(), (record) ->
record.vanish (done) ->
process.stdout.write 'who is '+chunk.slice(7).trim()+'?'+'\n\r'
if chunk.indexOf('delete') is 0
parts = chunk.split(' ').map (e) -> e.trim()
if parts.length > 4
record = parts[5]
key = parts[3]
value = parts[1]
else
record = parts[3]
key = parts[1]
value = null
console.log record, key, value, record is 'Erez'
return new Record record, (_record) ->
_record.remove key, value
if /^[A-Z]/.test(chunk.slice(0,1))
from = chunk.substring 0, chunk.indexOf(' ')
if /has\s([0-9])/.test(chunk)
quantity = /has\s([0-9])/.exec(chunk)[1]
parts = chunk.split(/[0-9]/)
_parts = parts[1].split(',').map (e) -> e.trim()
attrib = _parts[0]
value = _parts[1].split('and').map (e) -> e.trim()
process.stdout.write 'I see.\n\r'
return new_input from, attrib, value, (new_record) ->
console.log('hmmmm')
if chunk.indexOf('has a') > -1
parts = chunk.split(' has a ').map (e) -> e.trim()
if parts.length is 1
parts = chunk.split(' has an ').map (e) -> e.trim()
parts = parts[1].split(',').map (e) -> e.trim()
if parts.length > 1
attrib = parts[0]
value = parts[1]
else
attrib = parts[0]
value = true
process.stdout.write 'I see.\n\r'
new_input from, attrib, value, (new_record) ->
if new_record
process.stdout.write 'I already know '+value+'\n\r'
console.log(new_record)
else
console.log 'now I know', value
else
console.log 'ok, '+chunk.trim()