Skip to content

Commit

Permalink
feat(proxy): change profile params to be a map for cobrass.ThirdParty…
Browse files Browse the repository at this point in the history
…CommandLine (#48)
  • Loading branch information
plastikfan committed Dec 7, 2023
1 parent 010f64e commit 573a39a
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 54 deletions.
27 changes: 24 additions & 3 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/snivilised/cobrass/src/assistant"
"github.com/snivilised/cobrass/src/assistant/configuration"
ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
"github.com/snivilised/cobrass/src/clif"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/extendio/xfs/utils"
"github.com/snivilised/pixa/src/app/proxy"
Expand Down Expand Up @@ -132,6 +133,7 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
}

profile := inputs.ProfileFam.Native.Profile

if err := b.ProfilesCFG.Validate(profile); err != nil {
return err
}
Expand Down Expand Up @@ -213,14 +215,33 @@ func handleLangSetting(config configuration.ViperConfig) {
}

func (b *Bootstrap) viper() {
b.ProfilesCFG = make(proxy.ProfilesConfig)

// Ideally, the ProfileParameterSet would perform a check against
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can check here.
//
b.ProfilesCFG = b.optionsInfo.Config.Viper.GetStringMapStringSlice("profiles")
err := b.optionsInfo.Config.Viper.UnmarshalKey("sampler", &b.SamplerCFG)

if err != nil {
raw := b.optionsInfo.Config.Viper.Get("profiles")

if raw != nil {
profiles, ok := raw.(proxy.ProfilesFlagOptionAsAnyPair)
if !ok {
panic("Invalid type for 'profiles'")
}

for profile, pv := range profiles {
if pair, ok := pv.(proxy.ProfilesFlagOptionAsAnyPair); ok {
b.ProfilesCFG[profile] = make(clif.SpecifiedFlagsCollection)

for flag, optionAsAny := range pair {
b.ProfilesCFG[profile][flag] = fmt.Sprint(optionAsAny)
}
}
}
}

if err := b.optionsInfo.Config.Viper.UnmarshalKey("sampler", &b.SamplerCFG); err != nil {
panic(err)
}
}
13 changes: 10 additions & 3 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,22 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
}); xvErr == nil {
options := []string{}
present := make(cobrass.SpecifiedFlagsCollection)
presentCL := cobrass.ThirdPartyCommandLine{}
flagSet := cmd.Flags()

flagSet.Visit(func(f *pflag.Flag) {
options = append(options, fmt.Sprintf("--%v=%v", f.Name, f.Value))
options = append(options, fmt.Sprintf("--%v=%v [type: %v]", f.Name, f.Value, f.Value.Type()))

if isThirdPartyKnown(f.Name, shrinkPS.Native.ThirdPartySet.KnownBy) {
present[f.Name] = f.Value.String()
presentCL = append(presentCL, f.Name)
if f.Value.Type() != "bool" {
presentCL = append(presentCL, f.Value.String())
}
}
})
shrinkPS.Native.ThirdPartySet.Present = present
shrinkPS.Native.ThirdPartySet.LongPresentCL = presentCL

fmt.Printf("%v %v Running shrink, with options: '%v', args: '%v'\n",
AppEmoji, ApplicationName, options, strings.Join(args, "/"),
Expand All @@ -129,6 +135,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
// validate the profile
//
profile := inputs.RootInputs.ProfileFam.Native.Profile

if err := b.ProfilesCFG.Validate(profile); err != nil {
return err
}
Expand All @@ -137,7 +144,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
//
if err := b.SamplerCFG.Validate(
inputs.ParamSet.Native.Scheme,
&b.ProfilesCFG,
b.ProfilesCFG,
); err != nil {
return err
}
Expand All @@ -160,7 +167,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
inputs,
b.optionsInfo.Program,
b.optionsInfo.Config.Viper,
&b.ProfilesCFG,
b.ProfilesCFG,
&b.SamplerCFG,
)
} else {
Expand Down
20 changes: 9 additions & 11 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ func (e *ShrinkEntry) ConfigureOptions(o *nav.TraverseOptions) {

e.EntryBase.ConfigureOptions(o)

if e.Registry == nil {
e.Registry = NewRunnerRegistry(&SharedRunnerInfo{
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
program: e.Program,
profilesCFG: e.ProfilesCFG,
samplerCFG: e.SamplerCFG,
Inputs: e.Inputs,
})
}
e.Registry = NewRunnerRegistry(&SharedRunnerInfo{
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
program: e.Program,
profilesCFG: e.ProfilesCFG,
samplerCFG: e.SamplerCFG,
Inputs: e.Inputs,
})
}

func clearResumeFromWith(with nav.CreateNewRunnerWith) nav.CreateNewRunnerWith {
Expand Down Expand Up @@ -213,7 +211,7 @@ func EnterShrink(
inputs *ShrinkCommandInputs,
program Executor,
config configuration.ViperConfig,
profilesCFG *ProfilesConfig,
profilesCFG ProfilesConfig,
samplerCFG *SamplerConfig,
) error {
fmt.Printf("---> 🔊🔊 Directory: '%v'\n", inputs.RootInputs.ParamSet.Native.Directory)
Expand Down
2 changes: 1 addition & 1 deletion src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type EntryBase struct {
ThirdPartyCL cobrass.ThirdPartyCommandLine
Options *nav.TraverseOptions
Registry *RunnerRegistry
ProfilesCFG *ProfilesConfig
ProfilesCFG ProfilesConfig
SamplerCFG *SamplerConfig
}

Expand Down
16 changes: 11 additions & 5 deletions src/app/proxy/image-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/snivilised/cobrass"
"github.com/snivilised/cobrass/src/assistant"
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/cobrass/src/store"
)

Expand All @@ -17,11 +18,15 @@ type RootParameterSet struct { // should contain RootCommandInputs
Language string // TODO: move this to family store
}

type ProfilesConfig map[string][]string
type (
ProfilesFlagOptionAsAnyPair = map[string]any
ProfilesFlagOptionPair = map[string]string
ProfilesConfig map[string]clif.SpecifiedFlagsCollection
)

func (ps ProfilesConfig) Validate(name string) error {
func (pc ProfilesConfig) Validate(name string) error {
if name != "" {
if _, found := ps[name]; !found {
if _, found := pc[name]; !found {
return fmt.Errorf("no such profile: '%v'", name)
}
}
Expand Down Expand Up @@ -86,8 +91,9 @@ type ThirdPartySet struct {
Quality int
// Auxiliary
//
Present cobrass.SpecifiedFlagsCollection
KnownBy cobrass.KnownByCollection
Present cobrass.SpecifiedFlagsCollection
LongPresentCL cobrass.ThirdPartyCommandLine
KnownBy cobrass.KnownByCollection
}

// [blur]
Expand Down
10 changes: 10 additions & 0 deletions src/app/proxy/item-runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ func (r *SamplerRunner) OnNewShrinkItem(item *nav.TraverseItem,
// then we invoke as:
// r.shared.program.Execute(clif.Expand(positional, resultCL)...)

if profile := r.shared.Inputs.RootInputs.ProfileFam.Native.Profile; profile != "" {
secondary := r.shared.Inputs.ParamSet.Native.ThirdPartySet.LongPresentCL
eval := cobrass.Evaluate(r.shared.profilesCFG[profile],
r.shared.Inputs.ParamSet.Native.ThirdPartySet.KnownBy,
secondary,
)

_ = clif.Expand(positional, eval)
}

return r.shared.program.Execute(clif.Expand(positional, thirdPartyCL)...)
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/proxy/proxy-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SharedRunnerInfo struct {
Type RunnerTypeEnum
Options *nav.TraverseOptions
program Executor
profilesCFG *ProfilesConfig
profilesCFG ProfilesConfig
samplerCFG *SamplerConfig
Inputs *ShrinkCommandInputs
}
Expand Down
10 changes: 0 additions & 10 deletions src/app/proxy/runner-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@ package proxy

import (
"sync"

"github.com/snivilised/extendio/xfs/nav"
)

type NewRunnerParams struct {
Type RunnerTypeEnum
Options *nav.TraverseOptions
Program Executor
ProfilesCFG *ProfilesConfig
SamplerCFG *SamplerConfig
}

func NewRunnerRegistry(shared *SharedRunnerInfo) *RunnerRegistry {
return &RunnerRegistry{
pool: sync.Pool{
Expand Down
4 changes: 2 additions & 2 deletions src/app/proxy/sampler-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type (
}
)

func (cs SamplerConfig) Validate(name string, profiles *ProfilesConfig) error {
func (cs SamplerConfig) Validate(name string, profiles ProfilesConfig) error {
if name == "" {
return nil
}
Expand All @@ -33,7 +33,7 @@ func (cs SamplerConfig) Validate(name string, profiles *ProfilesConfig) error {
}

for _, p := range scheme.Profiles {
if _, found := (*profiles)[p]; !found {
if _, found := profiles[p]; !found {
return fmt.Errorf("profile(referenced by scheme: '%v'): '%v' not found in config",
name, p,
)
Expand Down
2 changes: 2 additions & 0 deletions src/app/proxy/sampler-runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ var _ = Describe("SamplerRunner", Ordered, func() {
"--no-files", "4",
"--files-gb", "*Energy-Explorers*",
"--profile", "adaptive",
"--gaussian-blur", "0.51",
"--interlace", "line",
},
},
}),
Expand Down
29 changes: 11 additions & 18 deletions test/data/configuration/pixa-test.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
profiles:
blur:
["--strip", "--interlace", "plane", "--gaussian-blur", "0.05"]
strip: true
interlace: "plane"
gaussian-blur: "0.05"
sf:
[
"--dry-run",
"--strip",
"--interlace",
"plane",
"--sampling-factor",
"4:2:0",
]
dry-run: true
strip: true
interlace: "plane"
sampling-factor: "4:2:0"
adaptive:
[
"--strip",
"--interlace",
"plane",
"--gaussian-blur",
"0.05",
"--adaptive-resize",
"60",
]
strip: true
interlace: "plane"
gaussian-blur: "0.25"
adaptive-resize: "60"
sampler:
files: 2
folders: 1
Expand Down

0 comments on commit 573a39a

Please sign in to comment.