"""
This module allows runtime creation and removal of items. It will also remove
any links from an Item before it is removed. This module requires the
JythonItemProvder and JythonItemChannelLinkProvider component scripts.
"""

from core.jsr223 import scope
scope.scriptExtension.importPreset(None)

import core
from core import osgi
from core.log import logging, LOG_PREFIX
from core.links import remove_all_links

ItemBuilderFactory = osgi.get_service(
        "org.openhab.core.items.ItemBuilderFactory"
    ) or osgi.get_service(
        "org.eclipse.smarthome.core.items.ItemBuilderFactory"
    )

ManagedItemProvider = osgi.get_service(
        "org.openhab.core.items.ManagedItemProvider"
    ) or osgi.get_service(
        "org.eclipse.smarthome.core.items.ManagedItemProvider"
    )

log = logging.getLogger("{}.core.items".format(LOG_PREFIX))

__all__ = ["add_item", "remove_item"]

def add_item(item_or_item_name, item_type=None, category=None, groups=None, label=None, tags=[], gi_base_type=None, group_function=None):
    try:
        if not isinstance(item_or_item_name, basestring) and not hasattr(item_or_item_name, 'name'):
            raise Exception("\"{}\" is not a string or Item".format(item_or_item_name))
        item = item_or_item_name
        if isinstance(item_or_item_name, basestring):
            item_name = item_or_item_name
            if item_type is None:
                raise Exception("Must provide item_type when creating an Item by name")

            base_item = None if item_type != "Group" or gi_base_type is None else ItemBuilderFactory.newItemBuilder(gi_base_type, item_name + "_baseItem").build()
            group_function = None if item_type != "Group" else group_function
            item = ItemBuilderFactory.newItemBuilder(item_type, item_name)\
                                                    .withCategory(category)\
                                                    .withGroups(groups)\
                                                    .withLabel(label)\
                                                    .withBaseItem(base_item)\
                                                    .withGroupFunction(group_function)\
                                                    .withTags(set(tags))\
                                                    .build()

        ManagedItemProvider.add(item)
        log.debug("Item added: [{}]".format(item))
        return item
    except:
        import traceback
        log.error(traceback.format_exc())
        return None

def remove_item(item_or_item_name):
    try:
        item = remove_all_links(item_or_item_name)
        if item is None:
            log.warn("Item cannot be removed because it does not exist in the ItemRegistry: [{}]".format(item_or_item_name))
            return None

        ManagedItemProvider.remove(item.name)
        if scope.itemRegistry.getItems(item.name) == []:
            log.debug("Item removed: [{}]".format(item.name))
            return item
        else:
            log.warn("Failed to remove Item from the ItemRegistry: [{}]".format(item.name))
            return None
    except:
        import traceback
        log.error(traceback.format_exc())
        return None

@@ -123,108 +123,50 @@
 ved.
- This module requires the%0AJythonItemProvder and JythonItemChannelLinkProvider component scripts.%0A%22%22%22
+%0A%22%22%22%0A__all__ = %5B%22add_item%22, %22remove_item%22%5D
 %0A%0Afr
@@ -179,16 +179,22 @@
 e.jsr223
+.scope
  import 
@@ -199,18 +199,36 @@
 t sc
-ope%0Ascope.
+riptExtension, itemRegistry%0A
 scri
@@ -817,47 +817,8 @@
 ))%0A%0A
-__all__ = %5B%22add_item%22, %22remove_item%22%5D%0A%0A
 def 
@@ -947,24 +947,957 @@
 tion=None):%0A
+    %22%22%22%0A    Adds an Item using a ManagedItemProvider.%0A%0A    Args:%0A        item_or_item_name (Item or str): Item object or name for the Item to%0A            create%0A        item_type (str): (optional, if item_oritem_name is an Item) the type%0A            of the Item%0A        category (str): (optional) the category (icon) for the Item%0A        groups (str): (optional) a list of groups the Item is a member of%0A        label (str): (optional) the label for the Item%0A        tags (list): (optional) a list of tags for the Item%0A        gi_base_type (str): (optional) the group Item base type for the Item%0A        group_function (GroupFunction): (optional) the group function used by%0A            the Item%0A%0A    Returns:%0A        Item or None: The Item that was created or None%0A%0A    Raises:%0A        TypeError: if item_or_item_name is not an Item or string, or if%0A            item_or_item_name is not an Item and item_type is not provided%0A    %22%22%22%0A
     try:%0A   
@@ -2012,25 +2012,25 @@
   raise 
-Exception
+TypeError
 (%22%5C%22%7B%7D%5C%22
@@ -2271,17 +2271,17 @@
 ise 
-Exception
+TypeError
 (%22Mu
@@ -3415,24 +3415,284 @@
 item_name):%0A
+    %22%22%22%0A    This function removes an Item using a ManagedItemProvider.%0A%0A    Args:%0A        item_or_item_name (Item or str): the Item object or name for the%0A            Item to create%0A%0A    Returns:%0A        Item or None: the Item that was removed or None%0A    %22%22%22%0A
     try:%0A   
@@ -3975,14 +3975,8 @@
  if 
-scope.
 item

Change jsr223.scope import and update docs (#246)