<commit_before>class FSHelper

  parseWatcherFile = (parentPath, file, user)->

    {name, size, mode} = file
    createdAt          = file.time
    type               = if file.isBroken then 'brokenLink' else \
                         if file.isDir then 'folder' else 'file'
    path               = parentPath + '/' + name
    group              = user

    # Fix that when we implemented RemoteDrives
    # if type is 'folder'
    #   if /^\/home\/(.*)\/RemoteDrives(|\/([^\/]+))$/gm.test path
    #     type = 'mount'

    return { size, user, group, createdAt, mode, type, parentPath, path, name }

  @parseWatcher = (parentPath, files)->

    data = []
    return data unless files
    files = [files] unless Array.isArray files

    sortedFiles = []
    for p in [yes, no]
      z = [x for x in files when x.isDir is p][0].sort (x,y)-> x.name > y.name
      sortedFiles.push x for x in z

    {nickname} = KD.whoami().profile
    for file in sortedFiles
      data.push FSHelper.createFile parseWatcherFile parentPath, file, nickname

    return data

  @folderOnChange = (path, change, treeController)->
    console.log "THEY CHANGED:", change, treeController
    file = @parseWatcher(path, change.file).first
    switch change.event
      when "added"
        treeController.addNode file
      when "removed"
        for npath, node of treeController.nodes
          if npath is file.path
            treeController.removeNodeView node
            break

  @grepInDirectory = (keyword, directory, callback, matchingLinesCount = 3) ->
    command = "grep #{keyword} '#{directory}' -n -r -i -I -H -T -C#{matchingLinesCount}"
    KD.getSingleton('kiteController').run command, (err, res) =>
      result = {}

      if res
        chunks = res.split "--\n"

        for chunk in chunks
          lines = chunk.split "\n"

          for line in lines when line
            [lineNumberWithPath, line] = line.split "\t"
            [lineNumber]               = lineNumberWithPath.match /\d+$/
            path                       = lineNumberWithPath.split(lineNumber)[0].trim()
            path                       = path.substring 0, path.length - 1
            isMatchedLine              = line.charAt(1) is ":"
            line                       = line.substring 2, line.length

            result[path] = {} unless result[path]
            result[path][lineNumber] = {
              lineNumber
              line
              isMatchedLine
              path
            }

      callback? result

  @exists = (path, callback=noop)->
    @getInfo path, (err, res)->
      callback err, res?

  @getInfo = (path, callback=noop)->
    KD.getSingleton('kiteController').run
      method   : "fs.getInfo"
      withArgs : {path}
    , callback

  @getSafePath = (path, callback=noop)->
    KD.getSingleton('kiteController').run
      method   : "fs.getSafePath"
      withArgs : {path}
    , callback

  @registry = {}

  @register = (file)->
    @setFileListeners file
    @registry[file.path] = file

  @deregister = (file)->
    delete @registry[file.path]

  @updateInstance = (fileData)-><commit_msg>Fixed ensureNonexistentPath.<commit_after>class FSHelper

  parseWatcherFile = (parentPath, file, user)->

    {name, size, mode} = file
    createdAt          = file.time
    type               = if file.isBroken then 'brokenLink' else \
                         if file.isDir then 'folder' else 'file'
    path               = parentPath + '/' + name
    group              = user

    # Fix that when we implemented RemoteDrives
    # if type is 'folder'
    #   if /^\/home\/(.*)\/RemoteDrives(|\/([^\/]+))$/gm.test path
    #     type = 'mount'

    return { size, user, group, createdAt, mode, type, parentPath, path, name }

  @parseWatcher = (parentPath, files)->

    data = []
    return data unless files
    files = [files] unless Array.isArray files

    sortedFiles = []
    for p in [yes, no]
      z = [x for x in files when x.isDir is p][0].sort (x,y)-> x.name > y.name
      sortedFiles.push x for x in z

    {nickname} = KD.whoami().profile
    for file in sortedFiles
      data.push FSHelper.createFile parseWatcherFile parentPath, file, nickname

    return data

  @folderOnChange = (path, change, treeController)->
    console.log "THEY CHANGED:", change, treeController
    file = @parseWatcher(path, change.file).first
    switch change.event
      when "added"
        treeController.addNode file
      when "removed"
        for npath, node of treeController.nodes
          if npath is file.path
            treeController.removeNodeView node
            break

  @grepInDirectory = (keyword, directory, callback, matchingLinesCount = 3) ->
    command = "grep #{keyword} '#{directory}' -n -r -i -I -H -T -C#{matchingLinesCount}"
    KD.getSingleton('kiteController').run command, (err, res) =>
      result = {}

      if res
        chunks = res.split "--\n"

        for chunk in chunks
          lines = chunk.split "\n"

          for line in lines when line
            [lineNumberWithPath, line] = line.split "\t"
            [lineNumber]               = lineNumberWithPath.match /\d+$/
            path                       = lineNumberWithPath.split(lineNumber)[0].trim()
            path                       = path.substring 0, path.length - 1
            isMatchedLine              = line.charAt(1) is ":"
            line                       = line.substring 2, line.length

            result[path] = {} unless result[path]
            result[path][lineNumber] = {
              lineNumber
              line
              isMatchedLine
              path
            }

      callback? result

  @exists = (path, callback=noop)->
    @getInfo path, (err, res)->
      callback err, res?

  @getInfo = (path, callback=noop)->
    KD.getSingleton('kiteController').run
      method   : "fs.getInfo"
      withArgs : {path}
    , callback

  @ensureNonexistentPath = (path, callback=noop)->
    KD.getSingleton('kiteController').run
      method   : "fs.ensureNonexistentPath"
      withArgs : {path}
    , callback

  @registry = {}

  @register = (file)->
    @setFileListeners file
    @registry[file.path] = file

  @deregister = (file)->
    delete @registry[file.path]

  @updateInstance = (fileData)->
{'fraction_non_alphanumeric': 0.117168818747011, 'fraction_numerical': 0.0022317870237525904, 'mean_word_length': 1.9140733859730608, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 31, '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': '4643311', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1805, 'n_words': 500}