[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/File - I18nFile.py:1.2

Marius Gedminas mgedmin@delfi.lt
Tue, 25 Jun 2002 06:47:44 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/File
In directory cvs.zope.org:/tmp/cvs-serv23410/lib/python/Zope/App/OFS/Content/File

Modified Files:
	I18nFile.py 
Log Message:
- Set default language before calling setData (more robust)
- Factored out common functionality into helper functions (_get,
  _get_or_add)
- Introduced _create for the sake of subclasses



=== Zope3/lib/python/Zope/App/OFS/Content/File/I18nFile.py 1.1 => 1.2 ===
 
 class I18nFile(Persistence.Persistent):
-    """I18n aware file object."""
+    """I18n aware file object.  It contains a number of File objects --
+    one for each language.
+    """
 
     __implements__ = II18nFile
 
@@ -38,19 +40,50 @@
         """ """
 
         self._data = {}
+        self.defaultLanguage = defaultLanguage
         self.setData(data, language=defaultLanguage)
-        self.setDefaultLanguage(defaultLanguage)
 
         if contentType is None:
-            self._contentType = ''
+            self.setContentType('')
         else:
-            self._contentType = contentType
+            self.setContentType(contentType)
 
 
     def __len__(self):
         return self.getSize()
 
 
+    def _create(self, data):
+        """Create a new subobject of the appropriate type.  Should be
+        overriden in subclasses.
+        """
+        return File(data)
+
+
+    def _get(self, language):
+        """Helper function -- return a subobject for a given language,
+        and if it does not exist, return a subobject for the default
+        language.
+        """
+        file = self._data.get(language)
+        if not file:
+            file = self._data[self.defaultLanguage]
+        return file
+
+
+    def _get_or_add(self, language, data=''):
+        """Helper function -- return a subobject for a given language,
+        and if it does not exist, create and return a new subobject.
+        """
+        if language is None:
+            language = self.defaultLanguage
+        file = self._data.get(language)
+        if not file:
+            self._data[language] = file = self._create(data)
+            self._p_changed = 1
+        return file
+
+
     ############################################################
     # Implementation methods for interface
     # Zope.App.OFS.IFile.IFile
@@ -72,37 +105,23 @@
         # content type explicitly passed in.
 
         if contentType is not None:
-            self._contentType = contentType
+            self.setContentType(contentType)
         self.setData(data, language)
 
 
     def getData(self, language=None):
         '''See interface IFile'''
-        file = self._data.get(language)
-        if not file:
-            file = self._data[self.defaultLanguage]
-        return file.getData()
+        return self._get(language).getData()
 
 
     def setData(self, data, language=None):
         '''See interface IFile'''
-
-        if language is None:
-            language = self.defaultLanguage
-        file = self._data.get(language)
-        if file:
-            file.setData(data)
-        else:
-            self._data[language] = File(data)
-            self._p_changed = 1
+        self._get_or_add(language).setData(data)
 
 
     def getSize(self, language=None):
         '''See interface IFile'''
-        file = self._data.get(language)
-        if not file:
-            file = self._data[self.defaultLanguage]
-        return file.getSize()
+        return self._get(language).getSize()
 
     #
     ############################################################