-
Notifications
You must be signed in to change notification settings - Fork 2
/
commons_filter.py
163 lines (148 loc) · 4.72 KB
/
commons_filter.py
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
import sys
import StringIO
import json
from phpserialize import loads
from collections import namedtuple
import codecs
import re
image_info = namedtuple('image_info', 'name, \
size, \
width, \
height, \
metadata, \
bits, \
media_type, \
major_mime, \
minor_mime, \
description, \
user, \
user_text, \
timestamp, \
sha1')
# lexical token symbols
DQUOTED, SQUOTED, UNQUOTED, COMMA, NEWLINE = xrange(5)
_pattern_tuples = (
(r'"[^"]*"', DQUOTED),
(r"'[^']*'", SQUOTED),
(r",", COMMA),
(r"$", NEWLINE), # matches end of string OR \n just before end of string
(r"[^,\n]+", UNQUOTED), # order in the above list is important
)
_matcher = re.compile(
'(' + ')|('.join([i[0] for i in _pattern_tuples]) + ')',
).match
_toktype = [None] + [i[1] for i in _pattern_tuples]
# need dummy at start because re.MatchObject.lastindex counts from 1
def csv_split(text):
"""Split a csv string into a list of fields.
Fields may be quoted with " or ' or be unquoted.
An unquoted string can contain both a " and a ', provided neither is at
the start of the string.
A trailing \n will be ignored if present.
"""
fields = []
pos = 0
want_field = True
while 1:
m = _matcher(text, pos)
if not m:
raise ValueError("Problem at offset %d in %r" % (pos, text))
ttype = _toktype[m.lastindex]
if want_field:
if ttype in (DQUOTED, SQUOTED):
fields.append(m.group(0)[1:-1])
want_field = False
elif ttype == UNQUOTED:
fields.append(m.group(0))
want_field = False
elif ttype == COMMA:
fields.append("")
else:
assert ttype == NEWLINE
fields.append("")
break
else:
if ttype == COMMA:
want_field = True
elif ttype == NEWLINE:
break
else:
print "*** Error dump ***" #, ttype, repr(m.group(0)), fields
#raise ValueError("Missing comma at offset %d in %r" % (pos, text))
pos = m.end(0)
return fields
class LineReader:
def __init__(self):
self.line = ''
self.seperator = "\n"
self.window = []
def add(self, character):
self.window.append(character)
self.line += character
if len(self.window) > len(self.seperator):
self.window.pop(0)
def found_line(self):
if ''.join(self.window) == self.seperator:
rv = self.line
self.line = ''
return rv
else:
return False
from pprint import pprint
def process(row):
ret = {}
try:
cur_file = image_info(*row)
except TypeError:
print 'could not open row'
return ret
if '.ogg' in cur_file.name:
try:
cur_metadata = loads(cur_file.metadata.replace('\\"', '\"'))
except:
cur_metadata = {}
ret = {'name': cur_file.name.replace('\\', ''),
'length': cur_metadata.get('length'),
'type': cur_file.media_type,
'user': cur_file.user_text,
'timestamp': cur_file.timestamp,
}
return ret
reader = LineReader()
inside_insert = False
search = 'INSERT INTO `image` VALUES'
err_ogg = 0
output_file = codecs.open('commons_oggs.json', 'w', 'utf-8')
while True:
c = sys.stdin.read(1)
if not len(c) > 0:
break
reader.add(c)
line = reader.found_line()
if len(reader.line) > len(search) and reader.line.startswith(search):
inside_insert = True
reader.line = ''
reader.seperator = '),'
if not inside_insert:
continue
if not line:
continue
# strip out '(' at beginning and '),' at end
string = line[1:-2]
# print line[1:-2]
try:
split_image = csv_split(string)
#pprint(split)
except Exception as e:
err_ogg += 1
print err_ogg
continue
else:
rv = process(split_image)
#string_array = string.split(',')
if len(rv.keys()) > 0:
output_file.write(json.dumps(rv))
output_file.write('\n')
elif '.ogg' in line:
err_ogg += 1
print err_ogg