<commit_before>require 'desktop/osx/database'

module Desktop
  class OSX
    attr_reader :desktop_image_path, :skip_reload, :skip_database
    class DesktopImagePermissionsError < StandardError; end
    class DesktopImageMissingError < StandardError; end

    def self.desktop_image=(image)
      self.new.desktop_image = image
    end

    def self.update_desktop_image_permissions
      self.new.update_desktop_image_permissions
    end

    def self.chown_command
      self.new.chown_command
    end

    def self.chmod_command
      self.new.chmod_command
    end

    def initialize(options = nil)
      options ||= {}
      @skip_reload = options[:skip_reload]
      @skip_database = options[:skip_database]
      @desktop_image_path = \
        options[:desktop_image_path] || default_desktop_image_path
    end

    def desktop_image=(image)
      write_default_desktop image
      clear_custom_desktop_image unless skip_database
      touch_desktop_image
      remove_cached_desktop_png
      reload_desktop unless skip_reload
    rescue Errno::EACCES => e
      raise DesktopImagePermissionsError.new(e)
    rescue Errno::ENOENT => e
      raise DesktopImageMissingError.new(e)
    end

    def update_desktop_image_permissions
      system(chown_command) && system(chmod_command)
    end

    def chown_command
      [' -h ', ' '].map do |option|
        "sudo chown#{option}$('whoami'):staff #{desktop_image_path}"
      end.join(" && ")
    end

    def chmod_command
      [' -h ', ' '].map do |option|
        "sudo chmod#{option}664 #{desktop_image_path}"
      end.join(" && ")
    end

    private

    def write_default_desktop(image)
      File.open(desktop_image_path, 'w') do |desktop|
        desktop.write image.data
      end
    end

    def clear_custom_desktop_image
      db = Database.new
      db.clear_desktop_image
      db.close
    end

    def touch_desktop_image
      unless system("touch -h #{desktop_image_path} 2>/dev/null")<commit_msg>Make attribute readers private for OSX<commit_after>require 'desktop/osx/database'

module Desktop
  class OSX
    class DesktopImagePermissionsError < StandardError; end
    class DesktopImageMissingError < StandardError; end

    def self.desktop_image=(image)
      self.new.desktop_image = image
    end

    def self.update_desktop_image_permissions
      self.new.update_desktop_image_permissions
    end

    def self.chown_command
      self.new.chown_command
    end

    def self.chmod_command
      self.new.chmod_command
    end

    def initialize(options = nil)
      options ||= {}
      @skip_reload = options[:skip_reload]
      @skip_database = options[:skip_database]
      @desktop_image_path = \
        options[:desktop_image_path] || default_desktop_image_path
    end

    def desktop_image=(image)
      write_default_desktop image
      clear_custom_desktop_image unless skip_database
      touch_desktop_image
      remove_cached_desktop_png
      reload_desktop unless skip_reload
    rescue Errno::EACCES => e
      raise DesktopImagePermissionsError.new(e)
    rescue Errno::ENOENT => e
      raise DesktopImageMissingError.new(e)
    end

    def update_desktop_image_permissions
      system(chown_command) && system(chmod_command)
    end

    def chown_command
      [' -h ', ' '].map do |option|
        "sudo chown#{option}$('whoami'):staff #{desktop_image_path}"
      end.join(" && ")
    end

    def chmod_command
      [' -h ', ' '].map do |option|
        "sudo chmod#{option}664 #{desktop_image_path}"
      end.join(" && ")
    end

    private

    attr_reader :desktop_image_path,
                :skip_reload,
                :skip_database

    def write_default_desktop(image)
      File.open(desktop_image_path, 'w') do |desktop|
        desktop.write image.data
      end
    end

    def clear_custom_desktop_image
      db = Database.new
      db.clear_desktop_image
      db.close
    end

    def touch_desktop_image
      unless system("touch -h #{desktop_image_path} 2>/dev/null")
{'fraction_non_alphanumeric': 0.08266932270916334, 'fraction_numerical': 0.00199203187250996, 'mean_word_length': 2.881159420289855, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1320871', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1200, 'n_words': 248}