Skip to content
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,5 @@ helping you find real world examples of functions, API's and libraries in 243 la

## Acknowledgements

This SDK is developed and maintained by [Richard Mwewa](https://gravatar.com/rly0nheart), in collaboration
This SDK is developed and maintained by [Ritchie Mwewa](https://gravatar.com/rly0nheart), in collaboration
with [Ben Boyter](https://boyter.org/about/), the creator of [Searchcode.com](https://searchcode.com).
265 changes: 141 additions & 124 deletions poetry.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[tool.poetry]
name = "searchcode"
version = "0.2.1"
version = "0.2.2"
description = "Python SDK for Searchcode."
authors = ["Richard Mwewa <rly0nheart@duck.com>"]
authors = ["Ritchie Mwewa <rly0nheart@duck.com>"]
license = "GPLv3+"
readme = "README.md"
homepage = "https://searchcode.com"
Expand All @@ -21,8 +21,8 @@ python = "^3.10"
requests = "^2.32.2"

[tool.poetry.group.dev.dependencies]
flake8 = "^7.1.1"
pytest = "^8.3.3"
flake8 = "^7.1.2"
pytest = "^8.3.5"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion searchcode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from ._main import *

__all__ = ["code_result", "code_search", "related_results"]
__all__ = ["code_result", "code_search"]
37 changes: 23 additions & 14 deletions searchcode/_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from platform import python_version, platform
from types import SimpleNamespace
from typing import List, Union, Dict, Optional, Tuple

Expand All @@ -10,7 +11,7 @@
get_source_ids,
)

__all__ = ["code_result", "code_search", "related_results"]
__all__ = ["code_result", "code_search"]


_BASE_API_ENDPOINT = "https://searchcode.com/api"
Expand All @@ -31,7 +32,14 @@ def _get_response(
:raises Exception: If the request fails or the server returns an error.
"""

response = requests.get(url=endpoint, params=params)
response = requests.get(
url=endpoint,
params=params,
headers={
"User-Agent": f"searchcode-sdk/0.2.2 "
f"(Python {python_version} on {platform}; +https://pypi.org/project/searchcode)"
},
)
response.raise_for_status()
return response.text if kwargs.get("is_callback") else response.json()

Expand Down Expand Up @@ -138,17 +146,18 @@ def code_result(_id: int) -> SimpleNamespace:
return response.get("code")


def related_results(_id: int) -> SimpleNamespace:
"""
Returns an array of results given a searchcode unique code id which are considered to be duplicates.

The matching is slightly fuzzy allowing so that small differences between files are ignored.
# This is deprecated.
# def related_results(_id: int) -> SimpleNamespace:
# """
# Returns an array of results given a searchcode unique code id which are considered to be duplicates.
#
# The matching is slightly fuzzy allowing so that small differences between files are ignored.

:param _id: The unique identifier of the code result.
:type _id: int
:return: A list of related results as a SimpleNamespace object.
:rtype: SimpleNamespace
"""
# :param _id: The unique identifier of the code result.
# :type _id: int
# :return: A list of related results as a SimpleNamespace object.
# :rtype: SimpleNamespace
# """

response = _get_response(endpoint=f"{_BASE_API_ENDPOINT}/related_results/{_id}")
return _response_to_namespace_obj(response=response)
# response = _get_response(endpoint=f"{_BASE_API_ENDPOINT}/related_results/{_id}")
# return _response_to_namespace_obj(response=response)
9 changes: 2 additions & 7 deletions tests/test_searchcode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import searchcode as sc


def test_filter_by_language():
search = sc.code_search(query="fn main()", languages=["Rust", "Go"])
for result in search.results:
assert result.filename.endswith(".rs") or result.filename.endswith(".go")
assert result.language in ["Rust", "Go"]


def test_filter_by_extension():
search = sc.code_search(query="gsub ext:erb")
for result in search.results:
Expand All @@ -20,7 +13,9 @@ def test_code_result():
assert "This file is part of Quake III Arena source code" in code


"""
def test_related_results():
related = sc.related_results(4061576)
assert isinstance(related, list)
assert len(related) == 0
"""