Skip to content

Commit

Permalink
Enhance join_with_slash(), and move to misc.py
Browse files Browse the repository at this point in the history
  • Loading branch information
danghai committed Aug 9, 2018
1 parent 8e6c120 commit 503ea5a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 80 deletions.
18 changes: 0 additions & 18 deletions sktm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@
import sktm.patchwork


def join_with_slash(base, *suffix):
"""
Join parts of URL or path by slashes
Args:
base: Base URL or path.
*suffix: Array of suffixes
Returns:
The URL or path string
"""
parts = [base.rstrip('/')]
for arg in suffix:
parts.append(arg.strip('/'))
ending = '/' if arg.endswith('/') else ''
return '/'.join(parts) + ending


class JobType(enum.IntEnum):
"""Job type"""
BASELINE = 0
Expand Down
9 changes: 4 additions & 5 deletions sktm/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import jenkinsapi

import sktm
from sktm.misc import TestResult
from sktm.misc import TestResult, join_with_slash


class JenkinsProject(object):
Expand Down Expand Up @@ -315,9 +314,9 @@ def get_result_url(self, buildid):
Result:
The URL of the build result.
"""
return sktm.join_with_slash(self.server.base_server_url(),
"job",
str(buildid))
return join_with_slash(self.server.base_server_url(),
"job",
str(buildid))

def get_result(self, buildid):
"""
Expand Down
20 changes: 20 additions & 0 deletions sktm/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ class TestResult(enum.IntEnum):
MERGE_FAILURE = 1
BUILD_FAILURE = 2
TEST_FAILURE = 4


def join_with_slash(base, *suffix_tuple):
"""
Join parts of URL or path by slashes. Trailing slash of base, and each
arg in suffix_tupple are removed. It only keeps trailing slash at the
end of the part if it is specified.
Args:
base: Base URL or path.
*suffix_tuple: Tuple of suffixes
Returns:
The URL or path string
"""
parts = [base.rstrip('/')]
for arg in suffix_tuple:
parts.append(arg.strip('/'))
ending = '/' if arg.endswith('/') else ''
return '/'.join(parts) + ending
42 changes: 18 additions & 24 deletions sktm/patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import dateutil.parser
import requests

import sktm
from sktm.misc import TestResult
from sktm.misc import TestResult, join_with_slash


