<commit_before>//
//  BartlebyDocument+SecurityScopes.swift
//  BartlebyKit
//
//  Created by <NAME> on 05/05/2016.
//
//

import Foundation

#if os(OSX)
    import AppKit
#else
    import UIKit
#endif


public enum SecurityScopedBookMarkError: Error {
    // Bookmarking
    case bookMarkFailed(message:String)
    // Scoped URL
    case getScopedURLRessourceFailed(message:String)
    case bookMarkIsStale
}


extension BartlebyDocument {

    // MARK: - API Security-Scoped Bookmarks Support

    // Simplified persistent Security Scoped Bookmarks
    // This extension allows to acquire , store and release securized URLs
    //
    // Usage :
    //
    // 1. Call acquireSecurizedURLFrom(...) - The first call must be consecutive to an explicit user Intent.
    // 2. Call releaseSecurizedUrl(...) when you want to release the resource
    //
    // Multiple consecutive call to acquireSecurizedURLFrom(..) counts for one call.
    //
    // If you need more information refer to:
    // https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16
    //


    ///
    ///Returns and acquires securized URL
    // If the Securiry scoped Bookmark does not exist, it creates one.
    ///
    /// **IMPORTANT**
    /// 1. You must call this method after a user explicit intent (NSOpenPanel ...)
    ///    You cannot get security scoped bookmark for an arbritrary URL.
    /// 2. Don't forget : Each call should be balanced with a `releaseSecurizedUrl(..) ` call
    ///
    /// - Parameters:
    ///   - url: the url to be accessed
    ///   - appScoped: is it an app scoped Bookmark?
    /// - Returns: the Securized URL
    /// - Throws: errors
    public func acquireSecurizedURLFrom(originalURL: URL, appScoped: Bool=false) throws ->URL {
        if !self._securityScopedBookmarkExits(originalURL, appScoped: false) {
            return try self._bookmarkURL(originalURL, appScoped: false).securizedURL
        } else {
            // The bookmark data exists
            let key=self._getBookMarkKeyFor(originalURL, appScoped: appScoped)
            if let securizedURL=self._activeSecurityBookmarks[key]{
                // The Url has already been acquired
                return securizedURL
            }else{
                if let idx=self.metadata.URLBookmarkData.index(where:{$0.key == key}){
                    //  Acquire the securized URL
                    let data=self.metadata.URLBookmarkData[idx].data
                    let securizedURL = try self._getSecurityScopedURLFrom(data)
                    self._startAccessingToSecurityScopedResourceAtURL(key,securizedURL)
                    return securizedURL
                }else{
                    throw SecurityScopedBookMarkError.getScopedURLRessourceFailed(message: "Void data when attempting to acquire securized URL")
                }
            }
        }
    }


    /// Release the access to the sandboxed securized URL
    /// Each acquireSecurizedURLFrom(...) must be balanced by releaseSecurizedUrl(...)
    ///
    /// - Parameters:
    ///   - originalURL: the original URL
    ///   - appScoped: is it an app scoped Bookmark?
    public func releaseSecurizedUrl(originalURL:URL,appScoped: Bool=false){
        if self._securityScopedBookmarkExits(originalURL,appScoped:appScoped ){
            let key=self._getBookMarkKeyFor(originalURL, appScoped: appScoped)
            self._stopAccessingToResourceIdentifiedBy(key)
        }else{
            self.log("Unable to release Bookmark for \(originalURL) appScoped: \(appScoped)", file: #file, function: #function, line: #line, category: Default.LOG_DEVELOPER_CATEGORY, decorative: false)
        }
    }
<commit_msg>Reordering<commit_after>//
//  BartlebyDocument+SecurityScopes.swift
//  BartlebyKit
//
//  Created by <NAME> on 05/05/2016.
//
//

import Foundation

#if os(OSX)
    import AppKit
#else
    import UIKit
#endif


public enum SecurityScopedBookMarkError: Error {
    // Bookmarking
    case bookMarkFailed(message:String)
    // Scoped URL
    case getScopedURLRessourceFailed(message:String)
    case bookMarkIsStale
}


extension BartlebyDocument {

    // MARK: - API Security-Scoped Bookmarks Support

    // Simplified persistent Security Scoped Bookmarks
    // This extension allows to acquire , store and release securized URLs
    //
    // Usage :
    //
    // 1. Call acquireSecurizedURLFrom(...) - The first call must be consecutive to an explicit user Intent.
    // 2. Call releaseSecurizedUrl(...) when you want to release the resource
    //
    // Multiple consecutive call to acquireSecurizedURLFrom(..) counts for one call.
    //
    // If you need more information refer to:
    // https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16
    //


    ///
    ///Returns and acquires securized URL
    // If the Securiry scoped Bookmark does not exist, it creates one.
    ///
    /// **IMPORTANT**
    /// 1. You must call this method after a user explicit intent (NSOpenPanel ...)
    ///    You cannot get security scoped bookmark for an arbritrary URL.
    /// 2. Don't forget : Each call should be balanced with a `releaseSecurizedUrl(..) ` call
    ///
    /// - Parameters:
    ///   - url: the url to be accessed
    ///   - appScoped: is it an app scoped Bookmark?
    /// - Returns: the Securized URL
    /// - Throws: errors
    public func acquireSecurizedURLFrom(originalURL: URL, appScoped: Bool=false) throws ->URL {
        if self._securityScopedBookmarkExits(originalURL, appScoped: false) {
            // The bookmark data exists
            let key=self._getBookMarkKeyFor(originalURL, appScoped: appScoped)
            if let securizedURL=self._activeSecurityBookmarks[key]{
                // The Url has already been acquired
                return securizedURL
            }else{
                if let idx=self.metadata.URLBookmarkData.index(where:{$0.key == key}){
                    //  Acquire the securized URL
                    let data=self.metadata.URLBookmarkData[idx].data
                    let securizedURL = try self._getSecurityScopedURLFrom(data)
                    self._startAccessingToSecurityScopedResourceAtURL(key,securizedURL)
                    return securizedURL
                }else{
                    throw SecurityScopedBookMarkError.getScopedURLRessourceFailed(message: "Void data when attempting to acquire securized URL")
                }
            }
        }else{
            // try to create and acquire
            let r = try self._bookmarkURL(originalURL, appScoped: false)
            // Return the securized url
            return r.securizedURL
        }
    }


    /// Release the access to the sandboxed securized URL
    /// Each acquireSecurizedURLFrom(...) must be balanced by releaseSecurizedUrl(...)
    ///
    /// - Parameters:
    ///   - originalURL: the original URL
    ///   - appScoped: is it an app scoped Bookmark?
    public func releaseSecurizedUrl(originalURL:URL,appScoped: Bool=false){
        if self._securityScopedBookmarkExits(originalURL,appScoped:appScoped ){
            let key=self._getBookMarkKeyFor(originalURL, appScoped: appScoped)
            self._stopAccessingToResourceIdentifiedBy(key)
        }else{
            self.log("Unable to release Bookmark for \(originalURL) appScoped: \(appScoped)", file: #file, function: #function, line: #line, category: Default.LOG_DEVELOPER_CATEGORY, decorative: false)
        }
    }

{'fraction_non_alphanumeric': 0.09505157331244288, 'fraction_numerical': 0.00626713670191931, 'mean_word_length': 2.3304347826086955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1115682', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2109, 'n_words': 592}