From f18a0793cad6e217299b3d1f3f1796903e41dfc9 Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Wed, 29 May 2019 16:14:03 +0200
Subject: [PATCH] editor : pilsvg -> svg are included in package and get load
 from python package path. support ok

---
 MANIFEST.in                    |  6 +++
 flatland/utils/graphics_pil.py | 16 +++++--
 notebooks/Editor2.ipynb        | 81 ++++++++--------------------------
 setup.py                       |  9 ++++
 4 files changed, 46 insertions(+), 66 deletions(-)

diff --git a/MANIFEST.in b/MANIFEST.in
index 30bf97c..60c8cd7 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -5,6 +5,12 @@ include LICENSE
 include README.rst
 include requirements_dev.txt
 
+
+
+graft svg
+graft env-data
+
+
 recursive-include tests *
 recursive-exclude * __pycache__
 recursive-exclude * *.py[co]
diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index 7d6d876..d9a2864 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -7,6 +7,7 @@ import numpy as np
 import time
 import io
 import os
+import site
 
 def enable_windows_cairo_support():
     if os.name=='nt':
@@ -52,7 +53,7 @@ class PILGL(GraphicsLayer):
             self.nPixCell = int(max(1,np.ceil(min(w,h))))
         else:
             self.nPixCell = 40
-            
+
         # Total grid size at native scale
         self.widthPx = self.width * self.nPixCell + self.linewidth
         self.heightPx = self.height * self.nPixCell + self.linewidth
@@ -253,8 +254,17 @@ class PILSVG(PILGL):
         self.agents_prev = []
 
     def pilFromSvgFile(self, sfPath):
-        with open(sfPath, "r") as fIn:
-            bytesPNG = svg2png(file_obj=fIn, output_height=self.nPixCell, output_width=self.nPixCell)
+        try:
+            with open(sfPath, "r") as fIn:
+                bytesPNG = svg2png(file_obj=fIn, output_height=self.nPixCell, output_width=self.nPixCell)
+        except:
+            newList=''
+            for directory in site.getsitepackages():
+                x = [word for word in os.listdir(directory) if word.startswith('flatland')]
+                if len(x) > 0 :
+                    newList = directory+'/'+x[0]
+            with open(newList+'/'+sfPath, "r") as fIn:
+                bytesPNG = svg2png(file_obj=fIn, output_height=self.nPixCell, output_width=self.nPixCell)
         with io.BytesIO(bytesPNG) as fIn:
             pil_img = Image.open(fIn)
             pil_img.load()
diff --git a/notebooks/Editor2.ipynb b/notebooks/Editor2.ipynb
index b51aae6..e52945e 100644
--- a/notebooks/Editor2.ipynb
+++ b/notebooks/Editor2.ipynb
@@ -58,10 +58,16 @@
    "metadata": {},
    "outputs": [
     {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "cairo installed: OK\n"
+     "ename": "SyntaxError",
+     "evalue": "invalid syntax (graphics_pil.py, line 263)",
+     "output_type": "error",
+     "traceback": [
+      "Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n",
+      "  File \u001b[0;32m\"c:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\IPython\\core\\interactiveshell.py\"\u001b[0m, line \u001b[0;32m3291\u001b[0m, in \u001b[0;35mrun_code\u001b[0m\n    exec(code_obj, self.user_global_ns, self.user_ns)\n",
+      "  File \u001b[0;32m\"<ipython-input-4-cd594490df80>\"\u001b[0m, line \u001b[0;32m1\u001b[0m, in \u001b[0;35m<module>\u001b[0m\n    from flatland.utils.editor import EditorMVC, EditorModel, View, Controller\n",
+      "  File \u001b[0;32m\"c:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\flatland_rl-0.1.1-py3.6.egg\\flatland\\utils\\editor.py\"\u001b[0m, line \u001b[0;32m21\u001b[0m, in \u001b[0;35m<module>\u001b[0m\n    import flatland.utils.rendertools as rt\n",
+      "\u001b[1;36m  File \u001b[1;32m\"c:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\flatland_rl-0.1.1-py3.6.egg\\flatland\\utils\\rendertools.py\"\u001b[1;36m, line \u001b[1;32m7\u001b[1;36m, in \u001b[1;35m<module>\u001b[1;36m\u001b[0m\n\u001b[1;33m    from flatland.utils.graphics_pil import PILGL, PILSVG\u001b[0m\n",
+      "\u001b[1;36m  File \u001b[1;32m\"c:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\flatland_rl-0.1.1-py3.6.egg\\flatland\\utils\\graphics_pil.py\"\u001b[1;36m, line \u001b[1;32m263\u001b[0m\n\u001b[1;33m    newList.append( [word for word in os.listdir(directory): if word.startswith('flatland')] )\u001b[0m\n\u001b[1;37m                                                           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
      ]
     }
    ],
@@ -71,19 +77,9 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "<Figure size 432x288 with 0 Axes>"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "mvc = EditorMVC(sGL=\"PIL\" ) # sGL=\"PIL\")"
    ]
@@ -106,52 +102,22 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": null,
    "metadata": {
     "scrolled": false
    },
-   "outputs": [
-    {
-     "data": {
-      "application/vnd.jupyter.widget-view+json": {
-       "model_id": "7e8d0b5604ed45d6a0ed9c935157169c",
-       "version_major": 2,
-       "version_minor": 0
-      },
-      "text/plain": [
-       "HBox(children=(Canvas(), VBox(children=(Text(value='temp.pkl', description='Filename'), Button(description='Re…"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "mvc.view.display()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": null,
    "metadata": {
     "scrolled": false
    },
-   "outputs": [
-    {
-     "data": {
-      "application/vnd.jupyter.widget-view+json": {
-       "model_id": "2efd4100033d401d9f79aba6d4cf1df4",
-       "version_major": 2,
-       "version_minor": 0
-      },
-      "text/plain": [
-       "Output()"
-      ]
-     },
-     "metadata": {},
-     "output_type": "display_data"
-    }
-   ],
+   "outputs": [],
    "source": [
     "mvc.view.wOutput.clear_output()\n",
     "mvc.view.wOutput"
@@ -159,20 +125,9 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": null,
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(0, 0)"
-      ]
-     },
-     "execution_count": 8,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "len(mvc.editor.env.agents), len(mvc.editor.env.agents_static)"
    ]
diff --git a/setup.py b/setup.py
index e8fe5f5..7ed4339 100644
--- a/setup.py
+++ b/setup.py
@@ -65,6 +65,14 @@ else:
     except:
         os.system("pip install pycairo==1.18.1")
 
+
+def get_all_svg_files(directory='./svg/'):
+    ret = []
+    for f in os.listdir(directory):
+        ret.append(directory+f)
+    return ret
+
+
 # Gather requirements from requirements_dev.txt
 # TODO : We could potentially split up the test/dev dependencies later
 install_reqs = []
@@ -101,6 +109,7 @@ setup(
     keywords='flatland',
     name='flatland-rl',
     packages=find_packages('.'),
+    data_files=[('svg', get_all_svg_files())],
     setup_requires=setup_requirements,
     test_suite='tests',
     tests_require=test_requirements,
-- 
GitLab