prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Check directory exists when instantiating `StaticFiles`
  The `StaticFiles` application should ensure that the directory exists at the point it is instantiated.
  
  (With an optional switch to turn this behavior off)
  
  </issue>
  <code>
  [start of starlette/staticfiles.py]
  1 import os
  2 import stat
  3 
  4 from aiofiles.os import stat as aio_stat
  5 
  6 from starlette.responses import FileResponse, PlainTextResponse, Response
  7 from starlette.types import ASGIInstance, Receive, Scope, Send
  8 
  9 
  10 class StaticFiles:
  11     def __init__(self, *, directory: str) -> None:
  12         self.directory = directory
  13         self.config_checked = False
  14 
  15     def __call__(self, scope: Scope) -> ASGIInstance:
  16         assert scope["type"] == "http"
  17         if scope["method"] not in ("GET", "HEAD"):
  18             return PlainTextResponse("Method Not Allowed", status_code=405)
  19         path = os.path.normpath(os.path.join(*scope["path"].split("/")))
  20         if path.startswith(".."):
  21             return PlainTextResponse("Not Found", status_code=404)
  22         path = os.path.join(self.directory, path)
  23         if self.config_checked:
  24             check_directory = None
  25         else:
  26             check_directory = self.directory
  27             self.config_checked = True
  28         return _StaticFilesResponder(scope, path=path, check_directory=check_directory)
  29 
  30 
  31 class _StaticFilesResponder:
  32     def __init__(self, scope: Scope, path: str, check_directory: str = None) -> None:
  33         self.scope = scope
  34         self.path = path
  35         self.check_directory = check_directory
  36 
  37     async def check_directory_configured_correctly(self) -> None:
  38         """
  39         Perform a one-off configuration check that StaticFiles is actually
  40         pointed at a directory, so that we can raise loud errors rather than
  41         just returning 404 responses.
  42         """
  43         directory = self.check_directory
  44         try:
  45             stat_result = await aio_stat(directory)
  46         except FileNotFoundError:
  47             raise RuntimeError("StaticFiles directory '%s' does not exist." % directory)
  48         if not (stat.S_ISDIR(stat_result.st_mode) or stat.S_ISLNK(stat_result.st_mode)):
  49             raise RuntimeError("StaticFiles path '%s' is not a directory." % directory)
  50 
  51     async def __call__(self, receive: Receive, send: Send) -> None:
  52         if self.check_directory is not None:
  53             await self.check_directory_configured_correctly()
  54 
  55         try:
  56             stat_result = await aio_stat(self.path)
  57         except FileNotFoundError:
  58             response = PlainTextResponse("Not Found", status_code=404)  # type: Response
  59         else:
  60             mode = stat_result.st_mode
  61             if not stat.S_ISREG(mode):
  62                 response = PlainTextResponse("Not Found", status_code=404)
  63             else:
  64                 response = FileResponse(self.path, stat_result=stat_result)
  65 
  66         await response(receive, send)
  67
  [end of starlette/staticfiles.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/starlette/staticfiles.py b/starlette/staticfiles.py
  --- a/starlette/staticfiles.py
  +++ b/starlette/staticfiles.py
  @@ -8,7 +8,9 @@
   
   
   class StaticFiles:
  -    def __init__(self, *, directory: str) -> None:
  +    def __init__(self, *, directory: str, check_dir: bool = True) -> None:
  +        if check_dir and not os.path.isdir(directory):
  +            raise RuntimeError("Directory '%s' does not exist" % directory)
           self.directory = directory
           self.config_checked = False
  
verification_info: {"golden_diff": "diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py\n--- a/starlette/staticfiles.py\n+++ b/starlette/staticfiles.py\n@@ -8,7 +8,9 @@\n \n \n class StaticFiles:\n-    def __init__(self, *, directory: str) -> None:\n+    def __init__(self, *, directory: str, check_dir: bool = True) -> None:\n+        if check_dir and not os.path.isdir(directory):\n+            raise RuntimeError(\"Directory '%s' does not exist\" % directory)\n         self.directory = directory\n         self.config_checked = False\n", "issue": "Check directory exists when instantiating `StaticFiles`\nThe `StaticFiles` application should ensure that the directory exists at the point it is instantiated.\r\n\r\n(With an optional switch to turn this behavior off)\n", "before_files": [{"content": "import os\nimport stat\n\nfrom aiofiles.os import stat as aio_stat\n\nfrom starlette.responses import FileResponse, PlainTextResponse, Response\nfrom starlette.types import ASGIInstance, Receive, Scope, Send\n\n\nclass StaticFiles:\n    def __init__(self, *, directory: str) -> None:\n        self.directory = directory\n        self.config_checked = False\n\n    def __call__(self, scope: Scope) -> ASGIInstance:\n        assert scope[\"type\"] == \"http\"\n        if scope[\"method\"] not in (\"GET\", \"HEAD\"):\n            return PlainTextResponse(\"Method Not Allowed\", status_code=405)\n        path = os.path.normpath(os.path.join(*scope[\"path\"].split(\"/\")))\n        if path.startswith(\"..\"):\n            return PlainTextResponse(\"Not Found\", status_code=404)\n        path = os.path.join(self.directory, path)\n        if self.config_checked:\n            check_directory = None\n        else:\n            check_directory = self.directory\n            self.config_checked = True\n        return _StaticFilesResponder(scope, path=path, check_directory=check_directory)\n\n\nclass _StaticFilesResponder:\n    def __init__(self, scope: Scope, path: str, check_directory: str = None) -> None:\n        self.scope = scope\n        self.path = path\n        self.check_directory = check_directory\n\n    async def check_directory_configured_correctly(self) -> None:\n        \"\"\"\n        Perform a one-off configuration check that StaticFiles is actually\n        pointed at a directory, so that we can raise loud errors rather than\n        just returning 404 responses.\n        \"\"\"\n        directory = self.check_directory\n        try:\n            stat_result = await aio_stat(directory)\n        except FileNotFoundError:\n            raise RuntimeError(\"StaticFiles directory '%s' does not exist.\" % directory)\n        if not (stat.S_ISDIR(stat_result.st_mode) or stat.S_ISLNK(stat_result.st_mode)):\n            raise RuntimeError(\"StaticFiles path '%s' is not a directory.\" % directory)\n\n    async def __call__(self, receive: Receive, send: Send) -> None:\n        if self.check_directory is not None:\n            await self.check_directory_configured_correctly()\n\n        try:\n            stat_result = await aio_stat(self.path)\n        except FileNotFoundError:\n            response = PlainTextResponse(\"Not Found\", status_code=404)  # type: Response\n        else:\n            mode = stat_result.st_mode\n            if not stat.S_ISREG(mode):\n                response = PlainTextResponse(\"Not Found\", status_code=404)\n            else:\n                response = FileResponse(self.path, stat_result=stat_result)\n\n        await response(receive, send)\n", "path": "starlette/staticfiles.py"}]}