Skip to content

ch2: Implement Python version #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Web Browser Engineering

This is a port of [Web Browser Engineering](https://browser.engineering/) series from Python to Rust done by Korean Rust User Group.

# Table

| Chapter | Author |
|-----------------------|-----------|
| Downloading Web Pages | @sanxiyn |
| Drawing to the Screen | @corona10 |
64 changes: 64 additions & 0 deletions python/graphics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import tkinter

import http


WIDTH, HEIGHT = 800, 600
HSTEP, VSTEP = 13, 18
SCROLL_STEP = 100


class Browser:

def __init__(self):
self.window = tkinter.Tk()
self.scroll = 0
self.window.bind("<Up>", self.scrollup)
self.window.bind("<Down>", self.scrolldown)
self.canvas = tkinter.Canvas(
self.window,
width=WIDTH,
height=HEIGHT
)
self.canvas.pack()

def load(self, url):
headers, body = http.request(url)
text = http.lex(body)
self.display_list = self.layout(text)
self.render()

def layout(self, text):
display_list = []
cursor_x, cursor_y = HSTEP, VSTEP
for c in text:
display_list.append((cursor_x, cursor_y, c))
cursor_x += HSTEP
if cursor_x >= WIDTH - HSTEP or c == '\n':
cursor_y += VSTEP
cursor_x = HSTEP
return display_list

def render(self):
self.canvas.delete("all")
for x, y, c in self.display_list:
if y > self.scroll + HEIGHT:
continue
if y + VSTEP < self.scroll:
continue
self.canvas.create_text(x, y - self.scroll, text=c)

def scrolldown(self, e):
self.scroll += SCROLL_STEP
self.render()

def scrollup(self, e):
self.scroll -= SCROLL_STEP
self.scroll = max(self.scroll, 0)
self.render()


if __name__ == '__main__':
import sys
Browser().load(sys.argv[1])
tkinter.mainloop()
27 changes: 13 additions & 14 deletions python/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket


def request(url):
# 1. Parse scheme
scheme, url = url.split("://", 1)
@@ -51,23 +52,21 @@ def request(url):
# 12. Return
return headers, body

def show(body):
# 13. Print content
in_angle = False
for c in body:
if c == "<":
in_angle = True
elif c == ">":
in_angle = False
elif not in_angle:
print(c, end="")

def load(url):
# 14. Wire up
headers, body = request(url)
show(body)

if __name__ == "__main__":
# 15. Run from command line
import sys
load(sys.argv[1])

def lex(body):
text = ''
in_angle = False
for c in body:
if c == '<':
in_angle = True
elif c == '>':
in_angle = False
elif not in_angle:
text += c
return text