Skip to content

Commit 0244c15

Browse files
author
Ben Regenspan
committed
Add chronological accuracy to Shabbos detection!
1 parent 3e66bd9 commit 0244c15

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ that can be used for serving the various supported web frameworks: ``gunicorn``,
5353
Commonly Used Arguments
5454
+++++++++++++++++++++++
5555

56+
* ``--city=CITY`` - Specify the name of your city, or nearest population center.
57+
It is important that this be set in order to correctly determine whether it
58+
currently is or is not Shabbos. City must be one of those listed at
59+
http://packages.python.org/astral/#cities
5660
* ``-c CONFIG, --config=CONFIG`` - Specify the path to a `config file`_
5761
* ``-b BIND, --bind=BIND`` - Specify a server socket to bind. Server sockets
5862
can be any of ``$(HOST)``, ``$(HOST):$(PORT)``, or ``unix:$(PATH)``.

gunicorn/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,3 +1055,16 @@ class ProxyAllowFrom(Setting):
10551055
desc = """\
10561056
Front-end's IPs from which allowed accept proxy requests (comma separate).
10571057
"""
1058+
1059+
class City(Setting):
1060+
name = "city"
1061+
section = "Server Mechanics"
1062+
cli = ["--city"]
1063+
validator = validate_string
1064+
default = "New York"
1065+
desc = """\
1066+
Name of nearby city to use for sun-up/sun-down calculations.
1067+
1068+
Must be in list of cities contained in
1069+
http://packages.python.org/astral/#astral-v0-6-2
1070+
"""

gunicorn/http/message.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import socket
99
from errno import ENOTCONN
1010

11+
from astral import Astral
12+
import pytz
13+
1114
from gunicorn.http.unreader import SocketUnreader
1215
from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body
1316
from gunicorn.http.errors import InvalidHeader, InvalidHeaderName, NoMoreData, \
1417
InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion, \
1518
LimitRequestLine, LimitRequestHeaders, UnkosherRequest
1619
from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest
1720
from gunicorn.six import BytesIO, urlsplit, bytes_to_str
18-
from gunicorn.util import SHABBOS, weekdayname
21+
from gunicorn.util import weekdayname
1922

2023
MAX_REQUEST_LINE = 8190
2124
MAX_HEADERS = 32768
@@ -162,10 +165,15 @@ def get_data(self, unreader, buf, stop=False):
162165
buf.write(data)
163166

164167
def parse(self, unreader):
165-
166-
day_today = weekdayname[datetime.today().weekday()]
167-
if day_today == SHABBOS:
168-
raise UnkosherRequest("I don't work on Shabbos!")
168+
now = datetime.now(pytz.utc)
169+
day_today = weekdayname[now.weekday()]
170+
if day_today in ('Fri', 'Sat',):
171+
a = Astral()
172+
city = a[self.cfg.city]
173+
times = city.sun(date=now, local=False)
174+
if ((day_today == 'Fri' and now > times['sunrise'])
175+
or (day_today == 'Sat' and now < times['sunset'])):
176+
raise UnkosherRequest("I don't work on Shabbos!")
169177

170178
buf = BytesIO()
171179
self.get_data(unreader, buf, stop=True)

gunicorn/util.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
4545
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
4646

47-
# This is an extremely naive definition of Shabbos
48-
# (TODO: use local sundown)
49-
SHABBOS = 'Sat'
50-
5147
# Server and Date aren't technically hop-by-hop
5248
# headers, but they are in the purview of the
5349
# origin server which the WSGI spec says we should

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def run(self):
8282
tests_require = tests_require,
8383
cmdclass = {'test': PyTest},
8484

85+
install_requires=[
86+
'astral==0.6.2',
87+
'pytz==2012h'
88+
],
89+
8590
entry_points="""
8691
8792
[console_scripts]

0 commit comments

Comments
 (0)