# Path: slicer_cli_web/docker_resource.py
# class DockerResource(Resource):
#     def __init__(self, name):
#     def getDockerImages(self, params):
#     def deleteImage(self, params):
#     def _deleteImage(self, names, deleteImage):
#     def _deleteDockerImages(self, removed):
#     def parseImageNameList(self, param):
#     def setImages(self, params):
#     def _createPutImageJob(self, nameList, baseFolder):
#     def storeEndpoints(self, imgName, cliName, undoFunction):
#     def deleteImageEndpoints(self, imageList=None):
#     def _generateAllItemEndPoints(self):
#     def addRestEndpoints(self, event):
#     def _dump(self, item, details=False):
#     def getItems(self, folder):
#     def getItem(self, item):
#     def deleteItem(self, item):
#     def getItemXML(self, item):
#     def getMatchingResource(self, name, path, type):
# 
# Path: slicer_cli_web/rest_slicer_cli.py
# def _getParamDefaultVal(param):
# def _canBeBatched(param):
# def _addInputParamToHandler(param, handlerDesc, required=True):
# def _addOutputParamToHandler(param, handlerDesc, required=True):
# def _addReturnParameterFileParamToHandler(handlerDesc):
# def batchCLIJob(cliItem, params, user, cliTitle):
# def batchCLITask(job):  # noqa C901
# def genHandlerToRunDockerCLI(cliItem):  # noqa C901
#     def getBatchParams(params):
#     def cliSubHandler(currentItem, params, user, token):
#     def cliHandler(resource, params):
# def genRESTEndPointsForSlicerCLIsForItem(restResource, cliItem, registerNamedRoute=False):
#         def undoFunction():
# 
# Path: slicer_cli_web/models/docker_image.py
# class CLIItem:
#     def __init__(self, item):
#         self.item = item
#         self._id = item['_id']
#         self.name = item['name']
#         meta = item['meta']
#         self.type = meta['type']
#         self.image = meta.get('image', 'UNKNOWN')
#         self.digest = meta.get('digest', self.image)
#         self.xml = meta['xml']
#         if isinstance(self.xml, str):
#             self.xml = self.xml.encode('utf8')
# 
#         self.restBasePath = self.image.replace(':', '_').replace('/', '_').replace('@', '_')
#         self.restPath = '%s/%s' % (self.restBasePath, self.name)
# 
#     @staticmethod
#     def find(itemId, user):
#         itemModel = Item()
#         item = itemModel.load(itemId, user=user, level=AccessType.READ)
#         if not item:
#             return None
#         return CLIItem(item)
# 
#     @staticmethod
#     def _findAllItemImpl(user=None, extraQuery=None):
#         itemModel = Item()
#         q = {'meta.slicerCLIType': 'task'}
# 
#         if extraQuery:
#             q.update(extraQuery)
# 
#         if user:
#             items = itemModel.findWithPermissions(q, user=user, level=AccessType.READ)
#         else:
#             items = itemModel.find(q)
# 
#         return [CLIItem(item) for item in items]
# 
#     @staticmethod
#     def findAllItems(user=None, baseFolder=None):
#         if not baseFolder:
#             return CLIItem._findAllItemImpl(user)
# 
#         folderModel = Folder()
#         graphLookup = {
#             'from': 'folder',
#             'startWith': '$_id',
#             'connectFromField': '_id',
#             'connectToField': 'parentId',
#             'as': 'children'
#         }
# 
#         if user and not user['admin']:
#             graphLookup['restrictSearchWithMatch'] = folderModel.permissionClauses(user,
#                                                                                    AccessType.READ)
# 
#         # see https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup
#         r = folderModel.collection.aggregate([
#             {
#                 '$match': dict(_id=baseFolder['_id'])
#             },
#             {
#                 '$graphLookup': graphLookup
#             },
#             {
#                 '$project': {
#                     'leaves': '$children._id',
#                 }
#             }
#         ])
# 
#         leaves = next(r)['leaves']
# 
#         return CLIItem._findAllItemImpl(user, {
#             'folderId': {'$in': [baseFolder['_id']] + leaves}
#         })


