CSV reading lists
This commit is contained in:
parent
50aab3005c
commit
468bbcc4fc
@ -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>
|
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
|
<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
|
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:
|
of subscriptions. At the moment, three formats of reading lists are supported:
|
||||||
<code>opml</code> and <code>foaf</code>. In the future, support for formats
|
<code>opml</code>, <code>foaf</code>, and <code>csv</code>. In the future,
|
||||||
like <code>xoxo</code> could be added.</p>
|
support for formats like <code>xoxo</code> could be added.</p>
|
||||||
<p><a href="normalization.html#overrides">Normalization overrides</a> can
|
<p><a href="normalization.html#overrides">Normalization overrides</a> can
|
||||||
also be defined here.</p>
|
also be defined here.</p>
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ def load(config_file):
|
|||||||
parser.read(config_file)
|
parser.read(config_file)
|
||||||
|
|
||||||
import config, planet
|
import config, planet
|
||||||
from planet import opml, foaf
|
from planet import opml, foaf, csv_config
|
||||||
log = planet.logger
|
log = planet.logger
|
||||||
if not log:
|
if not log:
|
||||||
log = planet.getLogger(config.log_level(),config.log_format())
|
log = planet.getLogger(config.log_level(),config.log_format())
|
||||||
@ -197,6 +197,8 @@ def load(config_file):
|
|||||||
opml.opml2config(data, cached_config)
|
opml.opml2config(data, cached_config)
|
||||||
elif content_type(list).find('foaf')>=0:
|
elif content_type(list).find('foaf')>=0:
|
||||||
foaf.foaf2config(data, cached_config)
|
foaf.foaf2config(data, cached_config)
|
||||||
|
elif content_type(list).find('csv')>=0:
|
||||||
|
csv_config.csv2config(data, cached_config)
|
||||||
else:
|
else:
|
||||||
from planet import shell
|
from planet import shell
|
||||||
import StringIO
|
import StringIO
|
||||||
@ -346,7 +348,8 @@ def reading_lists():
|
|||||||
for section in parser.sections():
|
for section in parser.sections():
|
||||||
if parser.has_option(section, 'content_type'):
|
if parser.has_option(section, 'content_type'):
|
||||||
type = parser.get(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)
|
result.append(section)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
28
planet/csv_config.py
Executable file
28
planet/csv_config.py
Executable 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)
|
3
tests/data/config/basic.csv
Normal file
3
tests/data/config/basic.csv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
url,name,filters
|
||||||
|
feed1,one
|
||||||
|
feed2,two,bar
|
|
7
tests/data/config/rlist-csv.ini
Normal file
7
tests/data/config/rlist-csv.ini
Normal 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
19
tests/test_config_csv.py
Normal 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'))
|
Loading…
x
Reference in New Issue
Block a user