From 9c7e546712aa0da6847f704ca67d118db0e16af7 Mon Sep 17 00:00:00 2001
From: Danilo Piparo <danilo.piparo@cern.ch>
Date: Tue, 15 Mar 2016 08:12:31 +0100
Subject: [PATCH] Pythonizations

---
 bindings/pyroot/CMakeLists.txt                  |  3 +++
 bindings/pyroot/ROOT.py                         | 15 ++-------------
 bindings/pyroot/pythonizations/__init__.py      |  7 +++++++
 bindings/pyroot/pythonizations/io/__init__.py   |  1 +
 bindings/pyroot/pythonizations/io/io.py         | 19 +++++++++++++++++++
 bindings/pyroot/pythonizations/tree/__init__.py |  1 +
 bindings/pyroot/pythonizations/tree/tree.py     | 15 +++++++++++++++
 7 files changed, 48 insertions(+), 13 deletions(-)
 create mode 100644 bindings/pyroot/pythonizations/__init__.py
 create mode 100644 bindings/pyroot/pythonizations/io/__init__.py
 create mode 100644 bindings/pyroot/pythonizations/io/io.py
 create mode 100644 bindings/pyroot/pythonizations/tree/__init__.py
 create mode 100644 bindings/pyroot/pythonizations/tree/tree.py

diff --git a/bindings/pyroot/CMakeLists.txt b/bindings/pyroot/CMakeLists.txt
index a8b5c06..1aa1987 100644
--- a/bindings/pyroot/CMakeLists.txt
+++ b/bindings/pyroot/CMakeLists.txt
@@ -46,5 +46,8 @@ set( JupyROOTDirName "JupyROOT")
 install (DIRECTORY ${JupyROOTDirName} DESTINATION ${runtimedir})
 file(COPY ${JupyROOTDirName} DESTINATION ${CMAKE_BINARY_DIR}/${runtimedir})
 
+install (DIRECTORY "pythonizations" DESTINATION ${runtimedir})
+file(COPY "pythonizations" DESTINATION ${CMAKE_BINARY_DIR}/${runtimedir})
+
 #---Install headers----------------------------------------------------------
 ROOT_INSTALL_HEADERS()
diff --git a/bindings/pyroot/ROOT.py b/bindings/pyroot/ROOT.py
index 65d4b0c..79c51b8 100644
--- a/bindings/pyroot/ROOT.py
+++ b/bindings/pyroot/ROOT.py
@@ -211,19 +211,8 @@ _root.gInterpreter = _ExpandMacroFunction( "TInterpreter", "Instance" )
 
 
 ### special case pythonization --------------------------------------------------
-def _TTree__iter__( self ):
-   i = 0
-   bytes_read = self.GetEntry(i)
-   while 0 < bytes_read:
-      yield self                   # TODO: not sure how to do this w/ C-API ...
-      i += 1
-      bytes_read = self.GetEntry(i)
-
-   if bytes_read == -1:
-      raise RuntimeError( "TTree I/O error" )
-
-_root.CreateScopeProxy( "TTree" ).__iter__    = _TTree__iter__
-
+import pythonizations
+pythonizations.pythonize(_root)
 
 ### RINT command emulation ------------------------------------------------------
 def _excepthook( exctype, value, traceb ):
diff --git a/bindings/pyroot/pythonizations/__init__.py b/bindings/pyroot/pythonizations/__init__.py
new file mode 100644
index 0000000..f57b8d8
--- /dev/null
+++ b/bindings/pyroot/pythonizations/__init__.py
@@ -0,0 +1,7 @@
+import tree
+import io
+
+
+def pythonize(rootModule):
+	io.pythonize(rootModule)
+	tree.pythonize(rootModule)
\ No newline at end of file
diff --git a/bindings/pyroot/pythonizations/io/__init__.py b/bindings/pyroot/pythonizations/io/__init__.py
new file mode 100644
index 0000000..830f300
--- /dev/null
+++ b/bindings/pyroot/pythonizations/io/__init__.py
@@ -0,0 +1 @@
+from io import *
\ No newline at end of file
diff --git a/bindings/pyroot/pythonizations/io/io.py b/bindings/pyroot/pythonizations/io/io.py
new file mode 100644
index 0000000..ca7009d
--- /dev/null
+++ b/bindings/pyroot/pythonizations/io/io.py
@@ -0,0 +1,19 @@
+
+# TFile -----------------------------------------------------------------------
+def _TFile__exit__(self,exc_type, exc_value, traceback):
+    self.Close()
+
+def _TFile__call__(self, name, mode = "READ"):
+    self = TFile.Open(name,mode)
+    return self
+
+def _TFile__enter__(self):
+    return self
+
+# Registry of pythonizations
+
+def pythonize(rootModule):
+    tFileSP = rootModule.CreateScopeProxy( "TFile" )
+    tFileSP.__exit__  = _TFile__exit__
+    tFileSP.__call__  = _TFile__call__
+    tFileSP.__enter__ = _TFile__enter__
\ No newline at end of file
diff --git a/bindings/pyroot/pythonizations/tree/__init__.py b/bindings/pyroot/pythonizations/tree/__init__.py
new file mode 100644
index 0000000..7cfe96d
--- /dev/null
+++ b/bindings/pyroot/pythonizations/tree/__init__.py
@@ -0,0 +1 @@
+from tree import *
diff --git a/bindings/pyroot/pythonizations/tree/tree.py b/bindings/pyroot/pythonizations/tree/tree.py
new file mode 100644
index 0000000..e59f1ef
--- /dev/null
+++ b/bindings/pyroot/pythonizations/tree/tree.py
@@ -0,0 +1,15 @@
+def _TTree__iter__( self ):
+   i = 0
+   bytes_read = self.GetEntry(i)
+   while 0 < bytes_read:
+      yield self                   # TODO: not sure how to do this w/ C-API ...
+      i += 1
+      bytes_read = self.GetEntry(i)
+
+   if bytes_read == -1:
+      raise RuntimeError( "TTree I/O error" )
+
+# Registry of pythonizations
+
+def pythonize(rootModule):
+    rootModule.CreateScopeProxy( "TTree" ).__iter__    = _TTree__iter__
\ No newline at end of file
-- 
2.5.4 (Apple Git-61)

