-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfechout.py
105 lines (80 loc) · 2.63 KB
/
fechout.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
#encapsulate the output of fec
import yaml
#from
import os
def checkin(path,filename):
print "going to process %s" % path
os.system("bash ./checkin.sh %s %s" % (path,filename))
class FechoutFile ():
u"""
Wrapper for fech, manage the zip file
"""
def __init__(self,name,sourcefile,baseurl, urlfile):
self.name=name
self.sourcefile=urlfile
self.sourceurl= baseurl + "/" + urlfile
self.outfile=None
self._attr={}
self._rows=[]
self._raw=[]
self.open()
def raw_line(self,line):
self._raw.append(line)
def pathname(self):
self.sourcefile = self.sourcefile.replace (".zip","")
year = self.sourcefile[0:4]
#print(year)
return "fech_yaml/%s/%s/" % (year,self.sourcefile)
def filename(self, count =0):
self._filename = self.name
if (count > 0):
self._filename = self._filename + "_%d" % count
self._filename = self._filename + ".yml"
return self._filename
def exists(self):
return os.path.exists(self.pathname () + self.filename())
def open(self):
try:
if not os.path.exists(self.pathname()):
os.makedirs(self.pathname())
except Exception , e:
print(e)
pass
def file_attributes(self,attr):
self._attr=attr
def rows(self,rows):
self._rows=rows
def create_yaml(self,rows,count):
#count = count + 1
filename=self.pathname () + self.filename(count)
print "writing %s" % filename
self.outfile=open(filename,"w")
self.outfile.write( yaml.dump(
{
'type': "chunk",
'sourceurl' : self.sourcefile,
'filename' : self.name,
'header' : self._attr,
'countrows' : len(rows),
'rows' : rows,
},
default_flow_style=False,
Dumper=yaml.CDumper
))
self.outfile.flush()
self.outfile.close()
print "going to checkin"
checkin(self.pathname(), self.filename(count) )
print "after checkin"
class Fechout ():
u"""
Wrapper for fech, manage the zip file
"""
def __init__(self):
self.url=None
def set_input_url(self, url):
self.url=url
def set_input_zipfilename(self, filename):
self.zipfile=filename
def create_file(self, name, sourcefile,baseurl, urlfile):
return FechoutFile(name,sourcefile,baseurl, urlfile)