<commit_before>package spec

import (
	"fmt"
	"io/ioutil"
	"path"

	"github.com/portworx/torpedo/pkg/errors"
	"github.com/sirupsen/logrus"
	"k8s.io/client-go/kubernetes/scheme"
)

// Factory is an application spec factory
type Factory struct {
	specDir    string
	specParser Parser
}

var appSpecFactory = make(map[string]*AppSpec)

// register registers a new spec with the factory
func (f *Factory) register(id string, app *AppSpec) {
	if _, ok := appSpecFactory[id]; !ok {
		logrus.Infof("Registering app: %v", id)
		appSpecFactory[id] = app
	}
}

// Get returns a registered application
func (f *Factory) Get(id string) (*AppSpec, error) {
	if d, ok := appSpecFactory[id]; ok && d.Enabled {
		dCopy, err := scheme.Scheme.DeepCopy(d)
		if err != nil {
			logrus.Errorf("Failed to create a copy of spec object: %#v Err: %v", d, err)
			return nil, err
		}

		return dCopy.(*AppSpec), nil
	}

	return nil, &errors.ErrNotFound{
		ID:   id,
		Type: "AppSpec",
	}
}

// GetAll returns all registered enabled applications
func (f *Factory) GetAll() []*AppSpec {
	var specs []*AppSpec
	for _, val := range appSpecFactory {
		if val.Enabled {
			valCopy, err := scheme.Scheme.DeepCopy(val)
			if err != nil {
				logrus.Errorf("Failed to create a copy of spec object: %#v Err: %v", val, err)
				return nil
			}

			specs = append(specs, valCopy.(*AppSpec))
		}
	}

	return specs
}

// NewFactory creates a new spec factory
func NewFactory(specDir string, parser Parser) (*Factory, error) {
	f := &Factory{
		specDir:    specDir,
		specParser: parser,
	}

	appDirList, err := ioutil.ReadDir(f.specDir)
	if err != nil {
		return nil, err
	}

	for _, file := range appDirList {
		if file.IsDir() {
			specID := file.Name()

			logrus.Infof("Parsing: %v...", path.Join(f.specDir, specID))

			specs, err := f.specParser.ParseSpecs(path.Join(f.specDir, file.Name()))
			if err != nil {
				return nil, err
			}

			if len(specs) == 0 {
				continue<commit_msg>Change DeepCopy for AppSpec<commit_after>package spec

import (
	"fmt"
	"io/ioutil"
	"path"

	"github.com/mohae/deepcopy"
	"github.com/portworx/torpedo/pkg/errors"
	"github.com/sirupsen/logrus"
)

// Factory is an application spec factory
type Factory struct {
	specDir    string
	specParser Parser
}

var appSpecFactory = make(map[string]*AppSpec)

// register registers a new spec with the factory
func (f *Factory) register(id string, app *AppSpec) {
	if _, ok := appSpecFactory[id]; !ok {
		logrus.Infof("Registering app: %v", id)
		appSpecFactory[id] = app
	}
}

// Get returns a registered application
func (f *Factory) Get(id string) (*AppSpec, error) {
	if d, ok := appSpecFactory[id]; ok && d.Enabled {
		dCopy := deepcopy.Copy(d)
		if copy, ok := dCopy.(*AppSpec); ok {
			return copy, nil
		}
		return nil, fmt.Errorf("error creating copy of spec: %v", d)
	}

	return nil, &errors.ErrNotFound{
		ID:   id,
		Type: "AppSpec",
	}
}

// GetAll returns all registered enabled applications
func (f *Factory) GetAll() []*AppSpec {
	var specs []*AppSpec
	for _, val := range appSpecFactory {
		if val.Enabled {
			valCopy := deepcopy.Copy(val)
			if copy, ok := valCopy.(*AppSpec); ok {
				specs = append(specs, copy)
			} else {
				logrus.Errorf("Error creating copy of spec: %v", val)
			}
		}
	}

	return specs
}

// NewFactory creates a new spec factory
func NewFactory(specDir string, parser Parser) (*Factory, error) {
	f := &Factory{
		specDir:    specDir,
		specParser: parser,
	}

	appDirList, err := ioutil.ReadDir(f.specDir)
	if err != nil {
		return nil, err
	}

	for _, file := range appDirList {
		if file.IsDir() {
			specID := file.Name()

			logrus.Infof("Parsing: %v...", path.Join(f.specDir, specID))

			specs, err := f.specParser.ParseSpecs(path.Join(f.specDir, file.Name()))
			if err != nil {
				return nil, err
			}

			if len(specs) == 0 {
				continue
{'fraction_non_alphanumeric': 0.14132985658409386, 'fraction_numerical': 0.0007822685788787483, 'mean_word_length': 3.4552845528455283, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1862558', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1381, 'n_words': 390}