From 483792a3782ebce017ff0e90d965763889ffd758 Mon Sep 17 00:00:00 2001
From: u214892 <u214892@sbb.ch>
Date: Wed, 12 Jun 2019 15:45:23 +0200
Subject: [PATCH] #57 PILSvg from resource

---
 flatland/__init__.py                    |  7 -----
 flatland/core/env_prediction_builder.py |  2 +-
 flatland/envs/predictions.py            |  2 +-
 flatland/utils/graphics_pil.py          | 34 ++++++++-----------------
 setup.py                                |  4 +--
 svg/__init__.py                         |  0
 6 files changed, 15 insertions(+), 34 deletions(-)
 create mode 100644 svg/__init__.py

diff --git a/flatland/__init__.py b/flatland/__init__.py
index b0399d2a..e69de29b 100644
--- a/flatland/__init__.py
+++ b/flatland/__init__.py
@@ -1,7 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""Top-level package for flatland."""
-
-__author__ = """S.P. Mohanty"""
-__email__ = 'mohanty@aicrowd.com'
-__version__ = '0.1.1'
diff --git a/flatland/core/env_prediction_builder.py b/flatland/core/env_prediction_builder.py
index 9f5e4dc5..060dbfc3 100644
--- a/flatland/core/env_prediction_builder.py
+++ b/flatland/core/env_prediction_builder.py
@@ -29,7 +29,7 @@ class PredictionBuilder:
 
     def get(self, handle=0):
         """
-        Called whenever step_prediction is called on the environment.
+        Called whenever predict is called on the environment.
 
         Parameters
         -------
diff --git a/flatland/envs/predictions.py b/flatland/envs/predictions.py
index 3338e681..43f0a93f 100644
--- a/flatland/envs/predictions.py
+++ b/flatland/envs/predictions.py
@@ -18,7 +18,7 @@ class DummyPredictorForRailEnv(PredictionBuilder):
 
     def get(self, handle=None):
         """
-        Called whenever step_prediction is called on the environment.
+        Called whenever predict is called on the environment.
 
         Parameters
         -------
diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index d93b0f75..a0dd2db7 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -1,12 +1,12 @@
 import io
 import os
-import site
 import time
 import tkinter as tk
 
 import numpy as np
 from PIL import Image, ImageDraw, ImageTk  # , ImageFont
 from numpy import array
+from pkg_resources import resource_string as resource_bytes
 
 from flatland.utils.graphics_layer import GraphicsLayer
 
@@ -239,18 +239,9 @@ class PILSVG(PILGL):
         self.lwAgents = []
         self.agents_prev = []
 
-    def pilFromSvgFile(self, sfPath):
-        try:
-            with open(sfPath, "r") as fIn:
-                bytesPNG = svg2png(file_obj=fIn, output_height=self.nPixCell, output_width=self.nPixCell)
-        except:  # noqa: E722
-            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)
+    def pilFromSvgFile(self, package, resource):
+        bytestring = resource_bytes(package, resource)
+        bytesPNG = svg2png(bytestring=bytestring, output_height=self.nPixCell, output_width=self.nPixCell)
         with io.BytesIO(bytesPNG) as fIn:
             pil_img = Image.open(fIn)
             pil_img.load()
@@ -313,10 +304,7 @@ class PILSVG(PILGL):
 
         lDirs = list("NESW")
 
-        # svgBG = SVG("./svg/Background_#91D1DD.svg")
-
         for sTrans, sFile in dDirFile.items():
-            sPathSvg = "./svg/" + sFile
 
             # Translate the ascii transition description in the format  "NE WS" to the 
             # binary list of transitions as per RailEnv - NESW (in) x NESW (out)
@@ -330,7 +318,7 @@ class PILSVG(PILGL):
             sTrans16 = "".join(lTrans16)
             binTrans = int(sTrans16, 2)
 
-            pilRail = self.pilFromSvgFile(sPathSvg)
+            pilRail = self.pilFromSvgFile('svg', sFile)
 
             if rotate:
                 # For rotations, we also store the base image
@@ -367,7 +355,7 @@ class PILSVG(PILGL):
                 print("Illegal target rail:", row, col, format(binTrans, "#018b")[2:])
 
             if isSelected:
-                svgBG = self.pilFromSvgFile("./svg/Selected_Target.svg")
+                svgBG = self.pilFromSvgFile("svg", "Selected_Target.svg")
                 self.clear_layer(3, 0)
                 self.drawImageRC(svgBG, (row, col), layer=3)
 
@@ -390,9 +378,9 @@ class PILSVG(PILGL):
 
         # Seed initial train/zug files indexed by tuple(iDirIn, iDirOut):
         dDirsFile = {
-            (0, 0): "svg/Zug_Gleis_#0091ea.svg",
-            (1, 2): "svg/Zug_1_Weiche_#0091ea.svg",
-            (0, 3): "svg/Zug_2_Weiche_#0091ea.svg"
+            (0, 0): "Zug_Gleis_#0091ea.svg",
+            (1, 2): "Zug_1_Weiche_#0091ea.svg",
+            (0, 3): "Zug_2_Weiche_#0091ea.svg"
         }
 
         # "paint" color of the train images we load
@@ -403,7 +391,7 @@ class PILSVG(PILGL):
         for tDirs, sPathSvg in dDirsFile.items():
             iDirIn, iDirOut = tDirs
 
-            pilZug = self.pilFromSvgFile(sPathSvg)
+            pilZug = self.pilFromSvgFile("svg", sPathSvg)
 
             # Rotate both the directions and the image and save in the dict
             for iDirRot in range(4):
@@ -429,7 +417,7 @@ class PILSVG(PILGL):
         self.drawImageRC(pilZug, (row, col), layer=1)
 
         if isSelected:
-            svgBG = self.pilFromSvgFile("./svg/Selected_Agent.svg")
+            svgBG = self.pilFromSvgFile("svg", "Selected_Agent.svg")
             self.clear_layer(2, 0)
             self.drawImageRC(svgBG, (row, col), layer=2)
 
diff --git a/setup.py b/setup.py
index d517c279..39bffd17 100644
--- a/setup.py
+++ b/setup.py
@@ -63,12 +63,12 @@ else:
 def get_all_svg_files(directory='./svg/'):
     ret = []
     for f in os.listdir(directory):
-        ret.append(directory + f)
+        if f != '__pycache__':
+            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 = []
 requirements_path = 'requirements_dev.txt'
 with open(requirements_path, 'r') as f:
diff --git a/svg/__init__.py b/svg/__init__.py
new file mode 100644
index 00000000..e69de29b
-- 
GitLab