Skip to content

Commit

Permalink
importlib_resources does not provide read_text anymore from python3.9 :/
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlatr committed Jan 29, 2024
1 parent 45e58ee commit a943d82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions burpa/_burpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from ._burp_commander import BurpCommander
from ._error import BurpaError
from ._utils import (get_valid_filename, parse_commas_separated_str, ensure_scheme,
parse_targets, setup_logger, perform, is_timenow_between, strip_tags)
parse_targets, setup_logger, perform, is_timenow_between, strip_tags, read_text)
from .__version__ import __version__, __author__

###################################################
Expand Down Expand Up @@ -671,7 +671,7 @@ def generate_csv(io: TextIO, issues: List[Dict[str, Any]], report_datetime:str)
return

# Add CWE information
jsondata = json.loads(importlib_resources.read_text('burpa', 'issue_defs.json'))
jsondata = json.loads(read_text('burpa', 'issue_defs.json'))

for i in issues:
# Discard request/response data.
Expand Down
29 changes: 28 additions & 1 deletion burpa/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import sys
import concurrent.futures
from datetime import datetime, time
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, TextIO, Tuple
from importlib_resources import files, Package, Resource

def get_valid_filename(s: str) -> str:
'''Return the given string converted to a string that can be used for a clean filename. Stolen from Django, I think.'''
Expand Down Expand Up @@ -153,6 +154,32 @@ def get_version(s:str) -> Tuple[int, int, int]:
def strip_tags(html:str) -> str:
return _tag.sub('', html)

def open_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> TextIO:
"""Return a file-like object opened for text reading of the resource."""
return (files(package) / resource).open(
'r', encoding=encoding, errors=errors
)


def read_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> str:
"""Return the decoded string of the resource.
The decoding-related arguments have the same semantics as those of
bytes.decode().
"""
with open_text(package, resource, encoding, errors) as fp:
return fp.read()

if __name__ == "__main__":

assert get_version("2.2.0") == (2,2,0)
Expand Down

0 comments on commit a943d82

Please sign in to comment.