3
3
This module contains various utilities.
4
4
5
5
"""
6
+
6
7
import base64
7
8
import cgi
8
9
import contextlib
45
46
46
47
from pyff import __version__
47
48
from pyff .constants import NS , config
48
- from pyff .exceptions import *
49
+ from pyff .exceptions import MetadataException , ResourceException
49
50
from pyff .logs import get_log
50
51
51
52
etree .set_default_parser (etree .XMLParser (resolve_entities = False ))
@@ -73,8 +74,8 @@ def debug_observer(e):
73
74
log .error (repr (e ))
74
75
75
76
76
- def trunc_str (x , l ):
77
- return (x [:l ] + '..' ) if len (x ) > l else x
77
+ def trunc_str (x , length ):
78
+ return (x [:length ] + '..' ) if len (x ) > length else x
78
79
79
80
80
81
def resource_string (name : str , pfx : Optional [str ] = None ) -> Optional [Union [str , bytes ]]:
@@ -143,7 +144,7 @@ def totimestamp(dt: datetime, epoch=datetime(1970, 1, 1)) -> int:
143
144
epoch = epoch .replace (tzinfo = dt .tzinfo )
144
145
145
146
td = dt - epoch
146
- ts = (td .microseconds + (td .seconds + td .days * 24 * 3600 ) * 10 ** 6 ) / 1e6
147
+ ts = (td .microseconds + (td .seconds + td .days * 24 * 3600 ) * 10 ** 6 ) / 1e6
147
148
return int (ts )
148
149
149
150
@@ -421,7 +422,7 @@ def filter_lang(elts: Any, langs: Optional[Sequence[str]] = None) -> list[Elemen
421
422
raise RuntimeError ('Configuration is missing langs' )
422
423
423
424
dflt = langs [0 ]
424
- lst = [find_lang (elts , l , dflt ) for l in langs ]
425
+ lst = [find_lang (elts , lang , dflt ) for lang in langs ]
425
426
if len (lst ) > 0 :
426
427
return lst
427
428
else :
@@ -477,7 +478,7 @@ def total_seconds(dt: timedelta) -> float:
477
478
if hasattr (dt , "total_seconds" ):
478
479
return dt .total_seconds ()
479
480
# TODO: Remove? I guess this is for Python < 3
480
- return (dt .microseconds + (dt .seconds + dt .days * 24 * 3600 ) * 10 ** 6 ) / 10 ** 6
481
+ return (dt .microseconds + (dt .seconds + dt .days * 24 * 3600 ) * 10 ** 6 ) / 10 ** 6
481
482
482
483
483
484
def etag (s ):
@@ -524,7 +525,7 @@ def has_tag(t, tag):
524
525
525
526
526
527
def url2host (url ):
527
- (host , sep , port ) = urlparse (url ).netloc .partition (':' )
528
+ (host , sep , _port ) = urlparse (url ).netloc .partition (':' )
528
529
return host
529
530
530
531
@@ -659,10 +660,10 @@ def is_text(x: Any) -> bool:
659
660
return isinstance (x , str ) or isinstance (x , str )
660
661
661
662
662
- def chunks (l , n ):
663
+ def chunks (input_list , n ):
663
664
"""Yield successive n-sized chunks from l."""
664
- for i in range (0 , len (l ), n ):
665
- yield l [i : i + n ]
665
+ for i in range (0 , len (input_list ), n ):
666
+ yield input_list [i : i + n ]
666
667
667
668
668
669
class DirAdapter (BaseAdapter ):
@@ -748,7 +749,7 @@ def safe_b64d(s: str) -> bytes:
748
749
749
750
def img_to_data (data : bytes , content_type : str ) -> Optional [str ]:
750
751
"""Convert a file (specified by a path) into a data URI."""
751
- mime_type , options = cgi .parse_header (content_type )
752
+ mime_type , _options = cgi .parse_header (content_type )
752
753
data64 = None
753
754
if len (data ) > config .icon_maxsize :
754
755
return None
@@ -952,7 +953,7 @@ def __init__(self):
952
953
def add_watcher (self , cb , * args , ** kwargs ):
953
954
self .watchers .append (Watchable .Watcher (cb , args , kwargs ))
954
955
955
- def remove_watcher (self , cb , * args , ** kwargs ):
956
+ def remove_watcher (self , cb , * _args , ** _kwargs ):
956
957
self .watchers .remove (Watchable .Watcher (cb ))
957
958
958
959
def notify (self , * args , ** kwargs ):
@@ -966,5 +967,5 @@ def notify(self, *args, **kwargs):
966
967
967
968
968
969
def utc_now () -> datetime :
969
- """ Return current time with tz=UTC """
970
+ """Return current time with tz=UTC"""
970
971
return datetime .now (tz = timezone .utc )
0 commit comments