Skip to content

Commit

Permalink
Refactor all the things! Turn into a module and use setuptools.
Browse files Browse the repository at this point in the history
Refactor into a module + switching to using setuptools with
entrypoints.
  • Loading branch information
MrElendig committed Dec 2, 2016
1 parent b4b22b9 commit fb31e06
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 77 deletions.
3 changes: 2 additions & 1 deletion README.rst
Expand Up @@ -17,10 +17,11 @@ Dependencies
| python-docopt
| python-yaml
| python-curtsies
| python-setuptools
Instalation
============
| python3 setup.py install
| pip --user install .
| or better, use your distributions build system
Usage
Expand Down
70 changes: 5 additions & 65 deletions bin/kittypack → kittypack/app.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python3

# kittypack: Grabs package info off archlinux.org/packages
# Copyright (C) 2012 Øyvind 'Mr.Elendig' Heggstad

Expand Down Expand Up @@ -33,8 +34,7 @@
import docopt
import yaml
import collections
import curtsies
import re
from kittypack import fmt


def query_remote(url, params):
Expand All @@ -53,66 +53,6 @@ def query_remote(url, params):
req.raise_for_status()


def template_output(pkg):
""" Creates a output string from a package
:parmans pkg: dict like object describing a package
:returns string
"""
template = ("{repo}/{arch}/{pkgname} {epoch}:{pkgver}-{pkgrel}{ood}\n"
" Updated: {last_update} Built: {build_date}")
data = {}
data["repo"] = curtsies.fmtstr(pkg["repo"], fg="magenta", bold=True)
data["arch"] = curtsies.fmtstr(pkg["arch"], fg="yellow", bold=True)
data["pkgname"] = curtsies.fmtstr(pkg["pkgname"], fg="green", bold=True)
if pkg["flag_date"]:
ver_colour = "red"
data["ood"] = curtsies.fmtstr(" <!>", fg=ver_colour)
else:
ver_colour = "green"
data["ood"] = ""
for itm in ("epoch", "pkgver", "pkgrel"):
# fmtstr doesn't like ints
data[itm] = curtsies.fmtstr(str(pkg[itm]), fg=ver_colour, bold=True)
data["last_update"] = pkg["last_update"]
data["build_date"] = pkg["build_date"]
return template.format(**data)


def format_output(fstring, pkg):
lookup = {
"%a": "arch",
"%B": "build_date",
"%b": "pkgbase",
"%C": "compressed_size",
"%c": "conflicts",
"%D": "depends",
"%d": "pkgdesc",
"%e": "epoch",
"%f": "filename",
"%f": "flag_date",
"%g": "groups",
"%I": "installed_size",
"%L": "licenses",
"%l": "pkgrel",
"%M": "mainainers",
"%n": "pkgname",
"%p": "packager",
"%P": "provides",
"%R": "replaces",
"%r": "repo",
"%U": "last_updated",
"%u": "url",
"%v": "pkgver",
}

tokens = set(re.findall('(%.)', fstring))
for token in tokens:
if token == "%%":
fstring = re.sub("%%", "%", fstring)
else:
fstring = re.sub(token, str(pkg[lookup[token]]), fstring)
return fstring


def read_config(path):
with open(path, "r") as fd:
Expand All @@ -131,7 +71,7 @@ def sort_by_repo(pkgs, conf):
return sorted(pkgs, key=sortkey)


if __name__ == "__main__":
def main():
args = docopt.docopt(__doc__) # will sys.exit(1) if invalid usage

if args["--config"]:
Expand Down Expand Up @@ -188,6 +128,6 @@ def sort_by_repo(pkgs, conf):
sys.exit(1)
pkgs = sort_by_repo(resp.parsed["results"], config)
if args["--format"]:
print("\n".join(format_output(args["--format"], pkg) for pkg in pkgs))
print("\n".join(fmt.format_output(args["--format"], pkg) for pkg in pkgs))
else:
print("\n\n".join(template_output(pkg) for pkg in pkgs))
print("\n\n".join(fmt.template_output(pkg) for pkg in pkgs))
87 changes: 87 additions & 0 deletions kittypack/fmt.py
@@ -0,0 +1,87 @@
#!/usr/bin/env python3

