Skip to content
This repository has been archived by the owner on May 28, 2022. It is now read-only.

Commit

Permalink
Merge 8797061 into 67b833e
Browse files Browse the repository at this point in the history
  • Loading branch information
riolowry committed Dec 7, 2013
2 parents 67b833e + 8797061 commit c314d46
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 52 deletions.
76 changes: 24 additions & 52 deletions src/freeseer/framework/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@

from PyQt4 import QtSql

from freeseer import settings
from freeseer import __version__
from freeseer.framework.presentation import Presentation
from freeseer.framework.failure import Failure, Report
from freeseer.framework.rss_parser import FeedParser
from freeseer.framework.plugin import PluginManager

log = logging.getLogger(__name__)

Expand All @@ -47,6 +48,7 @@ def __init__(self, configdir, talkdb_file="presentations.db"):
"""
self.configdir = configdir
self.talkdb_file = os.path.abspath("%s/%s" % (self.configdir, talkdb_file))
self.plugman = PluginManager(self.configdir)

if not os.path.isfile(self.talkdb_file):
file = open(self.talkdb_file, 'w')
Expand Down Expand Up @@ -360,13 +362,15 @@ def get_talk_between_time(self, event, room, starttime, endtime):
def add_talks_from_rss(self, rss):
"""Adds talks from an rss feed."""
entry = str(rss)
feedparser = FeedParser(entry)
plugin = self.plugman.get_plugin_by_name("Rss FeedParser", "Importer")
feedparser = plugin.plugin_object
feedparser.pres_list = feedparser.get_presentations_list(entry)

if len(feedparser.build_data_dictionary()) == 0:
if not feedparser.pres_list:
log.info("RSS: No data found.")

else:
for presentation in feedparser.build_data_dictionary():
for presentation in feedparser.pres_list:
talk = Presentation(presentation["Title"],
presentation["Speaker"],
presentation["Abstract"], # Description
Expand All @@ -381,56 +385,24 @@ def add_talks_from_csv(self, fname):
Title and speaker must be present.
"""
file = open(fname, 'r')
try:
reader = csv.DictReader(file)
for row in reader:
try:
title = unicode(row['Title'], 'utf-8')
speaker = unicode(row['Speaker'], 'utf-8')
except KeyError:
log.error("Missing Key in Row: %s", row)
return

try:
abstract = unicode(row['Abstract'], 'utf-8') # Description
except KeyError:
abstract = ''

try:
level = unicode(row['Level'], 'utf-8')
except KeyError:
level = ''

try:
event = unicode(row['Event'], 'utf-8')
except KeyError:
event = ''

try:
room = unicode(row['Room'], 'utf-8')
except KeyError:
room = ''

try:
time = unicode(row['Time'], 'utf-8')
except KeyError:
time = ''

talk = Presentation(title,
speaker,
abstract,
level,
event,
room,
time)
self.insert_presentation(talk)
entry = str(fname)
plugin = self.plugman.get_plugin_by_name("CSV Importer", "Importer")
importer = plugin.plugin_object
importer.pres_list = importer.get_presentations_list(entry)

except IOError:
log.error("CSV: File %s not found", file)
if not importer.pres_list:
log.info("CSV: No data found.")

finally:
file.close()
else:
for presentation in importer.pres_list:
talk = Presentation(presentation["Title"],
presentation["Speaker"],
presentation["Abstract"], # Description
presentation["Level"],
presentation["Event"],
presentation["Room"],
presentation["Time"])
self.insert_presentation(talk)

def export_talks_to_csv(self, fname):
fieldNames = ('Title',
Expand Down
20 changes: 20 additions & 0 deletions src/freeseer/framework/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, configdir, profile=None):
"AudioMixer": IAudioMixer,
"VideoInput": IVideoInput,
"VideoMixer": IVideoMixer,
"Importer": IImporter,
"Output": IOutput})
self.plugmanc.collectPlugins()

Expand Down Expand Up @@ -264,6 +265,17 @@ def get_videomixer_plugins(self):
unfiltered_plugins = self.plugmanc.getPluginsOfCategory("VideoMixer")
return self._get_supported_plugins(unfiltered_plugins)

def get_importer_plugins(self):
"""Returns a list of plugins that are supported by the users OS as detected by python's sys.platform library
Parameters:
none
Returns:
list of supported Importer plugins
"""
unfiltered_plugins = self.plugmanc.getPluginsOfCategory("Importer")
return self._get_supported_plugins(unfiltered_plugins)

