Commits

Lynn Rees committed 4ab8646

- read/write files

Comments (0)

Files changed (1)

callchain/chain.py

 # -*- coding: utf-8 -*-
 '''root chain mixins'''
 
+import codecs
 from itertools import chain
 
 from appspace.keys import NoAppError
         # sync with branch incoming and outgoing things
         return self.extend(branch.incoming).outextend(branch.outgoing)
 
+    def read(self, mode='rb', encoding=None, errors='strict', *paths):
+        '''
+        read incoming things from files
+
+        @param mode: mode to open files (default: 'r')
+        @param encoding: encoding for text data (default: `None`)
+        @param errors: error handling in encoding text data (default: 'strict')
+        @param *paths: sequence of filesystem paths
+        '''
+        with self._context:
+            append = self._append
+            for path in paths:
+                with codecs.open(path, mode, encoding) as f:
+                    append(f.read())
+        return self
+
+    def write(self, mode='rb', encoding=None, errors='strict', *paths):
+        '''
+        write outgoing things to files
+
+        @param mode: mode to open files (default: 'r')
+        @param encoding: encoding for text data (default: `None`)
+        @param errors: error handling in encoding text data (default: 'strict')
+        @param *paths: sequence of filesystem paths
+        '''
+        values = self.value()
+        for idx, path in enumerate(paths):
+            with codecs.open(path, mode, encoding) as f:
+                f.write(values[idx])
+        return self
+
 
 class EventRootMixin(RootMixin):