import functools
import json
import os
import pytest
import time
from pytest_girder.assertions import assertStatusOk, assertStatus
from girder.api import rest
from girder.models.collection import Collection
from girder.models.folder import Folder
from girder.models.item import Item
from slicer_cli_web import docker_resource
from slicer_cli_web import rest_slicer_cli
from slicer_cli_web.models import CLIItem
    colFolderB = Folder().createFolder(
        collection, 'folder B', parentType='collection',
        creator=admin)
    colFolderC = Folder().createFolder(
        colFolderA, 'folder C', creator=admin)
    Item().createItem('item A1', admin, colFolderA)
    Item().createItem('item B1', admin, colFolderB)
    Item().createItem('item B2', admin, colFolderB)
    Item().createItem('item C1', admin, colFolderC)
    Item().createItem('item C2', admin, colFolderC)
    Item().createItem('item C3', admin, colFolderC)
    Item().createItem('item Public 1', admin, publicFolder)
    Item().createItem('item Private 1', admin, privateFolder)
    folderD = Folder().createFolder(publicFolder, 'folder D', creator=admin)
    Item().createItem('item D1', admin, folderD)
    Item().createItem('item D2', admin, folderD)
    # Now test
    resp = server.request('/slicer_cli_web/path_match', params={
        'type': 'folder',
        'name': '^fold.*D',
    }, user=admin)
    assertStatusOk(resp)
    assert resp.json['name'] == 'folder D'

    resp = server.request('/slicer_cli_web/path_match', params={
        'type': 'item',
        'name': '^item',
        'path': '^/collection/.*/folder.*/folder',
    }, user=admin)
    assertStatusOk(resp)

