'''
vim: set expandtab sw=4 ts=4:

Interface to Keen IO.

Copyright (C) 2014 Dieter Adriaenssens <ruleant@users.sourceforge.net>

This file is part of buildtime-trend
<https://github.com/ruleant/buildtime-trend/>

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 3 of the License, or
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, see <http://www.gnu.org/licenses/>.
'''

import os


def keen_io_writable():
    '''
    Check if login keys for Keen IO API are set, to allow writing.
    '''
    if "KEEN_PROJECT_ID" in os.environ and "KEEN_WRITE_KEY" in os.environ:
        return True
    return False


def keen_io_readable():
    '''
    Check if login keys for Keen IO API are set, to allow reading.
    '''
    if "KEEN_PROJECT_ID" in os.environ and "KEEN_READ_KEY" in os.environ:
        return True
    return False

@@ -815,16 +815,45 @@
 port os%0A
+from keen import scoped_keys%0A
 %0A%0Adef ke
@@ -1265,28 +1265,629 @@
 eturn True%0A    return False%0A
+%0A%0Adef keen_io_generate_read_key(repo):%0A    '''%0A    Create scoped key for reading only the build-stages related data.%0A    Param repo : github repository slug (fe. ruleant/buildtime-trend)%0A    '''%0A    if %22KEEN_MASTER_KEY%22 in os.environ:%0A        master_key = os.getenv(%22KEEN_MASTER_KEY%22)%0A        privileges = %7B%0A            %22filters%22: %5B%7B%0A                %22property_name%22: %22build.repo%22,%0A                %22operator%22: %22eq%22,%0A                %22property_value%22: repo%0A            %7D%5D,%0A            %22allowed_operations%22: %5B %22read%22 %5D%0A        %7D%0A%0A        return scoped_keys.encrypt(master_key, privileges)%0A    return None%0A

add function to generate read key, issue #2