# kittypack: Grabs package info off archlinux.org/packages
# Copyright (C) 2012 Øyvind 'Mr.Elendig' Heggstad

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Various formaters etc for kittypack
import curtsies
import re

def template_output(pkg):
""" Creates a output string from a package
:parmans pkg: dict like object describing a package
:returns string
"""
template = ("{repo}/{arch}/{pkgname} {epoch}:{pkgver}-{pkgrel}{ood}\n"
" Updated: {last_update} Built: {build_date}")
data = {}
data["repo"] = curtsies.fmtstr(pkg["repo"], fg="magenta", bold=True)
data["arch"] = curtsies.fmtstr(pkg["arch"], fg="yellow", bold=True)
data["pkgname"] = curtsies.fmtstr(pkg["pkgname"], fg="green", bold=True)
if pkg["flag_date"]:
ver_colour = "red"
data["ood"] = curtsies.fmtstr(" <!>", fg=ver_colour)
else:
ver_colour = "green"
data["ood"] = ""
for itm in ("epoch", "pkgver", "pkgrel"):
# fmtstr doesn't like ints
data[itm] = curtsies.fmtstr(str(pkg[itm]), fg=ver_colour, bold=True)
data["last_update"] = pkg["last_update"]
data["build_date"] = pkg["build_date"]
return template.format(**data)


def format_output(fstring, pkg):
""" Creates a output string from a format string naively
:params fstring: format string
:parmans pkg: dict like object describing a package
:returns string
"""
lookup = {
"%a": "arch",
"%B": "build_date",
"%b": "pkgbase",
"%C": "compressed_size",
"%c": "conflicts",
"%D": "depends",
"%d": "pkgdesc",
"%e": "epoch",
"%f": "filename",
"%f": "flag_date",
"%g": "groups",
"%I": "installed_size",
"%L": "licenses",
"%l": "pkgrel",
"%M": "mainainers",
"%n": "pkgname",
"%p": "packager",
"%P": "provides",
"%R": "replaces",
"%r": "repo",
"%U": "last_updated",
"%u": "url",
"%v": "pkgver",
}

tokens = set(re.findall('(%.)', fstring))
for token in tokens:
if token == "%%":
fstring = re.sub("%%", "%", fstring)
else:
fstring = re.sub(token, str(pkg[lookup[token]]), fstring)
return fstring

File renamed without changes.
59 changes: 48 additions & 11 deletions setup.py
@@ -1,14 +1,51 @@
#!/usr/bin/env python3

import distutils.core

distutils.core.setup(
name='kittypack',
version='0.2',
description='A silly little tool to get info from archlinux.org/packages',
author="Øyvind 'Mr.Elendig' Heggstad",
author_email='mrelendig@har-ikkje.net',
url='https://github.com/MrElendig/kittypack',
scripts=['bin/kittypack'],
data_files=[('/etc/', ['conf/kittypack.conf'])]
# kittypack: Grabs package info off archlinux.org/packages
# Copyright (C) 2012 Øyvind 'Mr.Elendig' Heggstad

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


from setuptools import setup, find_packages

setup(
name="kittypack",
version="0.2.1",
description="A silly little tool to get info from archlinux.org/packages",
url="https://github.com/MrElendig/kittypack",
author="Øyvind \"Mr.Elendig\" Heggstad",
author_email="mrelendig@har-ikkje.net",
license="AGPLv3",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: System Administrators",
"Intended Audience :: End Users/Desktop",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Programming Language :: Python :: 3.5",
"Environment :: Console"
],

keywords="sample setuptools development",
packages=["kittypack"],
install_requires=["docopt", "requests", "pyyaml", "curtsies",
"setuptools"],
package_data={
"kittypack": ["kittypack.conf"],
},
entry_points={
"console_scripts": [
"kittypack=kittypack.app:main",
],
},
)

0 comments on commit fb31e06

Please sign in to comment.