summaryrefslogtreecommitdiff
path: root/obsapi
diff options
context:
space:
mode:
authorScott Bahling <sbahling@suse.de>2016-03-03 02:19:13 +0100
committerScott Bahling <sbahling@suse.de>2016-03-03 02:19:13 +0100
commit3ea5bd07c23838b1a96357089e8173a094dfaed1 (patch)
tree0ee4b228a35a37c6d120e85cc2b7cc6ebacaa8fc /obsapi
parent224e2ce1d43045e567a1c782830a24432531aa8b (diff)
downloadobsapi-3ea5bd07c23838b1a96357089e8173a094dfaed1.tar.gz
obsapi-3ea5bd07c23838b1a96357089e8173a094dfaed1.tar.xz
obsapi-3ea5bd07c23838b1a96357089e8173a094dfaed1.zip
Add formatter class and extend FileInfo class
Diffstat (limited to 'obsapi')
-rw-r--r--obsapi/__init__.py1
-rw-r--r--obsapi/core.py45
-rw-r--r--obsapi/formatter.py65
-rw-r--r--obsapi/templates/FileInfo.j227
4 files changed, 114 insertions, 24 deletions
diff --git a/obsapi/__init__.py b/obsapi/__init__.py
index 52eabbf..9f56ad4 100644
--- a/obsapi/__init__.py
+++ b/obsapi/__init__.py
@@ -3,3 +3,4 @@ from core import *
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions
+
diff --git a/obsapi/core.py b/obsapi/core.py
index 284199c..0148796 100644
--- a/obsapi/core.py
+++ b/obsapi/core.py
@@ -5,6 +5,9 @@ from requests.auth import HTTPBasicAuth
from io import BytesIO
from lxml import etree
from collections import namedtuple
+from datetime import datetime
+from obsapi.formatter import Formatter
+
try:
import osc.conf as osc_conf
osc_conf.get_config()
@@ -41,6 +44,8 @@ class FileInfo(object):
"Summary : {summary}"
"Description :"
"{description}"
+ "Provides :"
+ "{provides}"
)
src_str_template = ("{filename}"
@@ -58,36 +63,23 @@ class FileInfo(object):
def __init__(self, xml):
self.finfo = etree.fromstring(xml)
self.filename = self.finfo.get('filename', None)
+ self.formatter = Formatter(self)
@property
def info(self):
- if self.is_pkg:
- return dict(filename=self.filename,
- name=self.name,
- version=self.version,
- release=self.release,
- arch=self.arch,
- size=self.size,
- source=self.source,
- mtime=self.mtime,
- summary=self.summary,
- description=self.description,
- )
+ info = {'filename': [self.filename]}
+ for item in self.finfo:
+ info.setdefault(item.tag, []).append(item.text)
- else:
- return dict(filename=self.filename,
- size=self.size,
- mtime=self.mtime,
- )
+ for item, value in info.items():
+ if len(value) == 1:
+ info[item] = value[0]
+ # info[item] = '\n'.join(value)
- def __str__(self):
- if self.is_src:
- return self.src_str_template.format(**self.info)
+ return info
- if self.is_pkg:
- return self.pkg_str_template.format(**self.info)
-
- return self.file_str_template.format(**self.info)
+ def __str__(self):
+ return self.formatter.render()
def __getattr__(self, attr):
try:
@@ -101,6 +93,11 @@ class FileInfo(object):
return values
@property
+ def datetime(self):
+ if self.mtime:
+ return datetime.fromtimestamp(int(self.mtime))
+
+ @property
def xml(self):
return etree.tostring(self.finfo)
diff --git a/obsapi/formatter.py b/obsapi/formatter.py
new file mode 100644
index 0000000..fd5c70f
--- /dev/null
+++ b/obsapi/formatter.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+"""
+ obsapi: formatter module
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+
+ This module contains the FateFormatter class
+
+
+ :copyright: Copyright (c) 2016 Scott Bahling, SUSE Linux GmbH
+ :license: GPL-2.0, see COPYING for details
+"""
+import os
+from jinja2 import Environment, FileSystemLoader, TemplateNotFound, Template
+
+pkg_path = os.path.dirname(__file__)
+
+class Formatter(object):
+ """Formatter
+ Renders a string representation of an object based on Jinja2
+ templating.
+
+ :param obj: Object to be formatted
+ :param searchpath: Directory or list of directories to
+ search for templates
+
+ A template is selected from the searchpath based on the name
+ of the object class with a '.j2' extension. If no template
+ is found matching the class name, the default template
+ (default.j2) will be used.
+ The render() method will create a text output representing
+ the object based on the selected template.
+ """
+ default_searchpath = [os.path.expanduser('~/.obsapi/templates'),
+ os.path.join(pkg_path, 'templates'),
+ ]
+
+ def __init__(self, obj, searchpath=None):
+ searchpath = searchpath or self.default_searchpath
+ self.env = Environment(loader=FileSystemLoader(searchpath))
+ self.obj = obj
+
+ @property
+ def template_name(self):
+ return "%s.j2" % self.obj.__class__.__name__
+
+ @property
+ def searchpath(self):
+ return self.env.loader.searchpath
+
+ @searchpath.setter
+ def searchpath(self, path):
+ self.env.loader.searchpath = path
+
+ @property
+ def template(self):
+ try:
+ return self.env.get_template(self.template_name)
+ except TemplateNotFound:
+ try:
+ return self.env.get_template('default.j2')
+ except:
+ return Template('{{this.__class__.__name__}}')
+
+ def render(self):
+ return self.template.render(this=self.obj)
diff --git a/obsapi/templates/FileInfo.j2 b/obsapi/templates/FileInfo.j2
new file mode 100644
index 0000000..af77bcc
--- /dev/null
+++ b/obsapi/templates/FileInfo.j2
@@ -0,0 +1,27 @@
+{{this.filename}}
+{{'-' * this.filename|length}}
+{%- if this.is_pkg %}
+Name : {{this.name}}
+Version : {{this.version}}
+Release : {{this.release}}
+Architecture: {{this.arch}}
+Size : {{this.size}}
+{%- if this.source %}
+Source RPM : {{this.source}}
+{%- endif %}
+Build Date : {{this.datetime.strftime('%Y-%m-%dT%H:%M')}}
+Summary : {{this.summary}}
+Description :
+{{this.description}}
+Provides :
+{%- for provides in this.provides %}
+ {{provides}}
+{%- endfor %}
+Requires :
+{%- for requires in this.requires %}
+ {{requires}}
+{%- endfor %}
+{%- else %}
+Size : {{this.size}}
+Modified : {{this.mtime}}
+{%- endif %}