Skip to content
Open
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
52 changes: 52 additions & 0 deletions testUpdateHostsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
exclude_domain,
flush_dns_cache,
gather_custom_exclusions,
get_all_applications,
get_defaults,
get_file_by_url,
is_valid_user_provided_domain_format,
Expand Down Expand Up @@ -757,6 +758,57 @@ def test_update_both_pathways(self, mock_join_robust, *_):
self.assert_called_once(mock_join_robust)


class TestGetAllApplications(Base):
def setUp(self):
Base.setUp(self)
self.datapath = "data"
self.extensionspath = "extensions"
self.source_data_filename = "update.json"

@mock.patch(
"updateHostsFile.recursive_glob",
side_effect=[
["data/source1/update.json", "data/source2/update.json"],
["extensions/ext1/update.json"],
],
)
@mock.patch(
"json.load",
side_effect=[
{"name": "Source1", "url": "http://example1.com"},
{"name": "Source2", "url": "http://example2.com"},
{"name": "Extension1", "url": "http://example3.com"},
],
)
def test_get_all_applications(self, mock_json_load, mock_glob):
mock_file = mock.mock_open()
with mock.patch("builtins.open", mock_file):
applications = get_all_applications(
datapath=self.datapath,
extensionspath=self.extensionspath,
sourcedatafilename=self.source_data_filename,
)

self.assertEqual(len(applications), 3)
self.assertEqual(applications[0]["name"], "Source1")
self.assertEqual(applications[0]["type"], "unified")
self.assertEqual(applications[0]["path"], "data/source1/update.json")
self.assertEqual(applications[1]["name"], "Source2")
self.assertEqual(applications[1]["type"], "unified")
self.assertEqual(applications[2]["name"], "Extension1")
self.assertEqual(applications[2]["type"], "extension")

@mock.patch("updateHostsFile.recursive_glob", return_value=[])
def test_get_all_applications_empty(self, mock_glob):
applications = get_all_applications(
datapath=self.datapath,
extensionspath=self.extensionspath,
sourcedatafilename=self.source_data_filename,
)

self.assertEqual(len(applications), 0)


class TestUpdateAllSources(BaseStdout):
def setUp(self):
BaseStdout.setUp(self)
Expand Down
105 changes: 105 additions & 0 deletions updateHostsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ def main():
default=path_join_robust(BASEDIR_PATH, "blacklist"),
help="Blacklist file to use while generating hosts files.",
)
parser.add_argument(
"--list-applications",
"-l",
dest="listapplications",
default=False,
action="store_true",
help="List all data source applications and their metadata.",
)

global settings

Expand Down Expand Up @@ -259,6 +267,35 @@ def main():
sourcedatafilename = settings["sourcedatafilename"]
nounifiedhosts = settings["nounifiedhosts"]

# Handle list applications request
if settings.get("listapplications", False):
applications = get_all_applications(
datapath=datapath,
extensionspath=extensionspath,
sourcedatafilename=sourcedatafilename,
)

print("\n" + "=" * 80)
print("DATA SOURCE APPLICATIONS")
print("=" * 80 + "\n")

for app in applications:
print(f"Name: {app.get('name', 'N/A')}")
print(f"Type: {app.get('type', 'N/A')}")
print(f"Description: {app.get('description', 'N/A')}")
print(f"Home URL: {app.get('homeurl', 'N/A')}")
print(f"Data URL: {app.get('url', 'N/A')}")
print(f"License: {app.get('license', 'N/A')}")
print(f"Update Frequency: {app.get('frequency', 'N/A')}")
print(f"Issues: {app.get('issues', 'N/A')}")
paused = "Yes" if app.get('pause', False) else "No"
print(f"Paused: {paused}")
print(f"Path: {app.get('path', 'N/A')}")
print("-" * 80)

print(f"\nTotal applications: {len(applications)}")
return

updatesources = prompt_for_update(freshen=settings["freshen"], updateauto=auto)
if updatesources:
update_all_sources(sourcedatafilename, settings["hostfilename"])
Expand Down Expand Up @@ -718,6 +755,74 @@ def update_sources_data(sourcesdata, **sourcesparams):
return sourcesdata


def get_all_applications(**params):
"""
Retrieve metadata for all data source applications.

Parameters
----------
params : kwargs
Dictionary providing parameters for retrieving applications.
Currently, those fields are:

1) datapath
2) extensionspath
3) sourcedatafilename

Returns
-------
applications : list
A list of dictionaries containing metadata for each data source.
"""

def read_sources(path, source_type, sourcedatafilename):
"""
Helper function to read sources from a directory.

Parameters
----------
path : str
The directory path to search for sources.
source_type : str
The type of source (unified or extension).
sourcedatafilename : str
The filename to search for (e.g., update.json).

Returns
-------
sources : list
A list of dictionaries containing metadata for each source.
"""
sources = []
for source in sort_sources(recursive_glob(path, sourcedatafilename)):
try:
with open(source, "r", encoding="UTF-8") as updatefile:
updatedata = json.load(updatefile)
updatedata["type"] = source_type
updatedata["path"] = source
sources.append(updatedata)
except Exception as e:
print_failure(f"Error reading {source_type} source {source}: {e}")
return sources

applications = []
sourcedatafilename = params.get("sourcedatafilename", "update.json")
datapath = params.get("datapath", path_join_robust(BASEDIR_PATH, "data"))
extensionspath = params.get(
"extensionspath", path_join_robust(BASEDIR_PATH, "extensions")
)

# Get unified hosts sources
applications.extend(read_sources(datapath, "unified", sourcedatafilename))

# Get extension sources
applications.extend(
read_sources(extensionspath, "extension", sourcedatafilename)
)

return applications


def jsonarray(json_array_string):
"""
Transformer, converts a json array string hosts into one host per
Expand Down