aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevan Franchini <twitch153@gentoo.org>2015-07-13 14:45:58 -0400
committerDevan Franchini <twitch153@gentoo.org>2015-07-13 14:45:58 -0400
commita072bcba3fd58d5d860d855061e593e429d15d30 (patch)
treec7df76e50f2ea3b5c0064bae872cdcafed801170
parentlayman.cfg: Adds json_db config option example to config (diff)
downloadlayman-a072bcba.tar.gz
layman-a072bcba.tar.bz2
layman-a072bcba.zip
json_db.py: Adds JSON databasing support to layman
-rw-r--r--layman/db_modules/json_db/__init__.py24
-rw-r--r--layman/db_modules/json_db/json_db.py120
2 files changed, 144 insertions, 0 deletions
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 <twitch153@gentoo.org>
+#
+'''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