<commit_before>#!/usr/bin/env python

"""Script to store configuration and interfaces into the directory and resource registry"""

__author__ = '<NAME>, <NAME>'

from copy import deepcopy
import os
from collections import OrderedDict

from pyon.core.path import list_files_recursive
from pyon.datastore.couchdb.couchdb_standalone import CouchDataStore
from pyon.ion.directory_standalone import DirectoryStandalone
from pyon.ion.resregistry_standalone import ResourceRegistryStandalone


class InterfaceAdmin:
    """
    Administrates work with interfaces in the datastore
    """

    DIR_RESFILES_PATH = "/Config"
    DIR_CONFIG_PATH = "/Config"

    def __init__(self, sysname, config=None):
        self.sysname = sysname
        self.config = config
        self.dir = DirectoryStandalone(sysname=self.sysname, config=config)
        self.rr = ResourceRegistryStandalone(sysname=self.sysname, config=config)
        self._closed = False

    def close(self):
        self.dir.close()
        self.dir = None
        self.rr.close()
        self.rr = None
        self._closed = True

    def __del__(self):
        if not self._closed:
            print "WARNING: Call close() on InterfaceAdmin (and datastores)"

    def create_core_datastores(self):
        """
        Main entry point into creating core datastores
        """
        ds = CouchDataStore(config=self.config, scope=self.sysname)
        datastores = ['resources','events']
        count = 0
        for local_dsn in datastores:
            if not ds.exists_datastore(local_dsn):
                ds.create_datastore(local_dsn)
                count += 1
                # NOTE: Views and other datastores are created by containers' DatastoreManager
        print "store_interfaces: Created %s datastores..." % count


    def store_config(self, system_cfg):
        """
        Main entry point into storing system config
        """
        de = self.dir.lookup(self.DIR_CONFIG_PATH + "/CFG")
        if de:
            #print "store_interfaces: WARN: Config already exists. Overwrite"
            return
        print "store_interfaces: Storing system config in directory..."
        self.dir.register(self.DIR_CONFIG_PATH, "CFG", **deepcopy(system_cfg))

    def store_interfaces(self, object_definition_file=None,
                         service_definition_file=None, idempotent=False):
        """
        Main entry point into storing interfaces
        """
        self.idempotent = idempotent
        self.bulk_entries = {}
        self.bulk_resources = []
        self.serial_num = 1

        if object_definition_file:
            self.store_object_interfaces(file=object_definition_file)<commit_msg>OOIION-1610: Create all datastores in store_interfaces<commit_after>#!/usr/bin/env python

"""Script to store configuration and interfaces into the directory and resource registry"""

__author__ = '<NAME>, <NAME>'

from copy import deepcopy
import os
from collections import OrderedDict

from pyon.core.path import list_files_recursive
from pyon.datastore.couchdb.couchdb_standalone import CouchDataStore
from pyon.ion.directory_standalone import DirectoryStandalone
from pyon.ion.resregistry_standalone import ResourceRegistryStandalone


class InterfaceAdmin:
    """
    Administrates work with interfaces in the datastore
    """

    DIR_RESFILES_PATH = "/Config"
    DIR_CONFIG_PATH = "/Config"

    def __init__(self, sysname, config=None):
        self.sysname = sysname
        self.config = config
        self.dir = DirectoryStandalone(sysname=self.sysname, config=config)
        self.rr = ResourceRegistryStandalone(sysname=self.sysname, config=config)
        self._closed = False

    def close(self):
        self.dir.close()
        self.dir = None
        self.rr.close()
        self.rr = None
        self._closed = True

    def __del__(self):
        if not self._closed:
            print "WARNING: Call close() on InterfaceAdmin (and datastores)"

    def create_core_datastores(self):
        """
        Main entry point into creating core datastores
        """
        ds = CouchDataStore(config=self.config, scope=self.sysname)
        datastores = ['resources', 'events', 'state', 'objects']
        ds_created = []
        count = 0
        for local_dsn in datastores:
            if not ds.exists_datastore(local_dsn):
                ds.create_datastore(local_dsn)
                count += 1
                ds_created.append(local_dsn)
                # NOTE: Views and other datastores are created by containers' DatastoreManager
        print "store_interfaces: Created %s datastores: %s" % (count, ds_created)


    def store_config(self, system_cfg):
        """
        Main entry point into storing system config
        """
        de = self.dir.lookup(self.DIR_CONFIG_PATH + "/CFG")
        if de:
            #print "store_interfaces: WARN: Config already exists. Overwrite"
            return
        print "store_interfaces: Storing system config in directory..."
        self.dir.register(self.DIR_CONFIG_PATH, "CFG", **deepcopy(system_cfg))

    def store_interfaces(self, object_definition_file=None,
                         service_definition_file=None, idempotent=False):
        """
        Main entry point into storing interfaces
        """
        self.idempotent = idempotent
        self.bulk_entries = {}
        self.bulk_resources = []
        self.serial_num = 1

        if object_definition_file:
            self.store_object_interfaces(file=object_definition_file)
{'fraction_non_alphanumeric': 0.08556925308194344, 'fraction_numerical': 0.0018129079042784628, 'mean_word_length': 2.6439894319682957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6520777', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1519, 'n_words': 391}