SKIP_PATTERNS = [
Expand Down Expand Up @@ -86,7 +85,7 @@ def get_mbox_url(self):
Returns:
URL pointing at the object's mbox.
"""
return sktm.join_with_slash(self.url, self.mbox_sfx)
return join_with_slash(self.url, self.mbox_sfx)


class SeriesSummary(object):
Expand Down Expand Up @@ -309,10 +308,10 @@ def __get_patch_message(self, patch_id):
of requests exceptions, Exception in case of unexpected return code
(eg. nonexistent patch).
"""
mbox_url = sktm.join_with_slash(self.baseurl,
'patch',
str(patch_id),
self._get_mbox_url_sfx())
mbox_url = join_with_slash(self.baseurl,
'patch',
str(patch_id),
self._get_mbox_url_sfx())

try:
response = requests.get(mbox_url)
Expand Down Expand Up @@ -402,9 +401,9 @@ def _get_patch_url(self, patch):
Returns:
Patch URL.
"""
return sktm.join_with_slash(self.baseurl,
'patch',
str(patch.get('id')))
return join_with_slash(self.baseurl,
'patch',
str(patch.get('id')))

def _get_mbox_url_sfx(self):
"""
Expand Down Expand Up @@ -458,10 +457,8 @@ def _get_project_id(self, project_name):
Returns:
Integer representing project's ID.
"""
response = requests.get(
sktm.join_with_slash(self.apiurls.get("projects"),
project_name)
)
response = requests.get(join_with_slash(self.apiurls.get("projects"),
project_name))
if response.status_code != requests.codes.ok:
raise Exception("Can't get project data: %s %d" %
(project_name, response.status_code))
Expand All @@ -475,7 +472,7 @@ def __get_apiurls(self, baseurl):
Returns:
The JSON representation of the API URLs.
"""
response = requests.get(sktm.join_with_slash(baseurl, "api"))
response = requests.get(join_with_slash(baseurl, "api"))
if response.status_code != 200:
raise Exception("Can't get apiurls: %d" % response.status_code)

Expand Down Expand Up @@ -696,10 +693,8 @@ def get_patch_by_id(self, pid):
set of supported attributes depends on which API versions are
supported by a specific Patchwork instance.
"""
response = requests.get(
sktm.join_with_slash(self.apiurls.get("patches"),
str(pid))
)
response = requests.get(join_with_slash(self.apiurls.get("patches"),
str(pid)))

if response.status_code != 200:
raise Exception("Can't get patch by id %d (%d)" %
Expand Down Expand Up @@ -745,8 +740,7 @@ def __get_patchsets_by_patch(self, url, seen=set()):
continue
else:
series_list += self.__get_series_from_url(
sktm.join_with_slash(self.apiurls.get("series"),
str(sid))
join_with_slash(self.apiurls.get("series"), str(sid))
)
seen.add(sid)

Expand Down Expand Up @@ -811,8 +805,8 @@ def get_patchsets(self, patchlist):
sid = series.get("id")
if sid not in seen:
series_list += self.__get_series_from_url(
sktm.join_with_slash(self.apiurls.get("series"),
str(sid))
join_with_slash(self.apiurls.get("series"),
str(sid))
)
seen.add(sid)

Expand Down Expand Up @@ -870,7 +864,7 @@ def __get_rpc(self, baseurl):
Returns:
The XML RPC interface for the Patchwork
"""
rpc = xmlrpclib.ServerProxy(sktm.join_with_slash(baseurl, "xmlrpc/"))
rpc = xmlrpclib.ServerProxy(join_with_slash(baseurl, "xmlrpc/"))
try:
ver = rpc.pw_rpc_version()
# check for normal patchwork1 xmlrpc version numbers
Expand Down
2 changes: 1 addition & 1 deletion sktm/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import re
import smtplib

from sktm import join_with_slash
from sktm.misc import join_with_slash


SUBSTITUTE_RE = re.compile(r'\{[\w\.]+\}')
Expand Down
32 changes: 0 additions & 32 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,6 @@
import sktm


class TestIndependent(unittest.TestCase):
"""Test cases for independent functions in __init__.py."""

def test_join_with_slash(self):
"""Ensure join_with_slash return a good url, path string."""
base = "path/to/dir"
suffix = "file"
self.assertEqual("path/to/dir/file",
sktm.join_with_slash(base, suffix))
base = "path/to/dir/"
suffix = "file"
self.assertEqual("path/to/dir/file",
sktm.join_with_slash(base, suffix))
base = "path/to/dir1/"
suffix = "dir2/"
self.assertEqual("path/to/dir1/dir2/",
sktm.join_with_slash(base, suffix))
base = "path/to/dir1/"
suffix1 = "dir2/"
suffix2 = "file"
self.assertEqual("path/to/dir1/dir2/file",
sktm.join_with_slash(base, suffix1, suffix2))
base = "http://url.com/"
suffix = "part"
self.assertEqual("http://url.com/part",
sktm.join_with_slash(base, suffix))
base = "http://url.com"
suffix = "part"
self.assertEqual("http://url.com/part",
sktm.join_with_slash(base, suffix))


class TestInit(unittest.TestCase):
"""Test cases for the __init__ module."""

Expand Down
50 changes: 50 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2018 Red Hat, Inc. All rights reserved. This copyrighted
# material is made available to anyone wishing to use, modify, copy, or
# redistribute it subject to the terms and conditions of the GNU General Public
# License v.2 or later.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Tests for the misc.py."""

import unittest

import sktm.misc


class TestIndependent(unittest.TestCase):
"""Test cases for independent functions in misc.py."""

def test_join_with_slash(self):
"""Ensure join_with_slash return a good url, path string."""
base = "path/to/dir"
suffix = "file"
self.assertEqual("path/to/dir/file",
sktm.misc.join_with_slash(base, suffix))
base = "path/to/dir/"
suffix = "file"
self.assertEqual("path/to/dir/file",
sktm.misc.join_with_slash(base, suffix))
base = "path/to/dir1/"
suffix = "dir2/"
self.assertEqual("path/to/dir1/dir2/",
sktm.misc.join_with_slash(base, suffix))
base = "path/to/dir1/"
suffix1 = "dir2/"
suffix2 = "file"
self.assertEqual("path/to/dir1/dir2/file",
sktm.misc.join_with_slash(base, suffix1, suffix2))
base = "http://url.com/"
suffix = "part"
self.assertEqual("http://url.com/part",
sktm.misc.join_with_slash(base, suffix))
base = "http://url.com"
suffix = "part"
self.assertEqual("http://url.com/part",
sktm.misc.join_with_slash(base, suffix))

0 comments on commit 503ea5a

Please sign in to comment.