summaryrefslogtreecommitdiff
path: root/crocoite/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'crocoite/util.py')
-rw-r--r--crocoite/util.py34
1 files changed, 19 insertions, 15 deletions
diff --git a/crocoite/util.py b/crocoite/util.py
index bd26909..da377a3 100644
--- a/crocoite/util.py
+++ b/crocoite/util.py
@@ -22,26 +22,30 @@
Random utility functions
"""
-import random, sys, platform
+import random, sys, platform, os, json, urllib
+from datetime import datetime
import hashlib, pkg_resources
-from urllib.parse import urlsplit, urlunsplit
-def packageUrl (path):
- """
- Create URL for package data stored into WARC
- """
- return 'urn:' + __package__ + ':' + path
+from yarl import URL
+
+class StrJsonEncoder (json.JSONEncoder):
+ """ JSON encoder that turns unknown classes into a string and thus never
+ fails """
+ def default (self, obj):
+ if isinstance (obj, datetime):
+ return obj.isoformat ()
+
+ # make sure serialization always succeeds
+ try:
+ return json.JSONEncoder.default(self, obj)
+ except TypeError:
+ return str (obj)
async def getFormattedViewportMetrics (tab):
layoutMetrics = await tab.Page.getLayoutMetrics ()
# XXX: I’m not entirely sure which one we should use here
- return '{}x{}'.format (layoutMetrics['layoutViewport']['clientWidth'],
- layoutMetrics['layoutViewport']['clientHeight'])
-
-def removeFragment (u):
- """ Remove fragment from url (i.e. #hashvalue) """
- s = urlsplit (u)
- return urlunsplit ((s.scheme, s.netloc, s.path, s.query, ''))
+ viewport = layoutMetrics['layoutViewport']
+ return f"{viewport['clientWidth']}x{viewport['clientHeight']}"
def getSoftwareInfo ():
""" Get software info for inclusion into warcinfo """
@@ -79,7 +83,7 @@ def getRequirements (dist):
pkg = getattr (m, '__package__', None)
# is loaded?
if pkg in modules:
- if f:
+ if f and os.path.isfile (f):
with open (f, 'rb') as fd:
contents = fd.read ()
h = hashlib.new ('sha512')