Skip to content

Commit

Permalink
reworked enforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
István Bozsó committed Feb 9, 2020
1 parent 1e55e63 commit 5e878ee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
19 changes: 5 additions & 14 deletions utils/enforce.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
__all__ = (
"enforcer", "type_enforce",
"Enforcer",
)

class Enforcer(object):
__slots__ = ("exc",)

def __init__(self, exc):
self.exc = exc

def __call__(self, cond, *args, **kwargs):
def enforce(self, cond, *args, **kwargs):
if not cond:
raise self.exc(*args, **kwargs)
raise self(*args, **kwargs)


@ft.lru_cache()
def enforcer(exc):
return Enforcer(exc)

type_enforce = enforcer(TypeError)
class TypeEnforce(TypeError, Enforcer):
pass
48 changes: 37 additions & 11 deletions utils/simpledoc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = (
"HTML", "libs",
"HTML", "libs", "JSLib",
)

import re
Expand Down Expand Up @@ -741,12 +741,8 @@ def new(cls, *args, **kwargs):
def presentation(*args, **kwargs):
return Presentation(*args, **kwargs)

def use(self, path):
with self.tag("script", "async", src=path):
pass

# self.line("script", "", src=path)

def use(self, lib):
lib.add(self)

def image_paths(self, width, *paths):
return ImagePaths(self, width, *paths)
Expand All @@ -769,11 +765,41 @@ def proc_yt_opt(kwargs):
kwargs.pop(key, yt_opts[key])
for key in yt_opts
)

class Library(object):
__slots__ = (
"path",
)

def __init__(self, path):
self.path = path


class CSSLib(Library):
pass

libs = type("JSLibs", (object,), {
"shower": "https://shwr.me/shower/shower.min.js",
"mathjax": "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js",
"plotly": "https://cdn.plot.ly/plotly-latest.min.js",
class JSLib(Library):
__slots__ = (
"async",
)

def __init__(self, *args, **kwargs):
self.async = bool(kwargs.get("async", True))
Library.__init__(self, kwargs["path"])

def add(self, doc):
if self.async:
with doc.tag("script", "async", src=self.path):
pass
else:
with doc.tag("script", src=self.path):
pass


jslibs = type("JSLibs", (object,), {
"shower": JSLib(path="https://shwr.me/shower/shower.min.js"),
"mathjax": JSLib(path="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"),
"plotly": JSLib(path="https://cdn.plot.ly/plotly-latest.min.js"),
})

class Presentation(HTML):
Expand Down

0 comments on commit 5e878ee

Please sign in to comment.