<commit_before># encoding: utf-8
require 'csv'
require 'email/mboxrd/message'

module Imap
  module Backup
    module Serializer
      class Mbox < Base
        def initialize(path, folder)
          super
          container = File.dirname(mbox_pathname)
          Imap::Backup::Utils.make_folder(container, DIRECTORY_PERMISSIONS)
          assert_files
        end

        # TODO: cleanup locks, close file handles

        def uids
          return @uids if @uids

          @uids = []
          return @uids if not exist?

          CSV.foreach(imap_pathname) do |row|
            @uids << row[0]
          end
          @uids
        end

        def save(uid, message)
          uid = uid.to_s
          return if uids.include?(uid)
          message = Email::Mbox::Message.new(message)
          mbox = imap = nil
          begin
            mbox = File.open(mbox_pathname, 'w+')
            imap = File.open(imap_pathname, 'w+')
            mbox.write message.to_mbox
            imap.write uid + "\n"
          ensure
            mbox.close if mbox
            imap.close if imap
          end
        end

        private

        def assert_files
          mbox = mbox_exist?
          imap = imap_exist?
          raise '.imap file missing' if mbox and not imap
          raise '.mbox file missing' if imap and not mbox
        end

        def exist?
          mbox_exist? and imap_exist?
        end

        def mbox_exist?
          File.exist?(mbox_pathname)
        end

        def imap_exist?<commit_msg>Get the message body<commit_after># encoding: utf-8
require 'csv'
require 'email/mboxrd/message'

module Imap
  module Backup
    module Serializer
      class Mbox < Base
        def initialize(path, folder)
          super
          container = File.dirname(mbox_pathname)
          Imap::Backup::Utils.make_folder(container, DIRECTORY_PERMISSIONS)
          assert_files
        end

        # TODO: cleanup locks, close file handles

        def uids
          return @uids if @uids

          @uids = []
          return @uids if not exist?

          CSV.foreach(imap_pathname) do |row|
            @uids << row[0]
          end
          @uids
        end

        def save(uid, message)
          uid = uid.to_s
          return if uids.include?(uid)
          message = Email::Mboxrd::Message.new(message['RFC822'])
          mbox = imap = nil
          begin
            mbox = File.open(mbox_pathname, 'w+')
            imap = File.open(imap_pathname, 'w+')
            mbox.write message.to_mbox
            imap.write uid + "\n"
          ensure
            mbox.close if mbox
            imap.close if imap
          end
        end

        private

        def assert_files
          mbox = mbox_exist?
          imap = imap_exist?
          raise '.imap file missing' if mbox and not imap
          raise '.mbox file missing' if imap and not mbox
        end

        def exist?
          mbox_exist? and imap_exist?
        end

        def mbox_exist?
          File.exist?(mbox_pathname)
        end

        def imap_exist?
{'fraction_non_alphanumeric': 0.07494297816878462, 'fraction_numerical': 0.002280873248615184, 'mean_word_length': 1.4878444084278768, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '228160', 'n_tokens_mistral': 921, 'n_tokens_neox': 889, 'n_words': 250}