Skip to content

Commit

Permalink
refactor repository representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rechi committed Nov 19, 2018
1 parent e8f7b24 commit 0929831
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
40 changes: 40 additions & 0 deletions kodi_addon_checker/addons/Repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Copyright (C) 2018 Team Kodi
This file is part of Kodi - kodi.tv
SPDX-License-Identifier: GPL-3.0-only
See LICENSES/README.md for more information.
"""

from .Addon import Addon

import gzip
import requests
import xml.etree.ElementTree as ET
from io import BytesIO


class Repository(object):
def __init__(self, version, path):
super(Repository, self).__init__()
self.version = version
self.path = path
gz_file = requests.get(path, timeout=(10, 10)).content
with gzip.open(BytesIO(gz_file), 'rb') as xml_file:
content = xml_file.read()
tree = ET.fromstring(content)
self.addons = []
for addon in tree.findall("addon"):
self.addons.append(Addon(addon))

def __contains__(self, addonId):
for addon in self.addons:
if addon.id == addonId:
return True
return False

def find(self, addonId):
for addon in self.addons:
if addon.id == addonId:
return addon
return None
26 changes: 2 additions & 24 deletions kodi_addon_checker/check_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

import os
import xml.etree.ElementTree as ET
import requests
import logging
import gzip
from io import BytesIO

from .addons.Repository import Repository
from .record import Record, INFORMATION
from .report import Report
from . import check_artwork
Expand Down Expand Up @@ -112,26 +110,6 @@ def all_repo_addons():

for branch in branches:
branch_url = ROOT_URL.format(branch=branch)
repo_addons[branch] = _get_addons(branch_url)
repo_addons[branch] = Repository(branch, branch_url)

return repo_addons


def _get_addons(xml_url: str):
"""Gets addon.xml file for all the version of kodi
:xml_url: url of the version of kodi
"""
try:
gz_file = requests.get(xml_url, timeout=(10, 10)).content
with gzip.open(BytesIO(gz_file), 'rb') as xml_file:
content = xml_file.read()
tree = ET.fromstring(content)

return {
a.get("id"): a.get("version")
for a in tree.findall("addon")
}
except requests.exceptions.ReadTimeout as errrt:
LOGGER.error(errrt)
except requests.exceptions.ConnectTimeout as errct:
LOGGER.error(errct)
6 changes: 3 additions & 3 deletions kodi_addon_checker/check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def check_addon_dependencies(report: Report, repo_addons: dict, parsed_xml, bran
report.add(Record(INFORMATION if dependency.optional else WARNING,
"{} dependency {} does not require a minimum version, available: {}"
.format("Optional" if dependency.optional else "Required", dependency.id,
repo_addons[dependency.id])))
repo_addons.find(dependency.id).version)))

elif LooseVersion(repo_addons[dependency.id]) < dependency.version:
elif repo_addons.find(dependency.id).version < dependency.version:
report.add(Record(INFORMATION if dependency.optional else PROBLEM,
"Version mismatch for {} dependency {}, required: {}, Available: {}"
.format("optional" if dependency.optional else "required", dependency.id,
dependency.version, repo_addons[dependency.id])))
dependency.version, repo_addons.find(dependency.id).version)))

if dependency.id in VERSION_ATTRB:
try:
Expand Down
8 changes: 3 additions & 5 deletions kodi_addon_checker/check_old_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ def check_for_existing_addon(report: Report, addon_path: str, all_repo_addons: d
addon_xml = os.path.join(addon_path, "addon.xml")
addon_name, addon_version = _get_addon_name(addon_xml)

for branch in sorted(all_repo_addons):
repo_addons = all_repo_addons[branch]

if addon_name in repo_addons:
_check_versions(report, addon_name, branch, addon_version, repo_addons[addon_name], pr)
for branch, repo in sorted(all_repo_addons.items()):
if addon_name in repo:
_check_versions(report, addon_name, branch, addon_version, repo.find(addon_name).version, pr)
return

report.add(Record(INFORMATION, "This is a new addon"))
Expand Down
14 changes: 0 additions & 14 deletions script.test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from radon.raw import analyze
from distutils.version import LooseVersion
import xml.etree.ElementTree as ET
import requests

from PIL import Image

Expand Down Expand Up @@ -358,19 +357,6 @@ def number_of_lines(filepath):
return (analyze(data).lloc)


def _get_addons(xml_url):
"""
addon.xml for the target Kodi version
"""
content = requests.get(xml_url).content
tree = ET.fromstring(content)

return {
a.get("id"): a.get("version")
for a in tree.findall("addon")
}


def _get_users_dependencies(addon_path):
"""
User's addon.xml from pull request
Expand Down

0 comments on commit 0929831

Please sign in to comment.