Skip to content

Commit

Permalink
Added --format and man page
Browse files Browse the repository at this point in the history
  • Loading branch information
MrElendig committed Mar 21, 2015
1 parent 2f7daed commit 81d712d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
47 changes: 44 additions & 3 deletions bin/kittypack
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ Usage: kittypack [options] <pkg>
Options:
-r, --repository=<repo> Search only in <repo>
-a, --architecture=<arch> Search only in <arch>
-f, --format=<fstring> Use custom format string
-j, --json Print the raw json
-h, --help Show this screen
-c, --config=<config> Path to the config [default: /etc/kittypack.conf]
-h, --help Show this screen
"""

import requests
Expand All @@ -33,6 +34,7 @@ import docopt
import yaml
import collections
import curtsies
import re


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


def format_output(pkg):
def template_output(pkg):
""" Creates a output string from a package
:parmans pkg: dict like object describing a package
:returns string
Expand All @@ -76,6 +78,42 @@ def format_output(pkg):
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:
config = yaml.safe_load(fd)
Expand Down Expand Up @@ -149,4 +187,7 @@ if __name__ == "__main__":
print("No results found", file=sys.stderr)
sys.exit(1)
pkgs = sort_by_repo(resp.parsed["results"], config)
print("\n\n".join(format_output(pkg) for pkg in pkgs))
if args["--format"]:
print("\n".join(format_output(args["--format"], pkg) for pkg in pkgs))
else:
print("\n\n".join(template_output(pkg) for pkg in pkgs))
71 changes: 71 additions & 0 deletions doc/man/kittypack.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
=========
kittypack
=========

-------------------------------------------------------------
A tool to grab basic package info from archlinux.org/packages
-------------------------------------------------------------

:Author: Øyvind Heggstad <mrelendig@har-ikkje.net>
:Date: 2014-03-21
:Copyright: AGPLv3
:Version: 0.1
:Manual section: 1
:Manual group: None

SYNOPSIS
========

kittypack [options] <pkg>

DESCRIPTION
===========

This is a -program- written by Mr.Elendig.
It menaces with spikes of crud and is decorated with hanging rings of fail.
On the program is a picture of bugs in python.

OPTIONS
=======

-r, --repository=<repo> Search only in <repo>
-a, --architecture=<arch> Search only in <arch>
-f, --format=<fstring> Use custom format string
-j, --json Print the raw json
-c, --config=<config> Path to the config [default: /etc/kittypack.conf]
-h, --help Show the help screen

FORMATTING
==========

The output format supports the following tokens:

%% literal %
%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

BUGS
====

Everything

0 comments on commit 81d712d

Please sign in to comment.