#!/usr/bin/env python3
# 
# pycman.pkginfo - A Python implementation of Pacman
# Copyright (C) 2011 Rémy Oudompheng <remy@archlinux.org>
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

"""
Package information formatting

This module defines utility function to format package information
for terminal output.
"""

import time
import textwrap
import pyalpm

ATTRNAME_FORMAT = '%-14s : '
ATTR_INDENT = 17 * ' '

def format_attr(attrname, value, format = None):
	if isinstance(value, list):
		if len(value) == 0:
			valuestring = 'None'
		else:
			valuestring = '  '.join(str(v) for v in value)
	else:
		if format == "time":
			valuestring = time.strftime("%a %d %b %Y %X %Z", time.localtime(value))
		else:
			valuestring = str(value)
	return textwrap.fill(valuestring, width = 80,
			initial_indent = ATTRNAME_FORMAT % attrname,
			subsequent_indent = ATTR_INDENT,
			break_on_hyphens = False,
			break_long_words = False)

def format_attr_oneperline(attrname, value):
	if len(value) == 0:
		value = ['None']
	s = ATTRNAME_FORMAT % attrname
	s += ('\n' + ATTR_INDENT).join(value)
	return s

def display_pkginfo(pkg, level = 1, style = 'local'):
	"""
	Displays pretty-printed package information.

	Args :
	  pkg -- the package to display
	  level -- the level of detail (1 or 2)
	  style -- 'local' or 'sync'
	"""

	if style not in ['local', 'sync']:
		raise ValueError('Invalid style for package info formatting')

	if style == 'sync':
		print(format_attr('Repository', pkg.db.name))
	print(format_attr('Name', pkg.name))
	print(format_attr('Version', pkg.version))
	print(format_attr('URL', pkg.url))
	print(format_attr('Licenses', pkg.licenses))
	print(format_attr('Groups', pkg.groups))
	print(format_attr('Provides', pkg.provides))
	print(format_attr('Depends on', pkg.depends))
	print(format_attr_oneperline('Optional deps', pkg.optdepends))
	if style == 'local' or level == 2:
		print(format_attr('Required by', pkg.compute_requiredby()))
	print(format_attr('Conflicts with', pkg.conflicts))
	print(format_attr('Replaces', pkg.replaces))
	if style == 'sync':
		print(format_attr('Download size', '%.2f K' % (pkg.size / 1024)))
	print(format_attr('Installed size', '%.2f K' % (pkg.isize / 1024)))
	print(format_attr('Packager', pkg.packager))
	print(format_attr('Architecture', pkg.arch))
	print(format_attr('Build Date', pkg.builddate, format = 'time'))

	if style == 'local':
		# local installation information
		print(format_attr('Install Date', pkg.installdate, format = 'time'))
		if pkg.reason == pyalpm.PKG_REASON_EXPLICIT:
			reason = 'Explicitly installed'
		elif pkg.reason == pyalpm.PKG_REASON_DEPEND:
			reason = 'Installed as a dependency for another package'
		else:
			reason = 'N/A'
		print(format_attr('Install Reason', reason))
		print(format_attr('Install Script', 'Yes' if pkg.has_scriptlet else 'No'))
	else:
		print(format_attr('MD5 Sum', pkg.md5sum))

	print(format_attr('Description', pkg.desc))

	if level >= 2 and style == 'local':
		# print backup information
		print('Backup files:')
		if len(pkg.backup) == 0:
			print('(none)')
		else:
			print('\n'.join(["%s %s" % (md5, file) for (file, md5) in pkg.backup]))
	print('')

# vim: set ts=4 sw=4 noet:
