From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 0AAAE138D18 for ; Mon, 13 Jul 2015 18:48:47 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9673AE094A; Mon, 13 Jul 2015 18:48:45 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 740D8E0949 for ; Mon, 13 Jul 2015 18:48:44 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6ECE43409FE for ; Mon, 13 Jul 2015 18:48:43 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D8B6A905 for ; Mon, 13 Jul 2015 18:48:41 +0000 (UTC) From: "Devan Franchini" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Devan Franchini" Message-ID: <1436813158.a072bcba3fd58d5d860d855061e593e429d15d30.twitch153@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: layman/db_modules/json_db/ X-VCS-Repository: proj/layman X-VCS-Files: layman/db_modules/json_db/__init__.py layman/db_modules/json_db/json_db.py X-VCS-Directories: layman/db_modules/json_db/ X-VCS-Committer: twitch153 X-VCS-Committer-Name: Devan Franchini X-VCS-Revision: a072bcba3fd58d5d860d855061e593e429d15d30 X-VCS-Branch: master Date: Mon, 13 Jul 2015 18:48:41 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 793a7c98-bf64-4abf-a709-c7ff06f9e8f5 X-Archives-Hash: 548e2b63b1123cb83952dcbdff39321d commit: a072bcba3fd58d5d860d855061e593e429d15d30 Author: Devan Franchini gentoo org> AuthorDate: Mon Jul 13 18:45:58 2015 +0000 Commit: Devan Franchini gentoo org> CommitDate: Mon Jul 13 18:45:58 2015 +0000 URL: https://gitweb.gentoo.org/proj/layman.git/commit/?id=a072bcba json_db.py: Adds JSON databasing support to layman layman/db_modules/json_db/__init__.py | 24 +++++++ layman/db_modules/json_db/json_db.py | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/layman/db_modules/json_db/__init__.py b/layman/db_modules/json_db/__init__.py new file mode 100644 index 0000000..e095f4f --- /dev/null +++ b/layman/db_modules/json_db/__init__.py @@ -0,0 +1,24 @@ +# Copyright 2015 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +''' +JSON database plug-in module for layman. +''' + +module_spec = { + 'name': 'json_db', + 'description': __doc__, + 'provides':{ + 'json-module': { + 'name': 'json_db', + 'class': 'DBHandler', + 'description': __doc__, + 'functions': ['add_new', 'read_db', 'write'], + 'func_desc': { + 'add_new': 'Adds overlay from provided document text', + 'read_db': 'Reads the list of overlays from database file', + 'write': 'Writes the list of overlays to database file', + }, + } + } +} diff --git a/layman/db_modules/json_db/json_db.py b/layman/db_modules/json_db/json_db.py new file mode 100644 index 0000000..47413c6 --- /dev/null +++ b/layman/db_modules/json_db/json_db.py @@ -0,0 +1,120 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +################################################################################# +# LAYMAN JSON DB +################################################################################# +# File: json_db.py +# +# Access JSON overlay database(s). +# +# Copyright: +# (c) 2015 Devan Franchini +# Distributed under the terms of the GNU General Public License v2 +# +# Author(s): +# Devan Franchini +# +'''Handler for json overlay databases.''' + +from __future__ import unicode_literals + +__version__ = "$Id: json.py 273 2015-07-10 10:10:49Z twitch153 $" + +#=============================================================================== +# +# Dependencies +# +#------------------------------------------------------------------------------- + +import json +import sys + +from layman.compatibility import fileopen +from layman.overlays.overlay import Overlay + + +#py3.2+ +if sys.hexversion >= 0x30200f0: + _UNICODE = 'unicode' +else: + _UNICODE = 'UTF-8' + + + +#=============================================================================== +# +# Class DBHandler +# +#------------------------------------------------------------------------------- + +class DBHandler(object): + ''' + Handle a json overlay database. + ''' + + def __init__(self, config, overlays, paths=None, ignore=0, + ignore_init_read_errors=False): + + self.config = config + self.ignore = ignore + self.overlays = overlays + self.paths = paths + self.output = config['output'] + self.ignore_init_read_errors = ignore_init_read_errors + + self.output.debug('Initializing JSON overlay list handler', 8) + + + def read_db(self, path, text=None): + ''' + Read the overlay definition file. + ''' + document = text + + if not document: + try: + with fileopen(path, 'r') as df: + document = df.read() + except Exception as error: + if not self.ignore_init_read_errors: + msg = 'JSON DBHandler - Failed to read the overlay list at'\ + '("%(path)s")' % {'path': path} + self.output.error(msg) + raise error + + self.add_new(document, origin=path) + + + def add_new(self, document=None, origin=None): + ''' + Reads in provided json text and generates overlays to populate database. + ''' + if not document: + msg = 'JSON DBHandler - add_new() failed: JSON text cannot be none'\ + '.\nOrigin: %(path)s' % {'path': origin} + self.output.warn(msg) + return False + + load = json.loads(document)['repo'] + + for ovl in load: + overlay = Overlay(self.config, json=ovl, ignore=self.ignore) + self.overlays[overlay.name] = overlay + + return True + + + def write(self, path): + ''' + Write the list of overlays to a file. + ''' + try: + repo = {'@encoding': 'unicode', '@version': '1.0', 'repo': []} + repo['repo'] = [self.overlays[key].to_json() for key in self.overlays] + with fileopen(path, 'w') as df: + df.write(json.dumps(repo, sort_keys=True, indent=2)) + except Exception as err: + msg = 'Failed to write to local overlays file: %(path)s\nError was'\ + ': %(err)s' % {'path': path, 'err': err} + self.output.error(msg) + raise err