<commit_before># encoding: UTF-8

require 'zip'
require 'fileutils'

require_relative '../exceptions/no_project_error'

module GoodData
  class Project
    USERSPROJECTS_PATH = '/gdc/account/profile/%s/projects'
    PROJECTS_PATH = '/gdc/projects'
    PROJECT_PATH = '/gdc/projects/%s'
    SLIS_PATH = '/ldm/singleloadinterface'

    attr_accessor :connection

    class << self
      # Returns an array of all projects accessible by
      # current user
      def all
        json = GoodData.get GoodData.profile.projects
        json['projects'].map do |project|
          Project.new project
        end
      end

      # Returns a Project object identified by given string
      # The following identifiers are accepted
      #  - /gdc/md/<id>
      #  - /gdc/projects/<id>
      #  - <id>
      #
      def [](id)
        return id if id.respond_to?(:is_project?) && id.is_project?
        if id == :all
          Project.all
        else
          if id.to_s !~ /^(\/gdc\/(projects|md)\/)?[a-zA-Z\d]+$/
            raise ArgumentError.new('wrong type of argument. Should be either project ID or path')
          end

          id = id.match(/[a-zA-Z\d]+$/)[0] if id =~ /\//

          response = GoodData.get PROJECT_PATH % id
          Project.new response
        end
      end

      # Create a project from a given attributes
      # Expected keys:
      # - :title (mandatory)
      # - :summary
      # - :template (default /projects/blank)
      #
      def create(attributes, &block)
        GoodData.logger.info "Creating project #{attributes[:title]}"

        auth_token = attributes[:auth_token] || GoodData.connection.auth_token
        fail "You have to provide your token for creating projects as :auth_token parameter" if auth_token.nil? || auth_token.empty?

        json = {"project" =>
                  {
                    'meta' => {
                      'title' => attributes[:title],
                      'summary' => attributes[:summary] || 'No summary'
                    },
                    'content' => {
                      'guidedNavigation' => 1,
                      'authorizationToken' => <PASSWORD>_token,
                      'driver' => 'Pg'
                    }
                  }
        }
        json["project"]['meta']['projectTemplate'] = attributes[:template] if attributes[:template] && !attributes[:template].empty?
        project = Project.new json
        project.save

        # until it is enabled or deleted, recur. This should still end if there is a exception thrown out from RESTClient. This sometimes happens from WebApp when request is too long
        while project.state.to_s != "enabled"
          if project.state.to_s == "deleted"<commit_msg>Added couple of params to allow more granular control during project creation<commit_after># encoding: UTF-8

require 'zip'
require 'fileutils'

require_relative '../exceptions/no_project_error'

module GoodData
  class Project
    USERSPROJECTS_PATH = '/gdc/account/profile/%s/projects'
    PROJECTS_PATH = '/gdc/projects'
    PROJECT_PATH = '/gdc/projects/%s'
    SLIS_PATH = '/ldm/singleloadinterface'

    attr_accessor :connection

    class << self
      # Returns an array of all projects accessible by
      # current user
      def all
        json = GoodData.get GoodData.profile.projects
        json['projects'].map do |project|
          Project.new project
        end
      end

      # Returns a Project object identified by given string
      # The following identifiers are accepted
      #  - /gdc/md/<id>
      #  - /gdc/projects/<id>
      #  - <id>
      #
      def [](id)
        return id if id.respond_to?(:is_project?) && id.is_project?
        if id == :all
          Project.all
        else
          if id.to_s !~ /^(\/gdc\/(projects|md)\/)?[a-zA-Z\d]+$/
            raise ArgumentError.new('wrong type of argument. Should be either project ID or path')
          end

          id = id.match(/[a-zA-Z\d]+$/)[0] if id =~ /\//

          response = GoodData.get PROJECT_PATH % id
          Project.new response
        end
      end

      # Create a project from a given attributes
      # Expected keys:
      # - :title (mandatory)
      # - :summary
      # - :template (default /projects/blank)
      #
      def create(attributes, &block)
        GoodData.logger.info "Creating project #{attributes[:title]}"

        auth_token = attributes[:auth_token] || GoodData.connection.auth_token
        fail "You have to provide your token for creating projects as :auth_token parameter" if auth_token.nil? || auth_token.empty?

        json = {"project" =>
                  {
                    'meta' => {
                      'title' => attributes[:title],
                      'summary' => attributes[:summary] || 'No summary'
                    },
                    'content' => {
                      'guidedNavigation' => attributes[:guided_navigation] || 1,
                      'authorizationToken' => auth_token,
                      'driver' => attributes[:driver] || 'Pg'
                    }
                  }
        }
        json["project"]['meta']['projectTemplate'] = attributes[:template] if attributes[:template] && !attributes[:template].empty?
        project = Project.new json
        project.save

        # until it is enabled or deleted, recur. This should still end if there is a exception thrown out from RESTClient. This sometimes happens from WebApp when request is too long
        while project.state.to_s != "enabled"
          if project.state.to_s == "deleted"
{'fraction_non_alphanumeric': 0.11252921085745102, 'fraction_numerical': 0.0010785547366528852, 'mean_word_length': 1.894901144640999, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 26, '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': '4798694', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1496, 'n_words': 422}