def get_output_plugins(self):
"""
Returns a list of plugins that are supported by the users OS as
Expand Down Expand Up @@ -510,6 +522,14 @@ def generate_xml_metadata(self, metadata):
return ET.ElementTree(root)


class IImporter(IBackendPlugin):
CATEGORY = "Importer"

def get_presentations_list(self):
"""Builds a list with all presentations"""
raise NotImplementedError


class PluginError(Exception):
def __init__(self, message):
self.message = message
Empty file.
9 changes: 9 additions & 0 deletions src/freeseer/plugins/importer/csv_importer.freeseer-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Core]
Name = CSV Importer
Module = csv_importer

[Documentation]
Author = Rio Lowry
Version = 3.0.9999
Website = http://fosslc.org
Description = CSV Importer plugin allows user to importfrom csv files when adding presentations
71 changes: 71 additions & 0 deletions src/freeseer/plugins/importer/csv_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/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/Freeseer/freeseer/

"""
CSV Importer
--------------
An import plugin for CSV files used when adding presentations
@author: Rio Lowry
"""

import csv

from freeseer.framework.presentation import Presentation
from freeseer.framework.plugin import IImporter


class CsvImporter(IImporter):
"""CSV Importer plugin for Freeseer
Provides functionality to import presentations from a CSV file
"""

name = "CSV Importer"
os = ["linux", "linux2"]

def get_presentations_list(self, fname):
"""Returns list of dictionaries of all presentations in the csv file."""
presentations_list = []

try:
with open(fname) as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
talk = {'Title': unicode(row.get('Title', ''), 'utf-8'),
'Speaker': unicode(row.get('Speaker', ''), 'utf-8'),
'Abstract': unicode(row.get('Abstract', ''), 'utf-8'), # Description
'Level': unicode(row.get('Level', ''), 'utf-8'),
'Event': unicode(row.get('Event', ''), 'utf-8'),
'Room': unicode(row.get('Room', ''), 'utf-8'),
'Time': unicode(row.get('Time', ''), 'utf-8')
}

presentations_list.append(talk)

except IOError:
log.exception("CSV: File %s not found", csv_file)

return presentations_list
9 changes: 9 additions & 0 deletions src/freeseer/plugins/importer/rss_feedparser.freeseer-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Core]
Name = Rss FeedParser
Module = rss_feedparser

[Documentation]
Author = Rio Lowry
Version = 3.0.9999
Website = http://fosslc.org
Description = Rss FeedParser plugin provides a RSS parser used when adding presentations
119 changes: 119 additions & 0 deletions src/freeseer/plugins/importer/rss_feedparser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/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/Freeseer/freeseer/

"""
Rss FeedParser
--------------
An import plugin which provides a RSS parser used when adding presentations
@author: Rio Lowry
"""

from HTMLParser import HTMLParser

from feedparser import parse

from freeseer.framework.plugin import IImporter


class MLStripper(HTMLParser):
"""Simple stripper to remove markup
MLStripper from http://stackoverflow.com/a/925630/72321
"""
def __init__(self):
self.reset()
self.fed = []

def handle_data(self, d):
self.fed.append(d)

def get_data(self):
return ''.join(self.fed)


def strip_tags(html):
"""Helper functions to strip markup from passed object"""
s = MLStripper()
s.feed(html)
return s.get_data()


class FeedParser(IImporter):
"""FeedParser plugin for Freeseer
Provides functionality to allow a RSS feed to be fetched and parsed
"""

name = "Rss FeedParser"
os = ["linux", "linux2"]

def get_presentations_list(self, feed_url):
"""Takes feed_url, fetches, parses feed_url
Returns list of dictionaries of all presentations on parsed feed.
"""
parsed_feed = parse(feed_url)
self.presentations = parsed_feed.entries
self.presentations_length = len(self.presentations)
self.presentation_list = []
for i, entry in enumerate(self.presentations):
pres_data = entry["summary_detail"]["value"]

# We want to split the pres_data by 3 spaces because some field
# data contain spaces and we don't want to erroneously split that
# data.

pres_data = filter(None, pres_data.split(" "))

presentation = {'Title': entry.title.strip(),
'Speaker': self.get_presentation_field(pres_data, "field-field-speaker"),
'Abstract': self.get_presentation_field(pres_data, "field-field-abstract"),
'Level': self.get_presentation_field(pres_data, "field-field-level"),
'Status': self.get_presentation_field(pres_data, "field-field-status"),
'Time': self.get_presentation_field(pres_data, "field-field-time"),
'Event': self.get_presentation_field(pres_data, "field-field-event"),
'Room': self.get_presentation_field(pres_data, "field-field-room")
}
self.presentation_list.append(presentation)

return self.presentation_list

def get_presentation_field(self, presentation, field_name):
"""Returns the field_name of the presentation at presentation"""

# Due to the autogenerated structure of the rss feed the field data is
# offset by 4 elements from field_name in the passed presentation

item_presentation_offset = 4
for i, element in enumerate(presentation):
if field_name in element:

# data in element is in unicode, we want an error raised if
# there are characters that we are not expecting

field_data = unicode(presentation[i + item_presentation_offset])

return strip_tags(field_data).strip()
Loading

0 comments on commit c314d46

Please sign in to comment.