# Path: slicer_cli_web/docker_resource.py
# class DockerResource(Resource):
#     def __init__(self, name):
#     def getDockerImages(self, params):
#     def deleteImage(self, params):
#     def _deleteImage(self, names, deleteImage):
#     def _deleteDockerImages(self, removed):
#     def parseImageNameList(self, param):
#     def setImages(self, params):
#     def _createPutImageJob(self, nameList, baseFolder):
#     def storeEndpoints(self, imgName, cliName, undoFunction):
#     def deleteImageEndpoints(self, imageList=None):
#     def _generateAllItemEndPoints(self):
#     def addRestEndpoints(self, event):
#     def _dump(self, item, details=False):
#     def getItems(self, folder):
#     def getItem(self, item):
#     def deleteItem(self, item):
#     def getItemXML(self, item):
#     def getMatchingResource(self, name, path, type):
# 
# Path: slicer_cli_web/rest_slicer_cli.py
# def _getParamDefaultVal(param):
# def _canBeBatched(param):
# def _addInputParamToHandler(param, handlerDesc, required=True):
# def _addOutputParamToHandler(param, handlerDesc, required=True):
# def _addReturnParameterFileParamToHandler(handlerDesc):
# def batchCLIJob(cliItem, params, user, cliTitle):
# def batchCLITask(job):  # noqa C901
# def genHandlerToRunDockerCLI(cliItem):  # noqa C901
#     def getBatchParams(params):
#     def cliSubHandler(currentItem, params, user, token):
#     def cliHandler(resource, params):
# def genRESTEndPointsForSlicerCLIsForItem(restResource, cliItem, registerNamedRoute=False):
#         def undoFunction():
# 
# Path: slicer_cli_web/models/docker_image.py
# class CLIItem:
#     def __init__(self, item):
#         self.item = item
#         self._id = item['_id']
#         self.name = item['name']
#         meta = item['meta']
#         self.type = meta['type']
#         self.image = meta.get('image', 'UNKNOWN')
#         self.digest = meta.get('digest', self.image)
#         self.xml = meta['xml']
#         if isinstance(self.xml, str):
#             self.xml = self.xml.encode('utf8')
# 
#         self.restBasePath = self.image.replace(':', '_').replace('/', '_').replace('@', '_')
#         self.restPath = '%s/%s' % (self.restBasePath, self.name)
# 
#     @staticmethod
#     def find(itemId, user):
#         itemModel = Item()
#         item = itemModel.load(itemId, user=user, level=AccessType.READ)
#         if not item:
#             return None
#         return CLIItem(item)
# 
#     @staticmethod
#     def _findAllItemImpl(user=None, extraQuery=None):
#         itemModel = Item()
#         q = {'meta.slicerCLIType': 'task'}
# 
#         if extraQuery:
#             q.update(extraQuery)
# 
#         if user:
#             items = itemModel.findWithPermissions(q, user=user, level=AccessType.READ)
#         else:
#             items = itemModel.find(q)
# 
#         return [CLIItem(item) for item in items]
# 
#     @staticmethod
#     def findAllItems(user=None, baseFolder=None):
#         if not baseFolder:
#             return CLIItem._findAllItemImpl(user)
# 
#         folderModel = Folder()
#         graphLookup = {
#             'from': 'folder',
#             'startWith': '$_id',
#             'connectFromField': '_id',
#             'connectToField': 'parentId',
#             'as': 'children'
#         }
# 
#         if user and not user['admin']:
#             graphLookup['restrictSearchWithMatch'] = folderModel.permissionClauses(user,
#                                                                                    AccessType.READ)
# 
#         # see https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup
#         r = folderModel.collection.aggregate([
#             {
#                 '$match': dict(_id=baseFolder['_id'])
#             },
#             {
#                 '$graphLookup': graphLookup
#             },
#             {
#                 '$project': {
#                     'leaves': '$children._id',
#                 }
#             }
#         ])
# 
#         leaves = next(r)['leaves']
# 
#         return CLIItem._findAllItemImpl(user, {
#             'folderId': {'$in': [baseFolder['_id']] + leaves}
#         })


# Path: tests/test_rest_slicer_cli.py
import functools
import json
import os
import pytest
import time
from pytest_girder.assertions import assertStatusOk, assertStatus
from girder.api import rest
from girder.models.collection import Collection
from girder.models.folder import Folder
from girder.models.item import Item
from slicer_cli_web import docker_resource
from slicer_cli_web import rest_slicer_cli
from slicer_cli_web.models import CLIItem

    colFolderB = Folder().createFolder(
        collection, 'folder B', parentType='collection',
        creator=admin)
    colFolderC = Folder().createFolder(
        colFolderA, 'folder C', creator=admin)
    Item().createItem('item A1', admin, colFolderA)
    Item().createItem('item B1', admin, colFolderB)
    Item().createItem('item B2', admin, colFolderB)
    Item().createItem('item C1', admin, colFolderC)
    Item().createItem('item C2', admin, colFolderC)
    Item().createItem('item C3', admin, colFolderC)
    Item().createItem('item Public 1', admin, publicFolder)
    Item().createItem('item Private 1', admin, privateFolder)
    folderD = Folder().createFolder(publicFolder, 'folder D', creator=admin)
    Item().createItem('item D1', admin, folderD)
    Item().createItem('item D2', admin, folderD)
    # Now test
    resp = server.request('/slicer_cli_web/path_match', params={
        'type': 'folder',
        'name': '^fold.*D',
    }, user=admin)
    assertStatusOk(resp)
    assert resp.json['name'] == 'folder D'

    resp = server.request('/slicer_cli_web/path_match', params={
        'type': 'item',
        'name': '^item',
        'path': '^/collection/.*/folder.*/folder',
    }, user=admin)
    assertStatusOk(resp)

    assert resp.json['name'] == 'item C3'