[Checkins] SVN: z3c.csvvocabulary/trunk/src/ Upload project code.
Stephan Richter
srichter at cosmos.phy.tufts.edu
Sat Sep 16 08:43:34 EDT 2006
Log message for revision 70205:
Upload project code.
Changed:
A z3c.csvvocabulary/trunk/src/
A z3c.csvvocabulary/trunk/src/z3c/
A z3c.csvvocabulary/trunk/src/z3c/__init__.py
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/__init__.py
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/i18nextract.py
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/data/
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/data/sample.csv
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/tests.py
A z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py
-=-
Added: z3c.csvvocabulary/trunk/src/z3c/__init__.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/__init__.py 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/__init__.py 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1 @@
+# Make a package.
Property changes on: z3c.csvvocabulary/trunk/src/z3c/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,93 @@
+==============
+CSV Vocabulary
+==============
+
+This package provides a very simple vocabulary implementation using CSV
+files. The advantage of CSV files is that they provide an external point to
+specify data, which allows a non-developer to adjust the data themselves.
+
+ >>> import z3c.csvvocabulary
+
+ >>> import os.path
+ >>> path = os.path.dirname(z3c.csvvocabulary.__file__)
+
+CSV Vocabulary
+--------------
+
+The CSV Vocabulary implementation is really just a function that creates a
+simple vocabulary with titled terms. There is a ``sample.csv`` file in the
+``data`` directory of the ``testing`` sub-package, so let's create a
+vocabulary from that:
+
+ >>> csvfile = os.path.join(path, 'testing', 'data', 'sample.csv')
+
+ >>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile)
+ >>> samples
+ <zope.schema.vocabulary.SimpleVocabulary object at ...>
+
+ >>> sorted([term.value for term in samples])
+ ['value1', 'value2', 'value3', 'value4', 'value5']
+
+Let's now look at a term:
+
+ >>> term1 = samples.getTerm('value1')
+ >>> term1
+ <zope.schema.vocabulary.SimpleTerm object at ...>
+
+As you can see, the vocabulary automatically prefixes the value:
+
+ >>> term1.value
+ 'value1'
+
+ >>> term1.token
+ 'value1'
+
+ >>> term1.title
+ u'sample-value1'
+
+While it looks like the title is the wrong unicode string, it is really an
+I18n message:
+
+ >>> type(term1.title)
+ <type 'zope.i18nmessageid.message.Message'>
+
+ >>> term1.title.default
+ u'Title 1'
+
+ >>> term1.title.domain
+ 'zope'
+
+Of course, it is not always acceptable to make 'zope' the domain of the
+message. You can specify the message factory when initializing the vocabulary:
+
+ >>> from zope.i18nmessageid import MessageFactory
+ >>> exampleDomain = MessageFactory('example')
+
+ >>> samples = z3c.csvvocabulary.CSVVocabulary(csvfile, exampleDomain)
+ >>> term1 = samples.getTerm('value1')
+ >>> term1.title.domain
+ 'example'
+
+The vocabulary is designed to work with small data sets, typically choices in
+user interfaces. All terms are created upon initialization, so the vocabulary
+does not detect updates in the csv file or loads the data when needed. But as
+I said, this is totally okay.
+
+
+CSV Message String Extraction
+-----------------------------
+
+There is a simple function in ``i18nextract.py`` that extracts all message
+strings from the CSV files in a particular sub-tree. Here we just want to make
+sure that the function completes and some dummy data from the testing package
+will be used:
+
+ >>> basedir = os.path.dirname(z3c.__file__)
+
+ >>> catalog = z3c.csvvocabulary.csvStrings(path, basedir)
+ >>> pprint(catalog)
+ {u'sample-value1': [('...sample.csv', 1)],
+ u'sample-value2': [('...sample.csv', 2)],
+ u'sample-value3': [('...sample.csv', 3)],
+ u'sample-value4': [('...sample.csv', 4)],
+ u'sample-value5': [('...sample.csv', 5)]}
Property changes on: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/__init__.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/__init__.py 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/__init__.py 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,4 @@
+# Make a package.
+
+from z3c.csvvocabulary.vocabulary import CSVVocabulary
+from z3c.csvvocabulary.i18nextract import csvStrings
Property changes on: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/i18nextract.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/i18nextract.py 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/i18nextract.py 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,56 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""I18n Extraction Tool for CSV Files
+
+Important: The functionality provided in this package must be manually
+integrated into your string extraction script/tool.
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import os.path
+
+from z3c.csvvocabulary import vocabulary
+
+def _extractCsvStrings(arg, dirname, fnames):
+ catalog, basepath, exclude_dirs = arg
+ # Make sure we have a data directory
+ if os.path.split(dirname)[-1] != 'data':
+ return
+ # Make sure we are not stepping into an excluded dir
+ for exclude_dir in exclude_dirs:
+ if dirname.startswith(exclude_dir):
+ return
+ # Now we extract all strings from the csv files
+ for filename in fnames:
+ if filename.endswith('.csv'):
+ fullpath = os.path.join(dirname, filename)
+ vocab = vocabulary.CSVVocabulary(fullpath)
+ for index, term in enumerate(vocab):
+ if term.title not in catalog:
+ catalog[term.title] = []
+ reportpath = fullpath.replace(basepath, '')
+ catalog[term.title].append((reportpath, index+1))
+
+
+def csvStrings(path, base_dir, exclude_dirs=()):
+ """Extract message strings from CSV data files
+
+ This function allows the standard i18n extraction tool arguments for
+ simple integration.
+ """
+ catalog = {}
+ exclude_dirs = [os.path.join(path, dir) for dir in exclude_dirs]
+ os.path.walk(path, _extractCsvStrings, (catalog, base_dir, exclude_dirs))
+ return catalog
Property changes on: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/i18nextract.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/data/sample.csv
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/data/sample.csv 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/testing/data/sample.csv 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,5 @@
+"value1";"Title 1"
+"value2";"Title 2"
+"value3";"Title 3"
+"value4";"Title 4"
+"value5";"Title 5"
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/tests.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/tests.py 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/tests.py 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,32 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Base Components test setup
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite, pprint
+
+def test_suite():
+ return unittest.TestSuite((
+ DocFileSuite('README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ globs={'pprint': pprint}
+ ),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Added: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py 2006-09-16 12:42:00 UTC (rev 70204)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py 2006-09-16 12:43:33 UTC (rev 70205)
@@ -0,0 +1,41 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""CSV Vocabulary Implementation
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import csv
+import os.path
+
+from zope.schema import vocabulary
+from zope.i18nmessageid import MessageFactory
+
+_ = MessageFactory('zope')
+
+
+def CSVVocabulary(filename, messageFactory=_):
+ # Create a prefix
+ prefix = os.path.split(filename)[-1][:-4]
+ # Open a file and read the data
+ f = file(filename)
+ reader = csv.reader(f, delimiter=";")
+ # Create the terms and the vocabulary
+ terms = []
+ for id, title in reader:
+ title = unicode(title, 'latin1')
+ term = vocabulary.SimpleTerm(
+ id, title=messageFactory(prefix+'-'+id, default=title))
+ terms.append(term)
+ return vocabulary.SimpleVocabulary(terms)
Property changes on: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Checkins
mailing list