<commit_before>//
//  DocumentMetadata+Sugar.swift
//  BartlebyKit
//
//  Created by <NAME> on 03/01/2017.
//
//

import Foundation

public enum SugarError:Error{
    case thereIsNoSugarInYouBowl
    case salted
}

public extension DocumentMetadata{


    /// Sugar one is Related to the master document (the document that creates sub-documents)
    /// == Part of The first 512 bytes of the sugar
    ///
    public var firstPieceOfSugar:String {
        get{
            return PString.substr(self.sugar, 0, 512)
        }
    }


    /// Loads the sugar String and save it to self.sugar
    public func loadSugar()throws{
        // We gonna try to load
        if let data=FileManager.default.contents(atPath: self._bowlPath+"/"+self.persistentUID){
            if let cryptedSugar = String.init(data: data, encoding: String.Encoding.utf8){
                self.sugar = try Bartleby.cryptoDelegate.decryptString(cryptedSugar, useKey: Bartleby.configuration.KEY)
            }else{
                throw SugarError.salted
            }
        }else{
            throw SugarError.thereIsNoSugarInYouBowl
        }
    }


    /// Cooks a good pie
    ///
    /// - Parameter superSugar: the super sugar is equal to firstPieceOfSugar
    public func cookThePie(superSugar:String="")throws{
        let _ = try FileManager.default.createDirectory(atPath: self._bowlPath, withIntermediateDirectories: true)
        if self.sugar == Default.VOID_STRING {
            do{
                try loadSugar()
            }catch{
                // Sugar not found or too salted
                // Let's generate a new one
                self.sugar=self._sweeten(superSugar)
            }
        }
        let cryptedSugar = try Bartleby.cryptoDelegate.encryptString(self.sugar, useKey: Bartleby.configuration.KEY)
        try cryptedSugar.write(toFile: self._bowlPath+"/"+self.persistentUID, atomically: true, encoding: String.Encoding.utf8)
    }



    /// Sugar pump
    ///
    /// - Parameter superSugar: the super sugar
    /// - Returns: return the sugar
    fileprivate func _sweeten(_ superSugar:String="")->String{
        // Sugar not found or too salted
        // Let's generate a new one
        var sweet=""
        /// Do we have a super sugar?
        if sweet.characters.count >= 512{
            sweet=PString.substr(superSugar, 0, 512)+Bartleby.randomStringWithLength(512)
        }else{
            sweet=Bartleby.randomStringWithLength(1024)
        }
        return sweet
    }


    /// Tries to put the sugar in the Bowl
    public func putSomeSugarInYourBowl() throws{
        let cryptedSugar = try Bartleby.cryptoDelegate.encryptString(self.sugar, useKey: Bartleby.configuration.KEY)
        try cryptedSugar.write(toFile: self._bowlPath+"/"+self.persistentUID, atomically: true, encoding: String.Encoding.utf8)
    }


    // The sugar path
    fileprivate var _bowlPath:String{
        get{
            if self.appGroup != ""{
                if let url=FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: self.appGroup){
                    return url.path+"/bowl"
                }
            }
            return Bartleby.getSearchPath(.documentDirectory)!+"/bowl"
        }
    }
}

<commit_msg>Defensive Bowl regeneration<commit_after>//
//  DocumentMetadata+Sugar.swift
//  BartlebyKit
//
//  Created by <NAME> on 03/01/2017.
//
//

import Foundation

public enum SugarError:Error{
    case thereIsNoSugarInYouBowl
    case salted
}

public extension DocumentMetadata{


    /// Sugar one is Related to the master document (the document that creates sub-documents)
    /// == Part of The first 512 bytes of the sugar
    ///
    public var firstPieceOfSugar:String {
        get{
            return PString.substr(self.sugar, 0, 512)
        }
    }


    /// Loads the sugar String and save it to self.sugar
    public func loadSugar()throws{
        // We gonna try to load
        if let data=FileManager.default.contents(atPath: self._bowlPath+"/"+self.persistentUID){
            if let cryptedSugar = String.init(data: data, encoding: String.Encoding.utf8){
                self.sugar = try Bartleby.cryptoDelegate.decryptString(cryptedSugar, useKey: Bartleby.configuration.KEY)
            }else{
                throw SugarError.salted
            }
        }else{
            throw SugarError.thereIsNoSugarInYouBowl
        }
    }


    /// Cooks a good pie
    ///
    /// - Parameter superSugar: the super sugar is equal to firstPieceOfSugar
    public func cookThePie(superSugar:String="")throws{
        let _ = try FileManager.default.createDirectory(atPath: self._bowlPath, withIntermediateDirectories: true)
        if self.sugar == Default.VOID_STRING {
            do{
                try loadSugar()
            }catch{
                // Sugar not found or too salted
                // Let's generate a new one
                self.sugar=self._sweeten(superSugar)
            }
        }
        let cryptedSugar = try Bartleby.cryptoDelegate.encryptString(self.sugar, useKey: Bartleby.configuration.KEY)
        try cryptedSugar.write(toFile: self._bowlPath+"/"+self.persistentUID, atomically: true, encoding: String.Encoding.utf8)
    }



    /// Sugar pump
    ///
    /// - Parameter superSugar: the super sugar
    /// - Returns: return the sugar
    fileprivate func _sweeten(_ superSugar:String="")->String{
        // Sugar not found or too salted
        // Let's generate a new one
        var sweet=""
        /// Do we have a super sugar?
        if sweet.characters.count >= 512{
            sweet=PString.substr(superSugar, 0, 512)+Bartleby.randomStringWithLength(512)
        }else{
            sweet=Bartleby.randomStringWithLength(1024)
        }
        return sweet
    }


    /// Tries to put the sugar in the Bowl
    public func putSomeSugarInYourBowl() throws{
        // Create the bowl if necessary.
        let _ = try? FileManager.default.createDirectory(atPath: self._bowlPath, withIntermediateDirectories: true)
        // Before to put the sugar in your bowl
        let cryptedSugar = try Bartleby.cryptoDelegate.encryptString(self.sugar, useKey: Bartleby.configuration.KEY)
        try cryptedSugar.write(toFile: self._bowlPath+"/"+self.persistentUID, atomically: true, encoding: String.Encoding.utf8)
    }


    // The sugar path
    fileprivate var _bowlPath:String{
        get{
            if self.appGroup != ""{
                if let url=FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: self.appGroup){
                    return url.path+"/bowl"
                }
            }
            return Bartleby.getSearchPath(.documentDirectory)!+"/bowl"
        }
    }
}


{'fraction_non_alphanumeric': 0.09584140706513639, 'fraction_numerical': 0.009539424653450588, 'mean_word_length': 2.1443298969072164, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2139163', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1908, 'n_words': 481}