<commit_before>package slugviewer

import (
	"errors"
	"flag"
	"fmt"
	"strings"
	"io/ioutil"
	"time"
)

import (
	"manhattan/commands"
	pb "code.google.com/p/goprotobuf/proto"
	tp "tritium/proto"
)

const CmdName = "slugviewer"

type SlugViewCmd struct {
	options *Options
	flags   *flag.FlagSet
}

func New() *SlugViewCmd {
	cmd := SlugViewCmd{}
	cmd.flags = flag.NewFlagSet(CmdName, flag.ContinueOnError)
	cmd.options = &Options{}

	cmd.options.SetDefaults()
	cmd.options.SetupFlags(cmd.flags) 
	return &cmd
}

func (cmd *SlugViewCmd) Name() string {
	return CmdName
}

func (cmd *SlugViewCmd) Description() string {
	return "Opens up a slug for viewing."
}

func (cmd *SlugViewCmd) PrintUsage() {
	fmt.Println("Usage:")
	fmt.Println("moov " + CmdName + " <slug>")
	fmt.Println()
	fmt.Println("Flag Options:")
	cmd.flags.PrintDefaults()
}

func (cmd *SlugViewCmd) Execute(args []string) (err error) {
	args, err = commands.ParseFlags(cmd.flags, args)
	if err != nil {
		if err == flag.ErrHelp {
			return nil
		}
		return err
	}
	if len(args) > 0 {
		cmd.options.SlugLoc = args[0]
	}
	err = cmd.options.Reconcile()
	if err != nil {
		return commands.MakeHelpError(err)
	}

	var data []byte

	data, err = ioutil.ReadFile(cmd.options.SlugLoc)
	if err != nil {
		return errors.New("Problem opening the slug for reading.")
	}

	duration := time.Duration(0)
	slug := &tp.Slug{}
	for i := 0; i < 1000; i++ {
		startTime := time.Now()
		err = pb.Unmarshal(data, slug)
		duration += time.Now().Sub(startTime)
		if err != nil {
			return err
		}
	}

	if !cmd.options.TimeOnly {
		printSlug(slug, 0)
	}
	fmt.Printf("Duration of unmarshaling: %v\n", duration/time.Duration(100))

	return nil
}

func printIndent(str string, indLvl int, rest ...interface{}) {
	fmt.Printf(strings.Repeat("  ", indLvl)+str+"\n", rest...)
}

func printSlug(s *tp.Slug, indLvl int) {
	printIndent("Slug:", indLvl)
	indLvl += 1
	printIndent("Name -> %v", indLvl, s.GetName())
	printIndent("Version -> %v", indLvl, s.GetVersion())
	printIndent("Rewriter Rules:", indLvl)
	for ind, item := range s.GetRrules() {
		printIndent("Rrule[%d] -> %v", indLvl+1, ind, item)
	}
	printIndent("SSL Whitelist:", indLvl)
	for ind, item := range s.GetSslWhitelist() {
		printIndent("whitelist[%d] -> %v", indLvl+1, ind, item)
	}
	printIndent("Credentials -> %v", indLvl, s.GetCredentials())
	printIndent("Transforms:",indLvl)
	for ind, item := range s.GetTransformers() {
		printIndent("transform[%d]:", indLvl+1, ind)
		printTransform(item, indLvl+2)
	}
}

func printTransform(t *tp.Transform, indLvl int) {
	printIndent("Script Objects:", indLvl)
	for ind, item := range t.GetObjects() {
		printIndent("Script Obj[%d]:", indLvl+1, ind)
		printScriptObject(item, indLvl+2)
	}
	printIndent("Package:", indLvl)
	printPackage(t.GetPkg(), indLvl+1)
}<commit_msg>expose active layer names in slugviewer<commit_after>package slugviewer

import (
	"errors"
	"flag"
	"fmt"
	"strings"
	"io/ioutil"
	"time"
)

import (
	"manhattan/commands"
	pb "code.google.com/p/goprotobuf/proto"
	tp "tritium/proto"
)

const CmdName = "slugviewer"

type SlugViewCmd struct {
	options *Options
	flags   *flag.FlagSet
}

func New() *SlugViewCmd {
	cmd := SlugViewCmd{}
	cmd.flags = flag.NewFlagSet(CmdName, flag.ContinueOnError)
	cmd.options = &Options{}

	cmd.options.SetDefaults()
	cmd.options.SetupFlags(cmd.flags) 
	return &cmd
}

func (cmd *SlugViewCmd) Name() string {
	return CmdName
}

func (cmd *SlugViewCmd) Description() string {
	return "Opens up a slug for viewing."
}

func (cmd *SlugViewCmd) PrintUsage() {
	fmt.Println("Usage:")
	fmt.Println("moov " + CmdName + " <slug>")
	fmt.Println()
	fmt.Println("Flag Options:")
	cmd.flags.PrintDefaults()
}

func (cmd *SlugViewCmd) Execute(args []string) (err error) {
	args, err = commands.ParseFlags(cmd.flags, args)
	if err != nil {
		if err == flag.ErrHelp {
			return nil
		}
		return err
	}
	if len(args) > 0 {
		cmd.options.SlugLoc = args[0]
	}
	err = cmd.options.Reconcile()
	if err != nil {
		return commands.MakeHelpError(err)
	}

	var data []byte

	data, err = ioutil.ReadFile(cmd.options.SlugLoc)
	if err != nil {
		return errors.New("Problem opening the slug for reading.")
	}

	duration := time.Duration(0)
	slug := &tp.Slug{}
	for i := 0; i < 1000; i++ {
		startTime := time.Now()
		err = pb.Unmarshal(data, slug)
		duration += time.Now().Sub(startTime)
		if err != nil {
			return err
		}
	}

	if !cmd.options.TimeOnly {
		printSlug(slug, 0)
	}
	fmt.Printf("Duration of unmarshaling: %v\n", duration/time.Duration(100))

	return nil
}

func printIndent(str string, indLvl int, rest ...interface{}) {
	fmt.Printf(strings.Repeat("  ", indLvl)+str+"\n", rest...)
}

func printSlug(s *tp.Slug, indLvl int) {
	printIndent("Slug:", indLvl)
	indLvl += 1
	printIndent("Name -> %v", indLvl, s.GetName())
	printIndent("Version -> %v", indLvl, s.GetVersion())
	printIndent("Active Layer(s) -> %v", indLvl, s.GetActiveLayers())
	printIndent("Rewriter Rules:", indLvl)
	for ind, item := range s.GetRrules() {
		printIndent("Rrule[%d] -> %v", indLvl+1, ind, item)
	}
	printIndent("SSL Whitelist:", indLvl)
	for ind, item := range s.GetSslWhitelist() {
		printIndent("whitelist[%d] -> %v", indLvl+1, ind, item)
	}
	printIndent("Credentials -> %v", indLvl, s.GetCredentials())
	printIndent("Transforms:",indLvl)
	for ind, item := range s.GetTransformers() {
		printIndent("transform[%d]:", indLvl+1, ind)
		printTransform(item, indLvl+2)
	}
}

func printTransform(t *tp.Transform, indLvl int) {
	printIndent("Script Objects:", indLvl)
	for ind, item := range t.GetObjects() {
		printIndent("Script Obj[%d]:", indLvl+1, ind)
		printScriptObject(item, indLvl+2)
	}
	printIndent("Package:", indLvl)
	printPackage(t.GetPkg(), indLvl+1)
}
{'fraction_non_alphanumeric': 0.16722057022914116, 'fraction_numerical': 0.006996676578625153, 'mean_word_length': 4.358950328022493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '2989162', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2257, 'n_words': 496}