-
Notifications
You must be signed in to change notification settings - Fork 2
/
parsecsv.py
executable file
·93 lines (77 loc) · 2.87 KB
/
parsecsv.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# freeseer - vga/presentation capture software
#
# Copyright (C) 2013 Free and Open Source Software Learning Centre
# http://fosslc.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# For support, questions, suggestions or any other inquiries, visit:
# http://wiki.github.com/fosslc/freeseer/
import csv
import sys
import event
import validurl
from config import *
def parseCsv(filename, checkUrl):
'''
create a list of events based on the input csv file
'''
data = csv.reader(open(filename))
# Read the column names from the first line of the file
fields = data.next()
# class for the name of each event
# print fields
eventName = EventName(fields)
eventList = []
# for each of the event given
# create a event object for it with the data
for row in data:
# Zip together the field names and values
items = zip(fields, row)
componentUrl = {}
# create a dictionary of component name to its url for this event
for (name, value) in items:
if not name.strip()== 'name':
# Validate the url given
if checkUrl and not validurl.verifyUrl(value.strip()):
print >> sys.stderr, value.strip() + " invalid url"
exit(1)
componentUrl[name.strip()] = value.strip()
e = event.Event(eventName(items), componentUrl)
eventList.append(e)
return eventList
def varifyFields(fields):
'''
verify if all the fields given in in the component list
'''
for field in fields:
if field not in componentList and field != "name":
print >> sys.stderr, _("Unknow component: " + field + ".")
exit(1)
class EventName(object):
'''
class to handle the name of the event
if the event name is given, return the event name
otherwise automatically generate event name starting from 0
'''
def __init__(self, fields):
self.default = 0
self.useDefault = 'name' not in fields
def __call__(self, zip):
if self.useDefault:
self.default += 1;
return str(self.default-1)
else:
return [i[1] for i in zip if i[0] == "name"][0]