def search(path, restrictions): """ Search the given item path. @param path: Path of the item where the search should start. @type path: C{unicode} @param restrictions: The search restrictions. @type restrictions: C{unicode} @return: List of items paths matching the given query. @rtype: C{list} of C{unicode} @raise ItemSupportError: Indicates problems while parsing the restrictions or executing the search. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item cannot be found.") else: try: return [item.path for item in item.search(restrictions)] except CoreError, error: raise ItemSupportError( "Problems during search occurred.\nReason:'%s'" % error.message)
def performImport(sourcePath, targetParentPath, targetRepository, defaultProperties=None, copyData=True, ignoreLinks=False, determinePropertiesCallback=None): """ This method initiates the copy process and starts walking the source creating a new node in the destination tree for each item it passes. @param sourcePath: The item that should be imported. @type sourcePath: C{unicode} @param targetParentPath: The collection that should afterwards contain the copy. @type targetParentPath: C{unicode} @param targetRepository: The repository that should afterwards contain the copy. @type targetRepository: L{Repository<datafinder.script_api.repository.Repository>} @param defaultProperties: Optional properties which are set for every item. Default: C{None} @type defaultProperties: C{dict} of C{unicode},C{object} @param copyData: Flag indicating whether data of imported leafs is copy as well. Default: C{True} @type copyData: C{bool} @param ignoreLinks: Flag indicating the links are ignored during import. Default: C{False} @type ignoreLinks: C{bool} @param determinePropertiesCallback: Function determining properties used when importing a specific item. @type: determinePropertiesCallback: C{callable} using an item description as input and returns a dictionary describing the properties. @raise ItemSupportError: Raised when errors during the import occur. """ # pylint: disable=W0212 # W0212: We need to access the _repository attribute of the # repository description only for internal usage. cwr = repositoryManagerInstance.workingRepository try: sourceItem = cwr.getItem(sourcePath) targetParentItem = targetRepository._repository.getItem( targetParentPath) except ItemError: raise ItemSupportError("One of the items cannot be found.") else: mappedProperties = _mapProperties(dict(), defaultProperties, cwr) if not determinePropertiesCallback is None: determinePropertiesCallback = _createDeterminePropertiesCallback( determinePropertiesCallback, cwr) try: targetItemName = targetRepository.determineUniqueItemName( sourceItem.name, targetParentPath) targetRepository._repository.performImport( sourceItem, targetParentItem, targetItemName, mappedProperties, copyData, ignoreLinks, determinePropertiesCallback) except ItemError, error: errorMessage = "Problems during import of the following item:\n" errorMessage += "\n" + sourceItem.path + "\nReason: " + error.message raise ItemSupportError(errorMessage)
def getChildren(path): """ Determines the children of the given item. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Problem during retrieval of the item.") else: try: children = item.getChildren() except ItemError, error: errorMessage = "Cannot determine children.\nReason: '%s'" % error.message raise ItemSupportError(errorMessage) else:
def validate(properties, path=None): """ Validates the given properties. @param properties: Mapping of property identifiers to values. @type properties: C{dict} @param path: Optional item path which ensures that the validation is performed in the correct context. @type path: C{unicode} @raise PropertySupportError: Raised when a value does not conform to the defined property restrictions. """ reqPropDefs = dict() if not path is None: try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item '%s' cannot be found." % path) else: reqPropDefs = item.requiredPropertyDefinitions registry = _getPropertyDefinitionRegistry() for propId, value in properties.iteritems(): if propId in reqPropDefs: propDef = reqPropDefs[propId] else: propDef = registry.getPropertyDefinition(propId) try: propDef.validate(value) except PropertyError: raise PropertySupportError( "Value '%s' for property '%s' is not valid." \ % (str(value), propDef.displayName))
def createArchive(path, targetPath, defaultProperties=None): """ Archives the given path. """ cwr = repositoryManagerInstance.workingRepository try: item = cwr.getItem(path) targetItem = cwr.getItem(targetPath) except ItemError: raise ItemSupportError("One of the items has not been found.") else: try: mappedProperties = _mapProperties(dict(), defaultProperties, cwr) cwr.createArchive(item, targetItem, mappedProperties) except ItemError, error: errorMessage = "Cannot archive item.\nReason:'%s'" % error.message raise ItemSupportError(errorMessage)
def linkTargetPath(self): """ Returns the path of the link target. If it is no link None is returned. """ linkTargetPath = self.__item.linkTargetPath if self.isLink and linkTargetPath == None: raise ItemSupportError("Broken Link. Target does not exist.") return linkTargetPath
def dataUri(self): """ Returns the URI of the associated file object. """ try: return self.__item.dataUri except ItemError, error: raise ItemSupportError("%s" % error.message)
def determineUniqueItemName(self, proposedName, targetParentPath): """ Determines a unique name. """ try: targetParentItem = self._repository.getItem(targetParentPath) except ItemError: raise ItemSupportError("Item cannot be found.") else: return self._repository.determineUniqueItemName( proposedName, targetParentItem)
def delete(path): """ Deletes the item. @param path: Path to the item which has to be deleted. @type path: C{unicode} @raise ItemSupportError: Raised when the item could not be deleted. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item could not be found.") else: try: item.delete() except ItemError, error: raise ItemSupportError("Unable to delete item.\nReason: '%s'" % error.message)
def storeData(path, fileObject): """ Stores the data that has to be associated with this item. @param path: Path of the item where the data should be stored. @type path: C{unicode} @param fileObj: File-like object that can be read from. @raise ItemSupportError: Raised when an error occurred. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item cannot be found.") else: try: item.storeData(fileObject) except ItemError, error: raise ItemSupportError("Cannot write data.\nReason: '%s'" % error.message)
def storeProperties(path, properties): """ Adds/Updates the given properties of the item. @param path: The item whose properties should be updated. @type path: C{unicode} @param properties: Mapping of property identifiers to values. @type properties: C{dict} of C{unicode}, C{object} @raise ItemSupportError: Raised when difficulties with the item access occur. @raise PropertySupportError: Raised when values do not conform to the specified restrictions, values of system-specific properties are changed. """ cwr = repositoryManagerInstance.workingRepository try: item = cwr.getItem(path) except ItemError: raise ItemSupportError("Cannot find item '%s'." % path) else: mappedProperties = list() for propId, value in properties.iteritems(): if propId in item.properties: prop = item.properties[propId] prop.value = value else: prop = cwr.createProperty(propId, value) if (prop.propertyDefinition.category != const.MANAGED_SYSTEM_PROPERTY_CATEGORY and prop.propertyDefinition.category != const.UNMANAGED_SYSTEM_PROPERTY_CATEGORY): mappedProperties.append(prop) else: errorMessage = "You cannot change system-specific property values." raise PropertySupportError(errorMessage) try: item.updateProperties(mappedProperties) except ItemError, error: raise ItemSupportError("Cannot update properties.\nReason: '%s'" % str(error.args))
def retrieveData(path): """ Receives the data associated with this item. @param path: Path of the item form the data should be retrieved. @type path: C{unicode} @return: Readable file-like object. @raise ItemSupportError: Raised when the data cannot be accessed. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item cannot be found.") else: try: return item.retrieveData() except ItemError, error: raise ItemSupportError("Cannot read item data.\nReason: '%s'" % error.message)
def deleteProperties(path, propertyIdentifiers): """ Deletes the given properties from the item properties. @param path: The item where the properties should be deleted. @type path: C{unicode} @param propertyIdentifiers: List of property identifiers. @type propertyIdentifiers: C{list} of C{unicode} @raise ItemSupportError: Raised when difficulties with the item access occur. @raise PropertySupportError: Raised when system specific or data model specific properties should be removed. """ cwr = repositoryManagerInstance.workingRepository try: item = cwr.getItem(path) except ItemError: raise ItemSupportError("Cannot find item '%s'." % path) else: registry = _getPropertyDefinitionRegistry() propertiesForDeletion = list() for propId in propertyIdentifiers: if propId in item.requiredPropertyDefinitions: propDef = item.requiredPropertyDefinitions[propId] else: propDef = registry.getPropertyDefinition(propId) if propDef.category == const.USER_PROPERTY_CATEGORY: propertiesForDeletion.append(propId) else: raise PropertySupportError("Unable to delete property '%s' because it is not user-defined. " \ % propDef.displayName + \ "Only user-defined properties can be deleted." ) try: item.deleteProperties(propertiesForDeletion) except ItemError, error: raise ItemSupportError( "Cannot delete item properties.\nReason: '%s'" % str(error.args))
def _createItem(cwr, item, properties=None): """ Creates the given item object. @raise ItemSupportError: Raised when an error occurred. """ mappedProperties = _mapProperties(dict(), properties, cwr) try: item.create(mappedProperties) except ItemError, error: item.invalidate() raise ItemSupportError("Item cannot be created.\nReason: %s" % error.message)
def move(sourcePath, targetPath): """ Moves an item. @param sourcePath: Path of the source item. @type sourcePath: C{unicode} @param targetPath: Path of the target item representing the moved item. @type targetPath: C{unicode} @raise ItemSupportError: Raised when an item cannot be moved. """ try: item, targetItem = _getItemHelper(sourcePath, targetPath) except ItemError: raise ItemSupportError("One of the items cannot be found.") else: try: item.move(targetItem) except ItemError, error: targetItem.invalidate() raise ItemSupportError("Item cannot be moved.\nReason: '%s'" % error.message)
def walk(path): """ @param path: The item where the walk should start. @type path: C{unicode} @raise ItemSupportError: Raised when an error occurred. @see: L{walk<datafinder.core.item.visitor.base.ItemTreeWalkerBase.walk>} method to add further post-processing. """ cwr = repositoryManagerInstance.workingRepository try: item = cwr.getItem(path) except ItemError: raise ItemSupportError("The requested item cannot be found.") else: return [item.path for item in cwr.walk(item)]
def itemDescription(path): """ Returns the item description for the given item path. @param path: Path identifying the item. @type path: C{unicode} @return: Item description instance. @rtype: L{ItemDescription<datafinder.script_api.item.item_description.ItemDescription>} """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Problem during retrieval of the item.") else: return ItemDescription(item)
def refresh(path, stateOnly=False): """ Resets the state of the item so that its information is reloaded when accessed. @param path: The item to refresh. @type path: C{unicode} @param itemStateOnly: If set it indicates that only the item state is refreshed but no structural information. Default is C{False} @type stateOnly: C{bool} """ try: item = repositoryManagerInstance.workingRepository.getItem(path) item.refresh(stateOnly) except ItemError: raise ItemSupportError("Problem during refreshing.")
def createLeaf(path, properties=None): """ Creates a leaf. @param path: Path of the leaf which should be created. @type path: C{unicode} @param properties: Creation properties of the leaf. @type properties: C{dict} of C{unicode}, C{object} @raise ItemSupportError: Raised when the leaf could not be created. """ try: childName, parentItemPath = getChildParentPath(path) cwr = repositoryManagerInstance.workingRepository parentItem = cwr.getItem(parentItemPath) item = cwr.createLeaf(childName, parentItem) except ItemError, error: raise ItemSupportError("Leaf cannot be created.\nReason: '%s'" % error.message)
def createLink(path, linkTargetPath): """ Creates a link. @param path: Path of the link which should be created. @type path: C{unicode} @param linkTargetPath: Path of the item which is referenced by the link. @type linkTargetPath: C{unicode} @raise ItemSupportError: Raised when the link could not be created. """ try: childItemPath, parentItemPath = getChildParentPath(path) cwr = repositoryManagerInstance.workingRepository parentItem = cwr.getItem(parentItemPath) targetItem = cwr.getItem(linkTargetPath) item = cwr.createLink(childItemPath, targetItem, parentItem) except ItemError, error: raise ItemSupportError("Link cannot be created.\nReason: '%s'" % error.message)
def retrieveProperties(path): """ Retrieves the properties and maps them to the correct representation. @param path: path of the item whose properties should be retrieved. @type path: C{unicode} @return properties: Mapping of property identifiers to values. @rtype properties: C{dict} of C{unicode}, C{object} @raise ItemSupportError: Raised when problems during the property retrieval occur. """ try: item = repositoryManagerInstance.workingRepository.getItem(path) except ItemError: raise ItemSupportError("Item '%s' cannot be found." % path) else: properties = item.properties result = dict() for key, value in properties.iteritems(): result[key] = value.value return result