CSV reading lists

This commit is contained in:
Sam Ruby 2007-07-31 19:38:57 -04:00
parent 50aab3005c
commit 468bbcc4fc
6 changed files with 65 additions and 5 deletions

View File

@ -139,9 +139,9 @@ you are free to include as few or as many parameters as you like, most of
the predefined themes presume that at least <code>name</code> is defined.</p>
<p>The <code>content_type</code> parameter can be defined to indicate that
this subscription is a <em>reading list</em>, i.e., is an external list
of subscriptions. At the moment, two formats of reading lists are supported:
<code>opml</code> and <code>foaf</code>. In the future, support for formats
like <code>xoxo</code> could be added.</p>
of subscriptions. At the moment, three formats of reading lists are supported:
<code>opml</code>, <code>foaf</code>, and <code>csv</code>. In the future,
support for formats like <code>xoxo</code> could be added.</p>
<p><a href="normalization.html#overrides">Normalization overrides</a> can
also be defined here.</p>

View File

@ -138,7 +138,7 @@ def load(config_file):
parser.read(config_file)
import config, planet
from planet import opml, foaf
from planet import opml, foaf, csv_config
log = planet.logger
if not log:
log = planet.getLogger(config.log_level(),config.log_format())
@ -197,6 +197,8 @@ def load(config_file):
opml.opml2config(data, cached_config)
elif content_type(list).find('foaf')>=0:
foaf.foaf2config(data, cached_config)
elif content_type(list).find('csv')>=0:
csv_config.csv2config(data, cached_config)
else:
from planet import shell
import StringIO
@ -346,7 +348,8 @@ def reading_lists():
for section in parser.sections():
if parser.has_option(section, 'content_type'):
type = parser.get(section, 'content_type')
if type.find('opml')>=0 or type.find('foaf')>=0 or type.find('.')>=0:
if type.find('opml')>=0 or type.find('foaf')>=0 or \
type.find('csv')>=0 or type.find('.')>=0:
result.append(section)
return result

28
planet/csv_config.py Executable file
View File

@ -0,0 +1,28 @@
import csv
# input = csv, output = ConfigParser
def csv2config(input, config=None):
if not hasattr(input, 'read'):
input = csv.StringIO(input)
if not config:
config = ConfigParser()
reader = csv.DictReader(input)
for entry in reader:
section = entry[reader.fieldnames[0]]
config.add_section(section)
for name, value in entry.items():
if value and name != reader.fieldnames[0]:
config.set(section, name, value)
return config
if __name__ == "__main__":
# small main program which converts OPML into config.ini format
import sys, urllib
config = ConfigParser()
for input in sys.argv[1:]:
csv2config(urllib.urlopen(input), config)
config.write(sys.stdout)

View File

@ -0,0 +1,3 @@
url,name,filters
feed1,one
feed2,two,bar
1 url,name,filters
2 feed1,one
3 feed2,two,bar

View File

@ -0,0 +1,7 @@
[Planet]
name = CSV Test Configuration
cache_directory = tests/work/config/cache
filters = foo
[tests/data/config/basic.csv]
content_type = csv

19
tests/test_config_csv.py Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env python
import unittest
from planet import config
class ConfigCsvTest(unittest.TestCase):
def setUp(self):
config.load('tests/data/config/rlist-csv.ini')
# administrivia
def test_feeds(self):
feeds = config.subscriptions()
feeds.sort()
self.assertEqual(['feed1', 'feed2'], feeds)
def test_filters(self):
self.assertEqual(['foo','bar'], config.filters('feed2'))
self.assertEqual(['foo'], config.filters('feed1'))