* [gentoo-commits] proj/layman:master commit in: layman/db_modules/json_db/
@ 2015-07-13 18:48 Devan Franchini
0 siblings, 0 replies; 2+ messages in thread
From: Devan Franchini @ 2015-07-13 18:48 UTC (permalink / raw
To: gentoo-commits
commit: a072bcba3fd58d5d860d855061e593e429d15d30
Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org>
AuthorDate: Mon Jul 13 18:45:58 2015 +0000
Commit: Devan Franchini <twitch153 <AT> gentoo <DOT> 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 <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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [gentoo-commits] proj/layman:master commit in: layman/db_modules/json_db/
@ 2015-07-13 18:48 Devan Franchini
0 siblings, 0 replies; 2+ messages in thread
From: Devan Franchini @ 2015-07-13 18:48 UTC (permalink / raw
To: gentoo-commits
commit: 547b6f70d35c00f1e6387ebdbdb4eead46b5af41
Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org>
AuthorDate: Mon Jul 13 18:49:04 2015 +0000
Commit: Devan Franchini <twitch153 <AT> gentoo <DOT> org>
CommitDate: Mon Jul 13 18:49:04 2015 +0000
URL: https://gitweb.gentoo.org/proj/layman.git/commit/?id=547b6f70
json_db/__init__.py: Modifies function descriptions
layman/db_modules/json_db/__init__.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/layman/db_modules/json_db/__init__.py b/layman/db_modules/json_db/__init__.py
index e095f4f..1502b9d 100644
--- a/layman/db_modules/json_db/__init__.py
+++ b/layman/db_modules/json_db/__init__.py
@@ -15,9 +15,9 @@ module_spec = {
'description': __doc__,
'functions': ['add_new', 'read_db', 'write'],
'func_desc': {
- 'add_new': 'Adds overlay from provided document text',
+ 'add_new': 'Adds overlay(s) from provided database text',
'read_db': 'Reads the list of overlays from database file',
- 'write': 'Writes the list of overlays to database file',
+ 'write' : 'Writes the list of overlays to database file',
},
}
}
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-07-13 18:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-13 18:48 [gentoo-commits] proj/layman:master commit in: layman/db_modules/json_db/ Devan Franchini
-- strict thread matches above, loose matches on Subject: below --
2015-07-13 18:48 Devan Franchini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox