Skip to content

Commit

Permalink
Merge pull request #12 from zhongjiajie/chore
Browse files Browse the repository at this point in the history
Chore remove unused and use logger handler
  • Loading branch information
MikeMoore63 authored Jan 4, 2025
2 parents 5f2418a + cb82d9e commit d622a7f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ import logging
import sys
import threading

logger = logging.getLogger(__name__)
if not logging.getLogger().hasHandlers():
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.getLogger(__name__).setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)

# a rough filter for files to process
LICENSE_RE = r'^.*LICENSE$|^.*LICENSE.*\.(?!(exe|dll|go|c|h|py|pyc|rb|sh|sql|jsonl)$)([^.]+$)'
Expand Down Expand Up @@ -246,15 +247,15 @@ def _store_content(blob_name, ecosystem, mime_type, match=False):
try:
blob = storage.Blob(bucket=bucket, name=blob_path_name)
if blob.exists(sc):
logging.getLogger(__name__).debug(f"Checked object exists {blob_path_name}")
logger.debug(f"Checked object exists {blob_path_name}")
return blob_name, output
# 7 days thisstuff is not intended to change
# so provide hints to cloud storage to maximise this
blob.cache_control = "max-age=604800"
blob.upload_from_string(blob_content, content_type=mime_type)
logging.getLogger(__name__).info(f"Stored object {blob_path_name}")
logger.info(f"Stored object {blob_path_name}")
except exceptions.GoogleCloudError:
logging.getLogger(__name__).exception(f"Unable to store object {blob_path_name}")
logger.exception(f"Unable to store object {blob_path_name}")
return blob_name, output

# gen_license_input("spdxLic.jsonl", "spdxLicExceptions.jsonl", "spdxCache.json")
Expand Down
9 changes: 5 additions & 4 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import sys
import threading

logger = logging.getLogger(__name__)
if not logging.getLogger().hasHandlers():
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.getLogger(__name__).setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)

LICENSE_RE = r'^.*LICENSE$|^.*LICENSE.*\.(?!(exe|dll|go|c|h|py|pyc|rb|sh|sql|jsonl)$)([^.]+$)'

Expand Down Expand Up @@ -125,15 +126,15 @@ def _store_content(blob_name, ecosystem, mime_type, match=False):
try:
blob = storage.Blob(bucket=bucket, name=blob_path_name)
if blob.exists(sc):
logging.getLogger(__name__).debug(f"Checked object exists {blob_path_name}")
logger.debug(f"Checked object exists {blob_path_name}")
return blob_name, output
# 7 days thisstuff is not intended to change
# so provide hints to cloud storage to maximise this
blob.cache_control = "max-age=604800"
blob.upload_from_string(blob_content, content_type=mime_type)
logging.getLogger(__name__).info(f"Stored object {blob_path_name}")
logger.info(f"Stored object {blob_path_name}")
except exceptions.GoogleCloudError:
logging.getLogger(__name__).exception(f"Unable to store object {blob_path_name}")
logger.exception(f"Unable to store object {blob_path_name}")
return blob_name, output

# gen_license_input("spdxLic.jsonl", "spdxLicExceptions.jsonl", "spdxCache.json")
Expand Down
11 changes: 6 additions & 5 deletions src/spdx_matcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from importlib.metadata import version

__all__ = [
"__version__",
"normalize",
"LICENSE_HEADER_REMOVAL",
"COPYRIGHT_REMOVAL",
Expand All @@ -47,6 +46,8 @@
__version__ = version("spdx_matcher")
logger = logging.getLogger(__name__)

logger = logging.getLogger(__name__)

DEFAULT_CACHE_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "spdxCache.json"
)
Expand Down Expand Up @@ -605,7 +606,7 @@ def _license_regexps_match(regexp_to_match_input, license, fast_exit=True):
for item_num, initial_match in enumerate(
re.finditer(initial_regexp, normalized_all_license, flags=re.IGNORECASE)
):
logging.getLogger(__name__).debug(f"iterating regexp {item_num}")
logger.debug(f"iterating regexp {item_num}")
normalized_license = normalized_all_license[initial_match.end() :]
matches = 1
non_matches = 0
Expand Down Expand Up @@ -785,13 +786,13 @@ def analyse_license_text(original_content, avoid_license=None, avoid_exceptions=
if id in avoid_license:
continue
to_process = match_cache["licenses"][id]
logging.getLogger(__name__).debug(f"processing license {id}")
logger.debug(f"processing license {id}")
match, license_data, full_match = _license_regexps_match(
to_process["regexpForMatch"], original_content, fast_exit=True
)

if match == 1.0:
logging.getLogger(__name__).debug(f"matched license {id}")
logger.debug(f"matched license {id}")
analysed_length += to_process["text_length"]
analysis["licenses"][id] = license_data

Expand All @@ -803,7 +804,7 @@ def analyse_license_text(original_content, avoid_license=None, avoid_exceptions=
if id in avoid_exceptions:
continue
to_process = match_cache["exceptions"][id]
logging.getLogger(__name__).debug(f"processing exceptions {id}")
logger.debug(f"processing exceptions {id}")
match, license_data, full_match = _license_regexps_match(
to_process["regexpForMatch"], original_content, fast_exit=True
)
Expand Down
11 changes: 5 additions & 6 deletions tests/test_spdx_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import spdx_matcher
from pathlib import Path


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
"""
Expand Down Expand Up @@ -64,10 +63,10 @@

class TestSimple(unittest.TestCase):
def test_apache2(self):
logging.getLogger(__name__).debug("Starting normalize of apache2..")
logger.debug("Starting normalize of apache2..")
content = spdx_matcher.normalize(APACHE2,
remove_sections=spdx_matcher.REMOVE_FINGERPRINT)
logging.getLogger(__name__).debug("Finished normalize of apache2..")
logger.debug("Finished normalize of apache2..")
if not isinstance(content, bytes):
content = content.encode("utf-8")

Expand All @@ -92,10 +91,10 @@ def test_no_match(self):
self.assertEqual(len(analysis["exceptions"]), 0)

def test_backtracking_challenging(self):
logging.getLogger(__name__).debug("Starting normalize of challenging..")
logger.debug("Starting normalize of challenging..")
content = spdx_matcher.normalize(CHALLENGING,
remove_sections=spdx_matcher.REMOVE_FINGERPRINT)
logging.getLogger(__name__).debug("Finished normalize of challenging..")
logger.debug("Finished normalize of challenging..")
if not isinstance(content, bytes):
content = content.encode("utf-8")

Expand Down Expand Up @@ -139,7 +138,7 @@ def test_version(self):

class TestNormalize(unittest.TestCase):
def test_space_remove(self):
logging.getLogger(__name__).debug("Starting normalize test for specific symbol removal..")
logger.debug("Starting normalize test for specific symbol removal..")
source = "space remove for . new start"
expected = "space remove for. new start"
self.assertEqual(expected, spdx_matcher.normalize(source))
Expand Down

0 comments on commit d622a7f

Please sign in to comment.