<commit_before>require "erb"
require "fog"

module WebDiff
    class GalleryCreator
        def initialize(path)
            @path = path
            @fog = Fog::Storage.new({
                :provider                 => 'AWS',
                :aws_access_key_id        => WebDiff.configuration.aws_key,
                :aws_secret_access_key    => WebDiff.configuration.aws_secret
            })
        end

        def create_gallery(dir_name)
            @directory = FileUtils.mkdir("#{@path}/gallery").join('')

            # Generate HTML
            html = generate_html

            # Write file
            File.open("#{@directory}/gallery.html", 'w') do |outf|
                outf.write(html)
            end

            # Upload file
            @fog.directories.get("circle-artifacts").files.create(
                key: "artifacts.#{dir_name}/gallery/gallery.html",
                body: File.open("#{@directory}/gallery.html")
            )
        end

        def generate_html
            # Parse manifests
            parsed_manifests = parse_manifests
            files = parsed_manifests[:files]
            diffs = parsed_manifests[:diffs]

            # Template variables
            domain = @path.split('/').last
            directories = parse_directories(files, diffs)

            # HTML
            template = File.read(WebDiff.root + '/lib/web_diff/gallery/template.erb')
            return ERB.new(template).result(binding)
        end

        def parse_manifests
            files = []
            diffs = {}
            manifest_files = Dir["#{@path}/manifest_*.json"]
            manifest_files.each do |manifest|
                manifest = JSON.parse(File.read(manifest))
                files.concat(manifest['files'])
                diffs.reverse_merge!(manifest["diffs"])
            end<commit_msg>Get manifests based on remote option<commit_after>require "erb"
require "fog"

module WebDiff
    class GalleryCreator
        def initialize(path)
            @path = path
            @fog = Fog::Storage.new({
                :provider                 => 'AWS',
                :aws_access_key_id        => WebDiff.configuration.aws_key,
                :aws_secret_access_key    => WebDiff.configuration.aws_secret
            })
        end

        def create_gallery(dir_name)
            @directory = FileUtils.mkdir("#{@path}/gallery").join('')

            # Generate HTML
            html = generate_html

            # Write file
            File.open("#{@directory}/gallery.html", 'w') do |outf|
                outf.write(html)
            end

            # Upload file
            @fog.directories.get("circle-artifacts").files.create(
                key: "artifacts.#{dir_name}/gallery/gallery.html",
                body: File.open("#{@directory}/gallery.html")
            )
        end

        def generate_html
            # Parse manifests
            parsed_manifests = parse_manifests(get_manifests)
            files = parsed_manifests[:files]
            diffs = parsed_manifests[:diffs]

            # Template variables
            domain = @path.split('/').last
            directories = parse_directories(files, diffs)

            # HTML
            template = File.read(WebDiff.root + '/lib/web_diff/gallery/template.erb')
            return ERB.new(template).result(binding)
        end

        def get_manifests
            if WebDiff.configuration.remote
                # Download manifests
                manifest_files = []
            else
                # Get them locally
                manifest_files = Dir["#{@path}/manifest_*.json"]
            end
            return manifest_files
        end

        def parse_manifests(manifest_files)
            files = []
            diffs = {}
            manifest_files.each do |manifest|
                manifest = JSON.parse(File.read(manifest))
                files.concat(manifest['files'])
                diffs.reverse_merge!(manifest["diffs"])
            end
{'fraction_non_alphanumeric': 0.10325, 'fraction_numerical': 0.0, 'mean_word_length': 1.5387055837563453, 'pattern_counts': {'":': 0, '<': 3, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1532627', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1017, 'n_words': 185}