prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Widget for updatable image
  I don't want to reinvent the wheel, so will check before.
  I use GenPollText for my keyboard layout indicator but instead I want to see the flag (image). As I change layout the image should be changed. Do Qtile has such widget or proper way to do that?
  
  </issue>
  <code>
  [start of libqtile/widget/image.py]
  1 # Copyright (c) 2013 dequis
  2 # Copyright (c) 2014 Sean Vig
  3 # Copyright (c) 2014 Adi Sieker
  4 #
  5 # Permission is hereby granted, free of charge, to any person obtaining a copy
  6 # of this software and associated documentation files (the "Software"), to deal
  7 # in the Software without restriction, including without limitation the rights
  8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 # copies of the Software, and to permit persons to whom the Software is
  10 # furnished to do so, subject to the following conditions:
  11 #
  12 # The above copyright notice and this permission notice shall be included in
  13 # all copies or substantial portions of the Software.
  14 #
  15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21 # SOFTWARE.
  22 import os
  23 
  24 from libqtile import bar
  25 from libqtile.images import Img
  26 from libqtile.log_utils import logger
  27 from libqtile.widget import base
  28 
  29 
  30 class Image(base._Widget, base.MarginMixin):
  31     """Display a PNG image on the bar"""
  32     orientations = base.ORIENTATION_BOTH
  33     defaults = [
  34         ("scale", True, "Enable/Disable image scaling"),
  35         ("rotate", 0.0, "rotate the image in degrees counter-clockwise"),
  36         ("filename", None, "Image filename. Can contain '~'"),
  37     ]
  38 
  39     def __init__(self, length=bar.CALCULATED, **config):
  40         base._Widget.__init__(self, length, **config)
  41         self.add_defaults(Image.defaults)
  42         self.add_defaults(base.MarginMixin.defaults)
  43 
  44         # make the default 0 instead
  45         self._variable_defaults["margin"] = 0
  46 
  47     def _configure(self, qtile, bar):
  48         base._Widget._configure(self, qtile, bar)
  49         self.img = None
  50 
  51         if not self.filename:
  52             logger.warning("Image filename not set!")
  53             return
  54 
  55         self.filename = os.path.expanduser(self.filename)
  56 
  57         if not os.path.exists(self.filename):
  58             logger.warning("Image does not exist: {}".format(self.filename))
  59             return
  60 
  61         img = Img.from_path(self.filename)
  62         self.img = img
  63         img.theta = self.rotate
  64         if not self.scale:
  65             return
  66         if self.bar.horizontal:
  67             new_height = self.bar.height - (self.margin_y * 2)
  68             img.resize(height=new_height)
  69         else:
  70             new_width = self.bar.width - (self.margin_x * 2)
  71             img.resize(width=new_width)
  72 
  73     def draw(self):
  74         if self.img is None:
  75             return
  76 
  77         self.drawer.clear(self.background or self.bar.background)
  78         self.drawer.ctx.save()
  79         self.drawer.ctx.translate(self.margin_x, self.margin_y)
  80         self.drawer.ctx.set_source(self.img.pattern)
  81         self.drawer.ctx.paint()
  82         self.drawer.ctx.restore()
  83 
  84         if self.bar.horizontal:
  85             self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.width)
  86         else:
  87             self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.width)
  88 
  89     def calculate_length(self):
  90         if self.img is None:
  91             return 0
  92 
  93         if self.bar.horizontal:
  94             return self.img.width + (self.margin_x * 2)
  95         else:
  96             return self.img.height + (self.margin_y * 2)
  97
  [end of libqtile/widget/image.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/libqtile/widget/image.py b/libqtile/widget/image.py
  --- a/libqtile/widget/image.py
  +++ b/libqtile/widget/image.py
  @@ -46,6 +46,9 @@
   
       def _configure(self, qtile, bar):
           base._Widget._configure(self, qtile, bar)
  +        self._update_image()
  +
  +    def _update_image(self):
           self.img = None
   
           if not self.filename:
  @@ -94,3 +97,13 @@
               return self.img.width + (self.margin_x * 2)
           else:
               return self.img.height + (self.margin_y * 2)
  +
  +    def cmd_update(self, filename):
  +        old_length = self.calculate_length()
  +        self.filename = filename
  +        self._update_image()
  +
  +        if self.calculate_length() == old_length:
  +            self.draw()
  +        else:
  +            self.bar.draw()
  
verification_info: {"golden_diff": "diff --git a/libqtile/widget/image.py b/libqtile/widget/image.py\n--- a/libqtile/widget/image.py\n+++ b/libqtile/widget/image.py\n@@ -46,6 +46,9 @@\n \n     def _configure(self, qtile, bar):\n         base._Widget._configure(self, qtile, bar)\n+        self._update_image()\n+\n+    def _update_image(self):\n         self.img = None\n \n         if not self.filename:\n@@ -94,3 +97,13 @@\n             return self.img.width + (self.margin_x * 2)\n         else:\n             return self.img.height + (self.margin_y * 2)\n+\n+    def cmd_update(self, filename):\n+        old_length = self.calculate_length()\n+        self.filename = filename\n+        self._update_image()\n+\n+        if self.calculate_length() == old_length:\n+            self.draw()\n+        else:\n+            self.bar.draw()\n", "issue": "Widget for updatable image\nI don't want to reinvent the wheel, so will check before.\r\nI use GenPollText for my keyboard layout indicator but instead I want to see the flag (image). As I change layout the image should be changed. Do Qtile has such widget or proper way to do that?\n", "before_files": [{"content": "# Copyright (c) 2013 dequis\n# Copyright (c) 2014 Sean Vig\n# Copyright (c) 2014 Adi Sieker\n#\n# Permission is hereby granted, free of charge, to any person obtaining a copy\n# of this software and associated documentation files (the \"Software\"), to deal\n# in the Software without restriction, including without limitation the rights\n# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n# copies of the Software, and to permit persons to whom the Software is\n# furnished to do so, subject to the following conditions:\n#\n# The above copyright notice and this permission notice shall be included in\n# all copies or substantial portions of the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n# SOFTWARE.\nimport os\n\nfrom libqtile import bar\nfrom libqtile.images import Img\nfrom libqtile.log_utils import logger\nfrom libqtile.widget import base\n\n\nclass Image(base._Widget, base.MarginMixin):\n    \"\"\"Display a PNG image on the bar\"\"\"\n    orientations = base.ORIENTATION_BOTH\n    defaults = [\n        (\"scale\", True, \"Enable/Disable image scaling\"),\n        (\"rotate\", 0.0, \"rotate the image in degrees counter-clockwise\"),\n        (\"filename\", None, \"Image filename. Can contain '~'\"),\n    ]\n\n    def __init__(self, length=bar.CALCULATED, **config):\n        base._Widget.__init__(self, length, **config)\n        self.add_defaults(Image.defaults)\n        self.add_defaults(base.MarginMixin.defaults)\n\n        # make the default 0 instead\n        self._variable_defaults[\"margin\"] = 0\n\n    def _configure(self, qtile, bar):\n        base._Widget._configure(self, qtile, bar)\n        self.img = None\n\n        if not self.filename:\n            logger.warning(\"Image filename not set!\")\n            return\n\n        self.filename = os.path.expanduser(self.filename)\n\n        if not os.path.exists(self.filename):\n            logger.warning(\"Image does not exist: {}\".format(self.filename))\n            return\n\n        img = Img.from_path(self.filename)\n        self.img = img\n        img.theta = self.rotate\n        if not self.scale:\n            return\n        if self.bar.horizontal:\n            new_height = self.bar.height - (self.margin_y * 2)\n            img.resize(height=new_height)\n        else:\n            new_width = self.bar.width - (self.margin_x * 2)\n            img.resize(width=new_width)\n\n    def draw(self):\n        if self.img is None:\n            return\n\n        self.drawer.clear(self.background or self.bar.background)\n        self.drawer.ctx.save()\n        self.drawer.ctx.translate(self.margin_x, self.margin_y)\n        self.drawer.ctx.set_source(self.img.pattern)\n        self.drawer.ctx.paint()\n        self.drawer.ctx.restore()\n\n        if self.bar.horizontal:\n            self.drawer.draw(offsetx=self.offset, offsety=self.offsety, width=self.width)\n        else:\n            self.drawer.draw(offsety=self.offset, offsetx=self.offsetx, height=self.width)\n\n    def calculate_length(self):\n        if self.img is None:\n            return 0\n\n        if self.bar.horizontal:\n            return self.img.width + (self.margin_x * 2)\n        else:\n            return self.img.height + (self.margin_y * 2)\n", "path": "libqtile/widget/image.py"}]}