Skip to content

Commit

Permalink
Merge pull request #10 from waveform80/cleanup
Browse files Browse the repository at this point in the history
General tidy up
  • Loading branch information
lowfatcode authored Sep 4, 2022
2 parents b57cc18 + 34d1012 commit a965eb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
6 changes: 3 additions & 3 deletions phew/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def connect_to_wifi(ssid, password, timeout_seconds=30):
status = new_status
time.sleep(0.25)

if wlan.status() != 3:
return None
if wlan.status() == network.STAT_GOT_IP:
return wlan.ifconfig()[0]
return None

return wlan.ifconfig()[0]

# helper method to put the pico into access point mode
def access_point(ssid, password = None):
Expand Down
36 changes: 17 additions & 19 deletions phew/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def __init__(self, method, uri, protocol):
self.query = _parse_query_string(self.query_string)

def __str__(self):
return f"""request: {self.method} {self.path} {self.protocol}
headers: {str(self.headers)}
form: {str(self.form)}
data: {str(self.data)}"""
return f"""\
request: {self.method} {self.path} {self.protocol}
headers: {self.headers}
form: {self.form}
data: {self.data}"""


class Response:
Expand All @@ -69,9 +70,10 @@ def add_header(self, name, value):
self.headers[name] = value

def __str__(self):
return f"""status: {self.status}
headers: {str(self.headers)}
body: {str(self.body)}"""
return f"""\
status: {self.status}
headers: {self.headers}
body: {self.body}"""


content_type_map = {
Expand Down Expand Up @@ -121,33 +123,29 @@ def matches(self, request):
compare_parts = request.path.split("/")
if len(compare_parts) != len(self.path_parts):
return False
for i, compare in enumerate(compare_parts):
part = self.path_parts[i]
is_parameter = len(part) > 0 and part[0] == "<"
if not is_parameter and part != compare:
for part, compare in zip(self.path_parts, compare_parts):
if not part.startswith("<") and part != compare:
return False
return True

# call the route handler passing any named parameters in the path
def call_handler(self, request):
parameters = {}
compare_parts = request.path.split("/")
for i, compare in enumerate(compare_parts):
part = self.path_parts[i]
is_parameter = len(part) > 0 and part[0] == "<"
if is_parameter:
for part, compare in zip(self.path_parts, request.path.split("/")):
if part.startswith("<"):
name = part[1:-1]
parameters[name] = compare

return self.handler(request, **parameters)

def __str__(self):
return f"""path: {self.path}
methods: {str(self.methods)}
return f"""\
path: {self.path}
methods: {self.methods}
"""

def __repr__(self):
return f"<Route object {self.path} ({", ".join(self.methods)})>"
return f"<Route object {self.path} ({', '.join(self.methods)})>"


# parses the headers for a http request (or the headers attached to
Expand Down

0 comments on commit a965eb5

Please sign in to comment.