Skip to content

Commit

Permalink
chore: re-structure code using black
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Nov 23, 2024
1 parent 3a59a16 commit 659d599
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 341 deletions.
47 changes: 43 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
name: Main Workflow
on: [push]
jobs:
build-python:
build:
name: Build
strategy:
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9, "3.10", rc]
python-version: [
2.7,
3.5,
3.6,
3.7,
3.8,
3.9,
"3.10",
"3.11",
"3.12",
latest,
rc
]
runs-on: ubuntu-latest
container: python:${{ matrix.python-version }}
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- run: python --version
- run: pip install -r requirements.txt
- run: pip install -r extra.txt
- run: |
pip install black
black . --check
if: matrix.python-version == '3.12'
- run: |
pip install pytest
ADAPTER=tiny pytest
- run: ADAPTER=tiny python setup.py test
if: matrix.python-version != '3.12' && matrix.python-version != 'latest'
build-pypy:
name: Build PyPy
strategy:
matrix:
python-version: [2.7, 3.6, 3.9, "3.10"]
runs-on: ubuntu-latest
container: pypy:${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4
- run: pypy --version
- run: pip install -r requirements.txt
- run: |
pip install black
black . --check
if: matrix.python-version == '3.12'
- run: |
pip install pytest
ADAPTER=tiny pytest
- run: ADAPTER=tiny pypy setup.py test
if: matrix.python-version != '3.12' && matrix.python-version != 'latest'
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

session.shelve*

.DS_Store

/.env
/.venv

/dist
/build
/src/repos.egg-info
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

*
* Restructured code using `black`

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
python_files = *.py
testpaths = src/repos/test
45 changes: 18 additions & 27 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,19 @@
import setuptools

setuptools.setup(
name = "repos",
version = "0.2.0",
author = "Hive Solutions Lda.",
author_email = "development@hive.pt",
description = "Repos",
license = "Apache License, Version 2.0",
keywords = "repos server",
url = "http://repos.hive.pt",
zip_safe = False,
packages = [
"repos",
"repos.controllers",
"repos.models"
],
package_dir = {
"" : os.path.normpath("src")
},
install_requires = [
"appier",
"appier-extras",
"jinja2",
"pymongo"
],
classifiers = [
name="repos",
version="0.2.0",
author="Hive Solutions Lda.",
author_email="development@hive.pt",
description="Repos",
license="Apache License, Version 2.0",
keywords="repos server",
url="http://repos.hive.pt",
zip_safe=False,
packages=["repos", "repos.controllers", "repos.models"],
package_dir={"": os.path.normpath("src")},
install_requires=["appier", "appier-extras", "jinja2", "pymongo"],
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
"License :: OSI Approved :: Apache Software License",
Expand All @@ -43,8 +32,10 @@
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7"
"Programming Language :: Python :: 3.7",
],
long_description = open(os.path.join(os.path.dirname(__file__), "README.md"), "rb").read().decode("utf-8"),
long_description_content_type = "text/markdown"
long_description=open(os.path.join(os.path.dirname(__file__), "README.md"), "rb")
.read()
.decode("utf-8"),
long_description_content_type="text/markdown",
)
7 changes: 4 additions & 3 deletions src/repos/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import repos


class BaseController(appier.Controller):

@appier.route("/compress", "GET", json = True)
@appier.ensure(token = "admin")
@appier.route("/compress", "GET", json=True)
@appier.ensure(token="admin")
def compress(self):
path = repos.Artifact.compress()
return self.send_path(path, cache = True)
return self.send_path(path, cache=True)
105 changes: 50 additions & 55 deletions src/repos/controllers/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

import repos


class PackageController(appier.Controller):

@appier.route("/packages", "GET", json = True)
@appier.route("/packages", "GET", json=True)
def list(self):
self.ensure_auth()
object = appier.get_object(alias = True, find = True)
packages = repos.Package.find(map = True, **object)
object = appier.get_object(alias=True, find=True)
packages = repos.Package.find(map=True, **object)
return packages

@appier.route("/packages/<str:name>", "GET", json = True)
@appier.route("/packages/<str:name>", "GET", json=True)
def retrieve(self, name):
# ensures proper authentication for the retrieval of
# the package contents
Expand All @@ -31,98 +32,92 @@ def retrieve(self, name):
# tries to retrieve the value of the current artifact
# it can be either a local file tuple or remote URL
result = repos.Artifact.retrieve(
name = name,
version = version,
branch = branch,
tag = tag
name=name, version=version, branch=branch, tag=tag
)

# in case the resulting value is a string it's assumed
# that it should be an URL and proper redirect is ensured
if appier.legacy.is_string(result): return self.redirect(result)
if appier.legacy.is_string(result):
return self.redirect(result)

# otherwise the result should be a tuple and we must unpack
# it to check for proper contents
data, file_name, content_type = result
appier.verify(
not data == None,
message = "No data available in the package",
exception = appier.OperationalError
message="No data available in the package",
exception=appier.OperationalError,
)
content_type = content_type or "application/octet-stream"
return self.send_file(
data,
name = file_name,
content_type = content_type
)
return self.send_file(data, name=file_name, content_type=content_type)

@appier.route("/packages", "POST", json = True)
@appier.ensure(token = "admin")
@appier.route("/packages", "POST", json=True)
@appier.ensure(token="admin")
def publish(self):
name = self.field("name", mandatory = True)
version = self.field("version", mandatory = True)
name = self.field("name", mandatory=True)
version = self.field("version", mandatory=True)
branch = self.field("branch", "master")
tags = self.field("tags", [], cast = list)
tags = self.field("tags", [], cast=list)
contents = self.field("contents")
url = self.field("url")
url_tags = self.field("url_tags", [], cast = list)
url_tags = self.field("url_tags", [], cast=list)
identifier = self.field("identifier")
info = self.field("info")
type = self.field("type")
content_type = self.field("content_type")
if info: info = json.loads(info)
if contents: _name, _content_type, data = contents
else: data = None
if info:
info = json.loads(info)
if contents:
_name, _content_type, data = contents
else:
data = None
url_tags = dict([value.split(":", 1) for value in url_tags])
artifact = repos.Artifact.publish(
name,
version,
branch = branch,
tags = tags,
data = data,
url = url,
url_tags = url_tags,
identifier = identifier,
info = info,
type = type,
content_type = content_type
branch=branch,
tags=tags,
data=data,
url=url,
url_tags=url_tags,
identifier=identifier,
info=info,
type=type,
content_type=content_type,
)
return dict(
key = artifact.key,
package = artifact.package.name,
version = artifact.version,
file_name = artifact.file_name,
content_type = artifact.content_type
key=artifact.key,
package=artifact.package.name,
version=artifact.version,
file_name=artifact.file_name,
content_type=artifact.content_type,
)

@appier.route("/packages/<str:name>/info", "GET", json = True)
@appier.route("/packages/<str:name>/info", "GET", json=True)
def info(self, name):
self.ensure_auth()
version = self.field("version")
return repos.Artifact._info(name = name, version = version)
return repos.Artifact._info(name=name, version=version)

@appier.route("/packages/<str:name>/artifacts", "GET", json = True)
@appier.route("/packages/<str:name>/artifacts", "GET", json=True)
def artifacts(self, name):
self.ensure_auth()
object = appier.get_object(alias = True, find = True)
object = appier.get_object(alias=True, find=True)
artifacts = repos.Artifact.find(
package = name,
map = True,
sort = [("timestamp", -1)],
**object
package=name, map=True, sort=[("timestamp", -1)], **object
)
return artifacts

def ensure_auth(self):
username = appier.conf("REPO_USERNAME", None)
password = appier.conf("REPO_PASSWORD", None)
if not username: return
if not username:
return
authorization = self.request.authorization
is_valid = authorization == (username, password)
if not is_valid: raise appier.SecurityError(
message = "Authentication failed",
code = 401,
headers = {
"WWW-Authenticate" : "Basic realm=\"default\""
}
)
if not is_valid:
raise appier.SecurityError(
message="Authentication failed",
code=401,
headers={"WWW-Authenticate": 'Basic realm="default"'},
)
9 changes: 3 additions & 6 deletions src/repos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
import appier
import appier_extras


class ReposApp(appier.WebApp):

def __init__(self, *args, **kwargs):
appier.WebApp.__init__(
self,
name = "repos",
parts = (
appier_extras.AdminPart,
),
*args, **kwargs
self, name="repos", parts=(appier_extras.AdminPart,), *args, **kwargs
)

def _version(self):
Expand All @@ -25,6 +21,7 @@ def _description(self):
def _observations(self):
return "Modular repository management system"


if __name__ == "__main__":
app = ReposApp()
app.serve()
Expand Down
Loading

0 comments on commit 659d599

Please sign in to comment.