<commit_before>package unit

import (
	"bytes"
	"errors"
	"fmt"
	"strings"
)

// Description returns the first Description option found in the [Unit] section.
// If the option is not defined, an empty string is returned.
func (u *Unit) Description() string {
	if values := u.Contents["Unit"]["Description"]; len(values) > 0 {
		return values[0]
	}
	return ""
}

func NewUnit(raw string) (*Unit, error) {
	parsed, err := deserializeUnitFile(raw)
	if err != nil {
		return nil, err
	}
	return &Unit{parsed, raw}, nil
}

// NewUnitFromLegacyContents creates a Unit object from an obsolete unit
// file datastructure. This should only be used to remain backwards-compatible where necessary.<commit_msg>unit: switch to go-systemd unit deserializer<commit_after>package unit

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-systemd/unit"
)

func NewUnit(raw string) (*Unit, error) {
	reader := strings.NewReader(raw)
	opts, err := unit.Deserialize(reader)
	if err != nil {
		return nil, err
	}

	contents := mapOptions(opts)
	return &Unit{contents, raw}, nil
}

func mapOptions(opts []*unit.UnitOption) map[string]map[string][]string {
	contents := make(map[string]map[string][]string)
	for _, opt := range opts {
		if _, ok := contents[opt.Section]; !ok {
			contents[opt.Section] = make(map[string][]string)
		}

		if _, ok := contents[opt.Section][opt.Name]; !ok {
			contents[opt.Section][opt.Name] = make([]string, 0)
		}

		var vals []string
		if opt.Section == "X-Fleet" {
			// The go-systemd parser does not know that our X-Fleet options support
			// multivalue options, so we have to manually parse them here
			vals = parseMultivalueLine(opt.Value)
		} else {
			vals = []string{opt.Value}
		}

		contents[opt.Section][opt.Name] = append(contents[opt.Section][opt.Name], vals...)
	}

	return contents
}

// NewUnitFromLegacyContents creates a Unit object from an obsolete unit
// file datastructure. This should only be used to remain backwards-compatible where necessary.
{'fraction_non_alphanumeric': 0.13212180746561886, 'fraction_numerical': 0.0014734774066797642, 'mean_word_length': 4.17005076142132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5952105', 'n_tokens_mistral': 693, 'n_tokens_neox': 660, 'n_words': 195}