file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
12.1k
suffix
large_stringlengths
0
12k
middle
large_stringlengths
0
7.51k
fim_type
large_stringclasses
4 values
mole.go
KeepAliveInterval time.Duration `json:"keep-alive-interval" mapstructure:"keep-alive-interva" toml:"keep-alive-interval"` ConnectionRetries int `json:"connection-retries" mapstructure:"connection-retries" toml:"connection-retries"` WaitAndRetry time.Duration `json:"wait-and-retry" mapstructure:"wait-and-retry" toml:"wait-and-retry"` SshAgent string `json:"ssh-agent" mapstructure:"ssh-agent" toml:"ssh-agent"` Timeout time.Duration `json:"timeout" mapstructure:"timeout" toml:"timeout"` SshConfig string `json:"ssh-config" mapstructure:"ssh-config" toml:"ssh-config"` Rpc bool `json:"rpc" mapstructure:"rpc" toml:"rpc"` RpcAddress string `json:"rpc-address" mapstructure:"rpc-address" toml:"rpc-address"` } // ParseAlias translates a Configuration object to an Alias object. func (c Configuration) ParseAlias(name string) *alias.Alias { return &alias.Alias{ Name: name, TunnelType: c.TunnelType, Verbose: c.Verbose, Insecure: c.Insecure, Detach: c.Detach, Source: c.Source.List(), Destination: c.Destination.List(), Server: c.Server.String(), Key: c.Key, KeepAliveInterval: c.KeepAliveInterval.String(), ConnectionRetries: c.ConnectionRetries, WaitAndRetry: c.WaitAndRetry.String(), SshAgent: c.SshAgent, Timeout: c.Timeout.String(), SshConfig: c.SshConfig, Rpc: c.Rpc, RpcAddress: c.RpcAddress, } } // Client manages the overall state of the application based on its configuration. type Client struct { Conf *Configuration Tunnel *tunnel.Tunnel sigs chan os.Signal } // New initializes a new mole's client. func New(conf *Configuration) *Client { cli = &Client{ Conf: conf, sigs: make(chan os.Signal, 1), } return cli } // Start kicks off mole's client, establishing the tunnel and its channels // based on the client configuration attributes. func (c *Client) Start() error { // memguard is used to securely keep sensitive information in memory. // This call makes sure all data will be destroy when the program exits. defer memguard.Purge() if c.Conf.Id == "" { u, err := uuid.NewV4() if err != nil { return fmt.Errorf("could not auto generate app instance id: %v", err) } c.Conf.Id = u.String()[:8] } r, err := c.Running() if err != nil { log.WithFields(log.Fields{ "id": c.Conf.Id, }).WithError(err).Error("error while checking for another instance using the same id") return err } if r { log.WithFields(log.Fields{ "id": c.Conf.Id, }).Error("can't start. Another instance is already using the same id") return fmt.Errorf("can't start. Another instance is already using the same id %s", c.Conf.Id) } log.Infof("instance identifier is %s", c.Conf.Id) if c.Conf.Detach { var err error ic, err := NewDetachedInstance(c.Conf.Id) if err != nil { log.WithError(err).Errorf("error while creating directory to store mole instance related files") return err } err = startDaemonProcess(ic) if err != nil { log.WithFields(log.Fields{ "id": c.Conf.Id, }).WithError(err).Error("error starting ssh tunnel") return err } } else { go c.handleSignals() } if c.Conf.Verbose { log.SetLevel(log.DebugLevel) } d, err := fsutils.CreateInstanceDir(c.Conf.Id) if err != nil { log.WithFields(log.Fields{ "id": c.Conf.Id, }).WithError(err).Error("error creating directory for mole instance") return err } if c.Conf.Rpc { addr, err := rpc.Start(c.Conf.RpcAddress) if err != nil { return err } rd := filepath.Join(d.Dir, "rpc") err = ioutil.WriteFile(rd, []byte(addr.String()), 0644) if err != nil { log.WithFields(log.Fields{ "id": c.Conf.Id, }).WithError(err).Error("error creating file with rpc address") return err } c.Conf.RpcAddress = addr.String() log.Infof("rpc server address saved on %s", rd) }
t, err := createTunnel(c.Conf) if err != nil { log.WithFields(log.Fields{ "id": c.Conf.Id, }).WithError(err).Error("error creating tunnel") return err } c.Tunnel = t if err = c.Tunnel.Start(); err != nil { log.WithFields(log.Fields{ "tunnel": c.Tunnel.String(), }).WithError(err).Error("error while starting tunnel") return err } return nil } // Stop shuts down a detached mole's application instance. func (c *Client) Stop() error { pfp, err := fsutils.GetPidFileLocation(c.Conf.Id) if err != nil { return fmt.Errorf("error getting information about aliases directory: %v", err) } if _, err := os.Stat(pfp); os.IsNotExist(err) { return fmt.Errorf("no instance of mole with id %s is running", c.Conf.Id) } cntxt := &daemon.Context{ PidFileName: pfp, } d, err := cntxt.Search() if err != nil { return err } if c.Conf.Detach { err = os.RemoveAll(pfp) if err != nil { return err } } else { d, err := fsutils.InstanceDir(c.Conf.Id) if err != nil { return err } err = os.RemoveAll(d.Dir) if err != nil { return err } } err = d.Kill() if err != nil { return err } return nil } func (c *Client) handleSignals() { signal.Notify(c.sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) sig := <-c.sigs log.Debugf("process signal %s received", sig) err := c.Stop() if err != nil { log.WithError(err).Error("instance not properly stopped") } } // Merge overwrites Configuration from the given Alias. // // Certain attributes like Verbose, Insecure and Detach will be overwritten // only if they are found on the givenFlags which should contain the name of // all flags given by the user through UI (e.g. CLI). func (c *Configuration) Merge(al *alias.Alias, givenFlags []string) error { var fl flags = givenFlags if !fl.lookup("verbose") { c.Verbose = al.Verbose } if !fl.lookup("insecure") { c.Insecure = al.Insecure } if !fl.lookup("detach") { c.Detach = al.Detach } c.Id = al.Name c.TunnelType = al.TunnelType srcl := AddressInputList{} for _, src := range al.Source { err := srcl.Set(src) if err != nil { return err } } c.Source = srcl dstl := AddressInputList{} for _, dst := range al.Destination { err := dstl.Set(dst) if err != nil { return err } } c.Destination = dstl srv := AddressInput{} err := srv.Set(al.Server) if err != nil { return err } c.Server = srv c.Key = al.Key kai, err := time.ParseDuration(al.KeepAliveInterval) if err != nil { return err } c.KeepAliveInterval = kai c.ConnectionRetries = al.ConnectionRetries war, err := time.ParseDuration(al.WaitAndRetry) if err != nil { return err } c.WaitAndRetry = war c.SshAgent = al.SshAgent tim, err := time.ParseDuration(al.Timeout) if err != nil { return err } c.Timeout = tim c.SshConfig = al.SshConfig c.Rpc = al.Rpc c.RpcAddress = al.RpcAddress return nil } // ShowInstances returns the runtime information about all instances of mole // running on the system with rpc enabled. func ShowInstances() (*InstancesRuntime, error) { ctx := context.Background() data, err := rpc.ShowAll(ctx) if err != nil { return nil, err } var instances []Runtime err = mapstructure.Decode(data, &instances) if err != nil { return nil, err } runtime := InstancesRuntime(instances) if len(runtime) == 0 { return nil, fmt.Errorf
random_line_split
cliches.go
(match string) *BadTerm { return &BadTerm{match, "'%s' is a cliche. Avoid it like the plague."} } // ShouldNotCliche returns a slice of BadTerm's, none of which should be in the // a text (case insensitive). See existence_checks.go for details of BadTerms. func ShouldNotCliche() []TextCheck { return []TextCheck{ cliche("all hell broke loose"), cliche("american as apple pie"), cliche("hobson's choice"), cliche("i beg to differ"), cliche("jack of all trades"), cliche("a chip off the old block"), cliche("a clean slate"), cliche("a dark and stormy night"), cliche("a far cry"), cliche("a fate worse than death"), cliche("a fine kettle of fish"), cliche("a loose cannon"), cliche("a matter of concern"), cliche("a penny saved is a penny earned"), cliche("a tough row to hoe"), cliche("a word to the wise"), cliche("ace in the hole"), cliche("acid test"), cliche("add insult to injury"), cliche("against all odds"), cliche("air your dirty laundry"), cliche("alas and alack"), cliche("all fun and games"), cliche("all in a day's work"), cliche("all talk, no action"), cliche("all things being equal"), cliche("all thumbs"), cliche("all your eggs in one basket"), cliche("all's fair in love and war"), cliche("all's well that ends well"), cliche("almighty dollar"), cliche("an axe? to grind"), cliche("another day, another dollar"), cliche("armed to the teeth"), cliche("as a last resort"), cliche("as luck would have it"), cliche("as old as time"), cliche("as the crow flies"), cliche("at loose ends"), cliche("at my wits end"), cliche("at the end of the day"), cliche("attached hereto"), cliche("avoid like the plague"), cliche("babe in the woods"), cliche("back against the wall"), cliche("back in the saddle"), cliche("back to square one"), cliche("back to the drawing board"), cliche("bad to the bone"), cliche("badge of honor"), cliche("bald faced liar"), cliche("bald-faced lie"), cliche("ballpark figure"), cliche("banging your head against a brick wall"), cliche("baptism by fire"), cliche("barking up the wrong tree"), cliche("bat out of hell"), cliche("be all and end all"), cliche("beat a dead horse"), cliche("beat around the bush"), cliche("been there, done that"), cliche("beggars can't be choosers"), cliche("behind the eight ball"), cliche("bend over backwards"), cliche("benefit of the doubt"), cliche("bent out of shape"), cliche("best thing since sliced bread"), cliche("bet your bottom dollar"), cliche("better half"), cliche("better late than never"), cliche("better mousetrap"), cliche("better safe than sorry"), cliche("between scylla and charybdis"), cliche("between a rock and a hard place"), cliche("between a rock and a hard place"), cliche("between the devil and the deep blue sea"), cliche("betwixt and between"), cliche("beyond the pale"), cliche("bide your time"), cliche("big as life"), cliche("big cheese"), cliche("big fish in a small pond"), cliche("big man on campus"), cliche("bigger they are the harder they fall"), cliche("bird in the hand"), cliche("bird's eye view"), cliche("birds and the bees"), cliche("birds of a feather flock together"), cliche("bit the hand that feeds you"), cliche("bite the bullet"), cliche("bite the dust"), cliche("bitten off more than he can chew"), cliche("black as coal"), cliche("black as pitch"), cliche("black as the ace of spades"), cliche("blast from the past"), cliche("bleeding heart"), cliche("blessing in disguise"), cliche("blind ambition"), cliche("blind as a bat"), cliche("blind leading the blind"), cliche("blissful ignorance"), cliche("blood is thicker than water"), cliche("blood sweat and tears"), cliche("blow a fuse"), cliche("blow off steam"), cliche("blow your own horn"), cliche("blushing bride"), cliche("boils down to"), cliche("bolt from the blue"), cliche("bone to pick"), cliche("bored stiff"), cliche("bored to tears"), cliche("bottomless pit"), cliche("boys will be boys"), cliche("bright and early"), cliche("brings home the bacon"), cliche("broad across the beam"), cliche("broken record"), cliche("brought back to reality"), cliche("bulk large"), cliche("bull by the horns"), cliche("bull in a china shop"), cliche("burn the midnight oil"), cliche("burning question"), cliche("burning the candle at both ends"), cliche("burst your bubble"), cliche("bury the hatchet"), cliche("busy as a bee"), cliche("but that's another story"), cliche("by hook or by crook"), cliche("by no means"), cliche("call a spade a spade"), cliche("called onto the carpet"), cliche("calm before the storm"), cliche("can of worms"), cliche("can't cut the mustard"), cliche("can't hold a candle to"), cliche("case of mistaken identity"), cliche("cast aspersions"), cliche("cat got your tongue"), cliche("cat's meow"), cliche("caught in the crossfire"), cliche("caught red-handed"), cliche("chase a red herring"), cliche("checkered past"), cliche("chomping at the bit"), cliche("cleanliness is next to godliness"), cliche("clear as a bell"), cliche("clear as mud"), cliche("close to the vest"), cliche("cock and bull story"), cliche("cold shoulder"), cliche("come hell or high water"), cliche("comparing apples and oranges"), cliche("conspicuous by its absence"), cliche("conspicuous by its absence"), cliche("cool as a cucumber"), cliche("cool, calm, and collected"), cliche("cost a king's ransom"), cliche("count your blessings"), cliche("crack of dawn"), cliche("crash course"), cliche("creature comforts"), cliche("cross that bridge when you come to it"), cliche("crushing blow"), cliche("cry like a baby"), cliche("cry me a river"), cliche("cry over spilt milk"), cliche("crystal clear"), cliche("crystal clear"), cliche("curiosity killed the cat"), cliche("cut and dried"), cliche("cut through the red tape"), cliche("cut to the chase"), cliche("cute as a bugs ear"), cliche("cute as a button"), cliche("cute as a puppy"), cliche("cuts to the quick"), cliche("cutting edge"), cliche("dark before the dawn"), cliche("day in, day out"), cliche("dead as a doornail"), cliche("decision-making process"), cliche("devil is in the details"), cliche("dime a dozen"), cliche("divide and conquer"), cliche("dog and pony show"), cliche("dog days"), cliche("dog eat dog"), cliche("dog tired"), cliche("don't burn your bridges"), cliche("don't count your chickens"), cliche("don't look a gift horse in the mouth"), cliche("don't rock the boat"), cliche("don't step on anyone's toes"), cliche("don't take any wooden nickels"), cliche("down and out"), cliche("down at the heels"), cliche("down in the dumps"), cliche("down the hatch"), cliche("down to earth"), cliche("draw the line"), cliche("dressed to kill"), cliche("dressed to the nines"), cliche("drives me up the wall"), cliche("dubious distinction"), cliche("dull
cliche
identifier_name
cliches.go
// ShouldNotCliche returns a slice of BadTerm's, none of which should be in the // a text (case insensitive). See existence_checks.go for details of BadTerms. func ShouldNotCliche() []TextCheck { return []TextCheck{ cliche("all hell broke loose"), cliche("american as apple pie"), cliche("hobson's choice"), cliche("i beg to differ"), cliche("jack of all trades"), cliche("a chip off the old block"), cliche("a clean slate"), cliche("a dark and stormy night"), cliche("a far cry"), cliche("a fate worse than death"), cliche("a fine kettle of fish"), cliche("a loose cannon"), cliche("a matter of concern"), cliche("a penny saved is a penny earned"), cliche("a tough row to hoe"), cliche("a word to the wise"), cliche("ace in the hole"), cliche("acid test"), cliche("add insult to injury"), cliche("against all odds"), cliche("air your dirty laundry"), cliche("alas and alack"), cliche("all fun and games"), cliche("all in a day's work"), cliche("all talk, no action"), cliche("all things being equal"), cliche("all thumbs"), cliche("all your eggs in one basket"), cliche("all's fair in love and war"), cliche("all's well that ends well"), cliche("almighty dollar"), cliche("an axe? to grind"), cliche("another day, another dollar"), cliche("armed to the teeth"), cliche("as a last resort"), cliche("as luck would have it"), cliche("as old as time"), cliche("as the crow flies"), cliche("at loose ends"), cliche("at my wits end"), cliche("at the end of the day"), cliche("attached hereto"), cliche("avoid like the plague"), cliche("babe in the woods"), cliche("back against the wall"), cliche("back in the saddle"), cliche("back to square one"), cliche("back to the drawing board"), cliche("bad to the bone"), cliche("badge of honor"), cliche("bald faced liar"), cliche("bald-faced lie"), cliche("ballpark figure"), cliche("banging your head against a brick wall"), cliche("baptism by fire"), cliche("barking up the wrong tree"), cliche("bat out of hell"), cliche("be all and end all"), cliche("beat a dead horse"), cliche("beat around the bush"), cliche("been there, done that"), cliche("beggars can't be choosers"), cliche("behind the eight ball"), cliche("bend over backwards"), cliche("benefit of the doubt"), cliche("bent out of shape"), cliche("best thing since sliced bread"), cliche("bet your bottom dollar"), cliche("better half"), cliche("better late than never"), cliche("better mousetrap"), cliche("better safe than sorry"), cliche("between scylla and charybdis"), cliche("between a rock and a hard place"), cliche("between a rock and a hard place"), cliche("between the devil and the deep blue sea"), cliche("betwixt and between"), cliche("beyond the pale"), cliche("bide your time"), cliche("big as life"), cliche("big cheese"), cliche("big fish in a small pond"), cliche("big man on campus"), cliche("bigger they are the harder they fall"), cliche("bird in the hand"), cliche("bird's eye view"), cliche("birds and the bees"), cliche("birds of a feather flock together"), cliche("bit the hand that feeds you"), cliche("bite the bullet"), cliche("bite the dust"), cliche("bitten off more than he can chew"), cliche("black as coal"), cliche("black as pitch"), cliche("black as the ace of spades"), cliche("blast from the past"), cliche("bleeding heart"), cliche("blessing in disguise"), cliche("blind ambition"), cliche("blind as a bat"), cliche("blind leading the blind"), cliche("blissful ignorance"), cliche("blood is thicker than water"), cliche("blood sweat and tears"), cliche("blow a fuse"), cliche("blow off steam"), cliche("blow your own horn"), cliche("blushing bride"), cliche("boils down to"), cliche("bolt from the blue"), cliche("bone to pick"), cliche("bored stiff"), cliche("bored to tears"), cliche("bottomless pit"), cliche("boys will be boys"), cliche("bright and early"), cliche("brings home the bacon"), cliche("broad across the beam"), cliche("broken record"), cliche("brought back to reality"), cliche("bulk large"), cliche("bull by the horns"), cliche("bull in a china shop"), cliche("burn the midnight oil"), cliche("burning question"), cliche("burning the candle at both ends"), cliche("burst your bubble"), cliche("bury the hatchet"), cliche("busy as a bee"), cliche("but that's another story"), cliche("by hook or by crook"), cliche("by no means"), cliche("call a spade a spade"), cliche("called onto the carpet"), cliche("calm before the storm"), cliche("can of worms"), cliche("can't cut the mustard"), cliche("can't hold a candle to"), cliche("case of mistaken identity"), cliche("cast aspersions"), cliche("cat got your tongue"), cliche("cat's meow"), cliche("caught in the crossfire"), cliche("caught red-handed"), cliche("chase a red herring"), cliche("checkered past"), cliche("chomping at the bit"), cliche("cleanliness is next to godliness"), cliche("clear as a bell"), cliche("clear as mud"), cliche("close to the vest"), cliche("cock and bull story"), cliche("cold shoulder"), cliche("come hell or high water"), cliche("comparing apples and oranges"), cliche("conspicuous by its absence"), cliche("conspicuous by its absence"), cliche("cool as a cucumber"), cliche("cool, calm, and collected"), cliche("cost a king's ransom"), cliche("count your blessings"), cliche("crack of dawn"), cliche("crash course"), cliche("creature comforts"), cliche("cross that bridge when you come to it"), cliche("crushing blow"), cliche("cry like a baby"), cliche("cry me a river"), cliche("cry over spilt milk"), cliche("crystal clear"), cliche("crystal clear"), cliche("curiosity killed the cat"), cliche("cut and dried"), cliche("cut through the red tape"), cliche("cut to the chase"), cliche("cute as a bugs ear"), cliche("cute as a button"), cliche("cute as a puppy"), cliche("cuts to the quick"), cliche("cutting edge"), cliche("dark before the dawn"), cliche("day in, day out"), cliche("dead as a doornail"), cliche("decision-making process"), cliche("devil is in the details"), cliche("dime a dozen"), cliche("divide and conquer"), cliche("dog and pony show"), cliche("dog days"), cliche("dog eat dog"), cliche("dog tired"), cliche("don't burn your bridges"), cliche("don't count your chickens"), cliche("don't look a gift horse in the mouth"), cliche("don't rock the boat"), cliche("don't step on anyone's toes"), cliche("don't take any wooden nickels"), cliche("down and out"), cliche("down at the heels"), cliche("down in the dumps"), cliche("down the hatch"), cliche("down to earth"), cliche("draw the line"), cliche("dressed to kill"), cliche("dressed to the nines"), cliche("drives me up the wall"), cliche("dubious distinction"), cliche("dull as dishwater"), cliche
{ return &BadTerm{match, "'%s' is a cliche. Avoid it like the plague."} }
identifier_body
cliches.go
"), cliche("cry like a baby"), cliche("cry me a river"), cliche("cry over spilt milk"), cliche("crystal clear"), cliche("crystal clear"), cliche("curiosity killed the cat"), cliche("cut and dried"), cliche("cut through the red tape"), cliche("cut to the chase"), cliche("cute as a bugs ear"), cliche("cute as a button"), cliche("cute as a puppy"), cliche("cuts to the quick"), cliche("cutting edge"), cliche("dark before the dawn"),
cliche("dime a dozen"), cliche("divide and conquer"), cliche("dog and pony show"), cliche("dog days"), cliche("dog eat dog"), cliche("dog tired"), cliche("don't burn your bridges"), cliche("don't count your chickens"), cliche("don't look a gift horse in the mouth"), cliche("don't rock the boat"), cliche("don't step on anyone's toes"), cliche("don't take any wooden nickels"), cliche("down and out"), cliche("down at the heels"), cliche("down in the dumps"), cliche("down the hatch"), cliche("down to earth"), cliche("draw the line"), cliche("dressed to kill"), cliche("dressed to the nines"), cliche("drives me up the wall"), cliche("dubious distinction"), cliche("dull as dishwater"), cliche("duly authorized"), cliche("dyed in the wool"), cliche("eagle eye"), cliche("ear to the ground"), cliche("early bird catches the worm"), cliche("easier said than done"), cliche("easier said than done"), cliche("easy as pie"), cliche("eat your heart out"), cliche("eat your words"), cliche("eleventh hour"), cliche("enclosed herewith"), cliche("even the playing field"), cliche("every dog has its day"), cliche("every fiber of my being"), cliche("everything but the kitchen sink"), cliche("eye for an eye"), cliche("eyes peeled"), cliche("face the music"), cliche("facts of life"), cliche("fair weather friend"), cliche("fall by the wayside"), cliche("fan the flames"), cliche("far be it from me"), cliche("fast and loose"), cliche("feast or famine"), cliche("feather your nest"), cliche("feathered friends"), cliche("few and far between"), cliche("fifteen minutes of fame"), cliche("fills the bill"), cliche("filthy vermin"), cliche("fine kettle of fish"), cliche("first and foremost"), cliche("fish out of water"), cliche("fishing for a compliment"), cliche("fit as a fiddle"), cliche("fit the bill"), cliche("fit to be tied"), cliche("flash in the pan"), cliche("flat as a pancake"), cliche("flip your lid"), cliche("flog a dead horse"), cliche("fly by night"), cliche("fly the coop"), cliche("follow your heart"), cliche("for all intents and purposes"), cliche("for free"), cliche("for the birds"), cliche("for what it's worth"), cliche("force of nature"), cliche("force to be reckoned with"), cliche("forgive and forget"), cliche("fox in the henhouse"), cliche("free and easy"), cliche("free as a bird"), cliche("fresh as a daisy"), cliche("full steam ahead"), cliche("fun in the sun"), cliche("garbage in, garbage out"), cliche("gentle as a lamb"), cliche("get a kick out of"), cliche("get a leg up"), cliche("get down and dirty"), cliche("get the lead out"), cliche("get to the bottom of"), cliche("get with the program"), cliche("get your feet wet"), cliche("gets my goat"), cliche("gilding the lily"), cliche("gilding the lily"), cliche("give and take"), cliche("go against the grain"), cliche("go at it tooth and nail"), cliche("go for broke"), cliche("go him one better"), cliche("go the extra mile"), cliche("go with the flow"), cliche("goes without saying"), cliche("good as gold"), cliche("good deed for the day"), cliche("good things come to those who wait"), cliche("good time was had by all"), cliche("good times were had by all"), cliche("greased lightning"), cliche("greek to me"), cliche("green thumb"), cliche("green-eyed monster"), cliche("grist for the mill"), cliche("growing like a weed"), cliche("hair of the dog"), cliche("hand to mouth"), cliche("happy as a clam"), cliche("happy as a lark"), cliche("hasn't a clue"), cliche("have a nice day"), cliche("have a short fuse"), cliche("have high hopes"), cliche("have the last laugh"), cliche("haven't got a row to hoe"), cliche("he's got his hands full"), cliche("head honcho"), cliche("head over heels"), cliche("hear a pin drop"), cliche("heard it through the grapevine"), cliche("heart's content"), cliche("heavy as lead"), cliche("hem and haw"), cliche("high and dry"), cliche("high and mighty"), cliche("high as a kite"), cliche("his own worst enemy"), cliche("his work cut out for him"), cliche("hit paydirt"), cliche("hither and yon"), cliche("hold your head up high"), cliche("hold your horses"), cliche("hold your own"), cliche("hold your tongue"), cliche("honest as the day is long"), cliche("horns of a dilemma"), cliche("horns of a dilemma"), cliche("horse of a different color"), cliche("hot under the collar"), cliche("hour of need"), cliche("icing on the cake"), cliche("if and when"), cliche("if the shoe fits"), cliche("if the shoe were on the other foot"), cliche("if you catch my drift"), cliche("in a jam"), cliche("in a jiffy"), cliche("in a nutshell"), cliche("in a pig's eye"), cliche("in a pinch"), cliche("in a word"), cliche("in hot water"), cliche("in light of"), cliche("in reference to"), cliche("in short supply"), cliche("in the final analysis"), cliche("in the foreseeable future"), cliche("in the gutter"), cliche("in the last analysis"), cliche("in the long run"), cliche("in the matter of"), cliche("in the nick of time"), cliche("in the thick of it"), cliche("in your dreams"), cliche("innocent bystander"), cliche("it ain't over till the fat lady sings"), cliche("it goes without saying"), cliche("it stands to reason"), cliche("it takes all kinds"), cliche("it takes one to know one"), cliche("it's a small world"), cliche("it's not what you know, it's who you know"), cliche("it's only a matter of time"), cliche("ivory tower"), cliche("jockey for position"), cliche("jog your memory"), cliche("joined at the hip"), cliche("judge a book by its cover"), cliche("jump down your throat"), cliche("jump in with both feet"), cliche("jump on the bandwagon"), cliche("jump the gun"), cliche("jump to conclusions"), cliche("just a hop, skip, and a jump"), cliche("just the ticket"), cliche("justice is blind"), cliche("keep a stiff upper lip"), cliche("keep an eye on"), cliche("keep it simple, stupid"), cliche("keep the home fires burning"), cliche("keep up with the joneses"), cliche("keep your chin up"), cliche("keep your fingers crossed"), cliche("kick the bucket"),
cliche("day in, day out"), cliche("dead as a doornail"), cliche("decision-making process"), cliche("devil is in the details"),
random_line_split
spi_host.rs
0 => istate_clr: ReadWrite<u32, ISTATE_CLR::Register>), (0x0014 => _reserved), (0x1000 => tx_fifo: [WriteOnly<u8>; 128]), (0x1080 => rx_fifo: [ReadOnly<u8>; 128]), (0x1100 => @END), } } register_bitfields![u32, CTRL [
CSBSU OFFSET(2) NUMBITS(4) [], /// CSB from SCK hold time in SCK cycles + 1 (defined with respect to /// the last SCK edge) CSBHLD OFFSET(6) NUMBITS(4) [], /// SPI Clk Divider. Actual divider is IDIV+1. A value of 0 gives divide /// by 1 clock, 1 gives divide by 2 etc. IDIV OFFSET(10) NUMBITS(12) [], /// Polarity of CSB signal. 0:active low 1:active high CSBPOL OFFSET(22) NUMBITS(1) [], /// Order in which bits of byte are sent. 0: send bit 0 first. 1: send /// bit 7 first TXBITOR OFFSET(23) NUMBITS(1) [], /// Order in which bytes of buffer word are sent. /// 0: send byte 0 first. 1: send byte 3 first TXBYTOR OFFSET(24) NUMBITS(1) [], /// Order in which received bits are packed into byte. /// 0: first bit received is bit0 1: last bit received is bit 0 RXBITOR OFFSET(25) NUMBITS(1) [], /// Order in which received bytes are packed into word. /// 0: first byte received is byte 0 1: first byte received is byte 3 RXBYTOR OFFSET(26) NUMBITS(1) [], /// SPI Passthrough Mode. 0: Disable, 1: Enable. This is the host side /// control of whether passthrough is allowed. In order for full /// passthrough functionality, both the host and device passthrough /// functionality have to be enabled ENPASSTHRU OFFSET(27) NUMBITS(1) [] ], XACT [ /// Initiate transaction in buffer START OFFSET(0) NUMBITS(1) [], /// Bits-1 in last byte transferred. The default assumes last byte will /// have 8 bits, this should be sufficient for most usage. BCNT OFFSET(1) NUMBITS(3) [], /// Total number of transactions in bytes-1. If 64 bytes are to be /// transferred, this should be programmed as 63. SIZE OFFSET(4) NUMBITS(7) [], /// Poll for ready RDY_POLL OFFSET(11) NUMBITS(1) [], /// Delay before polling in PCLK cycles + 1 RDY_POLL_DLY OFFSET(12) NUMBITS(5) [] ], ICTRL [ /// TX interrupt enable TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE [ /// TX done interrupt TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE_CLR [ /// TX done interrupt clear TXDONE OFFSET(0) NUMBITS(1) [] ] ]; const SPI_HOST0_BASE_ADDR: u32 = 0x4070_0000; const SPI_HOST1_BASE_ADDR: u32 = 0x4071_0000; const SPI_HOST0_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST0_BASE_ADDR as *const Registers) }; const SPI_HOST1_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST1_BASE_ADDR as *const Registers) }; pub static mut SPI_HOST0: SpiHostHardware = SpiHostHardware::new(SPI_HOST0_REGISTERS); pub static mut SPI_HOST1: SpiHostHardware = SpiHostHardware::new(SPI_HOST1_REGISTERS); /// A SPI Host pub struct SpiHostHardware { registers: StaticRef<Registers>, transaction_len: Cell<usize>, tx_buffer: TakeCell<'static, [u8]>, rx_buffer: TakeCell<'static, [u8]>, client: OptionalCell<&'static dyn SpiMasterClient>, } impl SpiHostHardware { const fn new(base_addr: StaticRef<Registers>) -> SpiHostHardware { SpiHostHardware { registers: base_addr, transaction_len: Cell::new(0), tx_buffer: TakeCell::empty(), rx_buffer: TakeCell::empty(), client: OptionalCell::empty(), } } pub fn init(&self) { self.registers.ctrl.write( CTRL::CPOL::CLEAR + CTRL::CPHA::CLEAR + CTRL::CSBSU::CLEAR + CTRL::CSBHLD::CLEAR + CTRL::IDIV.val(2) + CTRL::CSBPOL::CLEAR + CTRL::TXBITOR::SET + CTRL::TXBYTOR::CLEAR + CTRL::RXBITOR::SET + CTRL::RXBYTOR::CLEAR + CTRL::ENPASSTHRU::CLEAR); self.registers.xact.write( XACT::START::CLEAR + XACT::BCNT.val(7) + XACT::SIZE.val(0) + XACT::RDY_POLL::CLEAR + XACT::RDY_POLL_DLY.val(0)); } fn enable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::SET); } fn disable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::CLEAR); } pub fn handle_interrupt(&self) { //debug!("SpiHostHardware::handle_interrupt: ISTATE = {:08x}", self.registers.istate.get()); if self.registers.istate.is_set(ISTATE::TXDONE) { self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.client.map(|client| { self.tx_buffer.take() .map(|tx_buf| { self.rx_buffer .map(|rx_buf| { self.read_data(rx_buf); }); client.read_write_done( tx_buf, self.rx_buffer.take(), self.transaction_len.get()) }); }); } self.disable_tx_interrupt(); } fn start_transaction( &self, write_buffer: Option<&'static mut [u8]>, read_buffer: Option<&'static mut [u8]>, transaction_len: usize) -> ReturnCode { //debug!("SpiHostHardware::start_transaction: transaction_len={}", transaction_len); // The transaction needs at least one byte. // It also cannot have more bytes than tx_fifo or rx_fifo is long. if (transaction_len == 0) || (transaction_len > self.registers.tx_fifo.len()) || (transaction_len > self.registers.rx_fifo.len()) { //debug!("SpiHostHardware::start_transaction: Invalid transaction_len={}", transaction_len); return ReturnCode::ESIZE; } self.registers.xact.modify(XACT::BCNT.val(7)); self.registers.xact.modify(XACT::SIZE.val((transaction_len - 1) as u32)); let mut tx_buf_len = 0; write_buffer.as_ref().map(|tx_buf| { tx_buf_len = min(tx_buf.len(), transaction_len); for idx in 0..tx_buf_len { self.registers.tx_fifo[idx].set(tx_buf[idx]); } }); // Clear the TX FIFO for additional bytes not supplied by write_buffer. // Since we have no control over how many bytes the SPI host reads, we // want to make sure to not accidentally leak information that made it // into the TX FIFO beyond the length of the `write_buffer`. for idx in tx_buf_len..transaction_len { self.registers.tx_fifo[idx].set(0xff); } write_buffer.map(|buf| { self.tx_buffer.replace(buf); }); read_buffer.map(|buf| { self.rx_buffer.replace(buf); }); self.transaction_len.set(transaction_len); self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.enable_tx_interrupt(); self.registers.xact.modify(XACT::START::SET); ReturnCode::SUCCESS } fn read_data(&self, read_buffer: &mut [u8]) { let read_len = min(read_buffer.len(), self.transaction_len.get()); for idx in 0..read_len { let val = self.registers.rx_fifo[idx].get(); read_buffer[idx] = val; } } } impl SpiHost for SpiHostHardware { fn spi_device_spi_host_passthrough(&self, enabled: bool) { self.registers.ctrl.modify( if
/// CPOL setting CPOL OFFSET(0) NUMBITS(1) [], /// CPHA setting CPHA OFFSET(1) NUMBITS(1) [], /// CSB to SCK setup time in SCK cycles + 1.5
random_line_split
spi_host.rs
(24) NUMBITS(1) [], /// Order in which received bits are packed into byte. /// 0: first bit received is bit0 1: last bit received is bit 0 RXBITOR OFFSET(25) NUMBITS(1) [], /// Order in which received bytes are packed into word. /// 0: first byte received is byte 0 1: first byte received is byte 3 RXBYTOR OFFSET(26) NUMBITS(1) [], /// SPI Passthrough Mode. 0: Disable, 1: Enable. This is the host side /// control of whether passthrough is allowed. In order for full /// passthrough functionality, both the host and device passthrough /// functionality have to be enabled ENPASSTHRU OFFSET(27) NUMBITS(1) [] ], XACT [ /// Initiate transaction in buffer START OFFSET(0) NUMBITS(1) [], /// Bits-1 in last byte transferred. The default assumes last byte will /// have 8 bits, this should be sufficient for most usage. BCNT OFFSET(1) NUMBITS(3) [], /// Total number of transactions in bytes-1. If 64 bytes are to be /// transferred, this should be programmed as 63. SIZE OFFSET(4) NUMBITS(7) [], /// Poll for ready RDY_POLL OFFSET(11) NUMBITS(1) [], /// Delay before polling in PCLK cycles + 1 RDY_POLL_DLY OFFSET(12) NUMBITS(5) [] ], ICTRL [ /// TX interrupt enable TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE [ /// TX done interrupt TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE_CLR [ /// TX done interrupt clear TXDONE OFFSET(0) NUMBITS(1) [] ] ]; const SPI_HOST0_BASE_ADDR: u32 = 0x4070_0000; const SPI_HOST1_BASE_ADDR: u32 = 0x4071_0000; const SPI_HOST0_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST0_BASE_ADDR as *const Registers) }; const SPI_HOST1_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST1_BASE_ADDR as *const Registers) }; pub static mut SPI_HOST0: SpiHostHardware = SpiHostHardware::new(SPI_HOST0_REGISTERS); pub static mut SPI_HOST1: SpiHostHardware = SpiHostHardware::new(SPI_HOST1_REGISTERS); /// A SPI Host pub struct SpiHostHardware { registers: StaticRef<Registers>, transaction_len: Cell<usize>, tx_buffer: TakeCell<'static, [u8]>, rx_buffer: TakeCell<'static, [u8]>, client: OptionalCell<&'static dyn SpiMasterClient>, } impl SpiHostHardware { const fn new(base_addr: StaticRef<Registers>) -> SpiHostHardware { SpiHostHardware { registers: base_addr, transaction_len: Cell::new(0), tx_buffer: TakeCell::empty(), rx_buffer: TakeCell::empty(), client: OptionalCell::empty(), } } pub fn init(&self) { self.registers.ctrl.write( CTRL::CPOL::CLEAR + CTRL::CPHA::CLEAR + CTRL::CSBSU::CLEAR + CTRL::CSBHLD::CLEAR + CTRL::IDIV.val(2) + CTRL::CSBPOL::CLEAR + CTRL::TXBITOR::SET + CTRL::TXBYTOR::CLEAR + CTRL::RXBITOR::SET + CTRL::RXBYTOR::CLEAR + CTRL::ENPASSTHRU::CLEAR); self.registers.xact.write( XACT::START::CLEAR + XACT::BCNT.val(7) + XACT::SIZE.val(0) + XACT::RDY_POLL::CLEAR + XACT::RDY_POLL_DLY.val(0)); } fn enable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::SET); } fn disable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::CLEAR); } pub fn handle_interrupt(&self) { //debug!("SpiHostHardware::handle_interrupt: ISTATE = {:08x}", self.registers.istate.get()); if self.registers.istate.is_set(ISTATE::TXDONE) { self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.client.map(|client| { self.tx_buffer.take() .map(|tx_buf| { self.rx_buffer .map(|rx_buf| { self.read_data(rx_buf); }); client.read_write_done( tx_buf, self.rx_buffer.take(), self.transaction_len.get()) }); }); } self.disable_tx_interrupt(); } fn start_transaction( &self, write_buffer: Option<&'static mut [u8]>, read_buffer: Option<&'static mut [u8]>, transaction_len: usize) -> ReturnCode { //debug!("SpiHostHardware::start_transaction: transaction_len={}", transaction_len); // The transaction needs at least one byte. // It also cannot have more bytes than tx_fifo or rx_fifo is long. if (transaction_len == 0) || (transaction_len > self.registers.tx_fifo.len()) || (transaction_len > self.registers.rx_fifo.len()) { //debug!("SpiHostHardware::start_transaction: Invalid transaction_len={}", transaction_len); return ReturnCode::ESIZE; } self.registers.xact.modify(XACT::BCNT.val(7)); self.registers.xact.modify(XACT::SIZE.val((transaction_len - 1) as u32)); let mut tx_buf_len = 0; write_buffer.as_ref().map(|tx_buf| { tx_buf_len = min(tx_buf.len(), transaction_len); for idx in 0..tx_buf_len { self.registers.tx_fifo[idx].set(tx_buf[idx]); } }); // Clear the TX FIFO for additional bytes not supplied by write_buffer. // Since we have no control over how many bytes the SPI host reads, we // want to make sure to not accidentally leak information that made it // into the TX FIFO beyond the length of the `write_buffer`. for idx in tx_buf_len..transaction_len { self.registers.tx_fifo[idx].set(0xff); } write_buffer.map(|buf| { self.tx_buffer.replace(buf); }); read_buffer.map(|buf| { self.rx_buffer.replace(buf); }); self.transaction_len.set(transaction_len); self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.enable_tx_interrupt(); self.registers.xact.modify(XACT::START::SET); ReturnCode::SUCCESS } fn read_data(&self, read_buffer: &mut [u8]) { let read_len = min(read_buffer.len(), self.transaction_len.get()); for idx in 0..read_len { let val = self.registers.rx_fifo[idx].get(); read_buffer[idx] = val; } } } impl SpiHost for SpiHostHardware { fn spi_device_spi_host_passthrough(&self, enabled: bool) { self.registers.ctrl.modify( if enabled { CTRL::ENPASSTHRU::SET } else { CTRL::ENPASSTHRU::CLEAR }); } fn wait_busy_clear_in_transactions(&self, enabled: bool) { self.registers.xact.modify( if enabled { XACT::RDY_POLL::SET } else { XACT::RDY_POLL::CLEAR }); } } impl SpiMaster for SpiHostHardware { type ChipSelect = bool; fn set_client(&self, client: &'static dyn kernel::hil::spi::SpiMasterClient) { self.client.set(client); } fn init(&self) {} fn is_busy(&self) -> bool { self.registers.istate.is_set(ISTATE::TXDONE) } fn read_write_bytes( &self, write_buffer: &'static mut [u8], read_buffer: Option<&'static mut [u8]>, len: usize, ) -> ReturnCode { // If busy, don't start if self.is_busy() { return ReturnCode::EBUSY; } self.start_transaction(Some(write_buffer), read_buffer, len) } fn write_byte(&self, _val: u8) { panic!("write_byte is not implemented"); } fn read_byte(&self) -> u8 { panic!("read_byte is not implemented"); } fn read_write_byte(&self, _val: u8) -> u8 { panic!("read_write_byte is not implemented"); } fn specify_chip_select(&self, _cs: Self::ChipSelect) { // Nothing to be done } /// Returns the actual rate set fn set_rate(&self, _rate: u32) -> u32
{ panic!("set_rate is not implemented"); }
identifier_body
spi_host.rs
byte received is byte 0 1: first byte received is byte 3 RXBYTOR OFFSET(26) NUMBITS(1) [], /// SPI Passthrough Mode. 0: Disable, 1: Enable. This is the host side /// control of whether passthrough is allowed. In order for full /// passthrough functionality, both the host and device passthrough /// functionality have to be enabled ENPASSTHRU OFFSET(27) NUMBITS(1) [] ], XACT [ /// Initiate transaction in buffer START OFFSET(0) NUMBITS(1) [], /// Bits-1 in last byte transferred. The default assumes last byte will /// have 8 bits, this should be sufficient for most usage. BCNT OFFSET(1) NUMBITS(3) [], /// Total number of transactions in bytes-1. If 64 bytes are to be /// transferred, this should be programmed as 63. SIZE OFFSET(4) NUMBITS(7) [], /// Poll for ready RDY_POLL OFFSET(11) NUMBITS(1) [], /// Delay before polling in PCLK cycles + 1 RDY_POLL_DLY OFFSET(12) NUMBITS(5) [] ], ICTRL [ /// TX interrupt enable TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE [ /// TX done interrupt TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE_CLR [ /// TX done interrupt clear TXDONE OFFSET(0) NUMBITS(1) [] ] ]; const SPI_HOST0_BASE_ADDR: u32 = 0x4070_0000; const SPI_HOST1_BASE_ADDR: u32 = 0x4071_0000; const SPI_HOST0_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST0_BASE_ADDR as *const Registers) }; const SPI_HOST1_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST1_BASE_ADDR as *const Registers) }; pub static mut SPI_HOST0: SpiHostHardware = SpiHostHardware::new(SPI_HOST0_REGISTERS); pub static mut SPI_HOST1: SpiHostHardware = SpiHostHardware::new(SPI_HOST1_REGISTERS); /// A SPI Host pub struct SpiHostHardware { registers: StaticRef<Registers>, transaction_len: Cell<usize>, tx_buffer: TakeCell<'static, [u8]>, rx_buffer: TakeCell<'static, [u8]>, client: OptionalCell<&'static dyn SpiMasterClient>, } impl SpiHostHardware { const fn new(base_addr: StaticRef<Registers>) -> SpiHostHardware { SpiHostHardware { registers: base_addr, transaction_len: Cell::new(0), tx_buffer: TakeCell::empty(), rx_buffer: TakeCell::empty(), client: OptionalCell::empty(), } } pub fn init(&self) { self.registers.ctrl.write( CTRL::CPOL::CLEAR + CTRL::CPHA::CLEAR + CTRL::CSBSU::CLEAR + CTRL::CSBHLD::CLEAR + CTRL::IDIV.val(2) + CTRL::CSBPOL::CLEAR + CTRL::TXBITOR::SET + CTRL::TXBYTOR::CLEAR + CTRL::RXBITOR::SET + CTRL::RXBYTOR::CLEAR + CTRL::ENPASSTHRU::CLEAR); self.registers.xact.write( XACT::START::CLEAR + XACT::BCNT.val(7) + XACT::SIZE.val(0) + XACT::RDY_POLL::CLEAR + XACT::RDY_POLL_DLY.val(0)); } fn enable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::SET); } fn disable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::CLEAR); } pub fn handle_interrupt(&self) { //debug!("SpiHostHardware::handle_interrupt: ISTATE = {:08x}", self.registers.istate.get()); if self.registers.istate.is_set(ISTATE::TXDONE) { self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.client.map(|client| { self.tx_buffer.take() .map(|tx_buf| { self.rx_buffer .map(|rx_buf| { self.read_data(rx_buf); }); client.read_write_done( tx_buf, self.rx_buffer.take(), self.transaction_len.get()) }); }); } self.disable_tx_interrupt(); } fn start_transaction( &self, write_buffer: Option<&'static mut [u8]>, read_buffer: Option<&'static mut [u8]>, transaction_len: usize) -> ReturnCode { //debug!("SpiHostHardware::start_transaction: transaction_len={}", transaction_len); // The transaction needs at least one byte. // It also cannot have more bytes than tx_fifo or rx_fifo is long. if (transaction_len == 0) || (transaction_len > self.registers.tx_fifo.len()) || (transaction_len > self.registers.rx_fifo.len()) { //debug!("SpiHostHardware::start_transaction: Invalid transaction_len={}", transaction_len); return ReturnCode::ESIZE; } self.registers.xact.modify(XACT::BCNT.val(7)); self.registers.xact.modify(XACT::SIZE.val((transaction_len - 1) as u32)); let mut tx_buf_len = 0; write_buffer.as_ref().map(|tx_buf| { tx_buf_len = min(tx_buf.len(), transaction_len); for idx in 0..tx_buf_len { self.registers.tx_fifo[idx].set(tx_buf[idx]); } }); // Clear the TX FIFO for additional bytes not supplied by write_buffer. // Since we have no control over how many bytes the SPI host reads, we // want to make sure to not accidentally leak information that made it // into the TX FIFO beyond the length of the `write_buffer`. for idx in tx_buf_len..transaction_len { self.registers.tx_fifo[idx].set(0xff); } write_buffer.map(|buf| { self.tx_buffer.replace(buf); }); read_buffer.map(|buf| { self.rx_buffer.replace(buf); }); self.transaction_len.set(transaction_len); self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.enable_tx_interrupt(); self.registers.xact.modify(XACT::START::SET); ReturnCode::SUCCESS } fn read_data(&self, read_buffer: &mut [u8]) { let read_len = min(read_buffer.len(), self.transaction_len.get()); for idx in 0..read_len { let val = self.registers.rx_fifo[idx].get(); read_buffer[idx] = val; } } } impl SpiHost for SpiHostHardware { fn spi_device_spi_host_passthrough(&self, enabled: bool) { self.registers.ctrl.modify( if enabled { CTRL::ENPASSTHRU::SET } else { CTRL::ENPASSTHRU::CLEAR }); } fn wait_busy_clear_in_transactions(&self, enabled: bool) { self.registers.xact.modify( if enabled { XACT::RDY_POLL::SET } else { XACT::RDY_POLL::CLEAR }); } } impl SpiMaster for SpiHostHardware { type ChipSelect = bool; fn set_client(&self, client: &'static dyn kernel::hil::spi::SpiMasterClient) { self.client.set(client); } fn init(&self) {} fn is_busy(&self) -> bool { self.registers.istate.is_set(ISTATE::TXDONE) } fn read_write_bytes( &self, write_buffer: &'static mut [u8], read_buffer: Option<&'static mut [u8]>, len: usize, ) -> ReturnCode { // If busy, don't start if self.is_busy() { return ReturnCode::EBUSY; } self.start_transaction(Some(write_buffer), read_buffer, len) } fn write_byte(&self, _val: u8) { panic!("write_byte is not implemented"); } fn read_byte(&self) -> u8 { panic!("read_byte is not implemented"); } fn read_write_byte(&self, _val: u8) -> u8 { panic!("read_write_byte is not implemented"); } fn specify_chip_select(&self, _cs: Self::ChipSelect) { // Nothing to be done } /// Returns the actual rate set fn set_rate(&self, _rate: u32) -> u32 { panic!("set_rate is not implemented"); } fn get_rate(&self) -> u32 { panic!("get_rate is not implemented"); } fn set_clock(&self, _polarity: ClockPolarity) { panic!("set_clock is not implemented"); } fn get_clock(&self) -> ClockPolarity { panic!("get_clock is not implemented"); } fn
set_phase
identifier_name
spi_host.rs
_reserved), (0x1000 => tx_fifo: [WriteOnly<u8>; 128]), (0x1080 => rx_fifo: [ReadOnly<u8>; 128]), (0x1100 => @END), } } register_bitfields![u32, CTRL [ /// CPOL setting CPOL OFFSET(0) NUMBITS(1) [], /// CPHA setting CPHA OFFSET(1) NUMBITS(1) [], /// CSB to SCK setup time in SCK cycles + 1.5 CSBSU OFFSET(2) NUMBITS(4) [], /// CSB from SCK hold time in SCK cycles + 1 (defined with respect to /// the last SCK edge) CSBHLD OFFSET(6) NUMBITS(4) [], /// SPI Clk Divider. Actual divider is IDIV+1. A value of 0 gives divide /// by 1 clock, 1 gives divide by 2 etc. IDIV OFFSET(10) NUMBITS(12) [], /// Polarity of CSB signal. 0:active low 1:active high CSBPOL OFFSET(22) NUMBITS(1) [], /// Order in which bits of byte are sent. 0: send bit 0 first. 1: send /// bit 7 first TXBITOR OFFSET(23) NUMBITS(1) [], /// Order in which bytes of buffer word are sent. /// 0: send byte 0 first. 1: send byte 3 first TXBYTOR OFFSET(24) NUMBITS(1) [], /// Order in which received bits are packed into byte. /// 0: first bit received is bit0 1: last bit received is bit 0 RXBITOR OFFSET(25) NUMBITS(1) [], /// Order in which received bytes are packed into word. /// 0: first byte received is byte 0 1: first byte received is byte 3 RXBYTOR OFFSET(26) NUMBITS(1) [], /// SPI Passthrough Mode. 0: Disable, 1: Enable. This is the host side /// control of whether passthrough is allowed. In order for full /// passthrough functionality, both the host and device passthrough /// functionality have to be enabled ENPASSTHRU OFFSET(27) NUMBITS(1) [] ], XACT [ /// Initiate transaction in buffer START OFFSET(0) NUMBITS(1) [], /// Bits-1 in last byte transferred. The default assumes last byte will /// have 8 bits, this should be sufficient for most usage. BCNT OFFSET(1) NUMBITS(3) [], /// Total number of transactions in bytes-1. If 64 bytes are to be /// transferred, this should be programmed as 63. SIZE OFFSET(4) NUMBITS(7) [], /// Poll for ready RDY_POLL OFFSET(11) NUMBITS(1) [], /// Delay before polling in PCLK cycles + 1 RDY_POLL_DLY OFFSET(12) NUMBITS(5) [] ], ICTRL [ /// TX interrupt enable TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE [ /// TX done interrupt TXDONE OFFSET(0) NUMBITS(1) [] ], ISTATE_CLR [ /// TX done interrupt clear TXDONE OFFSET(0) NUMBITS(1) [] ] ]; const SPI_HOST0_BASE_ADDR: u32 = 0x4070_0000; const SPI_HOST1_BASE_ADDR: u32 = 0x4071_0000; const SPI_HOST0_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST0_BASE_ADDR as *const Registers) }; const SPI_HOST1_REGISTERS: StaticRef<Registers> = unsafe { StaticRef::new(SPI_HOST1_BASE_ADDR as *const Registers) }; pub static mut SPI_HOST0: SpiHostHardware = SpiHostHardware::new(SPI_HOST0_REGISTERS); pub static mut SPI_HOST1: SpiHostHardware = SpiHostHardware::new(SPI_HOST1_REGISTERS); /// A SPI Host pub struct SpiHostHardware { registers: StaticRef<Registers>, transaction_len: Cell<usize>, tx_buffer: TakeCell<'static, [u8]>, rx_buffer: TakeCell<'static, [u8]>, client: OptionalCell<&'static dyn SpiMasterClient>, } impl SpiHostHardware { const fn new(base_addr: StaticRef<Registers>) -> SpiHostHardware { SpiHostHardware { registers: base_addr, transaction_len: Cell::new(0), tx_buffer: TakeCell::empty(), rx_buffer: TakeCell::empty(), client: OptionalCell::empty(), } } pub fn init(&self) { self.registers.ctrl.write( CTRL::CPOL::CLEAR + CTRL::CPHA::CLEAR + CTRL::CSBSU::CLEAR + CTRL::CSBHLD::CLEAR + CTRL::IDIV.val(2) + CTRL::CSBPOL::CLEAR + CTRL::TXBITOR::SET + CTRL::TXBYTOR::CLEAR + CTRL::RXBITOR::SET + CTRL::RXBYTOR::CLEAR + CTRL::ENPASSTHRU::CLEAR); self.registers.xact.write( XACT::START::CLEAR + XACT::BCNT.val(7) + XACT::SIZE.val(0) + XACT::RDY_POLL::CLEAR + XACT::RDY_POLL_DLY.val(0)); } fn enable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::SET); } fn disable_tx_interrupt(&self) { self.registers.ictrl.modify(ICTRL::TXDONE::CLEAR); } pub fn handle_interrupt(&self) { //debug!("SpiHostHardware::handle_interrupt: ISTATE = {:08x}", self.registers.istate.get()); if self.registers.istate.is_set(ISTATE::TXDONE) { self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.client.map(|client| { self.tx_buffer.take() .map(|tx_buf| { self.rx_buffer .map(|rx_buf| { self.read_data(rx_buf); }); client.read_write_done( tx_buf, self.rx_buffer.take(), self.transaction_len.get()) }); }); } self.disable_tx_interrupt(); } fn start_transaction( &self, write_buffer: Option<&'static mut [u8]>, read_buffer: Option<&'static mut [u8]>, transaction_len: usize) -> ReturnCode { //debug!("SpiHostHardware::start_transaction: transaction_len={}", transaction_len); // The transaction needs at least one byte. // It also cannot have more bytes than tx_fifo or rx_fifo is long. if (transaction_len == 0) || (transaction_len > self.registers.tx_fifo.len()) || (transaction_len > self.registers.rx_fifo.len()) { //debug!("SpiHostHardware::start_transaction: Invalid transaction_len={}", transaction_len); return ReturnCode::ESIZE; } self.registers.xact.modify(XACT::BCNT.val(7)); self.registers.xact.modify(XACT::SIZE.val((transaction_len - 1) as u32)); let mut tx_buf_len = 0; write_buffer.as_ref().map(|tx_buf| { tx_buf_len = min(tx_buf.len(), transaction_len); for idx in 0..tx_buf_len { self.registers.tx_fifo[idx].set(tx_buf[idx]); } }); // Clear the TX FIFO for additional bytes not supplied by write_buffer. // Since we have no control over how many bytes the SPI host reads, we // want to make sure to not accidentally leak information that made it // into the TX FIFO beyond the length of the `write_buffer`. for idx in tx_buf_len..transaction_len { self.registers.tx_fifo[idx].set(0xff); } write_buffer.map(|buf| { self.tx_buffer.replace(buf); }); read_buffer.map(|buf| { self.rx_buffer.replace(buf); }); self.transaction_len.set(transaction_len); self.registers.istate_clr.write(ISTATE_CLR::TXDONE::SET); self.enable_tx_interrupt(); self.registers.xact.modify(XACT::START::SET); ReturnCode::SUCCESS } fn read_data(&self, read_buffer: &mut [u8]) { let read_len = min(read_buffer.len(), self.transaction_len.get()); for idx in 0..read_len { let val = self.registers.rx_fifo[idx].get(); read_buffer[idx] = val; } } } impl SpiHost for SpiHostHardware { fn spi_device_spi_host_passthrough(&self, enabled: bool) { self.registers.ctrl.modify( if enabled { CTRL::ENPASSTHRU::SET } else
{ CTRL::ENPASSTHRU::CLEAR }
conditional_block
campaign-gant-chart.js
rendering performance for large charts. g.setShowComp(0); g.setShowTaskInfoLink(0); g.setShowTaskInfoRes(0); g.setShowTaskInfoComp(0); g.setFormatArr('Day', 'Week', 'Month', 'Quarter'); // Even with setUseSingleCell using Hour format on such a large chart can cause issues in some browsers //(pID, pName, pStart, pEnd, pStyle, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen, pDepend, pCaption, pNotes, pGantt) for (var i = 0; i < campaignList.length; i++) { var campaign = campaignList[i]; if(campaignIds.length > 0){ if((campaignIds.indexOf(campaign.id+"") == -1)) continue; } //populate campaigns var startDate = new Date(parseFloat(campaign.startDate)); var startDateMM = startDate.getMonth() + 1; var startDateDD = startDate.getDate(); var startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); var endDate = new Date(parseFloat(campaign.endDate)); var endDateMM = endDate.getMonth() + 1; var endDateDD = endDate.getDate(); var endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var campaignId = campaign.id; g.AddTaskItem(new JSGantt.TaskItem(campaignId, campaign.campaignName, startDateString, endDateString, 'ggroupblack', '#', 0, '-', 0, 1, 0, 0, '', '', '', g)); //populate campaignStages for (var j = 0; j < campaign.campaignStagesSet.length; j++) { var campaignStage = campaign.campaignStagesSet[j]; startDate = new Date(parseFloat(campaignStage.startDate)); startDateMM = startDate.getMonth() + 1; startDateDD = startDate.getDate(); startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignStage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var stageId = "1111" + campaignStage.id; g.AddTaskItem(new JSGantt.TaskItem(stageId, campaignStage.stageName, startDateString, endDateString, 'gtaskyellow', '#', 0, '-', 0, 1, campaignId, 0, '', '', '', g)); //populate campaignSubstages for (var k = 0; k < campaignStage.campaignSubstagesSet.length; k++) { var table = ""; var campaignSubstage = campaignStage.campaignSubstagesSet[k]; startDate = new Date(parseFloat(campaignSubstage.startDate)); startDateMM = startDate.getMonth() + 1; startDateDD = startDate.getDate(); startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignSubstage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var substageId = "9999" + campaignSubstage.id; var passDependency = "", failDependency = ""; if((campaignSubstage.ssIdForPass != '-1') && (campaignSubstage.ssIdForPass != '0')) passDependency = "9999" + campaignSubstage.ssIdForPass + "SS"; if((campaignSubstage.ssIdForFail != '-1') && (campaignSubstage.ssIdForFail != '0')) failDependency = "9999" + campaignSubstage.ssIdForFail + "FF"; var dependency = passDependency + "," + failDependency; var substageStatusList = campaignSubstage.campaignSubstageStatusList; var onEnterCount = 0, sentCount = 0, viewedCount = 0, passCount = 0, failCount = 0, noShowCount = 0; for(var l = 0; l < substageStatusList.length; l++){ var status = substageStatusList[l]; if(status.onEnter == true) onEnterCount = onEnterCount + 1; if(status.sent == true) sentCount = sentCount + 1; if(status.pass == true) passCount = passCount + 1; if(status.fail == true) failCount = failCount + 1; if(status.viewed == true) viewedCount = viewedCount + 1; if(status.noShow == true) noShowCount = noShowCount + 1; } table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No. of Users:</span><span class='gTaskText'>" + onEnterCount + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Remainders:</span><span class='gTaskText'>" + campaignSubstage.remainders + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForPass == 0) || (campaignSubstage.ssIdForPass == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForPass]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForFail == 0) || (campaignSubstage.ssIdForFail == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForFail]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show :</span><span class='gTaskText'>" + noShowList[campaignSubstage.noShow] + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Sent: </span><span class='gTaskText'>"+sentCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Viewed: </span><span class='gTaskText'>"+viewedCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show: </span><span class='gTaskText'>"+noShowCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass: </span><span class='gTaskText'>"+passCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail: </span><span class='gTaskText'>"+failCount+"</span></div>";
"gtaskpurple", '#', 0, content, 0, 0, stageId, 0, dependency, '', table, g)); } } } g.Draw(); } else { alert("Error, unable to create Gantt Chart"); } } function previewChart(divName){ element = $("#"+divName); /*$("#downloadChart").show();*/ $("#chartPreviewContainer").show(); $("#printChart").show(); html2canvas(element, { onrendered: function (canvas) { var imgsrc = canvas.toDataURL("image/png"); $("#chartPreviewImage").attr('src',imgsrc); getCanvas = canvas; } }); } function show
var taskStyleIndex = Math.floor(Math.random() * (taskStyle.length - 1)) + 0; var content = campaignSubstage.connectType == 0 ? campaignSubstage.contentId : campaignSubstage.connectUrl; g.AddTaskItem(new JSGantt.TaskItem(substageId, campaignSubstage.campaignSubStageName, startDateString, endDateString,
random_line_split
campaign-gant-chart.js
MM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignStage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var stageId = "1111" + campaignStage.id; g.AddTaskItem(new JSGantt.TaskItem(stageId, campaignStage.stageName, startDateString, endDateString, 'gtaskyellow', '#', 0, '-', 0, 1, campaignId, 0, '', '', '', g)); //populate campaignSubstages for (var k = 0; k < campaignStage.campaignSubstagesSet.length; k++) { var table = ""; var campaignSubstage = campaignStage.campaignSubstagesSet[k]; startDate = new Date(parseFloat(campaignSubstage.startDate)); startDateMM = startDate.getMonth() + 1; startDateDD = startDate.getDate(); startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignSubstage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var substageId = "9999" + campaignSubstage.id; var passDependency = "", failDependency = ""; if((campaignSubstage.ssIdForPass != '-1') && (campaignSubstage.ssIdForPass != '0')) passDependency = "9999" + campaignSubstage.ssIdForPass + "SS"; if((campaignSubstage.ssIdForFail != '-1') && (campaignSubstage.ssIdForFail != '0')) failDependency = "9999" + campaignSubstage.ssIdForFail + "FF"; var dependency = passDependency + "," + failDependency; var substageStatusList = campaignSubstage.campaignSubstageStatusList; var onEnterCount = 0, sentCount = 0, viewedCount = 0, passCount = 0, failCount = 0, noShowCount = 0; for(var l = 0; l < substageStatusList.length; l++){ var status = substageStatusList[l]; if(status.onEnter == true) onEnterCount = onEnterCount + 1; if(status.sent == true) sentCount = sentCount + 1; if(status.pass == true) passCount = passCount + 1; if(status.fail == true) failCount = failCount + 1; if(status.viewed == true) viewedCount = viewedCount + 1; if(status.noShow == true) noShowCount = noShowCount + 1; } table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No. of Users:</span><span class='gTaskText'>" + onEnterCount + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Remainders:</span><span class='gTaskText'>" + campaignSubstage.remainders + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForPass == 0) || (campaignSubstage.ssIdForPass == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForPass]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForFail == 0) || (campaignSubstage.ssIdForFail == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForFail]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show :</span><span class='gTaskText'>" + noShowList[campaignSubstage.noShow] + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Sent: </span><span class='gTaskText'>"+sentCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Viewed: </span><span class='gTaskText'>"+viewedCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show: </span><span class='gTaskText'>"+noShowCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass: </span><span class='gTaskText'>"+passCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail: </span><span class='gTaskText'>"+failCount+"</span></div>"; var taskStyleIndex = Math.floor(Math.random() * (taskStyle.length - 1)) + 0; var content = campaignSubstage.connectType == 0 ? campaignSubstage.contentId : campaignSubstage.connectUrl; g.AddTaskItem(new JSGantt.TaskItem(substageId, campaignSubstage.campaignSubStageName, startDateString, endDateString, "gtaskpurple", '#', 0, content, 0, 0, stageId, 0, dependency, '', table, g)); } } } g.Draw(); } else { alert("Error, unable to create Gantt Chart"); } } function previewChart(divName){ element = $("#"+divName); /*$("#downloadChart").show();*/ $("#chartPreviewContainer").show(); $("#printChart").show(); html2canvas(element, { onrendered: function (canvas) { var imgsrc = canvas.toDataURL("image/png"); $("#chartPreviewImage").attr('src',imgsrc); getCanvas = canvas; } }); } function showLoader(){ $("#form-loader").css("display","inline-block"); $(".wrapper").css("pointer-events", "none"); $(".wrapper").css("opacity", "0.5"); } function hideLoader(){ $("#form-loader").css("display","none"); $(".wrapper").css("pointer-events", ""); $(".wrapper").css("opacity", "1"); } function print(divId){ var contents = $("#"+divId).html(); var frame1 = $('<iframe />'); frame1[0].name = "frame1"; frame1.css({ "position": "absolute", "top": "-1000000px" }); $("body").append(frame1); var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument; frameDoc.document.open(); //Create a new HTML document. var currDate = new Date(); var title = "GanttChart-" + (currDate.getMonth() + 1) + "-" + currDate.getDate() + "-" + currDate.getFullYear(); frameDoc.document.write('<html><head><title>'+title+'</title>'); frameDoc.document.write('</head><body>'); //Append the external CSS file. //frameDoc.document.write('<link href="style.css" rel="stylesheet" type="text/css" />'); //Append the DIV contents. frameDoc.document.write(contents); frameDoc.document.write('</body></html>'); frameDoc.document.close(); setTimeout(function () { window.frames["frame1"].focus(); window.frames["frame1"].print(); frame1.remove(); }, 500); } function expandAllFolders(){ $(".gfoldercollapse").each(function( index ){ var pID = $(this).attr("id").split("_")[1]; console.log(index + ": " + $(this).text() + "\t id : " + pID); JSGantt.folder(pID, {"vTool":{"vToolCont":{},"moveInterval":20,"fadeInterval":23,"delayTimeout":22}}); }); } function printChart(divName)
{ console.log("Loading"); showLoader(); previewChart(divName); setTimeout(function () { print("chartPreviewContainer"); hideLoader(); }, 3500); $("#chartPreviewContainer").hide(); }
identifier_body
campaign-gant-chart.js
TaskItem(new JSGantt.TaskItem(campaignId, campaign.campaignName, startDateString, endDateString, 'ggroupblack', '#', 0, '-', 0, 1, 0, 0, '', '', '', g)); //populate campaignStages for (var j = 0; j < campaign.campaignStagesSet.length; j++) { var campaignStage = campaign.campaignStagesSet[j]; startDate = new Date(parseFloat(campaignStage.startDate)); startDateMM = startDate.getMonth() + 1; startDateDD = startDate.getDate(); startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignStage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var stageId = "1111" + campaignStage.id; g.AddTaskItem(new JSGantt.TaskItem(stageId, campaignStage.stageName, startDateString, endDateString, 'gtaskyellow', '#', 0, '-', 0, 1, campaignId, 0, '', '', '', g)); //populate campaignSubstages for (var k = 0; k < campaignStage.campaignSubstagesSet.length; k++) { var table = ""; var campaignSubstage = campaignStage.campaignSubstagesSet[k]; startDate = new Date(parseFloat(campaignSubstage.startDate)); startDateMM = startDate.getMonth() + 1; startDateDD = startDate.getDate(); startDateString = startDate.getFullYear() + "-" + (startDateMM < 10 ? ("0" + startDateMM) : startDateMM) + "-" + (startDateDD < 10 ? ("0" + startDateDD) : startDateDD); endDate = new Date(parseFloat(campaignSubstage.endDate)); endDateMM = endDate.getMonth() + 1; endDateDD = endDate.getDate(); endDateString = endDate.getFullYear() + "-" + (endDateMM < 10 ? ("0" + endDateMM) : endDateMM) + "-" + (endDateDD < 10 ? ("0" + endDateDD) : endDateDD); var substageId = "9999" + campaignSubstage.id; var passDependency = "", failDependency = ""; if((campaignSubstage.ssIdForPass != '-1') && (campaignSubstage.ssIdForPass != '0')) passDependency = "9999" + campaignSubstage.ssIdForPass + "SS"; if((campaignSubstage.ssIdForFail != '-1') && (campaignSubstage.ssIdForFail != '0')) failDependency = "9999" + campaignSubstage.ssIdForFail + "FF"; var dependency = passDependency + "," + failDependency; var substageStatusList = campaignSubstage.campaignSubstageStatusList; var onEnterCount = 0, sentCount = 0, viewedCount = 0, passCount = 0, failCount = 0, noShowCount = 0; for(var l = 0; l < substageStatusList.length; l++){ var status = substageStatusList[l]; if(status.onEnter == true) onEnterCount = onEnterCount + 1; if(status.sent == true) sentCount = sentCount + 1; if(status.pass == true) passCount = passCount + 1; if(status.fail == true) failCount = failCount + 1; if(status.viewed == true) viewedCount = viewedCount + 1; if(status.noShow == true) noShowCount = noShowCount + 1; } table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No. of Users:</span><span class='gTaskText'>" + onEnterCount + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Remainders:</span><span class='gTaskText'>" + campaignSubstage.remainders + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForPass == 0) || (campaignSubstage.ssIdForPass == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForPass]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail Substage:</span><span class='gTaskText'>" + (((campaignSubstage.ssIdForFail == 0) || (campaignSubstage.ssIdForFail == -1)) ? "Not Assigned" : substageList[campaignSubstage.ssIdForFail]) + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show :</span><span class='gTaskText'>" + noShowList[campaignSubstage.noShow] + "</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Sent: </span><span class='gTaskText'>"+sentCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Viewed: </span><span class='gTaskText'>"+viewedCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>No-Show: </span><span class='gTaskText'>"+noShowCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Pass: </span><span class='gTaskText'>"+passCount+"</span></div>"; table += "<div class='gTILine gTIsd'><span class='gTaskLabel'>Fail: </span><span class='gTaskText'>"+failCount+"</span></div>"; var taskStyleIndex = Math.floor(Math.random() * (taskStyle.length - 1)) + 0; var content = campaignSubstage.connectType == 0 ? campaignSubstage.contentId : campaignSubstage.connectUrl; g.AddTaskItem(new JSGantt.TaskItem(substageId, campaignSubstage.campaignSubStageName, startDateString, endDateString, "gtaskpurple", '#', 0, content, 0, 0, stageId, 0, dependency, '', table, g)); } } } g.Draw(); } else { alert("Error, unable to create Gantt Chart"); } } function previewChart(divName){ element = $("#"+divName); /*$("#downloadChart").show();*/ $("#chartPreviewContainer").show(); $("#printChart").show(); html2canvas(element, { onrendered: function (canvas) { var imgsrc = canvas.toDataURL("image/png"); $("#chartPreviewImage").attr('src',imgsrc); getCanvas = canvas; } }); } function showLoader(){ $("#form-loader").css("display","inline-block"); $(".wrapper").css("pointer-events", "none"); $(".wrapper").css("opacity", "0.5"); } function hideLoader(){ $("#form-loader").css("display","none"); $(".wrapper").css("pointer-events", ""); $(".wrapper").css("opacity", "1"); } function print(divId){ var contents = $("#"+divId).html(); var frame1 = $('<iframe />'); frame1[0].name = "frame1"; frame1.css({ "position": "absolute", "top": "-1000000px" }); $("body").append(frame1); var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument; frameDoc.document.open(); //Create a new HTML document. var currDate = new Date(); var title = "GanttChart-" + (currDate.getMonth() + 1) + "-" + currDate.getDate() + "-" + currDate.getFullYear(); frameDoc.document.write('<html><head><title>'+title+'</title>'); frameDoc.document.write('</head><body>'); //Append the external CSS file. //frameDoc.document.write('<link href="style.css" rel="stylesheet" type="text/css" />'); //Append the DIV contents. frameDoc.document.write(contents); frameDoc.document.write('</body></html>'); frameDoc.document.close(); setTimeout(function () { window.frames["frame1"].focus(); window.frames["frame1"].print(); frame1.remove(); }, 500); } function
expandAllFolders
identifier_name
adc.rs
MHz, >5k @ ADCK < 4MHz) /// inputs. Long = 1, } /// Adc Clock Divisors /// /// Note 1/16 divisor is only usable for the Bus clock pub enum ClockDivisor { /// Source / 1, No divison _1 = 0, /// Source / 2 _2 = 1, /// Source / 4 _4 = 2, /// Source / 8 _8 = 3, /// Source / 16 _16 = 4, } /// Enabled state pub struct Enabled; /// Disabled state pub struct Disabled; impl Adc<Enabled> { /// Poll to determine if ADC conversion is complete. /// /// Note: This flag is cleared when the sampling mode is changed, /// interrupts are enabled, [Adc::set_channel] is called, and when [Adc::result] is /// called (including [Adc::try_result]) pub fn is_done(&self) -> bool { self.peripheral.sc1.read().coco().bit() } /// Poll to determine if ADC conversion is underway pub fn is_converting(&self) -> bool { self.peripheral.sc2.read().adact().bit() } /// Grab the last ADC conversion result. pub fn result(&self) -> u16 { self.peripheral.r.read().adr().bits() } /// Poll for conversion completion, if done return the result. pub fn try_result(&self) -> Option<u16> { if self.is_done() { Some(self.result()) } else { None } } /// Set ADC target channel. /// /// In Single conversion mode (OneShot), setting the channel begins the conversion. In FIFO mode /// the channel is added to the FIFO buffer. /// /// Note: If the channel is changed while a conversion is in progress the /// current conversion will be cancelled. If in FIFO mode, conversion will /// resume once the FIFO channels are refilled. pub fn set_channel<T: Channel<Adc<Enabled>, ID = u8>>(&self, _pin: &T) { self.peripheral .sc1 .modify(|_, w| unsafe { w.adch().bits(T::channel()) }); } /// Set the ADC's configuration pub fn configure(self, config: AdcConfig) -> Adc<Enabled> { self.peripheral.sc3.modify(|_, w| { use pac::adc::sc3::{ADICLK_A, ADIV_A, ADLSMP_A, MODE_A}; w.adiclk() .variant(match config.clock_source { AdcClocks::Bus => // If divisor is 16, use the Bus / 2 clock source, else use // the 1:1 Bus clock source { match config.clock_divisor { ClockDivisor::_16 => ADICLK_A::_01, _ => ADICLK_A::_00, } } AdcClocks::External => ADICLK_A::_10, AdcClocks::Async => ADICLK_A::_11, }) .mode() .variant(match config.resolution { AdcResolution::_8bit => MODE_A::_00, AdcResolution::_10bit => MODE_A::_01, AdcResolution::_12bit => MODE_A::_10, }) .adlsmp() .variant(match config.sample_time { AdcSampleTime::Short => ADLSMP_A::_0, AdcSampleTime::Long => ADLSMP_A::_1, }) .adiv() .variant(match config.clock_divisor { ClockDivisor::_1 => ADIV_A::_00, ClockDivisor::_2 => ADIV_A::_01, ClockDivisor::_4 => ADIV_A::_10, _ => ADIV_A::_11, }) .adlpc() .bit(config.low_power) }); // It looks like SCGC has to be set before touching the peripheral // at all, else hardfault. Go back later to confirm that if using external clock // scgc can be cleared. // w.adc().variant(match config.clock_source { // AdcClocks::Bus => ADC_A::_1, // _ => ADC_A::_0, // }) Adc { peripheral: self.peripheral, _state: PhantomData, onchip_channels: self.onchip_channels, } } } impl Adc<Disabled> { /// Connects the bus clock to the adc via the SIM peripheral, allowing /// read and write access to ADC registers. /// /// Any attempt to access ADC registers while disabled results in a /// HardFault, generated by hardware. /// /// This also enables the bandgap voltage reference. pub fn enable(self) -> Adc<Enabled> { cortex_m::interrupt::free(|_| { unsafe { &(*pac::SIM::ptr()) }.scgc.modify(|_, w| { use pac::sim::scgc::ADC_A; w.adc().variant(ADC_A::_1) }); // Don't start a conversion (set channel to DummyDisable) self.peripheral.sc1.modify(|_, w| w.adch()._11111()); // Bandgap. Grab directly, Currently the bandgap isn't implemented // in [system::PMC]. We will eventually have to pass in the pmc // peripheral handle as a variable. unsafe { &(*pac::PMC::ptr()) } .spmsc1 .modify(|_, w| w.bgbe()._1()); }); Adc { peripheral: self.peripheral, _state: PhantomData, onchip_channels: self.onchip_channels, } } /// Set the ADC's configuration /// /// This is a sugar method for calling [Adc<Disabled>::enable] followed by /// [Adc<Enabled>::configure] pub fn configure(self, config: AdcConfig) -> Adc<Enabled> { self.enable().configure(config) } } impl<Mode> Adc<Mode> { /// Not Implemented pub fn into_interrupt(self) -> Adc<Mode> { unimplemented!("Interrupt is not yet implemented"); // Adc::<Mode> { // peripheral: self.peripheral, // _state: PhantomData, // onchip_channels: self.onchip_channels, // } } /// Not Implemented pub fn into_fifo(self, _depth: u8) -> Adc<Mode> { // self.peripheral // .sc4 // .modify(|_r, w| w.afdep().bits(depth & 0x7)); // Adc::<Mode> { // peripheral: self.peripheral, // _state: PhantomData, // onchip_channels: self.onchip_channels, // } unimplemented!("FIFO is not yet implemented"); } /// Not Implemented pub fn into_continuous(self) -> Adc<Mode> { unimplemented!("Continuous Conversion mode not yet implemented"); } } impl OnChipChannels { /// Request an instance of an on-chip [Vss] channel. pub fn vss(&mut self) -> Result<Analog<Vss<Input>>, Error> { self.vss.take().ok_or(Error::Moved) } /// Return the instance of [Vss] pub fn return_vss(&mut self, inst: Analog<Vss<Input>>) { self.vss.replace(inst); } /// Try to grab an instance of the onchip [TempSense] channel. pub fn tempsense(&mut self) -> Result<Analog<TempSense<Input>>, Error> { self.temp_sense.take().ok_or(Error::Moved) } /// Return the instance of [TempSense] pub fn return_tempsense(&mut self, inst: Analog<TempSense<Input>>) { self.temp_sense.replace(inst); } /// Try to grab an instance of the onchip [Bandgap] channel. /// /// The bandgap reference is a fixed 1.16V (nom, Factory trimmed to +/- /// 0.02V at Vdd=5.0 at 125C) signal that is available to the ADC Module. /// It can be used as a voltage reference for the ACMP and as an [Analog] /// channel that can be used to (roughly) check the VDD voltage pub fn bandgap(&mut self) -> Result<Analog<Bandgap<Input>>, Error> { self.bandgap.take().ok_or(Error::Moved) } /// Return the instance of [Bandgap] pub fn return_bandgap(&mut self, inst: Analog<Bandgap<Input>>) { self.bandgap.replace(inst); } /// Try to grab an instance of the onchip Voltage Reference High ([VrefH]) channel. pub fn vref_h(&mut self) -> Result<Analog<VrefH<Input>>, Error> { self.vref_h.take().ok_or(Error::Moved) } /// Return the instance of [VrefH] pub fn
return_vref_h
identifier_name
adc.rs
_freq.integer() / req_adc_freq.integer()) as u8; let mut output: u8 = 1; let mut err: i8 = (denom - output) as i8; let mut err_old: i8 = err; let max_divisor = match self.clock_source { AdcClocks::Bus => 16, _ => 8, }; while output < max_divisor { err = (denom - (output << 1)) as i8; if err.is_negative() { err = err.abs(); } if err <= err_old { output <<= 1; err_old = err; } else { break; } } // I am of the mind that this assert is okay, at least until the input // clock can be known at compile time. let ad_clock = source_freq.integer() / output as u32; assert!(400_000 <= ad_clock); assert!( ad_clock <= match self.low_power { false => 8_000_000, true => 4_000_000, } ); self.clock_divisor = match output { 1 => ClockDivisor::_1, 2 => ClockDivisor::_2, 4 => ClockDivisor::_4, 8 => ClockDivisor::_8, _ => ClockDivisor::_16, } } /// Set the divisor directly. panics if divisor isn't supported by the /// clock source. /// /// TODO: Refactor to remove assert. Add Clock Source as a type state pub fn set_divisor(&mut self, divisor: ClockDivisor) { // divisor can't be 16 unless using the Bus clock assert!( !(!matches!(self.clock_source, AdcClocks::Bus) && matches!(divisor, ClockDivisor::_16)) ); self.clock_divisor = divisor; } /// Sets the clock source, panics if divisor isn't supported /// /// TODO: Refactor to remove assert. Add Clock Source as a type state pub fn set_clock_source(&mut self, clock: AdcClocks) { // Panic if setting the clock to anything other than Bus if the divisor // is set to 16 assert!( !matches!(clock, AdcClocks::Bus) && matches!(self.clock_divisor, ClockDivisor::_16) ); self.clock_source = clock; } } impl Default for AdcConfig { fn default() -> AdcConfig { AdcConfig { clock_source: AdcClocks::Bus, clock_divisor: ClockDivisor::_1, resolution: AdcResolution::_12bit, sample_time: AdcSampleTime::Short, low_power: false, } } } /// Clock types available to the Adc peripheral /// /// Dividers will be chosen appropriately to suit requested clock rate. pub enum AdcClocks { /// Use the incoming Bus Clock Bus, /// jkl External, /// Available in Wait AND Stop Mode Async, } /// This enum represents the availabe ADC resolutions /// /// Regardless of resolution chosen, results are always right justified #[repr(u8)] pub enum AdcResolution { /// 8 bit AD conversion mode _8bit = 0, /// 10 bit AD conversion mode _10bit = 1, /// 12 bit AD conversion mode _12bit = 2, } /// Adc sample time pub enum AdcSampleTime { /// Sample for 3.5 ADC clock (ADCK) cycles. Short = 0, /// Sample for 23.5 ADC clock (ADCK) cycles. /// /// Required for high impedence (>2k @ADCK > 4MHz, >5k @ ADCK < 4MHz) /// inputs. Long = 1, } /// Adc Clock Divisors /// /// Note 1/16 divisor is only usable for the Bus clock pub enum ClockDivisor { /// Source / 1, No divison _1 = 0, /// Source / 2 _2 = 1, /// Source / 4 _4 = 2, /// Source / 8 _8 = 3, /// Source / 16 _16 = 4, } /// Enabled state pub struct Enabled; /// Disabled state pub struct Disabled; impl Adc<Enabled> { /// Poll to determine if ADC conversion is complete. /// /// Note: This flag is cleared when the sampling mode is changed, /// interrupts are enabled, [Adc::set_channel] is called, and when [Adc::result] is /// called (including [Adc::try_result]) pub fn is_done(&self) -> bool { self.peripheral.sc1.read().coco().bit() } /// Poll to determine if ADC conversion is underway pub fn is_converting(&self) -> bool { self.peripheral.sc2.read().adact().bit() } /// Grab the last ADC conversion result. pub fn result(&self) -> u16 { self.peripheral.r.read().adr().bits() } /// Poll for conversion completion, if done return the result. pub fn try_result(&self) -> Option<u16> { if self.is_done() { Some(self.result()) } else { None } } /// Set ADC target channel. /// /// In Single conversion mode (OneShot), setting the channel begins the conversion. In FIFO mode /// the channel is added to the FIFO buffer. /// /// Note: If the channel is changed while a conversion is in progress the /// current conversion will be cancelled. If in FIFO mode, conversion will /// resume once the FIFO channels are refilled. pub fn set_channel<T: Channel<Adc<Enabled>, ID = u8>>(&self, _pin: &T) { self.peripheral .sc1 .modify(|_, w| unsafe { w.adch().bits(T::channel()) }); } /// Set the ADC's configuration pub fn configure(self, config: AdcConfig) -> Adc<Enabled> { self.peripheral.sc3.modify(|_, w| { use pac::adc::sc3::{ADICLK_A, ADIV_A, ADLSMP_A, MODE_A}; w.adiclk() .variant(match config.clock_source { AdcClocks::Bus => // If divisor is 16, use the Bus / 2 clock source, else use // the 1:1 Bus clock source { match config.clock_divisor { ClockDivisor::_16 => ADICLK_A::_01, _ => ADICLK_A::_00, } } AdcClocks::External => ADICLK_A::_10, AdcClocks::Async => ADICLK_A::_11, }) .mode() .variant(match config.resolution { AdcResolution::_8bit => MODE_A::_00, AdcResolution::_10bit => MODE_A::_01, AdcResolution::_12bit => MODE_A::_10, }) .adlsmp() .variant(match config.sample_time { AdcSampleTime::Short => ADLSMP_A::_0, AdcSampleTime::Long => ADLSMP_A::_1, }) .adiv() .variant(match config.clock_divisor { ClockDivisor::_1 => ADIV_A::_00, ClockDivisor::_2 => ADIV_A::_01, ClockDivisor::_4 => ADIV_A::_10, _ => ADIV_A::_11, }) .adlpc() .bit(config.low_power) }); // It looks like SCGC has to be set before touching the peripheral // at all, else hardfault. Go back later to confirm that if using external clock // scgc can be cleared. // w.adc().variant(match config.clock_source { // AdcClocks::Bus => ADC_A::_1, // _ => ADC_A::_0, // }) Adc { peripheral: self.peripheral, _state: PhantomData, onchip_channels: self.onchip_channels, } } } impl Adc<Disabled> { /// Connects the bus clock to the adc via the SIM peripheral, allowing /// read and write access to ADC registers. /// /// Any attempt to access ADC registers while disabled results in a /// HardFault, generated by hardware. /// /// This also enables the bandgap voltage reference. pub fn enable(self) -> Adc<Enabled> { cortex_m::interrupt::free(|_| { unsafe { &(*pac::SIM::ptr()) }.scgc.modify(|_, w| { use pac::sim::scgc::ADC_A; w.adc().variant(ADC_A::_1) });
// Don't start a conversion (set channel to DummyDisable) self.peripheral.sc1.modify(|_, w| w.adch()._11111());
random_line_split
adc.rs
2bit = 2, } /// Adc sample time pub enum AdcSampleTime { /// Sample for 3.5 ADC clock (ADCK) cycles. Short = 0, /// Sample for 23.5 ADC clock (ADCK) cycles. /// /// Required for high impedence (>2k @ADCK > 4MHz, >5k @ ADCK < 4MHz) /// inputs. Long = 1, } /// Adc Clock Divisors /// /// Note 1/16 divisor is only usable for the Bus clock pub enum ClockDivisor { /// Source / 1, No divison _1 = 0, /// Source / 2 _2 = 1, /// Source / 4 _4 = 2, /// Source / 8 _8 = 3, /// Source / 16 _16 = 4, } /// Enabled state pub struct Enabled; /// Disabled state pub struct Disabled; impl Adc<Enabled> { /// Poll to determine if ADC conversion is complete. /// /// Note: This flag is cleared when the sampling mode is changed, /// interrupts are enabled, [Adc::set_channel] is called, and when [Adc::result] is /// called (including [Adc::try_result]) pub fn is_done(&self) -> bool { self.peripheral.sc1.read().coco().bit() } /// Poll to determine if ADC conversion is underway pub fn is_converting(&self) -> bool { self.peripheral.sc2.read().adact().bit() } /// Grab the last ADC conversion result. pub fn result(&self) -> u16 { self.peripheral.r.read().adr().bits() } /// Poll for conversion completion, if done return the result. pub fn try_result(&self) -> Option<u16> { if self.is_done() { Some(self.result()) } else { None } } /// Set ADC target channel. /// /// In Single conversion mode (OneShot), setting the channel begins the conversion. In FIFO mode /// the channel is added to the FIFO buffer. /// /// Note: If the channel is changed while a conversion is in progress the /// current conversion will be cancelled. If in FIFO mode, conversion will /// resume once the FIFO channels are refilled. pub fn set_channel<T: Channel<Adc<Enabled>, ID = u8>>(&self, _pin: &T) { self.peripheral .sc1 .modify(|_, w| unsafe { w.adch().bits(T::channel()) }); } /// Set the ADC's configuration pub fn configure(self, config: AdcConfig) -> Adc<Enabled> { self.peripheral.sc3.modify(|_, w| { use pac::adc::sc3::{ADICLK_A, ADIV_A, ADLSMP_A, MODE_A}; w.adiclk() .variant(match config.clock_source { AdcClocks::Bus => // If divisor is 16, use the Bus / 2 clock source, else use // the 1:1 Bus clock source { match config.clock_divisor { ClockDivisor::_16 => ADICLK_A::_01, _ => ADICLK_A::_00, } } AdcClocks::External => ADICLK_A::_10, AdcClocks::Async => ADICLK_A::_11, }) .mode() .variant(match config.resolution { AdcResolution::_8bit => MODE_A::_00, AdcResolution::_10bit => MODE_A::_01, AdcResolution::_12bit => MODE_A::_10, }) .adlsmp() .variant(match config.sample_time { AdcSampleTime::Short => ADLSMP_A::_0, AdcSampleTime::Long => ADLSMP_A::_1, }) .adiv() .variant(match config.clock_divisor { ClockDivisor::_1 => ADIV_A::_00, ClockDivisor::_2 => ADIV_A::_01, ClockDivisor::_4 => ADIV_A::_10, _ => ADIV_A::_11, }) .adlpc() .bit(config.low_power) }); // It looks like SCGC has to be set before touching the peripheral // at all, else hardfault. Go back later to confirm that if using external clock // scgc can be cleared. // w.adc().variant(match config.clock_source { // AdcClocks::Bus => ADC_A::_1, // _ => ADC_A::_0, // }) Adc { peripheral: self.peripheral, _state: PhantomData, onchip_channels: self.onchip_channels, } } } impl Adc<Disabled> { /// Connects the bus clock to the adc via the SIM peripheral, allowing /// read and write access to ADC registers. /// /// Any attempt to access ADC registers while disabled results in a /// HardFault, generated by hardware. /// /// This also enables the bandgap voltage reference. pub fn enable(self) -> Adc<Enabled> { cortex_m::interrupt::free(|_| { unsafe { &(*pac::SIM::ptr()) }.scgc.modify(|_, w| { use pac::sim::scgc::ADC_A; w.adc().variant(ADC_A::_1) }); // Don't start a conversion (set channel to DummyDisable) self.peripheral.sc1.modify(|_, w| w.adch()._11111()); // Bandgap. Grab directly, Currently the bandgap isn't implemented // in [system::PMC]. We will eventually have to pass in the pmc // peripheral handle as a variable. unsafe { &(*pac::PMC::ptr()) } .spmsc1 .modify(|_, w| w.bgbe()._1()); }); Adc { peripheral: self.peripheral, _state: PhantomData, onchip_channels: self.onchip_channels, } } /// Set the ADC's configuration /// /// This is a sugar method for calling [Adc<Disabled>::enable] followed by /// [Adc<Enabled>::configure] pub fn configure(self, config: AdcConfig) -> Adc<Enabled> { self.enable().configure(config) } } impl<Mode> Adc<Mode> { /// Not Implemented pub fn into_interrupt(self) -> Adc<Mode> { unimplemented!("Interrupt is not yet implemented"); // Adc::<Mode> { // peripheral: self.peripheral, // _state: PhantomData, // onchip_channels: self.onchip_channels, // } } /// Not Implemented pub fn into_fifo(self, _depth: u8) -> Adc<Mode> { // self.peripheral // .sc4 // .modify(|_r, w| w.afdep().bits(depth & 0x7)); // Adc::<Mode> { // peripheral: self.peripheral, // _state: PhantomData, // onchip_channels: self.onchip_channels, // } unimplemented!("FIFO is not yet implemented"); } /// Not Implemented pub fn into_continuous(self) -> Adc<Mode> { unimplemented!("Continuous Conversion mode not yet implemented"); } } impl OnChipChannels { /// Request an instance of an on-chip [Vss] channel. pub fn vss(&mut self) -> Result<Analog<Vss<Input>>, Error> { self.vss.take().ok_or(Error::Moved) } /// Return the instance of [Vss] pub fn return_vss(&mut self, inst: Analog<Vss<Input>>) { self.vss.replace(inst); } /// Try to grab an instance of the onchip [TempSense] channel. pub fn tempsense(&mut self) -> Result<Analog<TempSense<Input>>, Error> { self.temp_sense.take().ok_or(Error::Moved) } /// Return the instance of [TempSense] pub fn return_tempsense(&mut self, inst: Analog<TempSense<Input>>) { self.temp_sense.replace(inst); } /// Try to grab an instance of the onchip [Bandgap] channel. /// /// The bandgap reference is a fixed 1.16V (nom, Factory trimmed to +/- /// 0.02V at Vdd=5.0 at 125C) signal that is available to the ADC Module. /// It can be used as a voltage reference for the ACMP and as an [Analog] /// channel that can be used to (roughly) check the VDD voltage pub fn bandgap(&mut self) -> Result<Analog<Bandgap<Input>>, Error> { self.bandgap.take().ok_or(Error::Moved) } /// Return the instance of [Bandgap] pub fn return_bandgap(&mut self, inst: Analog<Bandgap<Input>>)
{ self.bandgap.replace(inst); }
identifier_body
adc.rs
_analog] method. /// Once measurements from that pin are completed it will be returned to an /// Output that is set high by calling the [Analog::outof_analog] method. /// /// ```rust /// let pta0 = gpioa.pta0.into_push_pull_output(); /// pta0.set_high(); /// let mut pta0 = pta0.into_analog(); // pta0 is hi-Z /// let value = adc.read(&mut pta0).unwrap_or(0); /// let pta0 = pta0.outof_analog(); // pta0 is push-pull output, set high. /// ``` /// /// Note: This is a hardware feature that requires effectively no clock cycles /// to complete. "Manually" reconfiguring the pins to HighImpedence before /// calling into_analog() is discouraged, but it would not hurt anything. pub struct Analog<Pin> { pin: Pin, } /// Interface for ADC Peripheral. /// /// Returned by calling [HALExt::split] on the pac [ADC] structure. Holds state /// of peripheral. pub struct Adc<State> { peripheral: ADC, _state: PhantomData<State>, /// Contains the On-Chip ADC Channels, like the MCU's temperature sensor. pub onchip_channels: OnChipChannels, } impl HALExt for ADC { type T = Adc<Disabled>; fn split(self) -> Adc<Disabled> { Adc { peripheral: self, _state: PhantomData, onchip_channels: OnChipChannels { vss: Some(Analog { pin: Vss::<Input> { _mode: PhantomData }, }), temp_sense: Some(Analog { pin: TempSense::<Input> { _mode: PhantomData }, }), bandgap: Some(Analog { pin: Bandgap::<Input> { _mode: PhantomData }, }), vref_h: Some(Analog { pin: VrefH::<Input> { _mode: PhantomData }, }), vref_l: Some(Analog { pin: VrefL::<Input> { _mode: PhantomData }, }), }, } } } /// Configuration struct for Adc peripheral. pub struct AdcConfig { /// Determines the clock source for the ADC peripheral /// /// Default is [AdcClocks::Bus] pub clock_source: AdcClocks, /// Divides the clock source to get the ADC clock into it's usable range of /// 400kHz - 8MHz (4MHz in low power mode). /// /// Default is [ClockDivisor::_1] (no divison) pub clock_divisor: ClockDivisor, /// Set the resolution of ADC conversion /// /// Default is [AdcResolution::_8bit] pub resolution: AdcResolution, /// Set ADC sample time. /// /// Default is [AdcSampleTime::Short] pub sample_time: AdcSampleTime, /// Set low power mode /// /// Default is false. pub low_power: bool, } impl AdcConfig { /// Calculate the ADC clock divisor /// /// Uses the current clock source and clock frequency to determine /// the best divisor to use in order to have minimal error between /// the ADC clock rate and the desired ADC clock rate. /// /// Note: This relies on trustworthy values for source_freq and valid /// values for req_adc_freq. In the future this should know or /// determine what the current clock frequency is instead of relying /// on the user to provide it. pub fn calculate_divisor(&mut self, source_freq: Hertz, req_adc_freq: Hertz) { let denom: u8 = (source_freq.integer() / req_adc_freq.integer()) as u8; let mut output: u8 = 1; let mut err: i8 = (denom - output) as i8; let mut err_old: i8 = err; let max_divisor = match self.clock_source { AdcClocks::Bus => 16, _ => 8, }; while output < max_divisor { err = (denom - (output << 1)) as i8; if err.is_negative() { err = err.abs(); } if err <= err_old { output <<= 1; err_old = err; } else { break; } } // I am of the mind that this assert is okay, at least until the input // clock can be known at compile time. let ad_clock = source_freq.integer() / output as u32; assert!(400_000 <= ad_clock); assert!( ad_clock <= match self.low_power { false => 8_000_000, true => 4_000_000, } ); self.clock_divisor = match output { 1 => ClockDivisor::_1, 2 => ClockDivisor::_2, 4 => ClockDivisor::_4, 8 => ClockDivisor::_8, _ => ClockDivisor::_16, } } /// Set the divisor directly. panics if divisor isn't supported by the /// clock source. /// /// TODO: Refactor to remove assert. Add Clock Source as a type state pub fn set_divisor(&mut self, divisor: ClockDivisor) { // divisor can't be 16 unless using the Bus clock assert!( !(!matches!(self.clock_source, AdcClocks::Bus) && matches!(divisor, ClockDivisor::_16)) ); self.clock_divisor = divisor; } /// Sets the clock source, panics if divisor isn't supported /// /// TODO: Refactor to remove assert. Add Clock Source as a type state pub fn set_clock_source(&mut self, clock: AdcClocks) { // Panic if setting the clock to anything other than Bus if the divisor // is set to 16 assert!( !matches!(clock, AdcClocks::Bus) && matches!(self.clock_divisor, ClockDivisor::_16) ); self.clock_source = clock; } } impl Default for AdcConfig { fn default() -> AdcConfig { AdcConfig { clock_source: AdcClocks::Bus, clock_divisor: ClockDivisor::_1, resolution: AdcResolution::_12bit, sample_time: AdcSampleTime::Short, low_power: false, } } } /// Clock types available to the Adc peripheral /// /// Dividers will be chosen appropriately to suit requested clock rate. pub enum AdcClocks { /// Use the incoming Bus Clock Bus, /// jkl External, /// Available in Wait AND Stop Mode Async, } /// This enum represents the availabe ADC resolutions /// /// Regardless of resolution chosen, results are always right justified #[repr(u8)] pub enum AdcResolution { /// 8 bit AD conversion mode _8bit = 0, /// 10 bit AD conversion mode _10bit = 1, /// 12 bit AD conversion mode _12bit = 2, } /// Adc sample time pub enum AdcSampleTime { /// Sample for 3.5 ADC clock (ADCK) cycles. Short = 0, /// Sample for 23.5 ADC clock (ADCK) cycles. /// /// Required for high impedence (>2k @ADCK > 4MHz, >5k @ ADCK < 4MHz) /// inputs. Long = 1, } /// Adc Clock Divisors /// /// Note 1/16 divisor is only usable for the Bus clock pub enum ClockDivisor { /// Source / 1, No divison _1 = 0, /// Source / 2 _2 = 1, /// Source / 4 _4 = 2, /// Source / 8 _8 = 3, /// Source / 16 _16 = 4, } /// Enabled state pub struct Enabled; /// Disabled state pub struct Disabled; impl Adc<Enabled> { /// Poll to determine if ADC conversion is complete. /// /// Note: This flag is cleared when the sampling mode is changed, /// interrupts are enabled, [Adc::set_channel] is called, and when [Adc::result] is /// called (including [Adc::try_result]) pub fn is_done(&self) -> bool { self.peripheral.sc1.read().coco().bit() } /// Poll to determine if ADC conversion is underway pub fn is_converting(&self) -> bool { self.peripheral.sc2.read().adact().bit() } /// Grab the last ADC conversion result. pub fn result(&self) -> u16 { self.peripheral.r.read().adr().bits() } /// Poll for conversion completion, if done return the result. pub fn try_result(&self) -> Option<u16> { if self.is_done() { Some(self.result()) } else
{ None }
conditional_block
TimeSeries.js
//append each month in the time series as a new object pair to the data variable for(i=0; i<Object.keys(dates).length; i++){ var new_entry = {}; new_entry.date = dates[i]; new_entry.price = Object.values(crude_prices)[i]; data.push(new_entry); } //set up the x and y values - may need to parse dates from YYYYMM to MM-YYYY var x = d3.scaleTime() .domain(d3.extent(dates)) //domain of inputs .range([0, width]); //range of outputs var y = d3.scaleLinear() .domain([0, d3.max(prices)+10]) //domain of inputs .range([height, 0]); //range of outputs var line = d3.line() .x(function(d){ return x(d.date); }) .y(function(d){ return y(d.price); }); //append an SVG element to the bottom bar, reshape it to the bottom dimensions, and append <g> tag with margins var TS_svg = d3.select("#bottom") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("class","line-chart") .attr("id", "WTI-chart") .attr("transform", "translate(" + margin.left + "," + "-"+margin.bottom + ")"); //apend Y Axis TS_svg.append("g") .attr("class","y-axis") .attr("id", "WTI-axis") .attr("height", height) .attr("transform", "translate(0,"+margin.top+")") .call(d3.axisLeft(y)); //append X Axis TS_svg.append("g") .attr("class", "x-axis") .attr("transform", "translate(0,"+margin.top+")") .attr("stroke","white") .call(d3.axisBottom(x)); //append the actual time series line TS_svg.append("path") .data(data) .attr("class", "line") .attr("d", line(data)); //create invisible popup tip box to show on mouseover of timeseries var tip_box = d3.select("body") .append("div") .attr("id", "tip-box") .style("opacity", 0); //create invisible dots on the timeline - when moused over, will give the date & price in popup var dot_labels = function(){ TS_svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class","dot") .attr("cx", function(d){ return x(d.date); }) .attr("cy", function(d){ return y(d.price); }) .attr("r", "4px") .style("opacity",0.0) .on("mouseover", function(d){ var h = document.getElementById("tip-box").offsetHeight; var f = d3.timeFormat("%b-%Y"); tip_box.transition() .duration(200) .style("opacity",0.9); tip_box.html(f(d.date) + "<br/>" + d.price.toFixed(3)) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - h) + "px"); }) .on("mouseout",function(d){ tip_box.transition() .duration(200) .style("opacity", 0); }); }; //remove every other Y Axis label to avoid cluttering d3.select("#WTI-axis").selectAll(".tick text") .attr("stroke-width", "1px") .attr("stroke","white") .attr("class",function(d,i){ //remove if(i%3 != 0){ d3.select(this).remove(); } }); //append the marker line that indicates the time state of the model TS_svg.append("line") .attr("x1",x(dates[0])) .attr("y1",0) .attr("x2",x(dates[0])) .attr("y1",height) .attr("stroke-width","4px") .attr("class","marker-line") .attr("id","marker-line"); //transition the marker line across of the time series var marker_transition = function(start){ var T = 0; for(i=start; i<dates.length; i++){ d3.select(".marker-line") .transition() .duration(1500) .delay(1500*T) .ease(d3.easeLinear) .attr("x1", x(dates[i]) ) .attr("x2", x(dates[i]) ); T++; } }; marker_transition(1); //find the index of the nearest value when marker is dragged/dropped on the timeline var find_nearest = function(dragged_x){ //get the x-axis coordinate for all the dates var x_dates = dates.map(function(d){ return x(d); }); //get the distance between each coordinate and the dragged_x var dist = x_dates.map(function(d){ return Math.abs(d - dragged_x); }); //get the index of the smallest distance return dist.indexOf(Math.min.apply(null,dist)); }; /* * When the line is dragged, events need to be dispatched to: * 1) The bar chart * 2) The map circles * 3) The Date: MM-YYYY */ //make marker line clickable and dragable (needs to also return its time state) var drag_line = d3.drag() .on("start",function(d){ //Stop previous transition d3.select(".marker-line") .transition() .duration(0); //make the line actively clickable d3.select(this) .raise() .classed("active", true); }) .on("drag",function(d){ //get the date closest to the new x time_state = dates[find_nearest(d3.event.x)]; //set the x values to the x value of the closest x d3.select(this) .attr("x1", x(time_state)) .attr("x2", x(time_state)); //delete and remake circles as the marker line moves var index = find_nearest(this.getAttribute("x1")); //propogate the index to the global variable current_timestate window.current_timestate = index; call_dispatch(index); }) .on("end",function(d){ //restart the transition using that nearest index var index = find_nearest(this.getAttribute("x1")); //propogate the index to the global variable current_timestate window.current_timestate = index; //marker starts moving again when drag stops marker_transition(index); //make dot labels again dot_labels(); //deactivate marker d3.select(this) .classed("active",false); }); d3.select(".marker-line") .call(drag_line); dot_labels(); }; var make_Import_TS = function(selected_city){ //Set up margins of graph axes - need to make room for tick labels var margin = {top: 0, right: 10, bottom: 6, left: 25}; //Set up dimensions of the graph var width = document.getElementById("bottom").offsetWidth - margin.left - margin.right; var height = document.getElementById("bottom").offsetHeight - margin.top - margin.bottom; //get the imports time series for the selected port var import_data; Data.objects.forEach(function(circle){ if(circle.circle.City == selected_city){ import_data = circle.circle.Imports; } }); var dates = Object.keys(crude_prices).map(function(date){ return parse_dates(date); }); dates = dates.slice(0,dates.length-6);
for(i=0; i<Object.keys(dates).length; i++){ var new_entry = {}; new_entry.date = dates[i]; new_entry.imports = Object.values(import_data)[i]; data.push(new_entry); } var imports_x = d3.scaleTime() .domain(d3.extent(dates)) .range([0, width]); var imports_scale = d3.scaleLinear() .domain([d3.min(import_data), d3.max(import_data)]) .range([height, 0]); var line = d3.line() .x(function(d){ return imports_x(d.date); }) .y(function(d){ return imports_scale(d.imports); }); var Import_svg = d3.select(".bottom") .select("svg") .append("g") .attr("id","imports-chart") .attr("transform", "translate(" + margin.left + "," + "-"+margin.bottom + ")"); Import_svg.append("g") .attr("class","y-axis") .attr("id", "Imports-axis") .attr("height", height) .attr("transform", "translate
var data =[]; //append each month in the time series as a new object pair to the data variable
random_line_split
TimeSeries.js
append each month in the time series as a new object pair to the data variable for(i=0; i<Object.keys(dates).length; i++){ var new_entry = {}; new_entry.date = dates[i]; new_entry.price = Object.values(crude_prices)[i]; data.push(new_entry); } //set up the x and y values - may need to parse dates from YYYYMM to MM-YYYY var x = d3.scaleTime() .domain(d3.extent(dates)) //domain of inputs .range([0, width]); //range of outputs var y = d3.scaleLinear() .domain([0, d3.max(prices)+10]) //domain of inputs .range([height, 0]); //range of outputs var line = d3.line() .x(function(d){ return x(d.date); }) .y(function(d){ return y(d.price); }); //append an SVG element to the bottom bar, reshape it to the bottom dimensions, and append <g> tag with margins var TS_svg = d3.select("#bottom") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("class","line-chart") .attr("id", "WTI-chart") .attr("transform", "translate(" + margin.left + "," + "-"+margin.bottom + ")"); //apend Y Axis TS_svg.append("g") .attr("class","y-axis") .attr("id", "WTI-axis") .attr("height", height) .attr("transform", "translate(0,"+margin.top+")") .call(d3.axisLeft(y)); //append X Axis TS_svg.append("g") .attr("class", "x-axis") .attr("transform", "translate(0,"+margin.top+")") .attr("stroke","white") .call(d3.axisBottom(x)); //append the actual time series line TS_svg.append("path") .data(data) .attr("class", "line") .attr("d", line(data)); //create invisible popup tip box to show on mouseover of timeseries var tip_box = d3.select("body") .append("div") .attr("id", "tip-box") .style("opacity", 0); //create invisible dots on the timeline - when moused over, will give the date & price in popup var dot_labels = function(){ TS_svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class","dot") .attr("cx", function(d){ return x(d.date); }) .attr("cy", function(d){ return y(d.price); }) .attr("r", "4px") .style("opacity",0.0) .on("mouseover", function(d){ var h = document.getElementById("tip-box").offsetHeight; var f = d3.timeFormat("%b-%Y"); tip_box.transition() .duration(200) .style("opacity",0.9); tip_box.html(f(d.date) + "<br/>" + d.price.toFixed(3)) .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - h) + "px"); }) .on("mouseout",function(d){ tip_box.transition() .duration(200) .style("opacity", 0); }); }; //remove every other Y Axis label to avoid cluttering d3.select("#WTI-axis").selectAll(".tick text") .attr("stroke-width", "1px") .attr("stroke","white") .attr("class",function(d,i){ //remove if(i%3 != 0){ d3.select(this).remove(); } }); //append the marker line that indicates the time state of the model TS_svg.append("line") .attr("x1",x(dates[0])) .attr("y1",0) .attr("x2",x(dates[0])) .attr("y1",height) .attr("stroke-width","4px") .attr("class","marker-line") .attr("id","marker-line"); //transition the marker line across of the time series var marker_transition = function(start){ var T = 0; for(i=start; i<dates.length; i++){ d3.select(".marker-line") .transition() .duration(1500) .delay(1500*T) .ease(d3.easeLinear) .attr("x1", x(dates[i]) ) .attr("x2", x(dates[i]) ); T++; } }; marker_transition(1); //find the index of the nearest value when marker is dragged/dropped on the timeline var find_nearest = function(dragged_x){ //get the x-axis coordinate for all the dates var x_dates = dates.map(function(d){ return x(d); }); //get the distance between each coordinate and the dragged_x var dist = x_dates.map(function(d){ return Math.abs(d - dragged_x); }); //get the index of the smallest distance return dist.indexOf(Math.min.apply(null,dist)); }; /* * When the line is dragged, events need to be dispatched to: * 1) The bar chart * 2) The map circles * 3) The Date: MM-YYYY */ //make marker line clickable and dragable (needs to also return its time state) var drag_line = d3.drag() .on("start",function(d){ //Stop previous transition d3.select(".marker-line") .transition() .duration(0); //make the line actively clickable d3.select(this) .raise() .classed("active", true); }) .on("drag",function(d){ //get the date closest to the new x time_state = dates[find_nearest(d3.event.x)]; //set the x values to the x value of the closest x d3.select(this) .attr("x1", x(time_state)) .attr("x2", x(time_state)); //delete and remake circles as the marker line moves var index = find_nearest(this.getAttribute("x1")); //propogate the index to the global variable current_timestate window.current_timestate = index; call_dispatch(index); }) .on("end",function(d){ //restart the transition using that nearest index var index = find_nearest(this.getAttribute("x1")); //propogate the index to the global variable current_timestate window.current_timestate = index; //marker starts moving again when drag stops marker_transition(index); //make dot labels again dot_labels(); //deactivate marker d3.select(this) .classed("active",false); }); d3.select(".marker-line") .call(drag_line); dot_labels(); }; var make_Import_TS = function(selected_city){ //Set up margins of graph axes - need to make room for tick labels var margin = {top: 0, right: 10, bottom: 6, left: 25}; //Set up dimensions of the graph var width = document.getElementById("bottom").offsetWidth - margin.left - margin.right; var height = document.getElementById("bottom").offsetHeight - margin.top - margin.bottom; //get the imports time series for the selected port var import_data; Data.objects.forEach(function(circle){ if(circle.circle.City == selected_city)
}); var dates = Object.keys(crude_prices).map(function(date){ return parse_dates(date); }); dates = dates.slice(0,dates.length-6); var data =[]; //append each month in the time series as a new object pair to the data variable for(i=0; i<Object.keys(dates).length; i++){ var new_entry = {}; new_entry.date = dates[i]; new_entry.imports = Object.values(import_data)[i]; data.push(new_entry); } var imports_x = d3.scaleTime() .domain(d3.extent(dates)) .range([0, width]); var imports_scale = d3.scaleLinear() .domain([d3.min(import_data), d3.max(import_data)]) .range([height, 0]); var line = d3.line() .x(function(d){ return imports_x(d.date); }) .y(function(d){ return imports_scale(d.imports); }); var Import_svg = d3.select(".bottom") .select("svg") .append("g") .attr("id","imports-chart") .attr("transform", "translate(" + margin.left + "," + "-"+margin.bottom + ")"); Import_svg.append("g") .attr("class","y-axis") .attr("id", "Imports-axis") .attr("height", height) .attr("transform", "
{ import_data = circle.circle.Imports; }
conditional_block
installed.rs
where T: AsRef<str> + 'a, I: IntoIterator<Item = &'a T> { let mut url = String::new(); let mut scopes_string = scopes.into_iter().fold(String::new(), |mut acc, sc| { acc.push_str(sc.as_ref()); acc.push_str(" "); acc }); // Remove last space scopes_string.pop(); url.push_str(auth_uri); vec![format!("?scope={}", scopes_string), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI.to_string())), format!("&response_type=code"), format!("&client_id={}", client_id)] .into_iter() .fold(url, |mut u, param| { u.push_str(&percent_encode(param.as_ref(), QUERY_ENCODE_SET)); u }) } pub struct InstalledFlow<C> { client: C, server: Option<server::Listening>, port: Option<u32>, auth_code_rcv: Option<Receiver<String>>, } /// cf. https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi pub enum InstalledFlowReturnMethod { /// Involves showing a URL to the user and asking to copy a code from their browser /// (default) Interactive, /// Involves spinning up a local HTTP server and Google redirecting the browser to /// the server with a URL containing the code (preferred, but not as reliable). The /// parameter is the port to listen on. HTTPRedirect(u32), } impl<C> InstalledFlow<C> where C: BorrowMut<hyper::Client> { /// Starts a new Installed App auth flow. /// If HTTPRedirect is chosen as method and the server can't be started, the flow falls /// back to Interactive. pub fn new(client: C, method: Option<InstalledFlowReturnMethod>) -> InstalledFlow<C> { let default = InstalledFlow { client: client, server: None, port: None, auth_code_rcv: None, }; match method { None => default, Some(InstalledFlowReturnMethod::Interactive) => default, // Start server on localhost to accept auth code. Some(InstalledFlowReturnMethod::HTTPRedirect(port)) => { let server = server::Server::http(format!("127.0.0.1:{}", port).as_str()); match server { Result::Err(_) => default, Result::Ok(server) => { let (tx, rx) = channel(); let listening = server.handle(InstalledFlowHandler { auth_code_snd: Mutex::new(tx) }); match listening { Result::Err(_) => default, Result::Ok(listening) => { InstalledFlow { client: default.client, server: Some(listening), port: Some(port), auth_code_rcv: Some(rx), } } } } } } } } /// Handles the token request flow; it consists of the following steps: /// . Obtain a auhorization code with user cooperation or internal redirect. /// . Obtain a token and refresh token using that code. /// . Return that token /// /// It's recommended not to use the DefaultAuthenticatorDelegate, but a specialized one. pub fn obtain_token<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<Token, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let authcode = try!(self.get_authorization_code(auth_delegate, &appsecret, scopes)); let tokens = try!(self.request_token(&appsecret, &authcode)); // Successful response if tokens.access_token.is_some() { let mut token = Token { access_token: tokens.access_token.unwrap(), refresh_token: tokens.refresh_token.unwrap(), token_type: tokens.token_type.unwrap(), expires_in: tokens.expires_in, expires_in_timestamp: None, }; token.set_expiry_absolute(); Result::Ok(token) } else { let err = io::Error::new(io::ErrorKind::Other, format!("Token API error: {} {}", tokens.error.unwrap_or("<unknown err>".to_string()), tokens.error_description .unwrap_or("".to_string())) .as_str()); Result::Err(Box::new(err)) } } /// Obtains an authorization code either interactively or via HTTP redirect (see /// InstalledFlowReturnMethod). fn get_authorization_code<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<String, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let result: Result<String, Box<Error>> = match self.server { None => { let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, None); match auth_delegate.present_user_url(&url, true /* need_code */) { None => { Result::Err(Box::new(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read code"))) } // Remove newline Some(mut code) => { code.pop(); Result::Ok(code) } } } Some(_) => { // The redirect URI must be this very localhost URL, otherwise Google refuses // authorization. let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, Some(format!("http://localhost:{}", self.port .unwrap_or(8080)))); auth_delegate.present_user_url(&url, false /* need_code */); match self.auth_code_rcv.as_ref().unwrap().recv() { Result::Err(e) => Result::Err(Box::new(e)), Result::Ok(s) => Result::Ok(s), } } }; self.server.as_mut().map(|l| l.close()).is_some(); result } /// Sends the authorization code to the provider in order to obtain access and refresh tokens. fn request_token(&mut self, appsecret: &ApplicationSecret, authcode: &str) -> Result<JSONTokenResponse, Box<Error>> { let redirect_uri; match self.port { None => redirect_uri = OOB_REDIRECT_URI.to_string(), Some(p) => redirect_uri = format!("http://localhost:{}", p), } let body = form_urlencoded::serialize(vec![("code".to_string(), authcode.to_string()), ("client_id".to_string(), appsecret.client_id.clone()), ("client_secret".to_string(), appsecret.client_secret.clone()), ("redirect_uri".to_string(), redirect_uri), ("grant_type".to_string(), "authorization_code".to_string())]); let result: Result<client::Response, hyper::Error> = self.client .borrow_mut() .post(&appsecret.token_uri) .body(&body) .header(header::ContentType("application/x-www-form-urlencoded".parse().unwrap())) .send(); let mut resp = String::new(); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(mut response) => { let result = response.read_to_string(&mut resp); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(_) => (), } } } let token_resp: Result<JSONTokenResponse, error::Error> = serde_json::from_str(&resp); match token_resp { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(tok) => Result::Ok(tok) as Result<JSONTokenResponse, Box<Error>>, } } } #[derive(Deserialize)] struct JSONTokenResponse { access_token: Option<String>, refresh_token: Option<String>, token_type: Option<String>, expires_in: Option<i64>, error: Option<String>, error_description: Option<String>, } /// HTTP handler handling the redirect from the provider. struct InstalledFlowHandler { auth_code_snd: Mutex<Sender<String>>, } impl server::Handler for InstalledFlowHandler { fn handle(&self, rq: server::Request, mut rp: server::Response) { match rq.uri { uri::RequestUri::AbsolutePath(path) =>
{ // We use a fake URL because the redirect goes to a URL, meaning we // can't use the url form decode (because there's slashes and hashes and stuff in // it). let url = hyper::Url::parse(&format!("http://example.com{}", path)); if url.is_err() { *rp.status_mut() = status::StatusCode::BadRequest; let _ = rp.send("Unparseable URL".as_ref()); } else { self.handle_url(url.unwrap()); *rp.status_mut() = status::StatusCode::Ok; let _ = rp.send("<html><head><title>Success</title></head><body>You may now \ close this window.</body></html>" .as_ref()); } }
conditional_block
installed.rs
T, I>(auth_uri: &str, client_id: &str, scopes: I, redirect_uri: Option<String>) -> String where T: AsRef<str> + 'a, I: IntoIterator<Item = &'a T> { let mut url = String::new(); let mut scopes_string = scopes.into_iter().fold(String::new(), |mut acc, sc| { acc.push_str(sc.as_ref()); acc.push_str(" "); acc }); // Remove last space scopes_string.pop(); url.push_str(auth_uri); vec![format!("?scope={}", scopes_string), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI.to_string())), format!("&response_type=code"), format!("&client_id={}", client_id)] .into_iter() .fold(url, |mut u, param| { u.push_str(&percent_encode(param.as_ref(), QUERY_ENCODE_SET)); u }) } pub struct InstalledFlow<C> { client: C, server: Option<server::Listening>, port: Option<u32>, auth_code_rcv: Option<Receiver<String>>, } /// cf. https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi pub enum InstalledFlowReturnMethod { /// Involves showing a URL to the user and asking to copy a code from their browser /// (default) Interactive, /// Involves spinning up a local HTTP server and Google redirecting the browser to /// the server with a URL containing the code (preferred, but not as reliable). The /// parameter is the port to listen on. HTTPRedirect(u32), } impl<C> InstalledFlow<C> where C: BorrowMut<hyper::Client> { /// Starts a new Installed App auth flow. /// If HTTPRedirect is chosen as method and the server can't be started, the flow falls /// back to Interactive. pub fn new(client: C, method: Option<InstalledFlowReturnMethod>) -> InstalledFlow<C> { let default = InstalledFlow { client: client, server: None, port: None, auth_code_rcv: None, }; match method { None => default, Some(InstalledFlowReturnMethod::Interactive) => default, // Start server on localhost to accept auth code. Some(InstalledFlowReturnMethod::HTTPRedirect(port)) => { let server = server::Server::http(format!("127.0.0.1:{}", port).as_str()); match server { Result::Err(_) => default, Result::Ok(server) => { let (tx, rx) = channel(); let listening = server.handle(InstalledFlowHandler { auth_code_snd: Mutex::new(tx) }); match listening { Result::Err(_) => default, Result::Ok(listening) => { InstalledFlow { client: default.client, server: Some(listening), port: Some(port), auth_code_rcv: Some(rx), } } } } } } } } /// Handles the token request flow; it consists of the following steps: /// . Obtain a auhorization code with user cooperation or internal redirect. /// . Obtain a token and refresh token using that code. /// . Return that token /// /// It's recommended not to use the DefaultAuthenticatorDelegate, but a specialized one. pub fn obtain_token<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<Token, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let authcode = try!(self.get_authorization_code(auth_delegate, &appsecret, scopes)); let tokens = try!(self.request_token(&appsecret, &authcode)); // Successful response if tokens.access_token.is_some() { let mut token = Token { access_token: tokens.access_token.unwrap(), refresh_token: tokens.refresh_token.unwrap(), token_type: tokens.token_type.unwrap(), expires_in: tokens.expires_in, expires_in_timestamp: None, }; token.set_expiry_absolute(); Result::Ok(token) } else { let err = io::Error::new(io::ErrorKind::Other, format!("Token API error: {} {}", tokens.error.unwrap_or("<unknown err>".to_string()), tokens.error_description .unwrap_or("".to_string())) .as_str()); Result::Err(Box::new(err)) } } /// Obtains an authorization code either interactively or via HTTP redirect (see /// InstalledFlowReturnMethod). fn get_authorization_code<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<String, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let result: Result<String, Box<Error>> = match self.server { None => { let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, None); match auth_delegate.present_user_url(&url, true /* need_code */) { None => { Result::Err(Box::new(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read code"))) } // Remove newline Some(mut code) => { code.pop(); Result::Ok(code) } } } Some(_) => { // The redirect URI must be this very localhost URL, otherwise Google refuses // authorization. let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, Some(format!("http://localhost:{}", self.port .unwrap_or(8080)))); auth_delegate.present_user_url(&url, false /* need_code */); match self.auth_code_rcv.as_ref().unwrap().recv() { Result::Err(e) => Result::Err(Box::new(e)), Result::Ok(s) => Result::Ok(s), } } }; self.server.as_mut().map(|l| l.close()).is_some(); result } /// Sends the authorization code to the provider in order to obtain access and refresh tokens. fn request_token(&mut self, appsecret: &ApplicationSecret, authcode: &str) -> Result<JSONTokenResponse, Box<Error>> { let redirect_uri; match self.port { None => redirect_uri = OOB_REDIRECT_URI.to_string(), Some(p) => redirect_uri = format!("http://localhost:{}", p), } let body = form_urlencoded::serialize(vec![("code".to_string(), authcode.to_string()), ("client_id".to_string(), appsecret.client_id.clone()), ("client_secret".to_string(), appsecret.client_secret.clone()), ("redirect_uri".to_string(), redirect_uri), ("grant_type".to_string(), "authorization_code".to_string())]); let result: Result<client::Response, hyper::Error> = self.client .borrow_mut() .post(&appsecret.token_uri) .body(&body) .header(header::ContentType("application/x-www-form-urlencoded".parse().unwrap())) .send(); let mut resp = String::new(); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(mut response) => { let result = response.read_to_string(&mut resp); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(_) => (), } } } let token_resp: Result<JSONTokenResponse, error::Error> = serde_json::from_str(&resp); match token_resp { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(tok) => Result::Ok(tok) as Result<JSONTokenResponse, Box<Error>>, } } } #[derive(Deserialize)] struct JSONTokenResponse { access_token: Option<String>, refresh_token: Option<String>, token_type: Option<String>, expires_in: Option<i64>, error: Option<String>, error_description: Option<String>, } /// HTTP handler handling the redirect from the provider.
impl server::Handler for InstalledFlowHandler { fn handle(&self, rq: server::Request, mut rp: server::Response) { match rq.uri { uri::RequestUri::AbsolutePath(path) => { // We use a fake URL because the redirect goes to a URL, meaning we // can't use the url form decode (because there's slashes and hashes and stuff in // it). let url = hyper::Url::parse(&format!("http://example.com{}", path)); if url.is_err() { *rp.status_mut() = status::StatusCode::BadRequest; let _ = rp.send("Unparseable URL".as_ref()); } else { self.handle_url(url.unwrap()); *rp.status_mut() = status::StatusCode::Ok; let _ = rp.send("<html><head><
struct InstalledFlowHandler { auth_code_snd: Mutex<Sender<String>>, }
random_line_split
installed.rs
, I>(auth_uri: &str, client_id: &str, scopes: I, redirect_uri: Option<String>) -> String where T: AsRef<str> + 'a, I: IntoIterator<Item = &'a T> { let mut url = String::new(); let mut scopes_string = scopes.into_iter().fold(String::new(), |mut acc, sc| { acc.push_str(sc.as_ref()); acc.push_str(" "); acc }); // Remove last space scopes_string.pop(); url.push_str(auth_uri); vec![format!("?scope={}", scopes_string), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI.to_string())), format!("&response_type=code"), format!("&client_id={}", client_id)] .into_iter() .fold(url, |mut u, param| { u.push_str(&percent_encode(param.as_ref(), QUERY_ENCODE_SET)); u }) } pub struct InstalledFlow<C> { client: C, server: Option<server::Listening>, port: Option<u32>, auth_code_rcv: Option<Receiver<String>>, } /// cf. https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi pub enum InstalledFlowReturnMethod { /// Involves showing a URL to the user and asking to copy a code from their browser /// (default) Interactive, /// Involves spinning up a local HTTP server and Google redirecting the browser to /// the server with a URL containing the code (preferred, but not as reliable). The /// parameter is the port to listen on. HTTPRedirect(u32), } impl<C> InstalledFlow<C> where C: BorrowMut<hyper::Client> { /// Starts a new Installed App auth flow. /// If HTTPRedirect is chosen as method and the server can't be started, the flow falls /// back to Interactive. pub fn new(client: C, method: Option<InstalledFlowReturnMethod>) -> InstalledFlow<C>
match listening { Result::Err(_) => default, Result::Ok(listening) => { InstalledFlow { client: default.client, server: Some(listening), port: Some(port), auth_code_rcv: Some(rx), } } } } } } } } /// Handles the token request flow; it consists of the following steps: /// . Obtain a auhorization code with user cooperation or internal redirect. /// . Obtain a token and refresh token using that code. /// . Return that token /// /// It's recommended not to use the DefaultAuthenticatorDelegate, but a specialized one. pub fn obtain_token<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<Token, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let authcode = try!(self.get_authorization_code(auth_delegate, &appsecret, scopes)); let tokens = try!(self.request_token(&appsecret, &authcode)); // Successful response if tokens.access_token.is_some() { let mut token = Token { access_token: tokens.access_token.unwrap(), refresh_token: tokens.refresh_token.unwrap(), token_type: tokens.token_type.unwrap(), expires_in: tokens.expires_in, expires_in_timestamp: None, }; token.set_expiry_absolute(); Result::Ok(token) } else { let err = io::Error::new(io::ErrorKind::Other, format!("Token API error: {} {}", tokens.error.unwrap_or("<unknown err>".to_string()), tokens.error_description .unwrap_or("".to_string())) .as_str()); Result::Err(Box::new(err)) } } /// Obtains an authorization code either interactively or via HTTP redirect (see /// InstalledFlowReturnMethod). fn get_authorization_code<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<String, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let result: Result<String, Box<Error>> = match self.server { None => { let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, None); match auth_delegate.present_user_url(&url, true /* need_code */) { None => { Result::Err(Box::new(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read code"))) } // Remove newline Some(mut code) => { code.pop(); Result::Ok(code) } } } Some(_) => { // The redirect URI must be this very localhost URL, otherwise Google refuses // authorization. let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, Some(format!("http://localhost:{}", self.port .unwrap_or(8080)))); auth_delegate.present_user_url(&url, false /* need_code */); match self.auth_code_rcv.as_ref().unwrap().recv() { Result::Err(e) => Result::Err(Box::new(e)), Result::Ok(s) => Result::Ok(s), } } }; self.server.as_mut().map(|l| l.close()).is_some(); result } /// Sends the authorization code to the provider in order to obtain access and refresh tokens. fn request_token(&mut self, appsecret: &ApplicationSecret, authcode: &str) -> Result<JSONTokenResponse, Box<Error>> { let redirect_uri; match self.port { None => redirect_uri = OOB_REDIRECT_URI.to_string(), Some(p) => redirect_uri = format!("http://localhost:{}", p), } let body = form_urlencoded::serialize(vec![("code".to_string(), authcode.to_string()), ("client_id".to_string(), appsecret.client_id.clone()), ("client_secret".to_string(), appsecret.client_secret.clone()), ("redirect_uri".to_string(), redirect_uri), ("grant_type".to_string(), "authorization_code".to_string())]); let result: Result<client::Response, hyper::Error> = self.client .borrow_mut() .post(&appsecret.token_uri) .body(&body) .header(header::ContentType("application/x-www-form-urlencoded".parse().unwrap())) .send(); let mut resp = String::new(); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(mut response) => { let result = response.read_to_string(&mut resp); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(_) => (), } } } let token_resp: Result<JSONTokenResponse, error::Error> = serde_json::from_str(&resp); match token_resp { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(tok) => Result::Ok(tok) as Result<JSONTokenResponse, Box<Error>>, } } } #[derive(Deserialize)] struct JSONTokenResponse { access_token: Option<String>, refresh_token: Option<String>, token_type: Option<String>, expires_in: Option<i64>, error: Option<String>, error_description: Option<String>, } /// HTTP handler handling the redirect from the provider. struct InstalledFlowHandler { auth_code_snd: Mutex<Sender<String>>, } impl server::Handler for InstalledFlowHandler { fn handle(&self, rq: server::Request, mut rp: server::Response) { match rq.uri { uri::RequestUri::AbsolutePath(path) => { // We use a fake URL because the redirect goes to a URL, meaning we // can't use the url form decode (because there's slashes and hashes and stuff in // it). let url = hyper::Url::parse(&format!("http://example.com{}", path)); if url.is_err() { *rp.status_mut() = status::StatusCode::BadRequest; let _ = rp.send("Unparseable URL".as_ref()); } else { self.handle_url(url.unwrap()); *rp.status_mut() = status::StatusCode::Ok; let _ = rp.send("<html><head
{ let default = InstalledFlow { client: client, server: None, port: None, auth_code_rcv: None, }; match method { None => default, Some(InstalledFlowReturnMethod::Interactive) => default, // Start server on localhost to accept auth code. Some(InstalledFlowReturnMethod::HTTPRedirect(port)) => { let server = server::Server::http(format!("127.0.0.1:{}", port).as_str()); match server { Result::Err(_) => default, Result::Ok(server) => { let (tx, rx) = channel(); let listening = server.handle(InstalledFlowHandler { auth_code_snd: Mutex::new(tx) });
identifier_body
installed.rs
T, I>(auth_uri: &str, client_id: &str, scopes: I, redirect_uri: Option<String>) -> String where T: AsRef<str> + 'a, I: IntoIterator<Item = &'a T> { let mut url = String::new(); let mut scopes_string = scopes.into_iter().fold(String::new(), |mut acc, sc| { acc.push_str(sc.as_ref()); acc.push_str(" "); acc }); // Remove last space scopes_string.pop(); url.push_str(auth_uri); vec![format!("?scope={}", scopes_string), format!("&redirect_uri={}", redirect_uri.unwrap_or(OOB_REDIRECT_URI.to_string())), format!("&response_type=code"), format!("&client_id={}", client_id)] .into_iter() .fold(url, |mut u, param| { u.push_str(&percent_encode(param.as_ref(), QUERY_ENCODE_SET)); u }) } pub struct InstalledFlow<C> { client: C, server: Option<server::Listening>, port: Option<u32>, auth_code_rcv: Option<Receiver<String>>, } /// cf. https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi pub enum
{ /// Involves showing a URL to the user and asking to copy a code from their browser /// (default) Interactive, /// Involves spinning up a local HTTP server and Google redirecting the browser to /// the server with a URL containing the code (preferred, but not as reliable). The /// parameter is the port to listen on. HTTPRedirect(u32), } impl<C> InstalledFlow<C> where C: BorrowMut<hyper::Client> { /// Starts a new Installed App auth flow. /// If HTTPRedirect is chosen as method and the server can't be started, the flow falls /// back to Interactive. pub fn new(client: C, method: Option<InstalledFlowReturnMethod>) -> InstalledFlow<C> { let default = InstalledFlow { client: client, server: None, port: None, auth_code_rcv: None, }; match method { None => default, Some(InstalledFlowReturnMethod::Interactive) => default, // Start server on localhost to accept auth code. Some(InstalledFlowReturnMethod::HTTPRedirect(port)) => { let server = server::Server::http(format!("127.0.0.1:{}", port).as_str()); match server { Result::Err(_) => default, Result::Ok(server) => { let (tx, rx) = channel(); let listening = server.handle(InstalledFlowHandler { auth_code_snd: Mutex::new(tx) }); match listening { Result::Err(_) => default, Result::Ok(listening) => { InstalledFlow { client: default.client, server: Some(listening), port: Some(port), auth_code_rcv: Some(rx), } } } } } } } } /// Handles the token request flow; it consists of the following steps: /// . Obtain a auhorization code with user cooperation or internal redirect. /// . Obtain a token and refresh token using that code. /// . Return that token /// /// It's recommended not to use the DefaultAuthenticatorDelegate, but a specialized one. pub fn obtain_token<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<Token, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let authcode = try!(self.get_authorization_code(auth_delegate, &appsecret, scopes)); let tokens = try!(self.request_token(&appsecret, &authcode)); // Successful response if tokens.access_token.is_some() { let mut token = Token { access_token: tokens.access_token.unwrap(), refresh_token: tokens.refresh_token.unwrap(), token_type: tokens.token_type.unwrap(), expires_in: tokens.expires_in, expires_in_timestamp: None, }; token.set_expiry_absolute(); Result::Ok(token) } else { let err = io::Error::new(io::ErrorKind::Other, format!("Token API error: {} {}", tokens.error.unwrap_or("<unknown err>".to_string()), tokens.error_description .unwrap_or("".to_string())) .as_str()); Result::Err(Box::new(err)) } } /// Obtains an authorization code either interactively or via HTTP redirect (see /// InstalledFlowReturnMethod). fn get_authorization_code<'a, AD: AuthenticatorDelegate, S, T>(&mut self, auth_delegate: &mut AD, appsecret: &ApplicationSecret, scopes: S) -> Result<String, Box<Error>> where T: AsRef<str> + 'a, S: Iterator<Item = &'a T> { let result: Result<String, Box<Error>> = match self.server { None => { let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, None); match auth_delegate.present_user_url(&url, true /* need_code */) { None => { Result::Err(Box::new(io::Error::new(io::ErrorKind::UnexpectedEof, "couldn't read code"))) } // Remove newline Some(mut code) => { code.pop(); Result::Ok(code) } } } Some(_) => { // The redirect URI must be this very localhost URL, otherwise Google refuses // authorization. let url = build_authentication_request_url(&appsecret.auth_uri, &appsecret.client_id, scopes, Some(format!("http://localhost:{}", self.port .unwrap_or(8080)))); auth_delegate.present_user_url(&url, false /* need_code */); match self.auth_code_rcv.as_ref().unwrap().recv() { Result::Err(e) => Result::Err(Box::new(e)), Result::Ok(s) => Result::Ok(s), } } }; self.server.as_mut().map(|l| l.close()).is_some(); result } /// Sends the authorization code to the provider in order to obtain access and refresh tokens. fn request_token(&mut self, appsecret: &ApplicationSecret, authcode: &str) -> Result<JSONTokenResponse, Box<Error>> { let redirect_uri; match self.port { None => redirect_uri = OOB_REDIRECT_URI.to_string(), Some(p) => redirect_uri = format!("http://localhost:{}", p), } let body = form_urlencoded::serialize(vec![("code".to_string(), authcode.to_string()), ("client_id".to_string(), appsecret.client_id.clone()), ("client_secret".to_string(), appsecret.client_secret.clone()), ("redirect_uri".to_string(), redirect_uri), ("grant_type".to_string(), "authorization_code".to_string())]); let result: Result<client::Response, hyper::Error> = self.client .borrow_mut() .post(&appsecret.token_uri) .body(&body) .header(header::ContentType("application/x-www-form-urlencoded".parse().unwrap())) .send(); let mut resp = String::new(); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(mut response) => { let result = response.read_to_string(&mut resp); match result { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(_) => (), } } } let token_resp: Result<JSONTokenResponse, error::Error> = serde_json::from_str(&resp); match token_resp { Result::Err(e) => return Result::Err(Box::new(e)), Result::Ok(tok) => Result::Ok(tok) as Result<JSONTokenResponse, Box<Error>>, } } } #[derive(Deserialize)] struct JSONTokenResponse { access_token: Option<String>, refresh_token: Option<String>, token_type: Option<String>, expires_in: Option<i64>, error: Option<String>, error_description: Option<String>, } /// HTTP handler handling the redirect from the provider. struct InstalledFlowHandler { auth_code_snd: Mutex<Sender<String>>, } impl server::Handler for InstalledFlowHandler { fn handle(&self, rq: server::Request, mut rp: server::Response) { match rq.uri { uri::RequestUri::AbsolutePath(path) => { // We use a fake URL because the redirect goes to a URL, meaning we // can't use the url form decode (because there's slashes and hashes and stuff in // it). let url = hyper::Url::parse(&format!("http://example.com{}", path)); if url.is_err() { *rp.status_mut() = status::StatusCode::BadRequest; let _ = rp.send("Unparseable URL".as_ref()); } else { self.handle_url(url.unwrap()); *rp.status_mut() = status::StatusCode::Ok; let _ = rp.send("<html><head
InstalledFlowReturnMethod
identifier_name
types.go
Group of cluster type const ( STATIC_CLUSTER ClusterType = "STATIC" SIMPLE_CLUSTER ClusterType = "SIMPLE" DYNAMIC_CLUSTER ClusterType = "DYNAMIC" EDS_CLUSTER ClusterType = "EDS" ) // LbType type LbType string // Group of load balancer type const ( LB_RANDOM LbType = "LB_RANDOM" LB_ROUNDROBIN LbType = "LB_ROUNDROBIN" ) // RoutingPriority type RoutingPriority string // Group of routing priority const ( DEFAULT RoutingPriority = "DEFAULT" HIGH RoutingPriority = "HIGH" ) // Cluster represents a cluster's information type Cluster struct { Name string `json:"name"` ClusterType ClusterType `json:"type"` SubType string `json:"sub_type"` //not used yet LbType LbType `json:"lb_type"` MaxRequestPerConn uint32 `json:"max_request_per_conn"` ConnBufferLimitBytes uint32 `json:"conn_buffer_limit_bytes"` CirBreThresholds CircuitBreakers `json:"circuit_breakers,omitempty"` OutlierDetection OutlierDetection `json:"outlier_detection,omitempty"` //not used yet HealthCheck HealthCheck `json:"health_check,omitempty"` Spec ClusterSpecInfo `json:"spec,omitempty"` LBSubSetConfig LBSubsetConfig `json:"lb_subset_config,omitempty"` TLS TLSConfig `json:"tls_context,omitempty"` Hosts []Host `json:"hosts"` } // HealthCheck is a configuration of health check // use DurationConfig to parse string to time.Duration type HealthCheck struct { HealthCheckConfig ProtocolCode byte `json:"-"` Timeout time.Duration `json:"-"` Interval time.Duration `json:"-"` IntervalJitter time.Duration `json:"-"` } // Host represenets a host information type Host struct { HostConfig MetaData Metadata `json:"-"` } // Listener contains the listener's information type Listener struct { ListenerConfig Addr net.Addr `json:"-"` ListenerTag uint64 `json:"-"` ListenerScope string `json:"-"` PerConnBufferLimitBytes uint32 `json:"-"` // do not support config InheritListener *net.TCPListener `json:"-"` Remain bool `json:"-"` LogLevel uint8 `json:"-"` DisableConnIo bool `json:"-"` } // TCPRoute type TCPRoute struct { Cluster string SourceAddrs []CidrRange DestinationAddrs []CidrRange SourcePort string DestinationPort string } // CidrRange type CidrRange struct { Address string Length uint32 IpNet *net.IPNet } // HealthCheckFilter type HealthCheckFilter struct { HealthCheckFilterConfig CacheTime time.Duration `json:"-"` } // FaultInject type FaultInject struct { FaultInjectConfig DelayDuration uint64 `json:"-"` } // StreamFaultInject type StreamFaultInject struct { Delay *DelayInject `json:"delay"` Abort *AbortInject `json:"abort"` UpstreamCluster string `json:"upstream_cluster"` Headers []HeaderMatcher `json:"headers"` } type DelayInject struct { DelayInjectConfig Delay time.Duration `json:"-"` } type AbortInject struct { Status int `json:"status"` Percent uint32 `json:"percentage"` } // Router, the list of routes that will be matched, in order, for incoming requests. // The first route that matches will be used. type Router struct { RouterConfig Metadata Metadata `json:"-"` } // RouteAction represents the information of route request to upstream clusters type RouteAction struct { RouterActionConfig MetadataMatch Metadata `json:"-"` Timeout time.Duration `json:"-"` } // Decorator type Decorator string // ClusterWeight. // clusters along with weights that indicate the percentage // of traffic to be forwarded to each cluster type ClusterWeight struct { ClusterWeightConfig MetadataMatch Metadata `json:"-"` } // RetryPolicy represents the retry parameters type RetryPolicy struct { RetryPolicyConfig RetryTimeout time.Duration `json:"-"` } // CircuitBreakers is a configuration of circuit breakers // CircuitBreakers implements json.Marshaler and json.Unmarshaler type CircuitBreakers struct { Thresholds []Thresholds } type Thresholds struct { Priority RoutingPriority `json:"priority"` MaxConnections uint32 `json:"max_connections"` MaxPendingRequests uint32 `json:"max_pending_requests"` MaxRequests uint32 `json:"max_requests"` MaxRetries uint32 `json:"max_retries"` } // OutlierDetection not used yet type OutlierDetection struct { Consecutive5xx uint32 Interval time.Duration BaseEjectionTime time.Duration MaxEjectionPercent uint32 ConsecutiveGatewayFailure uint32 EnforcingConsecutive5xx uint32 EnforcingConsecutiveGatewayFailure uint32 EnforcingSuccessRate uint32 SuccessRateMinimumHosts uint32 SuccessRateRequestVolume uint32 SuccessRateStdevFactor uint32 } // ClusterSpecInfo is a configuration of subscribe type ClusterSpecInfo struct { Subscribes []SubscribeSpec `json:"subscribe,omitempty"` } // SubscribeSpec describes the subscribe server type SubscribeSpec struct { ServiceName string `json:"service_name,omitempty"` } // LBSubsetConfig is a configuration of load balance subset type LBSubsetConfig struct { FallBackPolicy uint8 `json:"fall_back_policy"` DefaultSubset map[string]string `json:"default_subset"` SubsetSelectors [][]string `json:"subset_selectors"` } // TLSConfig is a configuration of tls context type TLSConfig struct { Status bool `json:"status"` Type string `json:"type"` ServerName string `json:"server_name,omitempty"` CACert string `json:"ca_cert,omitempty"` CertChain string `json:"cert_chain,omitempty"` PrivateKey string `json:"private_key,omitempty"` VerifyClient bool `json:"verify_client,omitempty"` InsecureSkip bool `json:"insecure_skip,omitempty"` CipherSuites string `json:"cipher_suites,omitempty"` EcdhCurves string `json:"ecdh_curves,omitempty"` MinVersion string `json:"min_version,omitempty"` MaxVersion string `json:"max_version,omitempty"` ALPN string `json:"alpn,omitempty"` Ticket string `json:"ticket,omitempty"` Fallback bool `json:"fall_back, omitempty"` ExtendVerify map[string]interface{} `json:"extend_verify,omitempty"` } // AccessLog for making up access log type AccessLog struct { Path string `json:"log_path,omitempty"` Format string `json:"log_format,omitempty"` } // FilterChain wraps a set of match criteria, an option TLS context, // a set of filters, and various other parameters. type FilterChain struct { FilterChainMatch string `json:"match,omitempty"` TLS TLSConfig `json:"tls_context,omitempty"` Filters []Filter `json:"filters"` // "proxy" and "connection_manager" used at this time } // Filter is a config to make up a filter type Filter struct { Type string `json:"type,omitempty"` Config map[string]interface{} `json:"config,omitempty"`
// TCPProxy type TCPProxy struct { StatPrefix string `json:"stat_prefix,omitempty"` Cluster string `json:"cluster,omitempty"` IdleTimeout *time.Duration `json:"idle_timeout,omitempty"` MaxConnectAttempts uint32 `json:"max_connect_attempts,omitempty"` Routes []*TCPRoute `json:"routes,omitempty"` } // WebSocketProxy type WebSocketProxy struct { StatPrefix string IdleTimeout *time.Duration MaxConnectAttempts uint32 } // Proxy type Proxy struct { Name string `json:"name"` DownstreamProtocol string `json:"downstream_protocol"` UpstreamProtocol string `json:"upstream_protocol"` RouterConfigName string `json:"router_config_name"` ValidateClusters bool `json:"validate_clusters"` ExtendConfig map[string]interface{} `json:"extend_config"` } // HeaderValueOption is header name/value pair plus option to control append behavior. type HeaderValueOption struct { Header *HeaderValue `json:"header"` Append *bool `json:"append"` } // HeaderValue is header name/value pair. type HeaderValue struct { Key string `json:"key"` Value string `json:"value"` } // RouterConfiguration is a filter for routers // Filter type is: "CONNECTION_MANAGER" type RouterConfiguration struct { RouterConfigName string `json:"router_config_name"` VirtualHosts []*VirtualHost `json:"virtual_hosts"` RequestHeadersToAdd []*HeaderValueOption `json:"request_headers_to_add"` ResponseHeadersToAdd []*HeaderValueOption `json:"response_headers_to_add"` ResponseHeadersToRemove []string `json:"response_headers_to_remove"` } // VirtualHost is used to make up the route table type VirtualHost struct {
} // Implements of filter config
random_line_split
server.go
() continue } latest := kv.configs[len(kv.configs)-1] kv.mu.RUnlock() config := kv.masters.Query(latest.Num + 1) if latest.Num < config.Num { kv.rf.Start(config) } } } } func (kv *ShardKV) migrationChecker() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitClean.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitClean.GetGidShard() config := kv.waitClean.GetConfig() if time.Now().Sub(start) > WaitCleanTimeOut { kv.rf.Start(CheckMigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitCleanTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := CheckMigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args CheckMigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply CheckMigrateShardReply ok := srv.Call("ShardKV.CheckMigrateShard", &args, &reply) /*if ok { DPrintf("ShardKV %d (gid = %d) CheckMigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) }*/ if ok && reply.Result == OK { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) migrationHelper() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitMigration.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitMigration.GetGidShards() config := kv.waitMigration.GetConfig() if time.Now().Sub(start) > WaitMigrationTimeOut { kv.rf.Start(MigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitMigrationTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := MigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args MigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply MigrateShardReply ok := srv.Call("ShardKV.MigrateShard", &args, &reply) DPrintf("ShardKV %d (gid = %d) MigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) if ok && (reply.Result == OK || reply.Result == ErrShardHasBeenCleaned) { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) init() { kv.mu.Lock() defer kv.mu.Unlock() data := kv.persister.ReadSnapshot() if len(data) == 0 { kv.lastApplied = 0 kv.database = NewShardDatabase() kv.lastOpId = make(map[int64]int64) kv.configs = make([]shardmaster.Config, 1) kv.configs[0].Groups = map[int][]string{} kv.waitClean = NewWaitClean() kv.waitMigration = NewWaitMigration() } else { r := bytes.NewBuffer(data) d := labgob.NewDecoder(r) kv.lastApplied = 0 kv.database = nil kv.lastOpId = nil kv.configs = nil kv.waitMigration = nil kv.waitClean = nil d.Decode(&kv.lastApplied) d.Decode(&kv.database) d.Decode(&kv.lastOpId) d.Decode(&kv.configs) d.Decode(&kv.waitClean) d.Decode(&kv.waitMigration) } } func (kv *ShardKV) saveShardKVState(force bool) { shouldSave := kv.maxraftstate != -1 && (force || kv.persister.RaftStateSize() > kv.maxraftstate) if shouldSave { w := new(bytes.Buffer) e := labgob.NewEncoder(w) e.Encode(kv.lastApplied) e.Encode(kv.database) e.Encode(kv.lastOpId) e.Encode(kv.configs) e.Encode(kv.waitClean) e.Encode(kv.waitMigration) snapshot := w.Bytes() kv.rf.SaveSnapshot(kv.lastApplied, snapshot) } } func (kv *ShardKV) stateMachine() { for { select { case <-kv.ctx.Done(): DPrintf("ShardKV %d (gid=%d)stateMachine closed\n", kv.me, kv.gid) return case applyMsg := <-kv.applyCh: if applyMsg.CommandValid { kv.mu.Lock() if kv.lastApplied+1 < applyMsg.CommandIndex { kv.mu.Unlock() kv.rf.Replay() } else { if kv.lastApplied+1 == applyMsg.CommandIndex { kv.lastApplied++ //DPrintf("ShardKV(gid=%d) %d stateMachine received command %v %+v\n", kv.me, kv.gid, reflect.TypeOf(applyMsg.Command), applyMsg) switch command := applyMsg.Command.(type) { case OpArgs: op := command result := OpResult{ClientId: op.ClientId, OpId: op.OpId} switch op.OpType { case "Get": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case op.ConfigNum != latest.Num || latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: shard := kv.database.GetShard(shardNum) if value, ok := shard.Get(op.Key); ok { result.Result = OK result.Value = value } else { result.Result = ErrNoKey } str, _ := json.Marshal(kv.database) DPrintf("ShardKV(gid=%d) %d shardNum %d database= %s get key:%v result: %s\n", kv.gid, kv.me, shardNum, str, op.Key, result.Result) kv.saveShardKVState(false) } go kv.applyWait.Trigger(result) case "Put": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case op.ConfigNum != latest.Num || latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: result.Result = OK if lastOpId, ok := kv.lastOpId[op.ClientId]; !ok || op.OpId > lastOpId { shard := kv.database.GetShard(shardNum) shard.Put(op.Key, op.Value, op.ClientId, op.OpId) kv.lastOpId[op.ClientId] = op.OpId kv.saveShardKVState(false) } } go kv.applyWait.Trigger(result) case "Append": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: result.Result = OK if lastOpId, ok := kv.lastOpId[op.ClientId]; !ok || op.OpId > lastOpId
{ shard := kv.database.GetShard(shardNum) shard.Append(op.Key, op.Value, op.ClientId, op.OpId) kv.lastOpId[op.ClientId] = op.OpId kv.saveShardKVState(false) }
conditional_block
server.go
("ShardKV %d (gid = %d) CheckMigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) }*/ if ok && reply.Result == OK { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) migrationHelper() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitMigration.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitMigration.GetGidShards() config := kv.waitMigration.GetConfig() if time.Now().Sub(start) > WaitMigrationTimeOut { kv.rf.Start(MigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitMigrationTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := MigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args MigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply MigrateShardReply ok := srv.Call("ShardKV.MigrateShard", &args, &reply) DPrintf("ShardKV %d (gid = %d) MigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) if ok && (reply.Result == OK || reply.Result == ErrShardHasBeenCleaned) { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) init() { kv.mu.Lock() defer kv.mu.Unlock() data := kv.persister.ReadSnapshot() if len(data) == 0 { kv.lastApplied = 0 kv.database = NewShardDatabase() kv.lastOpId = make(map[int64]int64) kv.configs = make([]shardmaster.Config, 1) kv.configs[0].Groups = map[int][]string{} kv.waitClean = NewWaitClean() kv.waitMigration = NewWaitMigration() } else { r := bytes.NewBuffer(data) d := labgob.NewDecoder(r) kv.lastApplied = 0 kv.database = nil kv.lastOpId = nil kv.configs = nil kv.waitMigration = nil kv.waitClean = nil d.Decode(&kv.lastApplied) d.Decode(&kv.database) d.Decode(&kv.lastOpId) d.Decode(&kv.configs) d.Decode(&kv.waitClean) d.Decode(&kv.waitMigration) } } func (kv *ShardKV) saveShardKVState(force bool) { shouldSave := kv.maxraftstate != -1 && (force || kv.persister.RaftStateSize() > kv.maxraftstate) if shouldSave { w := new(bytes.Buffer) e := labgob.NewEncoder(w) e.Encode(kv.lastApplied) e.Encode(kv.database) e.Encode(kv.lastOpId) e.Encode(kv.configs) e.Encode(kv.waitClean) e.Encode(kv.waitMigration) snapshot := w.Bytes() kv.rf.SaveSnapshot(kv.lastApplied, snapshot) } } func (kv *ShardKV) stateMachine() { for { select { case <-kv.ctx.Done(): DPrintf("ShardKV %d (gid=%d)stateMachine closed\n", kv.me, kv.gid) return case applyMsg := <-kv.applyCh: if applyMsg.CommandValid { kv.mu.Lock() if kv.lastApplied+1 < applyMsg.CommandIndex { kv.mu.Unlock() kv.rf.Replay() } else { if kv.lastApplied+1 == applyMsg.CommandIndex { kv.lastApplied++ //DPrintf("ShardKV(gid=%d) %d stateMachine received command %v %+v\n", kv.me, kv.gid, reflect.TypeOf(applyMsg.Command), applyMsg) switch command := applyMsg.Command.(type) { case OpArgs: op := command result := OpResult{ClientId: op.ClientId, OpId: op.OpId} switch op.OpType { case "Get": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case op.ConfigNum != latest.Num || latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: shard := kv.database.GetShard(shardNum) if value, ok := shard.Get(op.Key); ok { result.Result = OK result.Value = value } else { result.Result = ErrNoKey } str, _ := json.Marshal(kv.database) DPrintf("ShardKV(gid=%d) %d shardNum %d database= %s get key:%v result: %s\n", kv.gid, kv.me, shardNum, str, op.Key, result.Result) kv.saveShardKVState(false) } go kv.applyWait.Trigger(result) case "Put": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case op.ConfigNum != latest.Num || latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: result.Result = OK if lastOpId, ok := kv.lastOpId[op.ClientId]; !ok || op.OpId > lastOpId { shard := kv.database.GetShard(shardNum) shard.Put(op.Key, op.Value, op.ClientId, op.OpId) kv.lastOpId[op.ClientId] = op.OpId kv.saveShardKVState(false) } } go kv.applyWait.Trigger(result) case "Append": shardNum := key2shard(op.Key) latest := kv.configs[len(kv.configs)-1] switch { case latest.Shards[shardNum] != kv.gid: result.Result = ErrWrongGroup case kv.waitMigration.IsMigrationShard(shardNum): result.Result = ErrShardIsMigrating default: result.Result = OK if lastOpId, ok := kv.lastOpId[op.ClientId]; !ok || op.OpId > lastOpId { shard := kv.database.GetShard(shardNum) shard.Append(op.Key, op.Value, op.ClientId, op.OpId) kv.lastOpId[op.ClientId] = op.OpId kv.saveShardKVState(false) } } go kv.applyWait.Trigger(result) default: DPrintf("ShardKV %d (gid=%d) stateMachine received wrong opType OpArgs: %+v\n", kv.me, kv.gid, command) } case shardmaster.Config: newConfig := command oldConfig := kv.configs[len(kv.configs)-1] if newConfig.Num > oldConfig.Num && kv.waitMigration.IsEmpty() && kv.waitClean.IsEmpty() { kv.configs = append(kv.configs, newConfig) if oldConfig.Num > 0 { kv.waitMigration.Init(shardmaster.Config{ Num: newConfig.Num, Shards: oldConfig.Shards, Groups: oldConfig.Groups, }) kv.waitClean.Init(newConfig) for shardNum := 0; shardNum < shardmaster.NShards; shardNum++ { oldGid := oldConfig.Shards[shardNum] newGid := newConfig.Shards[shardNum] if kv.gid == oldGid && kv.gid != newGid { // old shard remove from this group kv.waitClean.AddGidShard(newGid, shardNum) } if kv.gid != oldGid && kv.gid == newGid { // new shard assign to this group kv.waitMigration.AddGidShard(oldGid, shardNum) }
} // remove shard from kv.Database and store in waitClean kv.waitClean.StoreCleanData(kv.database) if !kv.waitMigration.IsEmpty() { go kv.migrationHelper()
random_line_split
server.go
KV { // call labgob.Register on structures you want // Go's RPC library to marshall/unmarshall. kv := new(ShardKV) kv.me = me kv.maxraftstate = maxraftstate kv.make_end = make_end kv.gid = gid kv.masters = shardmaster.MakeClerk(masters) kv.applyCh = make(chan raft.ApplyMsg) kv.applyWait = NewWait() kv.persister = persister ctx, cancel := context.WithCancel(context.Background()) kv.ctx = ctx kv.close = cancel kv.init() kv.rf = raft.Make(servers, me, persister, kv.applyCh) if !kv.waitMigration.IsEmpty() { go kv.migrationHelper() } if !kv.waitClean.IsEmpty() { go kv.migrationChecker() } go kv.newConfigLearner() go kv.stateMachine() return kv } func (kv *ShardKV) start(args interface{}) (result string, value string) { var op OpArgs if getArgs, ok := args.(GetArgs); ok { op = OpArgs{ ConfigNum: getArgs.ConfigNum, ClientId: getArgs.ClientId, OpId: getArgs.OpId, Key: getArgs.Key, Value: "", OpType: "Get", } } else if putAppendArgs, ok := args.(PutAppendArgs); ok { op = OpArgs{ ConfigNum: putAppendArgs.ConfigNum, ClientId: putAppendArgs.ClientId, OpId: putAppendArgs.OpId, Key: putAppendArgs.Key, Value: putAppendArgs.Value, OpType: putAppendArgs.Op, } } else { return fmt.Sprintf("ErrArgsType:%+v", args), "" } resultCh := kv.applyWait.Register(op) defer kv.applyWait.Unregister(op) _, _, isLeader := kv.rf.Start(op) if !isLeader { return ErrWrongLeader, "" } t := time.NewTimer(OpTimeout) select { case <-kv.ctx.Done(): return ErrShardKVClosed, "" case <-t.C: return ErrOpTimeout, "" case opResult := <-resultCh: //DPrintf("ShardKV %d return client %d by resultCh result = %+v\n", kv.me, op.ClientId, opResult) return opResult.Result, opResult.Value } } func (kv *ShardKV) newConfigLearner() { t := time.NewTicker(100 * time.Millisecond) for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if !kv.waitClean.IsEmpty() || !kv.waitMigration.IsEmpty() { // is applying new Config kv.mu.RUnlock() continue } latest := kv.configs[len(kv.configs)-1] kv.mu.RUnlock() config := kv.masters.Query(latest.Num + 1) if latest.Num < config.Num { kv.rf.Start(config) } } } } func (kv *ShardKV) migrationChecker()
Result: ErrWaitCleanTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := CheckMigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args CheckMigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply CheckMigrateShardReply ok := srv.Call("ShardKV.CheckMigrateShard", &args, &reply) /*if ok { DPrintf("ShardKV %d (gid = %d) CheckMigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) }*/ if ok && reply.Result == OK { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) migrationHelper() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitMigration.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitMigration.GetGidShards() config := kv.waitMigration.GetConfig() if time.Now().Sub(start) > WaitMigrationTimeOut { kv.rf.Start(MigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitMigrationTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := MigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args MigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply MigrateShardReply ok := srv.Call("ShardKV.MigrateShard", &args, &reply) DPrintf("ShardKV %d (gid = %d) MigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) if ok && (reply.Result == OK || reply.Result == ErrShardHasBeenCleaned) { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) init() { kv.mu.Lock() defer kv.mu.Unlock() data := kv.persister.ReadSnapshot() if len(data) == 0 { kv.lastApplied = 0 kv.database = NewShardDatabase() kv.lastOpId = make(map[int64]int64) kv.configs = make([]shardmaster.Config, 1) kv.configs[0].Groups = map[int][]string{} kv.waitClean = NewWaitClean() kv.waitMigration = NewWaitMigration() } else { r := bytes.NewBuffer(data) d := labgob.NewDecoder(r) kv.lastApplied = 0 kv.database = nil kv.lastOpId = nil kv.configs = nil kv.waitMigration = nil kv.waitClean = nil d.Decode(&kv.lastApplied) d.Decode(&kv.database) d.Decode(&kv.lastOpId) d.Decode(&kv.configs) d.Decode(&kv.waitClean) d.Decode(&kv.waitMigration) } } func (kv *ShardKV) saveShardKVState(force bool) { shouldSave := kv.maxraftstate != -1 && (force || kv.persister.RaftStateSize() > kv.maxraftstate) if shouldSave { w := new(bytes.Buffer) e := labgob.NewEncoder(w) e.Encode(kv.lastApplied) e.Encode(kv.database) e.Encode(kv.lastOpId) e.Encode(kv.configs) e.Encode(kv.waitClean) e.Encode(kv.waitMigration) snapshot := w.Bytes() kv.rf.SaveSnapshot(kv.lastApplied, snapshot) } } func (kv *ShardKV) stateMachine() { for { select { case <-kv.ctx.Done(): DPrintf("ShardKV %d (gid=%d)stateMachine closed\n", kv.me, kv.gid) return case applyMsg := <-kv.applyCh: if applyMsg.CommandValid { kv.mu.Lock() if kv.lastApplied+1 < applyMsg.CommandIndex { kv.mu.Unlock() kv.rf.Replay() } else { if kv.lastApplied+1 == applyMsg.CommandIndex { kv.lastApplied++ //DPrintf("ShardKV(gid=%d) %d stateMachine received command %v %+v\n", kv.me, kv.gid, reflect.TypeOf(applyMsg.Command), applyMsg) switch command := applyMsg.Command.(type) { case Op
{ t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitClean.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitClean.GetGidShard() config := kv.waitClean.GetConfig() if time.Now().Sub(start) > WaitCleanTimeOut { kv.rf.Start(CheckMigrateShardReply{ ConfigNum: config.Num, Gid: -1,
identifier_body
server.go
KV { // call labgob.Register on structures you want // Go's RPC library to marshall/unmarshall. kv := new(ShardKV) kv.me = me kv.maxraftstate = maxraftstate kv.make_end = make_end kv.gid = gid kv.masters = shardmaster.MakeClerk(masters) kv.applyCh = make(chan raft.ApplyMsg) kv.applyWait = NewWait() kv.persister = persister ctx, cancel := context.WithCancel(context.Background()) kv.ctx = ctx kv.close = cancel kv.init() kv.rf = raft.Make(servers, me, persister, kv.applyCh) if !kv.waitMigration.IsEmpty() { go kv.migrationHelper() } if !kv.waitClean.IsEmpty() { go kv.migrationChecker() } go kv.newConfigLearner() go kv.stateMachine() return kv } func (kv *ShardKV) start(args interface{}) (result string, value string) { var op OpArgs if getArgs, ok := args.(GetArgs); ok { op = OpArgs{ ConfigNum: getArgs.ConfigNum, ClientId: getArgs.ClientId, OpId: getArgs.OpId, Key: getArgs.Key, Value: "", OpType: "Get", } } else if putAppendArgs, ok := args.(PutAppendArgs); ok { op = OpArgs{ ConfigNum: putAppendArgs.ConfigNum, ClientId: putAppendArgs.ClientId, OpId: putAppendArgs.OpId, Key: putAppendArgs.Key, Value: putAppendArgs.Value, OpType: putAppendArgs.Op, } } else { return fmt.Sprintf("ErrArgsType:%+v", args), "" } resultCh := kv.applyWait.Register(op) defer kv.applyWait.Unregister(op) _, _, isLeader := kv.rf.Start(op) if !isLeader { return ErrWrongLeader, "" } t := time.NewTimer(OpTimeout) select { case <-kv.ctx.Done(): return ErrShardKVClosed, "" case <-t.C: return ErrOpTimeout, "" case opResult := <-resultCh: //DPrintf("ShardKV %d return client %d by resultCh result = %+v\n", kv.me, op.ClientId, opResult) return opResult.Result, opResult.Value } } func (kv *ShardKV) newConfigLearner() { t := time.NewTicker(100 * time.Millisecond) for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if !kv.waitClean.IsEmpty() || !kv.waitMigration.IsEmpty() { // is applying new Config kv.mu.RUnlock() continue } latest := kv.configs[len(kv.configs)-1] kv.mu.RUnlock() config := kv.masters.Query(latest.Num + 1) if latest.Num < config.Num { kv.rf.Start(config) } } } } func (kv *ShardKV) migrationChecker() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitClean.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitClean.GetGidShard() config := kv.waitClean.GetConfig() if time.Now().Sub(start) > WaitCleanTimeOut { kv.rf.Start(CheckMigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitCleanTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := CheckMigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args CheckMigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply CheckMigrateShardReply ok := srv.Call("ShardKV.CheckMigrateShard", &args, &reply) /*if ok { DPrintf("ShardKV %d (gid = %d) CheckMigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) }*/ if ok && reply.Result == OK { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) migrationHelper() { t := time.NewTicker(100 * time.Millisecond) start := time.Now() for { select { case <-kv.ctx.Done(): return case <-t.C: kv.mu.RLock() if kv.waitMigration.IsEmpty() { kv.mu.RUnlock() return } gidShards := kv.waitMigration.GetGidShards() config := kv.waitMigration.GetConfig() if time.Now().Sub(start) > WaitMigrationTimeOut { kv.rf.Start(MigrateShardReply{ ConfigNum: config.Num, Gid: -1, Result: ErrWaitMigrationTimeOut, }) } else { for gid, shardNums := range gidShards { servers := config.Groups[gid] args := MigrateShardArgs{ config.Num, gid, shardNums, } go func(servers []string, args MigrateShardArgs) { for si := 0; si < len(servers); si++ { srv := kv.make_end(servers[si]) var reply MigrateShardReply ok := srv.Call("ShardKV.MigrateShard", &args, &reply) DPrintf("ShardKV %d (gid = %d) MigrateShard ok = %v args = %+v reply = %+v\n", kv.me, kv.gid, ok, args, reply) if ok && (reply.Result == OK || reply.Result == ErrShardHasBeenCleaned) { kv.rf.Start(reply) return } } }(servers, args) } } kv.mu.RUnlock() } } } func (kv *ShardKV) init() { kv.mu.Lock() defer kv.mu.Unlock() data := kv.persister.ReadSnapshot() if len(data) == 0 { kv.lastApplied = 0 kv.database = NewShardDatabase() kv.lastOpId = make(map[int64]int64) kv.configs = make([]shardmaster.Config, 1) kv.configs[0].Groups = map[int][]string{} kv.waitClean = NewWaitClean() kv.waitMigration = NewWaitMigration() } else { r := bytes.NewBuffer(data) d := labgob.NewDecoder(r) kv.lastApplied = 0 kv.database = nil kv.lastOpId = nil kv.configs = nil kv.waitMigration = nil kv.waitClean = nil d.Decode(&kv.lastApplied) d.Decode(&kv.database) d.Decode(&kv.lastOpId) d.Decode(&kv.configs) d.Decode(&kv.waitClean) d.Decode(&kv.waitMigration) } } func (kv *ShardKV)
(force bool) { shouldSave := kv.maxraftstate != -1 && (force || kv.persister.RaftStateSize() > kv.maxraftstate) if shouldSave { w := new(bytes.Buffer) e := labgob.NewEncoder(w) e.Encode(kv.lastApplied) e.Encode(kv.database) e.Encode(kv.lastOpId) e.Encode(kv.configs) e.Encode(kv.waitClean) e.Encode(kv.waitMigration) snapshot := w.Bytes() kv.rf.SaveSnapshot(kv.lastApplied, snapshot) } } func (kv *ShardKV) stateMachine() { for { select { case <-kv.ctx.Done(): DPrintf("ShardKV %d (gid=%d)stateMachine closed\n", kv.me, kv.gid) return case applyMsg := <-kv.applyCh: if applyMsg.CommandValid { kv.mu.Lock() if kv.lastApplied+1 < applyMsg.CommandIndex { kv.mu.Unlock() kv.rf.Replay() } else { if kv.lastApplied+1 == applyMsg.CommandIndex { kv.lastApplied++ //DPrintf("ShardKV(gid=%d) %d stateMachine received command %v %+v\n", kv.me, kv.gid, reflect.TypeOf(applyMsg.Command), applyMsg) switch command := applyMsg.Command.(type) { case Op
saveShardKVState
identifier_name
xor.go
<input type="search" id="codesearchQuery" value="" size="30" onkeydown="return codesearchKeyDown(event);"/> <form method="GET" action="http://www.google.com/codesearch" id="codesearch" class="search" onsubmit="return codeSearchSubmit();" style="display:inline;"> <input type="hidden" name="q" value=""/>
<span style="color: red">(TODO: remove for now?)</span> </form> </td> </tr> <tr> <td> <span style="color: gray;">(e.g. &ldquo;pem&rdquo; or &ldquo;xml&rdquo;)</span> </td> </tr> </table> --> </td> </tr> </table> </div> <div id="linkList"> <ul> <li class="navhead"><a href="../../../../index.html">Home</a></li> <li class="blank">&nbsp;</li> <li class="navhead">Documents</li> <li><a href="../../../../doc/go_tutorial.html">Tutorial</a></li> <li><a href="../../../../doc/effective_go.html">Effective Go</a></li> <li><a href="../../../../doc/go_faq.html">FAQ</a></li> <li><a href="../../../../doc/go_lang_faq.html">Language Design FAQ</a></li> <li><a href="http://www.youtube.com/watch?v=rKnDgT73v8s">Tech talk (1 hour)</a> (<a href="../../../../doc/go_talk-20091030.pdf">PDF</a>)</li> <li><a href="../../../../doc/go_spec.html">Language Specification</a></li> <li><a href="../../../../doc/go_mem.html">Memory Model</a></li> <li><a href="../../../../doc/go_for_cpp_programmers.html">Go for C++ Programmers</a></li> <li class="blank">&nbsp;</li> <li class="navhead">How To</li> <li><a href="../../../../doc/install.html">Install Go</a></li> <li><a href="../../../../doc/contribute.html">Contribute code</a></li> <li class="blank">&nbsp;</li> <li class="navhead">Programming</li> <li><a href="../../../../cmd/index.html">Command documentation</a></li> <li><a href="../../../../pkg/index.html">Package documentation</a></li> <li><a href="../../../index.html">Source files</a></li> <li class="blank">&nbsp;</li> <li class="navhead">Help</li> <li>#go-nuts on irc.freenode.net</li> <li><a href="http://groups.google.com/group/golang-nuts">Go Nuts mailing list</a></li> <li><a href="http://code.google.com/p/go/issues/list">Issue tracker</a></li> <li class="blank">&nbsp;</li> <li class="navhead">Go code search</li> <form method="GET" action="http://golang.org/search" class="search"> <input type="search" name="q" value="" size="25" style="width:80%; max-width:200px" /> <input type="submit" value="Go" /> </form> <li class="blank">&nbsp;</li> <li class="navhead">Last update</li> <li>Thu Nov 12 15:48:37 PST 2009</li> </ul> </div> <div id="content"> <h1 id="generatedHeader">Source file /src/pkg/crypto/block/xor.go</h1> <!-- The Table of Contents is automatically inserted in this <div>. Do not delete this <div>. --> <div id="nav"></div> <!-- Content is HTML-escaped elsewhere --> <pre> <a id="L1"></a><span class="comment">// Copyright 2009 The Go Authors. All rights reserved.</span> <a id="L2"></a><span class="comment">// Use of this source code is governed by a BSD-style</span> <a id="L3"></a><span class="comment">// license that can be found in the LICENSE file.</span> <a id="L5"></a><span class="comment">// Encrypt/decrypt data by xor with a pseudo-random data stream.</span> <a id="L7"></a>package block <a id="L9"></a>import ( <a id="L10"></a>&#34;io&#34;; <a id="L11"></a>&#34;os&#34;; <a id="L12"></a>) <a id="L14"></a><span class="comment">// A dataStream is an interface to an unending stream of data,</span> <a id="L15"></a><span class="comment">// used by XorReader and XorWriter to model a pseudo-random generator.</span> <a id="L16"></a><span class="comment">// Calls to Next() return sequential blocks of data from the stream.</span> <a id="L17"></a><span class="comment">// Each call must return at least one byte: there is no EOF.</span> <a id="L18"></a>type dataStream interface { <a id="L19"></a>Next() []byte; <a id="L20"></a>} <a id="L22"></a>type xorReader struct { <a id="L23"></a>r io.Reader; <a id="L24"></a>rand dataStream; <span class="comment">// pseudo-random</span> <a id="L25"></a>buf []byte; <span class="comment">// data available from last call to rand</span> <a id="L26"></a>} <a id="L28"></a>func newXorReader(rand dataStream, r io.Reader) io.Reader { <a id="L29"></a>x := new(xorReader); <a id="L30"></a>x.r = r; <a id="L31"></a>x.rand = rand; <a id="L32"></a>return x; <a id="L33"></a>} <a id="L35"></a>func (x *xorReader) Read(p []byte) (n int, err os.Error) { <a id="L36"></a>n, err = x.r.Read(p); <a id="L38"></a><span class="comment">// xor input with stream.</span> <a id="L39"></a>bp := 0; <a id="L40"></a>buf := x.buf; <a id="L41"></a>for i := 0; i &lt; n; i++ { <a id="L42"></a>if bp &gt;= len(buf) { <a id="L43"></a>buf = x.rand.Next(); <a id="L44"></a>bp = 0; <a id="L45"></a>} <a id="L46"></a>p[i] ^= buf[bp]; <a id="L47"></a>bp++; <a id="L48"></a>} <a id="L49"></a>x.buf = buf[bp:len(buf)]; <a id="L50"></a>return n, err; <a id="L51"></a>} <a id="L53"></a>type xorWriter struct { <a id="L54"></a>w io.Writer; <a id="L55"></a>rand dataStream; <span class="comment">// pseudo-random</span> <a id="L56"></a>buf []byte; <span class="comment">// last buffer returned by rand</span> <a id="L57"></a>extra []byte; <span class="comment">// extra random data (use before buf)</span> <a id="L58"></a>work []byte; <span class="comment">// work space</span> <a id="L59"></a>} <a id="L61"></a>func newXorWriter(rand dataStream, w io.Writer) io.Writer { <a id="L62"></a>x := new(xorWriter); <a id="L63"></a>x.w = w; <a id="L64"></a>x.rand = rand; <a id="L65"></a>x.work = make([]byte, 4096); <a id="L66"></a>return x; <a id="L67"></a>} <a id="L69"></a>func (x *xorWriter) Write(p []byte) (n int, err os.Error) { <a id="L7
<input type="submit" value="Code search" />
random_line_split
app.js
Chase & Co'], ['McDonald\'s Corporation'], ['Merck & Co., Inc.'], ['Microsoft Corporation'], ['Pfizer Inc'], ['The Coca-Cola Company'], ['The Home Depot, Inc.'], ['The Procter & Gamble Company'], ['United Technologies Corporation'], ['Verizon Communications'], ['Wal-Mart Stores, Inc.']]; for (var i = 0, l = myData.length, rand = Math.random; i < l; i++) { var data = myData[i]; data[1] = ((rand() * 10000) >> 0) / 100; data[2] = ((rand() * 10000) >> 0) / 100; data[3] = ((rand() * 10000) >> 0) / 100; data[4] = ((rand() * 10000) >> 0) / 100; data[5] = ((rand() * 10000) >> 0) / 100; } //create data store to be shared among the grid and bar series. var ds = Ext.create('Ext.data.ArrayStore', { fields : [{ name : 'company' }, { name : 'price', type : 'float' }, { name : 'revenue %', type : 'float' }, { name : 'growth %', type : 'float' }, { name : 'product %', type : 'float' }, { name : 'market %', type : 'float' }], data : myData }); //create radar dataset model. var chs = Ext.create('Ext.data.JsonStore', { fields : ['Name', 'Data'], data : [{ 'Name' : 'Price', 'Data' : 100 }, { 'Name' : 'Revenue %', 'Data' : 100 }, { 'Name' : 'Growth %', 'Data' : 100 }, { 'Name' : 'Product %', 'Data' : 100 }, { 'Name' : 'Market %', 'Data' : 100 }] }); //Radar chart will render information for a selected company in the //list. Selection can also be done via clicking on the bars in the series. var radarChart = Ext.create('Ext.chart.Chart', { margin : '0 0 0 0', insetPadding : 20, flex : 1.2, animate : true, store : chs, axes : [{ steps : 5, type : 'Radial', position : 'radial', maximum : 100 }], series : [{ type : 'radar', xField : 'Name', yField : 'Data', showInLegend : false, showMarkers : true, markerConfig : { radius : 4, size : 4 }, style : { fill : 'rgb(194,214,240)', opacity : 0.5, 'stroke-width' : 0.5 } }] }); //create a grid that will list the dataset items. var gridPanel = Ext.create('Ext.grid.Panel', { id : 'company-form', flex : 0.60, store : ds, title : 'Company Data', requires : ['Ext.ux.grid.filter.*'], features : [{ ftype : 'filters', local : true }], dockedItems : [{ xtype : 'toolbar', items : [{ xtype : 'combo', queryMode : 'local', store : ds, displayField : 'company', listeners : { change : function(me, newVal, oldVal, eOpts) { console.log(me.getStore()); me.getStore().filter("company", newVal); } } }, { xtype: 'button', text: 'reset', handler: function(me){ console.log("click"); me.up('toolbar').down('combo').getStore().clearFilter(); } }] }], columns : [{ id : 'company', text : 'Company', flex : 1, filterable : true, sortable : true,
} else if (record.get("price") < 25) { metaData.style = "background-color:red;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else { metaData.style = "background-color:blue;"; } return '<span style="font-weight:bolder;color:white;">' + value + '</span>'; } }, { text : 'Price', width : 75, sortable : true, dataIndex : 'price', align : 'right', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } return '<span style="color:blue;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } }, { text : 'Revenue', width : 75, sortable : true, align : 'right', dataIndex : 'revenue %', renderer : perc }, { text : 'Growth', width : 75, sortable : true, align : 'right', dataIndex : 'growth %', renderer : perc }, { text : 'Product', width : 75, sortable : true, align : 'right', dataIndex : 'product %', renderer : perc }, { text : 'Market', width : 75, sortable : true, align : 'right', dataIndex : 'market %', renderer : perc }], listeners : { selectionchange : function(model, records) { var json, name, i, l, items, series, fields; if (records[0]) { rec = records[0]; if (!form) { form = this.up('form').getForm(); fields = form.getFields(); fields.each(function(field) { if (field.name != 'company') { field.setDisabled(false); } }); } else { fields = form.getFields(); } // prevent change events from firing fields.each(function(field) { field.suspendEvents(); }); form.loadRecord(rec); updateRecord(rec); fields.each(function(field) { field.resumeEvents(); }); } } } }); //create a bar series to be at the top of the panel. var barChart = Ext.create('Ext.chart.Chart', { flex : 1, shadow : true, animate : true, store : ds, legend : { position : 'right' }, axes : [{ type : 'Numeric', position : 'left', fields : ['price', 'revenue %', 'growth %'], minimum : 0, hidden : true }, { type : 'Category', position : 'bottom', fields : ['company'], label : { renderer : function(v) { return Ext.String.ellipsis(v, 15, false); }, font : '9px Arial', rotate : { degrees : 330 } } }], series : [{ type : 'column', axis : 'left', highlight : true, // style : { // fill : '#456d9f' // }, highlightCfg : { fill : '#a2b5ca' }, label : { contrast : true, display : 'insideEnd', field : 'price', color : '#000', orientation : 'vertical', 'text-anchor' : 'middle' }, listeners : { 'itemmouseup' : function(item) { var
dataIndex : 'company', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (record.get("price") > 50) { metaData.style = "background-color:green;"; return '<span style="font-weight:bolder;">' + value + '</span>';
random_line_split
app.js
var bd = Ext.getBody(), form = false, rec = false, selectedStoreItem = false, //performs the highlight of an item in the bar series selectItem = function(storeItem) { var name = storeItem.get('company'), series = barChart.series.get(0), i, items, l; series.highlight = true; series.unHighlightItem(); series.cleanHighlights(); for ( i = 0, items = series.items, l = items.length; i < l; i++) { if (name == items[i].storeItem.get('company')) { selectedStoreItem = items[i].storeItem; series.highlightItem(items[i]); break; } } series.highlight = false; }, //updates a record modified via the form updateRecord = function(rec) { var name, series, i, l, items, json = [{ 'Name' : 'Price', 'Data' : rec.get('price') }, { 'Name' : 'Revenue %', 'Data' : rec.get('revenue %') }, { 'Name' : 'Growth %', 'Data' : rec.get('growth %') }, { 'Name' : 'Product %', 'Data' : rec.get('product %') }, { 'Name' : 'Market %', 'Data' : rec.get('market %') }]; chs.loadData(json); selectItem(rec); }, createListeners = function() { return { // buffer so we don't refire while the user is still typing buffer : 200, change : function(field, newValue, oldValue, listener) { if (rec && form) { if (newValue > field.maxValue) { field.setValue(field.maxValue); } else { form.updateRecord(rec); updateRecord(rec); } } } }; }; // sample static data for the store var myData = [['3m Co'], ['Alcoa Inc'], ['Altria Group Inc'], ['American Express Company'], ['American International Group, Inc.'], ['AT&T Inc'], ['Boeing Co.'], ['Caterpillar Inc.'], ['Citigroup, Inc.'], ['E.I. du Pont de Nemours and Company'], ['Exxon Mobil Corp'], ['General Electric Company'], ['General Motors Corporation'], ['Hewlett-Packard Co'], ['Honeywell Intl Inc'], ['Intel Corporation'], ['International Business Machines'], ['Johnson & Johnson'], ['JP Morgan & Chase & Co'], ['McDonald\'s Corporation'], ['Merck & Co., Inc.'], ['Microsoft Corporation'], ['Pfizer Inc'], ['The Coca-Cola Company'], ['The Home Depot, Inc.'], ['The Procter & Gamble Company'], ['United Technologies Corporation'], ['Verizon Communications'], ['Wal-Mart Stores, Inc.']]; for (var i = 0, l = myData.length, rand = Math.random; i < l; i++) { var data = myData[i]; data[1] = ((rand() * 10000) >> 0) / 100; data[2] = ((rand() * 10000) >> 0) / 100; data[3] = ((rand() * 10000) >> 0) / 100; data[4] = ((rand() * 10000) >> 0) / 100; data[5] = ((rand() * 10000) >> 0) / 100; } //create data store to be shared among the grid and bar series. var ds = Ext.create('Ext.data.ArrayStore', { fields : [{ name : 'company' }, { name : 'price', type : 'float' }, { name : 'revenue %', type : 'float' }, { name : 'growth %', type : 'float' }, { name : 'product %', type : 'float' }, { name : 'market %', type : 'float' }], data : myData }); //create radar dataset model. var chs = Ext.create('Ext.data.JsonStore', { fields : ['Name', 'Data'], data : [{ 'Name' : 'Price', 'Data' : 100 }, { 'Name' : 'Revenue %', 'Data' : 100 }, { 'Name' : 'Growth %', 'Data' : 100 }, { 'Name' : 'Product %', 'Data' : 100 }, { 'Name' : 'Market %', 'Data' : 100 }] }); //Radar chart will render information for a selected company in the //list. Selection can also be done via clicking on the bars in the series. var radarChart = Ext.create('Ext.chart.Chart', { margin : '0 0 0 0', insetPadding : 20, flex : 1.2, animate : true, store : chs, axes : [{ steps : 5, type : 'Radial', position : 'radial', maximum : 100 }], series : [{ type : 'radar', xField : 'Name', yField : 'Data', showInLegend : false, showMarkers : true, markerConfig : { radius : 4, size : 4 }, style : { fill : 'rgb(194,214,240)', opacity : 0.5, 'stroke-width' : 0.5 } }] }); //create a grid that will list the dataset items. var gridPanel = Ext.create('Ext.grid.Panel', { id : 'company-form', flex : 0.60, store : ds, title : 'Company Data', requires : ['Ext.ux.grid.filter.*'], features : [{ ftype : 'filters', local : true }], dockedItems : [{ xtype : 'toolbar', items : [{ xtype : 'combo', queryMode : 'local', store : ds, displayField : 'company', listeners : { change : function(me, newVal, oldVal, eOpts) { console.log(me.getStore()); me.getStore().filter("company", newVal); } } }, { xtype: 'button', text: 'reset', handler: function(me){ console.log("click"); me.up('toolbar').down('combo').getStore().clearFilter(); } }] }], columns : [{ id : 'company', text : 'Company', flex : 1, filterable : true, sortable : true, dataIndex : 'company', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (record.get("price") > 50) { metaData.style = "background-color:green;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else if (record.get("price") < 25) { metaData.style = "background-color:red;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else { metaData.style = "background-color:blue;"; } return '<span style="font-weight:bolder;color:white;">' + value + '</span>'; } }, { text : 'Price', width : 75, sortable : true, dataIndex : 'price', align : 'right', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } return '<span style="color:blue;">' +
{ if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; } return '<span style="color:blue;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; }
identifier_body
app.js
series.highlight = false; }, //updates a record modified via the form updateRecord = function(rec) { var name, series, i, l, items, json = [{ 'Name' : 'Price', 'Data' : rec.get('price') }, { 'Name' : 'Revenue %', 'Data' : rec.get('revenue %') }, { 'Name' : 'Growth %', 'Data' : rec.get('growth %') }, { 'Name' : 'Product %', 'Data' : rec.get('product %') }, { 'Name' : 'Market %', 'Data' : rec.get('market %') }]; chs.loadData(json); selectItem(rec); }, createListeners = function() { return { // buffer so we don't refire while the user is still typing buffer : 200, change : function(field, newValue, oldValue, listener) { if (rec && form) { if (newValue > field.maxValue) { field.setValue(field.maxValue); } else { form.updateRecord(rec); updateRecord(rec); } } } }; }; // sample static data for the store var myData = [['3m Co'], ['Alcoa Inc'], ['Altria Group Inc'], ['American Express Company'], ['American International Group, Inc.'], ['AT&T Inc'], ['Boeing Co.'], ['Caterpillar Inc.'], ['Citigroup, Inc.'], ['E.I. du Pont de Nemours and Company'], ['Exxon Mobil Corp'], ['General Electric Company'], ['General Motors Corporation'], ['Hewlett-Packard Co'], ['Honeywell Intl Inc'], ['Intel Corporation'], ['International Business Machines'], ['Johnson & Johnson'], ['JP Morgan & Chase & Co'], ['McDonald\'s Corporation'], ['Merck & Co., Inc.'], ['Microsoft Corporation'], ['Pfizer Inc'], ['The Coca-Cola Company'], ['The Home Depot, Inc.'], ['The Procter & Gamble Company'], ['United Technologies Corporation'], ['Verizon Communications'], ['Wal-Mart Stores, Inc.']]; for (var i = 0, l = myData.length, rand = Math.random; i < l; i++) { var data = myData[i]; data[1] = ((rand() * 10000) >> 0) / 100; data[2] = ((rand() * 10000) >> 0) / 100; data[3] = ((rand() * 10000) >> 0) / 100; data[4] = ((rand() * 10000) >> 0) / 100; data[5] = ((rand() * 10000) >> 0) / 100; } //create data store to be shared among the grid and bar series. var ds = Ext.create('Ext.data.ArrayStore', { fields : [{ name : 'company' }, { name : 'price', type : 'float' }, { name : 'revenue %', type : 'float' }, { name : 'growth %', type : 'float' }, { name : 'product %', type : 'float' }, { name : 'market %', type : 'float' }], data : myData }); //create radar dataset model. var chs = Ext.create('Ext.data.JsonStore', { fields : ['Name', 'Data'], data : [{ 'Name' : 'Price', 'Data' : 100 }, { 'Name' : 'Revenue %', 'Data' : 100 }, { 'Name' : 'Growth %', 'Data' : 100 }, { 'Name' : 'Product %', 'Data' : 100 }, { 'Name' : 'Market %', 'Data' : 100 }] }); //Radar chart will render information for a selected company in the //list. Selection can also be done via clicking on the bars in the series. var radarChart = Ext.create('Ext.chart.Chart', { margin : '0 0 0 0', insetPadding : 20, flex : 1.2, animate : true, store : chs, axes : [{ steps : 5, type : 'Radial', position : 'radial', maximum : 100 }], series : [{ type : 'radar', xField : 'Name', yField : 'Data', showInLegend : false, showMarkers : true, markerConfig : { radius : 4, size : 4 }, style : { fill : 'rgb(194,214,240)', opacity : 0.5, 'stroke-width' : 0.5 } }] }); //create a grid that will list the dataset items. var gridPanel = Ext.create('Ext.grid.Panel', { id : 'company-form', flex : 0.60, store : ds, title : 'Company Data', requires : ['Ext.ux.grid.filter.*'], features : [{ ftype : 'filters', local : true }], dockedItems : [{ xtype : 'toolbar', items : [{ xtype : 'combo', queryMode : 'local', store : ds, displayField : 'company', listeners : { change : function(me, newVal, oldVal, eOpts) { console.log(me.getStore()); me.getStore().filter("company", newVal); } } }, { xtype: 'button', text: 'reset', handler: function(me){ console.log("click"); me.up('toolbar').down('combo').getStore().clearFilter(); } }] }], columns : [{ id : 'company', text : 'Company', flex : 1, filterable : true, sortable : true, dataIndex : 'company', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (record.get("price") > 50) { metaData.style = "background-color:green;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else if (record.get("price") < 25) { metaData.style = "background-color:red;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else { metaData.style = "background-color:blue;"; } return '<span style="font-weight:bolder;color:white;">' + value + '</span>'; } }, { text : 'Price', width : 75, sortable : true, dataIndex : 'price', align : 'right', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } return '<span style="color:blue;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } }, { text : 'Revenue', width : 75, sortable : true, align : 'right', dataIndex : 'revenue %', renderer : perc }, { text : 'Growth', width : 75, sortable : true, align : 'right', dataIndex : 'growth %', renderer : perc }, { text : 'Product', width : 75, sortable : true, align : 'right', dataIndex : 'product %', renderer : perc }, { text : 'Market', width : 75, sortable : true, align : 'right', dataIndex : 'market %', renderer : perc }], listeners : { selectionchange : function(model, records) { var json, name, i, l, items, series, fields; if (records[0]) { rec = records
{ if (name == items[i].storeItem.get('company')) { selectedStoreItem = items[i].storeItem; series.highlightItem(items[i]); break; } }
conditional_block
app.js
(value, metaData, record, rowIndex, colIndex, store, view) { if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; } return '<span style="color:blue;">' + Ext.util.Format.number(value, '0.00 %') + '</span>'; } var bd = Ext.getBody(), form = false, rec = false, selectedStoreItem = false, //performs the highlight of an item in the bar series selectItem = function(storeItem) { var name = storeItem.get('company'), series = barChart.series.get(0), i, items, l; series.highlight = true; series.unHighlightItem(); series.cleanHighlights(); for ( i = 0, items = series.items, l = items.length; i < l; i++) { if (name == items[i].storeItem.get('company')) { selectedStoreItem = items[i].storeItem; series.highlightItem(items[i]); break; } } series.highlight = false; }, //updates a record modified via the form updateRecord = function(rec) { var name, series, i, l, items, json = [{ 'Name' : 'Price', 'Data' : rec.get('price') }, { 'Name' : 'Revenue %', 'Data' : rec.get('revenue %') }, { 'Name' : 'Growth %', 'Data' : rec.get('growth %') }, { 'Name' : 'Product %', 'Data' : rec.get('product %') }, { 'Name' : 'Market %', 'Data' : rec.get('market %') }]; chs.loadData(json); selectItem(rec); }, createListeners = function() { return { // buffer so we don't refire while the user is still typing buffer : 200, change : function(field, newValue, oldValue, listener) { if (rec && form) { if (newValue > field.maxValue) { field.setValue(field.maxValue); } else { form.updateRecord(rec); updateRecord(rec); } } } }; }; // sample static data for the store var myData = [['3m Co'], ['Alcoa Inc'], ['Altria Group Inc'], ['American Express Company'], ['American International Group, Inc.'], ['AT&T Inc'], ['Boeing Co.'], ['Caterpillar Inc.'], ['Citigroup, Inc.'], ['E.I. du Pont de Nemours and Company'], ['Exxon Mobil Corp'], ['General Electric Company'], ['General Motors Corporation'], ['Hewlett-Packard Co'], ['Honeywell Intl Inc'], ['Intel Corporation'], ['International Business Machines'], ['Johnson & Johnson'], ['JP Morgan & Chase & Co'], ['McDonald\'s Corporation'], ['Merck & Co., Inc.'], ['Microsoft Corporation'], ['Pfizer Inc'], ['The Coca-Cola Company'], ['The Home Depot, Inc.'], ['The Procter & Gamble Company'], ['United Technologies Corporation'], ['Verizon Communications'], ['Wal-Mart Stores, Inc.']]; for (var i = 0, l = myData.length, rand = Math.random; i < l; i++) { var data = myData[i]; data[1] = ((rand() * 10000) >> 0) / 100; data[2] = ((rand() * 10000) >> 0) / 100; data[3] = ((rand() * 10000) >> 0) / 100; data[4] = ((rand() * 10000) >> 0) / 100; data[5] = ((rand() * 10000) >> 0) / 100; } //create data store to be shared among the grid and bar series. var ds = Ext.create('Ext.data.ArrayStore', { fields : [{ name : 'company' }, { name : 'price', type : 'float' }, { name : 'revenue %', type : 'float' }, { name : 'growth %', type : 'float' }, { name : 'product %', type : 'float' }, { name : 'market %', type : 'float' }], data : myData }); //create radar dataset model. var chs = Ext.create('Ext.data.JsonStore', { fields : ['Name', 'Data'], data : [{ 'Name' : 'Price', 'Data' : 100 }, { 'Name' : 'Revenue %', 'Data' : 100 }, { 'Name' : 'Growth %', 'Data' : 100 }, { 'Name' : 'Product %', 'Data' : 100 }, { 'Name' : 'Market %', 'Data' : 100 }] }); //Radar chart will render information for a selected company in the //list. Selection can also be done via clicking on the bars in the series. var radarChart = Ext.create('Ext.chart.Chart', { margin : '0 0 0 0', insetPadding : 20, flex : 1.2, animate : true, store : chs, axes : [{ steps : 5, type : 'Radial', position : 'radial', maximum : 100 }], series : [{ type : 'radar', xField : 'Name', yField : 'Data', showInLegend : false, showMarkers : true, markerConfig : { radius : 4, size : 4 }, style : { fill : 'rgb(194,214,240)', opacity : 0.5, 'stroke-width' : 0.5 } }] }); //create a grid that will list the dataset items. var gridPanel = Ext.create('Ext.grid.Panel', { id : 'company-form', flex : 0.60, store : ds, title : 'Company Data', requires : ['Ext.ux.grid.filter.*'], features : [{ ftype : 'filters', local : true }], dockedItems : [{ xtype : 'toolbar', items : [{ xtype : 'combo', queryMode : 'local', store : ds, displayField : 'company', listeners : { change : function(me, newVal, oldVal, eOpts) { console.log(me.getStore()); me.getStore().filter("company", newVal); } } }, { xtype: 'button', text: 'reset', handler: function(me){ console.log("click"); me.up('toolbar').down('combo').getStore().clearFilter(); } }] }], columns : [{ id : 'company', text : 'Company', flex : 1, filterable : true, sortable : true, dataIndex : 'company', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (record.get("price") > 50) { metaData.style = "background-color:green;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else if (record.get("price") < 25) { metaData.style = "background-color:red;"; return '<span style="font-weight:bolder;">' + value + '</span>'; } else { metaData.style = "background-color:blue;"; } return '<span style="font-weight:bolder;color:white;">' + value + '</span>'; } }, { text : 'Price', width : 75, sortable : true, dataIndex : 'price', align : 'right', renderer : function(value, metaData, record, rowIndex, colIndex, store, view) { if (value > 50) { metaData.style = ""; return '<span style="color:green;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>'; } else if (value < 25) { return '<span style="color:red;font-weight:bolder;">' + Ext.util.Format.currency(value, ' $', 2, true) + '</span>';
perc
identifier_name
voice_recognition.py
1.03, -0.50], 'right': [0.08, -1.0, 1.19, 1.94, -0.67, 1.03, 0.50] } } self._collide_lsub = rospy.Subscriber( 'robot/limb/left/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'left') self._collide_rsub = rospy.Subscriber( 'robot/limb/right/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'right') self._disable_pub = { 'left': rospy.Publisher( 'robot/limb/left/suppress_collision_avoidance', Empty, queue_size=10), 'right': rospy.Publisher( 'robot/limb/right/suppress_collision_avoidance', Empty, queue_size=10) } self._rs = baxter_interface.RobotEnable(CHECK_VERSION) self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool, queue_size=10) def _update_collision(self, data, limb): self._arm_state['collide'][limb] = len(data.collision_object) > 0 self._check_arm_state() def _check_arm_state(self): """ Check for goals and behind collision field. If s1 joint is over the peak, collision will need to be disabled to get the arm around the head-arm collision force-field. """ diff_check = lambda a, b: abs(a - b) <= self._tuck_threshold for limb in self._limbs: angles = [self._arms[limb].joint_angle(joint) for joint in self._arms[limb].joint_names()] # Check if in a goal position untuck_goal = map(diff_check, angles, self._joint_moves['untuck'][limb]) tuck_goal = map(diff_check, angles[0:2], self._joint_moves['tuck'][limb][0:2]) if all(untuck_goal): self._arm_state['tuck'][limb] = 'untuck' elif all(tuck_goal): self._arm_state['tuck'][limb] = 'tuck' else: self._arm_state['tuck'][limb] = 'none' # Check if shoulder is flipped over peak self._arm_state['flipped'][limb] = ( self._arms[limb].joint_angle(limb + '_s1') <= self._peak_angle) def _prepare_to_tuck(self): # If arms are in "tucked" state, disable collision avoidance # before enabling robot, to avoid arm jerking from "force-field". head = baxter_interface.Head() start_disabled = not self._rs.state().enabled at_goal = lambda: (abs(head.pan()) <= baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE) rospy.loginfo("Moving head to neutral position") while not at_goal() and not rospy.is_shutdown(): if start_disabled: [pub.publish(Empty()) for pub in self._disable_pub.values()] if not self._rs.state().enabled: self._enable_pub.publish(True) head.set_pan(0.0, 50.0, timeout=0) self._tuck_rate.sleep() if start_disabled: while self._rs.state().enabled == True and not rospy.is_shutdown(): [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() def _move_to(self, tuck, disabled): if any(disabled.values()): [pub.publish(Empty()) for pub in self._disable_pub.values()] while (any(self._arm_state['tuck'][limb] != goal for limb, goal in tuck.viewitems()) and not rospy.is_shutdown()): if self._rs.state().enabled == False: self._enable_pub.publish(True) for limb in self._limbs: if disabled[limb]: self._disable_pub[limb].publish(Empty()) if limb in tuck: self._arms[limb].set_joint_positions(dict(zip( self._arms[limb].joint_names(), self._joint_moves[tuck[limb]][limb]))) self._check_arm_state() self._tuck_rate.sleep() if any(self._arm_state['collide'].values()): self._rs.disable() return def supervised_tuck(self): # Update our starting state to check if arms are tucked self._prepare_to_tuck() self._check_arm_state() # Tuck Arms if self._tuck == True: # If arms are already tucked, report this to user and exit. if all(self._arm_state['tuck'][limb] == 'tuck' for limb in self._limbs): rospy.loginfo("Tucking: Arms already in 'Tucked' position.") self._done = True return else: rospy.loginfo("Tucking: One or more arms not Tucked.") any_flipped = not all(self._arm_state['flipped'].values()) if any_flipped: rospy.loginfo( "Moving to neutral start position with collision %s.", "on" if any_flipped else "off") # Move to neutral pose before tucking arms to avoid damage self._check_arm_state() actions = dict() disabled = {'left': True, 'right': True} for limb in self._limbs: if not self._arm_state['flipped'][limb]: actions[limb] = 'untuck' disabled[limb] = False self._move_to(actions, disabled) # Disable collision and Tuck Arms rospy.loginfo("Tucking: Tucking with collision avoidance off.") actions = {'left': 'tuck', 'right': 'tuck'} disabled = {'left': True, 'right': True} self._move_to(actions, disabled) self._done = True return # Untuck Arms else: # If arms are tucked disable collision and untuck arms if any(self._arm_state['flipped'].values()): rospy.loginfo("Untucking: One or more arms Tucked;" " Disabling Collision Avoidance and untucking.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return # If arms already untucked, move to neutral location else: rospy.loginfo("Untucking: Arms already Untucked;" " Moving to neutral position.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return def clean_shutdown(self): """Handles ROS shutdown (Ctrl-C) safely.""" if not self._done: rospy.logwarn('Aborting: Shutting down safely...') if any(self._arm_state['collide'].values()): while self._rs.state().enabled != False: [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() #------------------------------------------------------------------# def enable_robot(act): #rospy.init_node('rsdk_robot_enable') rs = baxter_interface.RobotEnable(CHECK_VERSION) if act == 'state': print rs.state() elif act == 'enable': rs.enable() elif act == 'disable': rs.disable() elif act == 'reset': rs.reset() elif act == 'stop': rs.stop() return 0 #------------------------------------------------------------------# def speak(x): rospy.wait_for_service('ros_mary') try: add_two_ints = rospy.ServiceProxy('ros_mary',ros_mary) resp1 = add_two_ints(x) except rospy.ServiceException, e: print "Service call failed: %s"%e #------------------------------------------------------------------# speech = gapi.Speech('sp') if len(sys.argv)==2: if sys.argv[1] in gapi.languages.keys(): speech.lang = gapi.languages[sys.argv[1]] elif sys.argv[1] in gapi.languages.values(): speech.lang = sys.argv[1] def handler(fileName): global speech translator = gapi.Translator(speech.lang, 'en-uk') try: cfileName = psw.convert(fileName) phrase = speech.getText(cfileName) import os os.remove(fileName) os.remove(cfileName) all_words = phrase.split(' ') words = phrase.split(' ') for i in range(len(words)): words[i] = str(words[i]) all_words[i] = str(all_words[i]) print all_words[i] print 'the phrase is:',phrase if 'wake' in words:
speak('Ready to work!, sir.') Tuck_arms(False)
conditional_block
voice_recognition.py
self._limbs = ('left', 'right') self._arms = { 'left': baxter_interface.Limb('left'), 'right': baxter_interface.Limb('right'), } self._tuck = tuck_cmd self._tuck_rate = rospy.Rate(20.0) # Hz self._tuck_threshold = 0.2 # radians self._peak_angle = -1.6 # radians self._arm_state = { 'tuck': {'left': 'none', 'right': 'none'}, 'collide': {'left': False, 'right': False}, 'flipped': {'left': False, 'right': False} } self._joint_moves = { 'tuck': { 'left': [-1.0, -2.07, 3.0, 2.55, 0.0, 0.01, 0.0], 'right': [1.0, -2.07, -3.0, 2.55, -0.0, 0.01, 0.0] }, 'untuck': { 'left': [-0.08, -1.0, -1.19, 1.94, 0.67, 1.03, -0.50], 'right': [0.08, -1.0, 1.19, 1.94, -0.67, 1.03, 0.50] } } self._collide_lsub = rospy.Subscriber( 'robot/limb/left/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'left') self._collide_rsub = rospy.Subscriber( 'robot/limb/right/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'right') self._disable_pub = { 'left': rospy.Publisher( 'robot/limb/left/suppress_collision_avoidance', Empty, queue_size=10), 'right': rospy.Publisher( 'robot/limb/right/suppress_collision_avoidance', Empty, queue_size=10) } self._rs = baxter_interface.RobotEnable(CHECK_VERSION) self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool, queue_size=10) def _update_collision(self, data, limb): self._arm_state['collide'][limb] = len(data.collision_object) > 0 self._check_arm_state() def _check_arm_state(self): """ Check for goals and behind collision field. If s1 joint is over the peak, collision will need to be disabled to get the arm around the head-arm collision force-field. """ diff_check = lambda a, b: abs(a - b) <= self._tuck_threshold for limb in self._limbs: angles = [self._arms[limb].joint_angle(joint) for joint in self._arms[limb].joint_names()] # Check if in a goal position untuck_goal = map(diff_check, angles, self._joint_moves['untuck'][limb]) tuck_goal = map(diff_check, angles[0:2], self._joint_moves['tuck'][limb][0:2]) if all(untuck_goal): self._arm_state['tuck'][limb] = 'untuck' elif all(tuck_goal): self._arm_state['tuck'][limb] = 'tuck' else: self._arm_state['tuck'][limb] = 'none' # Check if shoulder is flipped over peak self._arm_state['flipped'][limb] = ( self._arms[limb].joint_angle(limb + '_s1') <= self._peak_angle) def _prepare_to_tuck(self): # If arms are in "tucked" state, disable collision avoidance # before enabling robot, to avoid arm jerking from "force-field". head = baxter_interface.Head() start_disabled = not self._rs.state().enabled at_goal = lambda: (abs(head.pan()) <= baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE) rospy.loginfo("Moving head to neutral position") while not at_goal() and not rospy.is_shutdown(): if start_disabled: [pub.publish(Empty()) for pub in self._disable_pub.values()] if not self._rs.state().enabled: self._enable_pub.publish(True) head.set_pan(0.0, 50.0, timeout=0) self._tuck_rate.sleep() if start_disabled: while self._rs.state().enabled == True and not rospy.is_shutdown(): [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() def _move_to(self, tuck, disabled): if any(disabled.values()): [pub.publish(Empty()) for pub in self._disable_pub.values()] while (any(self._arm_state['tuck'][limb] != goal for limb, goal in tuck.viewitems()) and not rospy.is_shutdown()): if self._rs.state().enabled == False: self._enable_pub.publish(True) for limb in self._limbs: if disabled[limb]: self._disable_pub[limb].publish(Empty()) if limb in tuck: self._arms[limb].set_joint_positions(dict(zip( self._arms[limb].joint_names(), self._joint_moves[tuck[limb]][limb]))) self._check_arm_state() self._tuck_rate.sleep() if any(self._arm_state['collide'].values()): self._rs.disable() return def supervised_tuck(self): # Update our starting state to check if arms are tucked self._prepare_to_tuck() self._check_arm_state() # Tuck Arms if self._tuck == True: # If arms are already tucked, report this to user and exit. if all(self._arm_state['tuck'][limb] == 'tuck' for limb in self._limbs): rospy.loginfo("Tucking: Arms already in 'Tucked' position.") self._done = True return else: rospy.loginfo("Tucking: One or more arms not Tucked.") any_flipped = not all(self._arm_state['flipped'].values()) if any_flipped: rospy.loginfo( "Moving to neutral start position with collision %s.", "on" if any_flipped else "off") # Move to neutral pose before tucking arms to avoid damage self._check_arm_state() actions = dict() disabled = {'left': True, 'right': True} for limb in self._limbs: if not self._arm_state['flipped'][limb]: actions[limb] = 'untuck' disabled[limb] = False self._move_to(actions, disabled) # Disable collision and Tuck Arms rospy.loginfo("Tucking: Tucking with collision avoidance off.") actions = {'left': 'tuck', 'right': 'tuck'} disabled = {'left': True, 'right': True} self._move_to(actions, disabled) self._done = True return # Untuck Arms else: # If arms are tucked disable collision and untuck arms if any(self._arm_state['flipped'].values()): rospy.loginfo("Untucking: One or more arms Tucked;" " Disabling Collision Avoidance and untucking.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped'])
else: rospy.loginfo("Untucking: Arms already Untucked;" " Moving to neutral position.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return def clean_shutdown(self): """Handles ROS shutdown (Ctrl-C) safely.""" if not self._done: rospy.logwarn('Aborting: Shutting down safely...') if any(self._arm_state['collide'].values()): while self._rs.state().enabled != False: [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() #------------------------------------------------------------------# def enable_robot(act): #rospy.init_node('rsdk_robot_enable') rs = baxter_interface.RobotEnable(CHECK_VERSION) if act == 'state': print rs.state() elif act == 'enable': rs.enable() elif act == 'disable': rs.disable()
actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return # If arms already untucked, move to neutral location
random_line_split
voice_recognition.py
self._limbs = ('left', 'right') self._arms = { 'left': baxter_interface.Limb('left'), 'right': baxter_interface.Limb('right'), } self._tuck = tuck_cmd self._tuck_rate = rospy.Rate(20.0) # Hz self._tuck_threshold = 0.2 # radians self._peak_angle = -1.6 # radians self._arm_state = { 'tuck': {'left': 'none', 'right': 'none'}, 'collide': {'left': False, 'right': False}, 'flipped': {'left': False, 'right': False} } self._joint_moves = { 'tuck': { 'left': [-1.0, -2.07, 3.0, 2.55, 0.0, 0.01, 0.0], 'right': [1.0, -2.07, -3.0, 2.55, -0.0, 0.01, 0.0] }, 'untuck': { 'left': [-0.08, -1.0, -1.19, 1.94, 0.67, 1.03, -0.50], 'right': [0.08, -1.0, 1.19, 1.94, -0.67, 1.03, 0.50] } } self._collide_lsub = rospy.Subscriber( 'robot/limb/left/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'left') self._collide_rsub = rospy.Subscriber( 'robot/limb/right/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'right') self._disable_pub = { 'left': rospy.Publisher( 'robot/limb/left/suppress_collision_avoidance', Empty, queue_size=10), 'right': rospy.Publisher( 'robot/limb/right/suppress_collision_avoidance', Empty, queue_size=10) } self._rs = baxter_interface.RobotEnable(CHECK_VERSION) self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool, queue_size=10) def _update_collision(self, data, limb):
def _check_arm_state(self): """ Check for goals and behind collision field. If s1 joint is over the peak, collision will need to be disabled to get the arm around the head-arm collision force-field. """ diff_check = lambda a, b: abs(a - b) <= self._tuck_threshold for limb in self._limbs: angles = [self._arms[limb].joint_angle(joint) for joint in self._arms[limb].joint_names()] # Check if in a goal position untuck_goal = map(diff_check, angles, self._joint_moves['untuck'][limb]) tuck_goal = map(diff_check, angles[0:2], self._joint_moves['tuck'][limb][0:2]) if all(untuck_goal): self._arm_state['tuck'][limb] = 'untuck' elif all(tuck_goal): self._arm_state['tuck'][limb] = 'tuck' else: self._arm_state['tuck'][limb] = 'none' # Check if shoulder is flipped over peak self._arm_state['flipped'][limb] = ( self._arms[limb].joint_angle(limb + '_s1') <= self._peak_angle) def _prepare_to_tuck(self): # If arms are in "tucked" state, disable collision avoidance # before enabling robot, to avoid arm jerking from "force-field". head = baxter_interface.Head() start_disabled = not self._rs.state().enabled at_goal = lambda: (abs(head.pan()) <= baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE) rospy.loginfo("Moving head to neutral position") while not at_goal() and not rospy.is_shutdown(): if start_disabled: [pub.publish(Empty()) for pub in self._disable_pub.values()] if not self._rs.state().enabled: self._enable_pub.publish(True) head.set_pan(0.0, 50.0, timeout=0) self._tuck_rate.sleep() if start_disabled: while self._rs.state().enabled == True and not rospy.is_shutdown(): [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() def _move_to(self, tuck, disabled): if any(disabled.values()): [pub.publish(Empty()) for pub in self._disable_pub.values()] while (any(self._arm_state['tuck'][limb] != goal for limb, goal in tuck.viewitems()) and not rospy.is_shutdown()): if self._rs.state().enabled == False: self._enable_pub.publish(True) for limb in self._limbs: if disabled[limb]: self._disable_pub[limb].publish(Empty()) if limb in tuck: self._arms[limb].set_joint_positions(dict(zip( self._arms[limb].joint_names(), self._joint_moves[tuck[limb]][limb]))) self._check_arm_state() self._tuck_rate.sleep() if any(self._arm_state['collide'].values()): self._rs.disable() return def supervised_tuck(self): # Update our starting state to check if arms are tucked self._prepare_to_tuck() self._check_arm_state() # Tuck Arms if self._tuck == True: # If arms are already tucked, report this to user and exit. if all(self._arm_state['tuck'][limb] == 'tuck' for limb in self._limbs): rospy.loginfo("Tucking: Arms already in 'Tucked' position.") self._done = True return else: rospy.loginfo("Tucking: One or more arms not Tucked.") any_flipped = not all(self._arm_state['flipped'].values()) if any_flipped: rospy.loginfo( "Moving to neutral start position with collision %s.", "on" if any_flipped else "off") # Move to neutral pose before tucking arms to avoid damage self._check_arm_state() actions = dict() disabled = {'left': True, 'right': True} for limb in self._limbs: if not self._arm_state['flipped'][limb]: actions[limb] = 'untuck' disabled[limb] = False self._move_to(actions, disabled) # Disable collision and Tuck Arms rospy.loginfo("Tucking: Tucking with collision avoidance off.") actions = {'left': 'tuck', 'right': 'tuck'} disabled = {'left': True, 'right': True} self._move_to(actions, disabled) self._done = True return # Untuck Arms else: # If arms are tucked disable collision and untuck arms if any(self._arm_state['flipped'].values()): rospy.loginfo("Untucking: One or more arms Tucked;" " Disabling Collision Avoidance and untucking.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return # If arms already untucked, move to neutral location else: rospy.loginfo("Untucking: Arms already Untucked;" " Moving to neutral position.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return def clean_shutdown(self): """Handles ROS shutdown (Ctrl-C) safely.""" if not self._done: rospy.logwarn('Aborting: Shutting down safely...') if any(self._arm_state['collide'].values()): while self._rs.state().enabled != False: [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() #------------------------------------------------------------------# def enable_robot(act): #rospy.init_node('rsdk_robot_enable') rs = baxter_interface.RobotEnable(CHECK_VERSION) if act == 'state': print rs.state() elif act == 'enable': rs.enable() elif act == 'disable': rs.disable
self._arm_state['collide'][limb] = len(data.collision_object) > 0 self._check_arm_state()
identifier_body
voice_recognition.py
{ 'tuck': { 'left': [-1.0, -2.07, 3.0, 2.55, 0.0, 0.01, 0.0], 'right': [1.0, -2.07, -3.0, 2.55, -0.0, 0.01, 0.0] }, 'untuck': { 'left': [-0.08, -1.0, -1.19, 1.94, 0.67, 1.03, -0.50], 'right': [0.08, -1.0, 1.19, 1.94, -0.67, 1.03, 0.50] } } self._collide_lsub = rospy.Subscriber( 'robot/limb/left/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'left') self._collide_rsub = rospy.Subscriber( 'robot/limb/right/collision_avoidance_state', CollisionAvoidanceState, self._update_collision, 'right') self._disable_pub = { 'left': rospy.Publisher( 'robot/limb/left/suppress_collision_avoidance', Empty, queue_size=10), 'right': rospy.Publisher( 'robot/limb/right/suppress_collision_avoidance', Empty, queue_size=10) } self._rs = baxter_interface.RobotEnable(CHECK_VERSION) self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool, queue_size=10) def _update_collision(self, data, limb): self._arm_state['collide'][limb] = len(data.collision_object) > 0 self._check_arm_state() def _check_arm_state(self): """ Check for goals and behind collision field. If s1 joint is over the peak, collision will need to be disabled to get the arm around the head-arm collision force-field. """ diff_check = lambda a, b: abs(a - b) <= self._tuck_threshold for limb in self._limbs: angles = [self._arms[limb].joint_angle(joint) for joint in self._arms[limb].joint_names()] # Check if in a goal position untuck_goal = map(diff_check, angles, self._joint_moves['untuck'][limb]) tuck_goal = map(diff_check, angles[0:2], self._joint_moves['tuck'][limb][0:2]) if all(untuck_goal): self._arm_state['tuck'][limb] = 'untuck' elif all(tuck_goal): self._arm_state['tuck'][limb] = 'tuck' else: self._arm_state['tuck'][limb] = 'none' # Check if shoulder is flipped over peak self._arm_state['flipped'][limb] = ( self._arms[limb].joint_angle(limb + '_s1') <= self._peak_angle) def _prepare_to_tuck(self): # If arms are in "tucked" state, disable collision avoidance # before enabling robot, to avoid arm jerking from "force-field". head = baxter_interface.Head() start_disabled = not self._rs.state().enabled at_goal = lambda: (abs(head.pan()) <= baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE) rospy.loginfo("Moving head to neutral position") while not at_goal() and not rospy.is_shutdown(): if start_disabled: [pub.publish(Empty()) for pub in self._disable_pub.values()] if not self._rs.state().enabled: self._enable_pub.publish(True) head.set_pan(0.0, 50.0, timeout=0) self._tuck_rate.sleep() if start_disabled: while self._rs.state().enabled == True and not rospy.is_shutdown(): [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() def _move_to(self, tuck, disabled): if any(disabled.values()): [pub.publish(Empty()) for pub in self._disable_pub.values()] while (any(self._arm_state['tuck'][limb] != goal for limb, goal in tuck.viewitems()) and not rospy.is_shutdown()): if self._rs.state().enabled == False: self._enable_pub.publish(True) for limb in self._limbs: if disabled[limb]: self._disable_pub[limb].publish(Empty()) if limb in tuck: self._arms[limb].set_joint_positions(dict(zip( self._arms[limb].joint_names(), self._joint_moves[tuck[limb]][limb]))) self._check_arm_state() self._tuck_rate.sleep() if any(self._arm_state['collide'].values()): self._rs.disable() return def supervised_tuck(self): # Update our starting state to check if arms are tucked self._prepare_to_tuck() self._check_arm_state() # Tuck Arms if self._tuck == True: # If arms are already tucked, report this to user and exit. if all(self._arm_state['tuck'][limb] == 'tuck' for limb in self._limbs): rospy.loginfo("Tucking: Arms already in 'Tucked' position.") self._done = True return else: rospy.loginfo("Tucking: One or more arms not Tucked.") any_flipped = not all(self._arm_state['flipped'].values()) if any_flipped: rospy.loginfo( "Moving to neutral start position with collision %s.", "on" if any_flipped else "off") # Move to neutral pose before tucking arms to avoid damage self._check_arm_state() actions = dict() disabled = {'left': True, 'right': True} for limb in self._limbs: if not self._arm_state['flipped'][limb]: actions[limb] = 'untuck' disabled[limb] = False self._move_to(actions, disabled) # Disable collision and Tuck Arms rospy.loginfo("Tucking: Tucking with collision avoidance off.") actions = {'left': 'tuck', 'right': 'tuck'} disabled = {'left': True, 'right': True} self._move_to(actions, disabled) self._done = True return # Untuck Arms else: # If arms are tucked disable collision and untuck arms if any(self._arm_state['flipped'].values()): rospy.loginfo("Untucking: One or more arms Tucked;" " Disabling Collision Avoidance and untucking.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return # If arms already untucked, move to neutral location else: rospy.loginfo("Untucking: Arms already Untucked;" " Moving to neutral position.") self._check_arm_state() suppress = deepcopy(self._arm_state['flipped']) actions = {'left': 'untuck', 'right': 'untuck'} self._move_to(actions, suppress) self._done = True return def clean_shutdown(self): """Handles ROS shutdown (Ctrl-C) safely.""" if not self._done: rospy.logwarn('Aborting: Shutting down safely...') if any(self._arm_state['collide'].values()): while self._rs.state().enabled != False: [pub.publish(Empty()) for pub in self._disable_pub.values()] self._enable_pub.publish(False) self._tuck_rate.sleep() #------------------------------------------------------------------# def enable_robot(act): #rospy.init_node('rsdk_robot_enable') rs = baxter_interface.RobotEnable(CHECK_VERSION) if act == 'state': print rs.state() elif act == 'enable': rs.enable() elif act == 'disable': rs.disable() elif act == 'reset': rs.reset() elif act == 'stop': rs.stop() return 0 #------------------------------------------------------------------# def speak(x): rospy.wait_for_service('ros_mary') try: add_two_ints = rospy.ServiceProxy('ros_mary',ros_mary) resp1 = add_two_ints(x) except rospy.ServiceException, e: print "Service call failed: %s"%e #------------------------------------------------------------------# speech = gapi.Speech('sp') if len(sys.argv)==2: if sys.argv[1] in gapi.languages.keys(): speech.lang = gapi.languages[sys.argv[1]] elif sys.argv[1] in gapi.languages.values(): speech.lang = sys.argv[1] def
handler
identifier_name
_criticizer_base.py
.") @property def inputs(self): self.assert_sampled() return self._inputs @property def representations_full(self) -> tfd.Distribution: return self._representations_full @property def latents_full(self) -> tfd.Distribution: return self._representations_full @property def factors_full(self) -> tf.Tensor: return self._factors_full @property def original_factors_full(self) -> tf.Tensor: return self._original_factors_full @property def representations(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def latents(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def representations_mean(self): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.mean().numpy() for z in self.representations] @property def representations_variance(self): r""" Return the variance of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.variance().numpy() for z in self.representations] def representations_sample(self, n=()): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [ z.sample(sample_shape=n, seed=self.randint).numpy() for z in self.representations ] @property def reconstructions(self): r""" Return the reconstructed `Distributions` of inputs for training and testing """ self.assert_sampled() return self._reconstructions @property def reconstructions_mean(self): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.mean().numpy() for j in i] for i in self._reconstructions] @property def reconstructions_variance(self): r""" Return the variance of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.variance().numpy() for j in i] for i in self._reconstructions] def reconstructions_sample(self, n=()): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.sample(sample_shape=n, seed=self.randint).numpy() for j in i] for i in self._reconstructions] @property def original_factors(self): r""" Return the training and testing original factors, i.e. the factors before discretizing """ self.assert_sampled() # the original factors is the same for all samples set return self._original_factors @property def n_factors(self): return self.factors[0].shape[1] @property def n_representations(self): r""" return the number of latent codes """ return self.representations[0].event_shape[0] @property def n_codes(self): r""" same as `n_representations`, return the number of latent codes """ return self.n_representations @property def n_train(self): r""" Return number of samples for training """ return self.factors[0].shape[0] @property def n_test(self): r""" Return number of samples for testing """ return self.factors[1].shape[0] @property def factors(self): r""" Return the target variable (i.e. the factors of variation) for training and testing """ self.assert_sampled() return self._factors @property def factor_names(self): self.assert_sampled() # the dataset is unchanged, always at 0-th index return np.array(self._factor_names) @property def code_names(self): return np.array([f"Z{i}" for i in range(self.n_representations)]) @property def random_state(self): return self._rand @property def randint(self): return self._rand.randint(1e8) ############## proxy to VAE methods def index(self, factor_name): r""" Return the column index of given factor_names within the factor matrix """ return self._factor_names.index(str(factor_name)) def
(self, inputs, mask=None, sample_shape=()): r""" Encode inputs to latent codes Arguments: inputs : a single Tensor or list of Tensor Returns: `tensorflow_probability.Distribution`, q(z|x) the latent distribution """ inputs = tf.nest.flatten(inputs)[:len(self._vae.encoder.inputs)] latents = self._vae.encode(inputs[0] if len(inputs) == 1 else inputs, training=False, mask=mask, sample_shape=sample_shape) # only support single returned latent variable now for z in tf.nest.flatten(latents): assert isinstance(z, tfd.Distribution), \ "The latent code return from `vae.encode` must be instance of " + \ "tensorflow_probability.Distribution, but returned: %s" % \ str(z) return latents def decode(self, latents, mask=None, sample_shape=()): r""" Decode the latents into reconstruction distribution """ outputs = self._vae.decode(latents, training=False, mask=mask, sample_shape=sample_shape) for o in tf.nest.flatten(outputs): assert isinstance(o, tfd.Distribution), \ "vae decode method must return reconstruction distribution, but " + \ "returned: %s" % str(o) return outputs ############## Experiment setup def traversing(self, indices=None, min_val=-1., max_val=1., num=10, n_samples=2, mode='linear'): r""" Arguments: indices : a list of Integer or None. The indices of latent code for traversing. If None, all latent codes are used. Return: numpy.ndarray : traversed latent codes for training and testing, the shape is `[len(indices) * n_samples * num, n_representations]` """ self.assert_sampled() num = int(num) n_samples = int(n_samples) assert num > 1 and n_samples > 0, "num > 1 and n_samples > 0" # ====== indices ====== # if indices is None: indices = list(range(self.n_representations)) else: indices = [int(i) for i in tf.nest.flatten(indices)] assert all(i < self.n_factors for i in indices), \ "There are %d factors, but the factor indices are: %s" % \ (self.n_factors, str(indices)) indices = np.array(indices) # ====== check the mode ====== # all_mode = ('quantile', 'linear') mode = str(mode).strip().lower() assert mode in all_mode, \ "Only support %s, but given mode='%s'" % (str(all_mode), mode) # ====== helpers ====== # def _traverse(z): sampled_indices = self._rand.choice(z.shape[0], size=int(n_samples), replace=False) Zs = [] for i in sampled_indices: n = len(indices) * num z_i = np.repeat(np.expand_dims(z[i], 0), n, axis=0) for j, idx in enumerate(indices): start = j * num end = (j + 1) * num # linear if mode == 'linear': z_i[start:end, idx] = np.linspace(min_val, max_val, num) # Gaussian quantile elif mode == 'quantile': base_code = z_i[0, idx] print(base_code) exit() # Gaussian linear elif mode == '': raise NotImplementedError Zs.append(z_i) Zs = np.concatenate(Zs, axis=0) return Zs, sampled_indices # ====== traverse through latent space ====== # z_train, z_test = self.representations_mean z_train, train_ids = _traverse(z_train) z_test, test_ids = _traverse(z_test) return z_train, z_test def conditioning(self, known={}, logical_not=False, n_samples=None): r""" Conditioning the sampled dataset on known factors Arguments: known : a mapping from index or name of factor to a callable, the callable must return a list of boolean indices, which indicates the samples to be selected logical_not : a Boolean, if True applying the opposed conditioning of the known factors n_samples : an Integer (Optional), maximum number of selected samples. Return: a new `Criticizer` with the conditioned data and representations Example: ``` # conditioning on: (1st-factor > 2) and (2nd-factor ==
encode
identifier_name
_criticizer_base.py
representations.") @property def inputs(self): self.assert_sampled() return self._inputs @property def representations_full(self) -> tfd.Distribution: return self._representations_full @property def latents_full(self) -> tfd.Distribution: return self._representations_full @property def factors_full(self) -> tf.Tensor: return self._factors_full @property def original_factors_full(self) -> tf.Tensor: return self._original_factors_full @property def representations(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def latents(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def representations_mean(self): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.mean().numpy() for z in self.representations] @property def representations_variance(self): r""" Return the variance of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.variance().numpy() for z in self.representations] def representations_sample(self, n=()): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [ z.sample(sample_shape=n, seed=self.randint).numpy() for z in self.representations ] @property def reconstructions(self): r""" Return the reconstructed `Distributions` of inputs for training and testing """ self.assert_sampled() return self._reconstructions @property def reconstructions_mean(self): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.mean().numpy() for j in i] for i in self._reconstructions] @property def reconstructions_variance(self): r""" Return the variance of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.variance().numpy() for j in i] for i in self._reconstructions] def reconstructions_sample(self, n=()): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.sample(sample_shape=n, seed=self.randint).numpy() for j in i] for i in self._reconstructions] @property def original_factors(self): r""" Return the training and testing original factors, i.e. the factors before discretizing """ self.assert_sampled() # the original factors is the same for all samples set return self._original_factors @property def n_factors(self): return self.factors[0].shape[1] @property def n_representations(self): r""" return the number of latent codes """ return self.representations[0].event_shape[0] @property def n_codes(self): r""" same as `n_representations`, return the number of latent codes """ return self.n_representations @property def n_train(self): r""" Return number of samples for training """ return self.factors[0].shape[0] @property def n_test(self): r""" Return number of samples for testing """ return self.factors[1].shape[0] @property def factors(self): r""" Return the target variable (i.e. the factors of variation) for training and testing """ self.assert_sampled() return self._factors @property def factor_names(self): self.assert_sampled() # the dataset is unchanged, always at 0-th index return np.array(self._factor_names) @property def code_names(self): return np.array([f"Z{i}" for i in range(self.n_representations)]) @property def random_state(self): return self._rand @property def randint(self): return self._rand.randint(1e8) ############## proxy to VAE methods def index(self, factor_name): r""" Return the column index of given factor_names within the factor matrix """ return self._factor_names.index(str(factor_name)) def encode(self, inputs, mask=None, sample_shape=()): r""" Encode inputs to latent codes Arguments: inputs : a single Tensor or list of Tensor Returns: `tensorflow_probability.Distribution`, q(z|x) the latent distribution """ inputs = tf.nest.flatten(inputs)[:len(self._vae.encoder.inputs)] latents = self._vae.encode(inputs[0] if len(inputs) == 1 else inputs, training=False, mask=mask, sample_shape=sample_shape)
for z in tf.nest.flatten(latents): assert isinstance(z, tfd.Distribution), \ "The latent code return from `vae.encode` must be instance of " + \ "tensorflow_probability.Distribution, but returned: %s" % \ str(z) return latents def decode(self, latents, mask=None, sample_shape=()): r""" Decode the latents into reconstruction distribution """ outputs = self._vae.decode(latents, training=False, mask=mask, sample_shape=sample_shape) for o in tf.nest.flatten(outputs): assert isinstance(o, tfd.Distribution), \ "vae decode method must return reconstruction distribution, but " + \ "returned: %s" % str(o) return outputs ############## Experiment setup def traversing(self, indices=None, min_val=-1., max_val=1., num=10, n_samples=2, mode='linear'): r""" Arguments: indices : a list of Integer or None. The indices of latent code for traversing. If None, all latent codes are used. Return: numpy.ndarray : traversed latent codes for training and testing, the shape is `[len(indices) * n_samples * num, n_representations]` """ self.assert_sampled() num = int(num) n_samples = int(n_samples) assert num > 1 and n_samples > 0, "num > 1 and n_samples > 0" # ====== indices ====== # if indices is None: indices = list(range(self.n_representations)) else: indices = [int(i) for i in tf.nest.flatten(indices)] assert all(i < self.n_factors for i in indices), \ "There are %d factors, but the factor indices are: %s" % \ (self.n_factors, str(indices)) indices = np.array(indices) # ====== check the mode ====== # all_mode = ('quantile', 'linear') mode = str(mode).strip().lower() assert mode in all_mode, \ "Only support %s, but given mode='%s'" % (str(all_mode), mode) # ====== helpers ====== # def _traverse(z): sampled_indices = self._rand.choice(z.shape[0], size=int(n_samples), replace=False) Zs = [] for i in sampled_indices: n = len(indices) * num z_i = np.repeat(np.expand_dims(z[i], 0), n, axis=0) for j, idx in enumerate(indices): start = j * num end = (j + 1) * num # linear if mode == 'linear': z_i[start:end, idx] = np.linspace(min_val, max_val, num) # Gaussian quantile elif mode == 'quantile': base_code = z_i[0, idx] print(base_code) exit() # Gaussian linear elif mode == '': raise NotImplementedError Zs.append(z_i) Zs = np.concatenate(Zs, axis=0) return Zs, sampled_indices # ====== traverse through latent space ====== # z_train, z_test = self.representations_mean z_train, train_ids = _traverse(z_train) z_test, test_ids = _traverse(z_test) return z_train, z_test def conditioning(self, known={}, logical_not=False, n_samples=None): r""" Conditioning the sampled dataset on known factors Arguments: known : a mapping from index or name of factor to a callable, the callable must return a list of boolean indices, which indicates the samples to be selected logical_not : a Boolean, if True applying the opposed conditioning of the known factors n_samples : an Integer (Optional), maximum number of selected samples. Return: a new `Criticizer` with the conditioned data and representations Example: ``` # conditioning on: (1st-factor > 2) and (2nd-factor == 3
# only support single returned latent variable now
random_line_split
_criticizer_base.py
.") @property def inputs(self): self.assert_sampled() return self._inputs @property def representations_full(self) -> tfd.Distribution:
@property def latents_full(self) -> tfd.Distribution: return self._representations_full @property def factors_full(self) -> tf.Tensor: return self._factors_full @property def original_factors_full(self) -> tf.Tensor: return self._original_factors_full @property def representations(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def latents(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def representations_mean(self): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.mean().numpy() for z in self.representations] @property def representations_variance(self): r""" Return the variance of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.variance().numpy() for z in self.representations] def representations_sample(self, n=()): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [ z.sample(sample_shape=n, seed=self.randint).numpy() for z in self.representations ] @property def reconstructions(self): r""" Return the reconstructed `Distributions` of inputs for training and testing """ self.assert_sampled() return self._reconstructions @property def reconstructions_mean(self): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.mean().numpy() for j in i] for i in self._reconstructions] @property def reconstructions_variance(self): r""" Return the variance of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.variance().numpy() for j in i] for i in self._reconstructions] def reconstructions_sample(self, n=()): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.sample(sample_shape=n, seed=self.randint).numpy() for j in i] for i in self._reconstructions] @property def original_factors(self): r""" Return the training and testing original factors, i.e. the factors before discretizing """ self.assert_sampled() # the original factors is the same for all samples set return self._original_factors @property def n_factors(self): return self.factors[0].shape[1] @property def n_representations(self): r""" return the number of latent codes """ return self.representations[0].event_shape[0] @property def n_codes(self): r""" same as `n_representations`, return the number of latent codes """ return self.n_representations @property def n_train(self): r""" Return number of samples for training """ return self.factors[0].shape[0] @property def n_test(self): r""" Return number of samples for testing """ return self.factors[1].shape[0] @property def factors(self): r""" Return the target variable (i.e. the factors of variation) for training and testing """ self.assert_sampled() return self._factors @property def factor_names(self): self.assert_sampled() # the dataset is unchanged, always at 0-th index return np.array(self._factor_names) @property def code_names(self): return np.array([f"Z{i}" for i in range(self.n_representations)]) @property def random_state(self): return self._rand @property def randint(self): return self._rand.randint(1e8) ############## proxy to VAE methods def index(self, factor_name): r""" Return the column index of given factor_names within the factor matrix """ return self._factor_names.index(str(factor_name)) def encode(self, inputs, mask=None, sample_shape=()): r""" Encode inputs to latent codes Arguments: inputs : a single Tensor or list of Tensor Returns: `tensorflow_probability.Distribution`, q(z|x) the latent distribution """ inputs = tf.nest.flatten(inputs)[:len(self._vae.encoder.inputs)] latents = self._vae.encode(inputs[0] if len(inputs) == 1 else inputs, training=False, mask=mask, sample_shape=sample_shape) # only support single returned latent variable now for z in tf.nest.flatten(latents): assert isinstance(z, tfd.Distribution), \ "The latent code return from `vae.encode` must be instance of " + \ "tensorflow_probability.Distribution, but returned: %s" % \ str(z) return latents def decode(self, latents, mask=None, sample_shape=()): r""" Decode the latents into reconstruction distribution """ outputs = self._vae.decode(latents, training=False, mask=mask, sample_shape=sample_shape) for o in tf.nest.flatten(outputs): assert isinstance(o, tfd.Distribution), \ "vae decode method must return reconstruction distribution, but " + \ "returned: %s" % str(o) return outputs ############## Experiment setup def traversing(self, indices=None, min_val=-1., max_val=1., num=10, n_samples=2, mode='linear'): r""" Arguments: indices : a list of Integer or None. The indices of latent code for traversing. If None, all latent codes are used. Return: numpy.ndarray : traversed latent codes for training and testing, the shape is `[len(indices) * n_samples * num, n_representations]` """ self.assert_sampled() num = int(num) n_samples = int(n_samples) assert num > 1 and n_samples > 0, "num > 1 and n_samples > 0" # ====== indices ====== # if indices is None: indices = list(range(self.n_representations)) else: indices = [int(i) for i in tf.nest.flatten(indices)] assert all(i < self.n_factors for i in indices), \ "There are %d factors, but the factor indices are: %s" % \ (self.n_factors, str(indices)) indices = np.array(indices) # ====== check the mode ====== # all_mode = ('quantile', 'linear') mode = str(mode).strip().lower() assert mode in all_mode, \ "Only support %s, but given mode='%s'" % (str(all_mode), mode) # ====== helpers ====== # def _traverse(z): sampled_indices = self._rand.choice(z.shape[0], size=int(n_samples), replace=False) Zs = [] for i in sampled_indices: n = len(indices) * num z_i = np.repeat(np.expand_dims(z[i], 0), n, axis=0) for j, idx in enumerate(indices): start = j * num end = (j + 1) * num # linear if mode == 'linear': z_i[start:end, idx] = np.linspace(min_val, max_val, num) # Gaussian quantile elif mode == 'quantile': base_code = z_i[0, idx] print(base_code) exit() # Gaussian linear elif mode == '': raise NotImplementedError Zs.append(z_i) Zs = np.concatenate(Zs, axis=0) return Zs, sampled_indices # ====== traverse through latent space ====== # z_train, z_test = self.representations_mean z_train, train_ids = _traverse(z_train) z_test, test_ids = _traverse(z_test) return z_train, z_test def conditioning(self, known={}, logical_not=False, n_samples=None): r""" Conditioning the sampled dataset on known factors Arguments: known : a mapping from index or name of factor to a callable, the callable must return a list of boolean indices, which indicates the samples to be selected logical_not : a Boolean, if True applying the opposed conditioning of the known factors n_samples : an Integer (Optional), maximum number of selected samples. Return: a new `Criticizer` with the conditioned data and representations Example: ``` # conditioning on: (1st-factor > 2) and (2nd-factor == 3
return self._representations_full
identifier_body
_criticizer_base.py
# main arguments self._inputs = None self._factors = None self._original_factors = None self._factor_names = None self._representations = None self._reconstructions = None # concatenated train and test self._representations_full = None self._factors_full = None self._original_factors_full = None # others self._rand = random_state self._is_multi_latents = 0 @property def is_multi_latents(self): return self._is_multi_latents @property def is_sampled(self): if self._factors is None or self._representations is None: return False return True def assert_sampled(self): if not self.is_sampled: raise RuntimeError("Call the `sample_batch` method to sample mini-batch " "of ground-truth data and learned representations.") @property def inputs(self): self.assert_sampled() return self._inputs @property def representations_full(self) -> tfd.Distribution: return self._representations_full @property def latents_full(self) -> tfd.Distribution: return self._representations_full @property def factors_full(self) -> tf.Tensor: return self._factors_full @property def original_factors_full(self) -> tf.Tensor: return self._original_factors_full @property def representations(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def latents(self): r""" Return the learned latent representations `Distribution` (i.e. the latent code) for training and testing """ self.assert_sampled() return self._representations @property def representations_mean(self): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.mean().numpy() for z in self.representations] @property def representations_variance(self): r""" Return the variance of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [z.variance().numpy() for z in self.representations] def representations_sample(self, n=()): r""" Return the mean of learned representations distribution (i.e. the latent code) for training and testing """ self.assert_sampled() return [ z.sample(sample_shape=n, seed=self.randint).numpy() for z in self.representations ] @property def reconstructions(self): r""" Return the reconstructed `Distributions` of inputs for training and testing """ self.assert_sampled() return self._reconstructions @property def reconstructions_mean(self): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.mean().numpy() for j in i] for i in self._reconstructions] @property def reconstructions_variance(self): r""" Return the variance of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.variance().numpy() for j in i] for i in self._reconstructions] def reconstructions_sample(self, n=()): r""" Return the mean of reconstructed distributions of inputs for training and testing """ self.assert_sampled() return [[j.sample(sample_shape=n, seed=self.randint).numpy() for j in i] for i in self._reconstructions] @property def original_factors(self): r""" Return the training and testing original factors, i.e. the factors before discretizing """ self.assert_sampled() # the original factors is the same for all samples set return self._original_factors @property def n_factors(self): return self.factors[0].shape[1] @property def n_representations(self): r""" return the number of latent codes """ return self.representations[0].event_shape[0] @property def n_codes(self): r""" same as `n_representations`, return the number of latent codes """ return self.n_representations @property def n_train(self): r""" Return number of samples for training """ return self.factors[0].shape[0] @property def n_test(self): r""" Return number of samples for testing """ return self.factors[1].shape[0] @property def factors(self): r""" Return the target variable (i.e. the factors of variation) for training and testing """ self.assert_sampled() return self._factors @property def factor_names(self): self.assert_sampled() # the dataset is unchanged, always at 0-th index return np.array(self._factor_names) @property def code_names(self): return np.array([f"Z{i}" for i in range(self.n_representations)]) @property def random_state(self): return self._rand @property def randint(self): return self._rand.randint(1e8) ############## proxy to VAE methods def index(self, factor_name): r""" Return the column index of given factor_names within the factor matrix """ return self._factor_names.index(str(factor_name)) def encode(self, inputs, mask=None, sample_shape=()): r""" Encode inputs to latent codes Arguments: inputs : a single Tensor or list of Tensor Returns: `tensorflow_probability.Distribution`, q(z|x) the latent distribution """ inputs = tf.nest.flatten(inputs)[:len(self._vae.encoder.inputs)] latents = self._vae.encode(inputs[0] if len(inputs) == 1 else inputs, training=False, mask=mask, sample_shape=sample_shape) # only support single returned latent variable now for z in tf.nest.flatten(latents): assert isinstance(z, tfd.Distribution), \ "The latent code return from `vae.encode` must be instance of " + \ "tensorflow_probability.Distribution, but returned: %s" % \ str(z) return latents def decode(self, latents, mask=None, sample_shape=()): r""" Decode the latents into reconstruction distribution """ outputs = self._vae.decode(latents, training=False, mask=mask, sample_shape=sample_shape) for o in tf.nest.flatten(outputs): assert isinstance(o, tfd.Distribution), \ "vae decode method must return reconstruction distribution, but " + \ "returned: %s" % str(o) return outputs ############## Experiment setup def traversing(self, indices=None, min_val=-1., max_val=1., num=10, n_samples=2, mode='linear'): r""" Arguments: indices : a list of Integer or None. The indices of latent code for traversing. If None, all latent codes are used. Return: numpy.ndarray : traversed latent codes for training and testing, the shape is `[len(indices) * n_samples * num, n_representations]` """ self.assert_sampled() num = int(num) n_samples = int(n_samples) assert num > 1 and n_samples > 0, "num > 1 and n_samples > 0" # ====== indices ====== # if indices is None: indices = list(range(self.n_representations)) else: indices = [int(i) for i in tf.nest.flatten(indices)] assert all(i < self.n_factors for i in indices), \ "There are %d factors, but the factor indices are: %s" % \ (self.n_factors, str(indices)) indices = np.array(indices) # ====== check the mode ====== # all_mode = ('quantile', 'linear') mode = str(mode).strip().lower() assert mode in all_mode, \ "Only support %s, but given mode='%s'" % (str(all_mode), mode) # ====== helpers ====== # def _traverse(z): sampled_indices = self._rand.choice(z.shape[0], size=int(n_samples), replace=False) Zs = [] for i in sampled_indices: n = len(indices) * num z_i = np.repeat(np.expand_dims(z[i], 0), n, axis=0) for j, idx in enumerate(indices): start = j * num end = (j + 1) * num # linear if mode == 'linear': z_i[start:end, idx] = np.linspace(min_val, max_val, num) # Gaussian quantile elif mode == 'quantile': base_code = z_i[0, idx] print(base_code) exit() # Gaussian linear elif mode == '': raise NotImplementedError Zs.append(z_i) Zs =
random_state = np.random.RandomState(seed=random_state)
conditional_block
patterns.rs
8..71 'any': fn any<&str>() -> &str 68..73 'any()': &str 74..76 '{}': () 81..100 'if let...y() {}': () 88..89 '1': i32 88..89 '1': i32 92..95 'any': fn any<i32>() -> i32 92..97 'any()': i32 98..100 '{}': () 105..127 'if let...y() {}': () 112..116 '1u32': u32 112..116 '1u32': u32 119..122 'any': fn any<u32>() -> u32 119..124 'any()': u32 125..127 '{}': () 132..154 'if let...y() {}': () 139..143 '1f32': f32 139..143 '1f32': f32 146..149 'any': fn any<f32>() -> f32 146..151 'any()': f32 152..154 '{}': () 159..180 'if let...y() {}': () 166..169 '1.0': f64 166..169 '1.0': f64 172..175 'any': fn any<f64>() -> f64 172..177 'any()': f64 178..180 '{}': () 185..207 'if let...y() {}': () 192..196 'true': bool 192..196 'true': bool 199..202 'any': fn any<bool>() -> bool 199..204 'any()': bool 205..207 '{}': () "### ); } #[test] fn infer_range_pattern()
); } #[test] fn infer_pattern_match_ergonomics() { assert_snapshot!( infer(r#" struct A<T>(T); fn test() { let A(n) = &A(1); let A(n) = &mut A(1); } "#), @r###" 28..79 '{ ...(1); }': () 38..42 'A(n)': A<i32> 40..41 'n': &i32 45..50 '&A(1)': &A<i32> 46..47 'A': A<i32>(i32) -> A<i32> 46..50 'A(1)': A<i32> 48..49 '1': i32 60..64 'A(n)': A<i32> 62..63 'n': &mut i32 67..76 '&mut A(1)': &mut A<i32> 72..73 'A': A<i32>(i32) -> A<i32> 72..76 'A(1)': A<i32> 74..75 '1': i32 "### ); } #[test] fn infer_pattern_match_ergonomics_ref() { mark::check!(match_ergonomics_ref); assert_snapshot!( infer(r#" fn test() { let v = &(1, &2); let (_, &w) = v; } "#), @r###" 11..57 '{ ...= v; }': () 21..22 'v': &(i32, &i32) 25..33 '&(1, &2)': &(i32, &i32) 26..33 '(1, &2)': (i32, &i32) 27..28 '1': i32 30..32 '&2': &i32 31..32 '2': i32 43..50 '(_, &w)': (i32, &i32) 44..45 '_': i32 47..49 '&w': &i32 48..49 'w': i32 53..54 'v': &(i32, &i32) "### ); } #[test] fn infer_pattern_match_slice() { assert_snapshot!( infer(r#" fn test() { let slice: &[f64] = &[0.0]; match slice { &[] => {}, &[a] => { a; }, &[b, c] => { b; c; } _ => {} } } "#), @r###" 11..210 '{ ... } }': () 21..26 'slice': &[f64] 37..43 '&[0.0]': &[f64; _] 38..43 '[0.0]': [f64; _] 39..42 '0.0': f64 49..208 'match ... }': () 55..60 'slice': &[f64] 71..74 '&[]': &[f64] 72..74 '[]': [f64] 78..80 '{}': () 90..94 '&[a]': &[f64] 91..94 '[a]': [f64] 92..93 'a': f64 98..124 '{ ... }': () 112..113 'a': f64 134..141 '&[b, c]': &[f64] 135..141 '[b, c]': [f64] 136..137 'b': f64 139..140 'c': f64 145..186 '{ ... }': () 159..160 'b': f64 174..175 'c': f64 195..196 '_': &[f64] 200..202 '{}': () "### ); } #[test] fn infer_pattern_match_arr() { assert_snapshot!( infer(r#" fn test() { let arr: [f64; 2] = [0.0, 1.0]; match arr { [1.0, a] => { a; }, [b, c] => { b; c; } } } "#), @r###" 11..180 '{ ... } }': () 21..24 'arr': [f64; _] 37..47 '[0.0, 1.0]': [f64; _] 38..41 '0.0': f64 43..46 '1.0': f64 53..178 'match ... }': () 59..62 'arr':
{ assert_snapshot!( infer_with_mismatches(r#" fn test(x: &i32) { if let 1..76 = 2u32 {} if let 1..=76 = 2u32 {} } "#, true), @r###" 9..10 'x': &i32 18..76 '{ ...2 {} }': () 24..46 'if let...u32 {}': () 31..36 '1..76': u32 39..43 '2u32': u32 44..46 '{}': () 51..74 'if let...u32 {}': () 58..64 '1..=76': u32 67..71 '2u32': u32 72..74 '{}': () "###
identifier_body
patterns.rs
8..71 'any': fn any<&str>() -> &str 68..73 'any()': &str 74..76 '{}': () 81..100 'if let...y() {}': () 88..89 '1': i32 88..89 '1': i32 92..95 'any': fn any<i32>() -> i32 92..97 'any()': i32 98..100 '{}': () 105..127 'if let...y() {}': () 112..116 '1u32': u32 112..116 '1u32': u32 119..122 'any': fn any<u32>() -> u32 119..124 'any()': u32 125..127 '{}': () 132..154 'if let...y() {}': () 139..143 '1f32': f32 139..143 '1f32': f32 146..149 'any': fn any<f32>() -> f32 146..151 'any()': f32 152..154 '{}': () 159..180 'if let...y() {}': () 166..169 '1.0': f64 166..169 '1.0': f64 172..175 'any': fn any<f64>() -> f64 172..177 'any()': f64 178..180 '{}': () 185..207 'if let...y() {}': () 192..196 'true': bool 192..196 'true': bool 199..202 'any': fn any<bool>() -> bool 199..204 'any()': bool 205..207 '{}': () "### ); } #[test] fn infer_range_pattern() { assert_snapshot!( infer_with_mismatches(r#" fn test(x: &i32) { if let 1..76 = 2u32 {} if let 1..=76 = 2u32 {} } "#, true), @r###" 9..10 'x': &i32 18..76 '{ ...2 {} }': () 24..46 'if let...u32 {}': () 31..36 '1..76': u32 39..43 '2u32': u32 44..46 '{}': () 51..74 'if let...u32 {}': () 58..64 '1..=76': u32 67..71 '2u32': u32 72..74 '{}': () "### ); } #[test] fn infer_pattern_match_ergonomics() { assert_snapshot!( infer(r#" struct A<T>(T); fn test() { let A(n) = &A(1); let A(n) = &mut A(1); } "#), @r###" 28..79 '{ ...(1); }': () 38..42 'A(n)': A<i32> 40..41 'n': &i32 45..50 '&A(1)': &A<i32> 46..47 'A': A<i32>(i32) -> A<i32> 46..50 'A(1)': A<i32> 48..49 '1': i32 60..64 'A(n)': A<i32> 62..63 'n': &mut i32 67..76 '&mut A(1)': &mut A<i32> 72..73 'A': A<i32>(i32) -> A<i32> 72..76 'A(1)': A<i32> 74..75 '1': i32 "### ); } #[test] fn
() { mark::check!(match_ergonomics_ref); assert_snapshot!( infer(r#" fn test() { let v = &(1, &2); let (_, &w) = v; } "#), @r###" 11..57 '{ ...= v; }': () 21..22 'v': &(i32, &i32) 25..33 '&(1, &2)': &(i32, &i32) 26..33 '(1, &2)': (i32, &i32) 27..28 '1': i32 30..32 '&2': &i32 31..32 '2': i32 43..50 '(_, &w)': (i32, &i32) 44..45 '_': i32 47..49 '&w': &i32 48..49 'w': i32 53..54 'v': &(i32, &i32) "### ); } #[test] fn infer_pattern_match_slice() { assert_snapshot!( infer(r#" fn test() { let slice: &[f64] = &[0.0]; match slice { &[] => {}, &[a] => { a; }, &[b, c] => { b; c; } _ => {} } } "#), @r###" 11..210 '{ ... } }': () 21..26 'slice': &[f64] 37..43 '&[0.0]': &[f64; _] 38..43 '[0.0]': [f64; _] 39..42 '0.0': f64 49..208 'match ... }': () 55..60 'slice': &[f64] 71..74 '&[]': &[f64] 72..74 '[]': [f64] 78..80 '{}': () 90..94 '&[a]': &[f64] 91..94 '[a]': [f64] 92..93 'a': f64 98..124 '{ ... }': () 112..113 'a': f64 134..141 '&[b, c]': &[f64] 135..141 '[b, c]': [f64] 136..137 'b': f64 139..140 'c': f64 145..186 '{ ... }': () 159..160 'b': f64 174..175 'c': f64 195..196 '_': &[f64] 200..202 '{}': () "### ); } #[test] fn infer_pattern_match_arr() { assert_snapshot!( infer(r#" fn test() { let arr: [f64; 2] = [0.0, 1.0]; match arr { [1.0, a] => { a; }, [b, c] => { b; c; } } } "#), @r###" 11..180 '{ ... } }': () 21..24 'arr': [f64; _] 37..47 '[0.0, 1.0]': [f64; _] 38..41 '0.0': f64 43..46 '1.0': f64 53..178 'match ... }': () 59..62 'arr':
infer_pattern_match_ergonomics_ref
identifier_name
patterns.rs
let (c, d) = (1, "hello"); for (e, f) in some_iter { let g = e; } if let [val] = opt { let h = val; } let lambda = |a: u64, b, c: i32| { a + b; c }; let ref ref_to_x = x; let mut mut_x = x; let ref mut mut_ref_to_x = x; let k = mut_ref_to_x; } "#), @r###" 9..10 'x': &i32 18..369 '{ ...o_x; }': () 28..29 'y': &i32 32..33 'x': &i32 43..45 '&z': &i32 44..45 'z': i32 48..49 'x': &i32 59..60 'a': i32 63..64 'z': i32 74..80 '(c, d)': (i32, &str) 75..76 'c': i32 78..79 'd': &str 83..95 '(1, "hello")': (i32, &str) 84..85 '1': i32 87..94 '"hello"': &str 102..152 'for (e... }': () 106..112 '(e, f)': ({unknown}, {unknown}) 107..108 'e': {unknown} 110..111 'f': {unknown} 116..125 'some_iter': {unknown} 126..152 '{ ... }': () 140..141 'g': {unknown} 144..145 'e': {unknown} 158..205 'if let... }': () 165..170 '[val]': [{unknown}] 166..169 'val': {unknown} 173..176 'opt': [{unknown}] 177..205 '{ ... }': () 191..192 'h': {unknown} 195..198 'val': {unknown} 215..221 'lambda': |u64, u64, i32| -> i32 224..256 '|a: u6...b; c }': |u64, u64, i32| -> i32 225..226 'a': u64 233..234 'b': u64 236..237 'c': i32 244..256 '{ a + b; c }': i32 246..247 'a': u64 246..251 'a + b': u64 250..251 'b': u64 253..254 'c': i32 267..279 'ref ref_to_x': &&i32 282..283 'x': &i32 293..302 'mut mut_x': &i32 305..306 'x': &i32 316..336 'ref mu...f_to_x': &mut &i32 339..340 'x': &i32 350..351 'k': &mut &i32 354..366 'mut_ref_to_x': &mut &i32 "### ); } #[test] fn infer_literal_pattern() { assert_snapshot!( infer_with_mismatches(r#" fn any<T>() -> T { loop {} } fn test(x: &i32) { if let "foo" = any() {} if let 1 = any() {} if let 1u32 = any() {} if let 1f32 = any() {} if let 1.0 = any() {} if let true = any() {} } "#, true), @r###" 18..29 '{ loop {} }': T 20..27 'loop {}': ! 25..27 '{}': () 38..39 'x': &i32 47..209 '{ ...) {} }': () 53..76 'if let...y() {}': () 60..65 '"foo"': &str 60..65 '"foo"': &str 68..71 'any': fn any<&str>() -> &str 68..73 'any()': &str 74..76 '{}': () 81..100 'if let...y() {}': () 88..89 '1': i32 88..89 '1': i32 92..95 'any': fn any<i32>() -> i32 92..97 'any()': i32 98..100 '{}': () 105..127 'if let...y() {}': () 112..116 '1u32': u32 112..116 '1u32': u32 119..122 'any': fn any<u32>() -> u32 119..124 'any()': u32 125..127 '{}': () 132..154 'if let...y() {}': () 139..143 '1f32': f32 139..143 '1f32': f32 146..149 'any': fn any<f32>() -> f32 146..151 'any()': f32 152..154 '{}': () 159..180 'if let...y() {}': () 166..169 '1.0': f64 166..169 '1.0': f64 172..175 'any': fn any<f64>() -> f64 172..177 'any()': f64 178..180 '{}': () 185..207 'if let...y() {}': () 192..196 'true': bool 192..196 'true': bool 199..202 'any': fn any<bool>() -> bool 199..204 'any()': bool 205..207 '{}': () "### ); } #[test] fn infer_range_pattern() { assert_snapshot!( infer_with_mismatches(r#" fn test(x: &i32) { if let 1..76 = 2u32 {} if let 1..=76 = 2u32 {} } "#, true), @r###" 9..10 'x': &i32 18..76 '{ ...2 {} }': () 24..46 'if let...u32 {}': () 31..36 '1..76': u32 39..43 '2u32': u32 44..46 '{}': () 51..74 'if let...u32 {}': () 58..64 '1..=76': u32 67..71 '2u32': u32 72..74 '{}': () "### ); } #[test] fn infer_pattern_match_ergonomics() { assert_snapshot!( infer(r#" struct A<T>(T); fn test() { let A(n) = &A(1); let A(n) = &
infer(r#" fn test(x: &i32) { let y = x; let &z = x; let a = z;
random_line_split
dual_encoder.py
(filename, vocab): """ Load glove vectors from a .txt file. Optionally limit the vocabulary to save memory. `vocab` should be a set. """ dct = {} vectors = array.array('d') current_idx = 0 with codecs.open(filename, "r", encoding="utf-8") as f: for _, line in enumerate(f): tokens = line.split(" ") word = tokens[0] entries = tokens[1:] if not vocab or word in vocab: dct[word] = current_idx vectors.extend(float(x) for x in entries) current_idx += 1 word_dim = len(entries) num_vectors = len(dct) tf.logging.info("Found {} out of {} vectors in Glove".format(num_vectors, len(vocab))) return [np.array(vectors).reshape(num_vectors, word_dim), dct] def buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, embedding_dim): initial_embeddings = np.random.uniform(-0.25, 0.25, (len(vocab_dict), embedding_dim)).astype("float32") for word, glove_word_idx in glove_dict.items(): word_idx = vocab_dict.get(word) initial_embeddings[word_idx, :] = glove_vectors[glove_word_idx] return initial_embeddings FLAGS = tf.flags.FLAGS def get_embeddings(hparams): if hparams.glove_path and hparams.vocab_path: tf.logging.info("Loading Glove embeddings...") vocab_array, vocab_dict = loadVOCAB(hparams.vocab_path) glove_vectors, glove_dict = loadGLOVE(hparams.glove_path, vocab=set(vocab_array)) initializer = buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, hparams.embedding_dim) else: tf.logging.info("No glove/vocab path specificed, starting with random embeddings.") initializer = tf.random_uniform_initializer(-0.25, 0.25) return tf.get_variable("word_embeddings", shape=[hparams.vocab_size, hparams.embedding_dim], initializer=initializer) class DualEncoders:
logits = self.inference() probs = tf.sigmoid(logits, name="probs_op") losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf.to_float(self.targets), name="CrossEntropy") mean_loss = tf.reduce_mean(losses, name="Mean_CE_Loss") train_op = tf.contrib.layers.optimize_loss(loss=mean_loss, global_step=self.global_step, learning_rate=self.learning_rate, clip_gradients=self.hparams.max_grad_norm, optimizer=hparams.optimizer) ema = tf.train.ExponentialMovingAverage(decay=0.99) mean_loss_ema_op = ema.apply([mean_loss]) with tf.control_dependencies([self.targets]): # update only when train targets passed train_op_group = tf.group(train_op, mean_loss_ema_op) self.probs_op = probs self.train_loss_op = ema.average(mean_loss) self.train_op = train_op_group self.train_summaries = tf.summary.merge([tf.summary.scalar("loss", mean_loss), tf.summary.scalar("learning_rate", self.learning_rate)]) self.val_probs, self.val_summary = self.validation_accuracy(probs, self.val_targets, mean_loss) def inference(self): W_emb = get_embeddings(self.hparams) context_emb = tf.nn.embedding_lookup(W_emb, self.context, name="ContextEmbedding") utterance_emb = tf.nn.embedding_lookup(W_emb, self.utterance, name="UtteranceEmbedding") with tf.variable_scope("BidirectionalLSTM"): argsdict = {"forget_bias": 2.0, "use_peepholes": True, "state_is_tuple": True} fw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) bw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) seq = tf.concat([context_emb, utterance_emb],axis=0) seqlen = tf.concat([self.context_len,self.utterance_len], axis=0) _, rnn_states = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, inputs=seq, sequence_length=seqlen, dtype=tf.float32) fw_encoding_context, fw_encoding_utter = tf.split(rnn_states[0].h, 2, axis=0) bw_encoding_context, bw_encoding_utter = tf.split(rnn_states[1].h, 2, axis=0) encoding_context = tf.concat([fw_encoding_context, bw_encoding_context], axis=1) encoding_utterance = tf.concat([fw_encoding_utter, bw_encoding_utter], axis=1) with tf.variable_scope("Prediction"): M = tf.get_variable(name="M", shape=[2 * self.hparams.rnn_dim, 2 * self.hparams.rnn_dim], initializer=tf.random_uniform_initializer(-0.25, 0.25)) generated_response = tf.matmul(encoding_context, M) generated_response = tf.expand_dims(generated_response, 2) encoding_utterance = tf.expand_dims(encoding_utterance, 2) logits = tf.matmul(generated_response, encoding_utterance, True) logits = tf.reshape(logits, [-1]) return logits def validation_accuracy(self, pred_labels, val_labels, val_loss): shaped_probs = tf.reshape(pred_labels, [-1, 10]) def get_top(k): return tf.reduce_mean(tf.cast(tf.nn.in_top_k(shaped_probs, val_labels, k=k), tf.float32)) ema = tf.train.ExponentialMovingAverage(decay=0.99) top1, top2, top3, top5 = [get_top(k) for k in [1, 2, 3, 5]] maintain_averages = ema.apply([top1, top2, top3, top5, val_loss]) with tf.control_dependencies([self.val_targets]): # update only when validation targets passed self.update_averages = tf.group(maintain_averages) # TODO reset shadow variables between validation sessions self.val_loss = ema.average(val_loss) self.top1_av = ema.average(top1) self.top2_av = ema.average(top2) self.top3_av = ema.average(top3) self.top5_av = ema.average(top5) val_summary = tf.summary.merge([tf.summary.scalar("validation_loss", self.val_loss), tf.summary.scalar("top1", self.top1_av), tf.summary.scalar("top2", self.top2_av), tf.summary.scalar("top3", self.top3_av), tf.summary.scalar("top5", self.top5_av), tf.summary.histogram("correct_probs_distribution", shaped_probs[:, 0]), tf.summary.histogram("incorrect_probs_distribution", shaped_probs[:, 1:])]) return shaped_probs, val_summary def setSession(self, session): self._sess = session def save_model(self, saver, location, step): saver.save(self._sess, location, global_step=step) def load_model(self, saver, location): print("Variable initializaion") init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self._sess.run(init_op) ckpt = tf.train.get_checkpoint_state(location) if ckpt and ckpt.model_checkpoint_path: print('Restoring model') saver.restore(self._sess, ckpt.model_checkpoint_path) def batch_fit(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict["utterance"], self.utterance_len: batch_dict["utterance_len"], self.targets: batch_dict["label"]} train_summary, step, _, loss = self._sess.run([self.train_summaries, self.global_step, self.train_op, self.train_loss_op], feed_dict=feed_dict) return loss, step, train_summary def predict(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict
def __init__(self, hparams): self.hparams = hparams self.global_step = tf.Variable(0, trainable=False, name='global_step') self.learning_rate = tf.train.exponential_decay( self.hparams.learning_rate, # Base learning rate. self.global_step, # Current index into the dataset. self.hparams.decay_step, # Decay step. self.hparams.decay_rate, # Decay rate. staircase=self.hparams.staircase, name="learning_rate_decay") self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate) self.context = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Context") self.context_len = tf.placeholder(tf.int64, [None], name="ContextLenValue") self.utterance = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Utterance") self.utterance_len = tf.placeholder(tf.int64, [None], name="UtteranceLenValue") self.targets = tf.placeholder(tf.int64, [None], name="TargetLabels") self.val_targets = tf.placeholder(tf.int64, [None], name="ValidationLabels")
identifier_body
dual_encoder.py
VE(filename, vocab): """ Load glove vectors from a .txt file. Optionally limit the vocabulary to save memory. `vocab` should be a set. """ dct = {} vectors = array.array('d') current_idx = 0 with codecs.open(filename, "r", encoding="utf-8") as f: for _, line in enumerate(f): tokens = line.split(" ") word = tokens[0] entries = tokens[1:] if not vocab or word in vocab: dct[word] = current_idx vectors.extend(float(x) for x in entries) current_idx += 1 word_dim = len(entries) num_vectors = len(dct) tf.logging.info("Found {} out of {} vectors in Glove".format(num_vectors, len(vocab))) return [np.array(vectors).reshape(num_vectors, word_dim), dct] def buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, embedding_dim): initial_embeddings = np.random.uniform(-0.25, 0.25, (len(vocab_dict), embedding_dim)).astype("float32") for word, glove_word_idx in glove_dict.items(): word_idx = vocab_dict.get(word) initial_embeddings[word_idx, :] = glove_vectors[glove_word_idx] return initial_embeddings FLAGS = tf.flags.FLAGS def get_embeddings(hparams): if hparams.glove_path and hparams.vocab_path: tf.logging.info("Loading Glove embeddings...") vocab_array, vocab_dict = loadVOCAB(hparams.vocab_path) glove_vectors, glove_dict = loadGLOVE(hparams.glove_path, vocab=set(vocab_array)) initializer = buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, hparams.embedding_dim) else: tf.logging.info("No glove/vocab path specificed, starting with random embeddings.") initializer = tf.random_uniform_initializer(-0.25, 0.25) return tf.get_variable("word_embeddings", shape=[hparams.vocab_size, hparams.embedding_dim], initializer=initializer) class DualEncoders: def __init__(self, hparams): self.hparams = hparams self.global_step = tf.Variable(0, trainable=False, name='global_step')
self.hparams.learning_rate, # Base learning rate. self.global_step, # Current index into the dataset. self.hparams.decay_step, # Decay step. self.hparams.decay_rate, # Decay rate. staircase=self.hparams.staircase, name="learning_rate_decay") self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate) self.context = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Context") self.context_len = tf.placeholder(tf.int64, [None], name="ContextLenValue") self.utterance = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Utterance") self.utterance_len = tf.placeholder(tf.int64, [None], name="UtteranceLenValue") self.targets = tf.placeholder(tf.int64, [None], name="TargetLabels") self.val_targets = tf.placeholder(tf.int64, [None], name="ValidationLabels") logits = self.inference() probs = tf.sigmoid(logits, name="probs_op") losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf.to_float(self.targets), name="CrossEntropy") mean_loss = tf.reduce_mean(losses, name="Mean_CE_Loss") train_op = tf.contrib.layers.optimize_loss(loss=mean_loss, global_step=self.global_step, learning_rate=self.learning_rate, clip_gradients=self.hparams.max_grad_norm, optimizer=hparams.optimizer) ema = tf.train.ExponentialMovingAverage(decay=0.99) mean_loss_ema_op = ema.apply([mean_loss]) with tf.control_dependencies([self.targets]): # update only when train targets passed train_op_group = tf.group(train_op, mean_loss_ema_op) self.probs_op = probs self.train_loss_op = ema.average(mean_loss) self.train_op = train_op_group self.train_summaries = tf.summary.merge([tf.summary.scalar("loss", mean_loss), tf.summary.scalar("learning_rate", self.learning_rate)]) self.val_probs, self.val_summary = self.validation_accuracy(probs, self.val_targets, mean_loss) def inference(self): W_emb = get_embeddings(self.hparams) context_emb = tf.nn.embedding_lookup(W_emb, self.context, name="ContextEmbedding") utterance_emb = tf.nn.embedding_lookup(W_emb, self.utterance, name="UtteranceEmbedding") with tf.variable_scope("BidirectionalLSTM"): argsdict = {"forget_bias": 2.0, "use_peepholes": True, "state_is_tuple": True} fw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) bw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) seq = tf.concat([context_emb, utterance_emb],axis=0) seqlen = tf.concat([self.context_len,self.utterance_len], axis=0) _, rnn_states = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, inputs=seq, sequence_length=seqlen, dtype=tf.float32) fw_encoding_context, fw_encoding_utter = tf.split(rnn_states[0].h, 2, axis=0) bw_encoding_context, bw_encoding_utter = tf.split(rnn_states[1].h, 2, axis=0) encoding_context = tf.concat([fw_encoding_context, bw_encoding_context], axis=1) encoding_utterance = tf.concat([fw_encoding_utter, bw_encoding_utter], axis=1) with tf.variable_scope("Prediction"): M = tf.get_variable(name="M", shape=[2 * self.hparams.rnn_dim, 2 * self.hparams.rnn_dim], initializer=tf.random_uniform_initializer(-0.25, 0.25)) generated_response = tf.matmul(encoding_context, M) generated_response = tf.expand_dims(generated_response, 2) encoding_utterance = tf.expand_dims(encoding_utterance, 2) logits = tf.matmul(generated_response, encoding_utterance, True) logits = tf.reshape(logits, [-1]) return logits def validation_accuracy(self, pred_labels, val_labels, val_loss): shaped_probs = tf.reshape(pred_labels, [-1, 10]) def get_top(k): return tf.reduce_mean(tf.cast(tf.nn.in_top_k(shaped_probs, val_labels, k=k), tf.float32)) ema = tf.train.ExponentialMovingAverage(decay=0.99) top1, top2, top3, top5 = [get_top(k) for k in [1, 2, 3, 5]] maintain_averages = ema.apply([top1, top2, top3, top5, val_loss]) with tf.control_dependencies([self.val_targets]): # update only when validation targets passed self.update_averages = tf.group(maintain_averages) # TODO reset shadow variables between validation sessions self.val_loss = ema.average(val_loss) self.top1_av = ema.average(top1) self.top2_av = ema.average(top2) self.top3_av = ema.average(top3) self.top5_av = ema.average(top5) val_summary = tf.summary.merge([tf.summary.scalar("validation_loss", self.val_loss), tf.summary.scalar("top1", self.top1_av), tf.summary.scalar("top2", self.top2_av), tf.summary.scalar("top3", self.top3_av), tf.summary.scalar("top5", self.top5_av), tf.summary.histogram("correct_probs_distribution", shaped_probs[:, 0]), tf.summary.histogram("incorrect_probs_distribution", shaped_probs[:, 1:])]) return shaped_probs, val_summary def setSession(self, session): self._sess = session def save_model(self, saver, location, step): saver.save(self._sess, location, global_step=step) def load_model(self, saver, location): print("Variable initializaion") init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self._sess.run(init_op) ckpt = tf.train.get_checkpoint_state(location) if ckpt and ckpt.model_checkpoint_path: print('Restoring model') saver.restore(self._sess, ckpt.model_checkpoint_path) def batch_fit(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict["utterance"], self.utterance_len: batch_dict["utterance_len"], self.targets: batch_dict["label"]} train_summary, step, _, loss = self._sess.run([self.train_summaries, self.global_step, self.train_op, self.train_loss_op], feed_dict=feed_dict) return loss, step, train_summary def predict(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict
self.learning_rate = tf.train.exponential_decay(
random_line_split
dual_encoder.py
return [vocab, dct] def loadGLOVE(filename, vocab): """ Load glove vectors from a .txt file. Optionally limit the vocabulary to save memory. `vocab` should be a set. """ dct = {} vectors = array.array('d') current_idx = 0 with codecs.open(filename, "r", encoding="utf-8") as f: for _, line in enumerate(f): tokens = line.split(" ") word = tokens[0] entries = tokens[1:] if not vocab or word in vocab: dct[word] = current_idx vectors.extend(float(x) for x in entries) current_idx += 1 word_dim = len(entries) num_vectors = len(dct) tf.logging.info("Found {} out of {} vectors in Glove".format(num_vectors, len(vocab))) return [np.array(vectors).reshape(num_vectors, word_dim), dct] def buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, embedding_dim): initial_embeddings = np.random.uniform(-0.25, 0.25, (len(vocab_dict), embedding_dim)).astype("float32") for word, glove_word_idx in glove_dict.items(): word_idx = vocab_dict.get(word) initial_embeddings[word_idx, :] = glove_vectors[glove_word_idx] return initial_embeddings FLAGS = tf.flags.FLAGS def get_embeddings(hparams): if hparams.glove_path and hparams.vocab_path: tf.logging.info("Loading Glove embeddings...") vocab_array, vocab_dict = loadVOCAB(hparams.vocab_path) glove_vectors, glove_dict = loadGLOVE(hparams.glove_path, vocab=set(vocab_array)) initializer = buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, hparams.embedding_dim) else: tf.logging.info("No glove/vocab path specificed, starting with random embeddings.") initializer = tf.random_uniform_initializer(-0.25, 0.25) return tf.get_variable("word_embeddings", shape=[hparams.vocab_size, hparams.embedding_dim], initializer=initializer) class DualEncoders: def __init__(self, hparams): self.hparams = hparams self.global_step = tf.Variable(0, trainable=False, name='global_step') self.learning_rate = tf.train.exponential_decay( self.hparams.learning_rate, # Base learning rate. self.global_step, # Current index into the dataset. self.hparams.decay_step, # Decay step. self.hparams.decay_rate, # Decay rate. staircase=self.hparams.staircase, name="learning_rate_decay") self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate) self.context = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Context") self.context_len = tf.placeholder(tf.int64, [None], name="ContextLenValue") self.utterance = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Utterance") self.utterance_len = tf.placeholder(tf.int64, [None], name="UtteranceLenValue") self.targets = tf.placeholder(tf.int64, [None], name="TargetLabels") self.val_targets = tf.placeholder(tf.int64, [None], name="ValidationLabels") logits = self.inference() probs = tf.sigmoid(logits, name="probs_op") losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf.to_float(self.targets), name="CrossEntropy") mean_loss = tf.reduce_mean(losses, name="Mean_CE_Loss") train_op = tf.contrib.layers.optimize_loss(loss=mean_loss, global_step=self.global_step, learning_rate=self.learning_rate, clip_gradients=self.hparams.max_grad_norm, optimizer=hparams.optimizer) ema = tf.train.ExponentialMovingAverage(decay=0.99) mean_loss_ema_op = ema.apply([mean_loss]) with tf.control_dependencies([self.targets]): # update only when train targets passed train_op_group = tf.group(train_op, mean_loss_ema_op) self.probs_op = probs self.train_loss_op = ema.average(mean_loss) self.train_op = train_op_group self.train_summaries = tf.summary.merge([tf.summary.scalar("loss", mean_loss), tf.summary.scalar("learning_rate", self.learning_rate)]) self.val_probs, self.val_summary = self.validation_accuracy(probs, self.val_targets, mean_loss) def inference(self): W_emb = get_embeddings(self.hparams) context_emb = tf.nn.embedding_lookup(W_emb, self.context, name="ContextEmbedding") utterance_emb = tf.nn.embedding_lookup(W_emb, self.utterance, name="UtteranceEmbedding") with tf.variable_scope("BidirectionalLSTM"): argsdict = {"forget_bias": 2.0, "use_peepholes": True, "state_is_tuple": True} fw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) bw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) seq = tf.concat([context_emb, utterance_emb],axis=0) seqlen = tf.concat([self.context_len,self.utterance_len], axis=0) _, rnn_states = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, inputs=seq, sequence_length=seqlen, dtype=tf.float32) fw_encoding_context, fw_encoding_utter = tf.split(rnn_states[0].h, 2, axis=0) bw_encoding_context, bw_encoding_utter = tf.split(rnn_states[1].h, 2, axis=0) encoding_context = tf.concat([fw_encoding_context, bw_encoding_context], axis=1) encoding_utterance = tf.concat([fw_encoding_utter, bw_encoding_utter], axis=1) with tf.variable_scope("Prediction"): M = tf.get_variable(name="M", shape=[2 * self.hparams.rnn_dim, 2 * self.hparams.rnn_dim], initializer=tf.random_uniform_initializer(-0.25, 0.25)) generated_response = tf.matmul(encoding_context, M) generated_response = tf.expand_dims(generated_response, 2) encoding_utterance = tf.expand_dims(encoding_utterance, 2) logits = tf.matmul(generated_response, encoding_utterance, True) logits = tf.reshape(logits, [-1]) return logits def validation_accuracy(self, pred_labels, val_labels, val_loss): shaped_probs = tf.reshape(pred_labels, [-1, 10]) def get_top(k): return tf.reduce_mean(tf.cast(tf.nn.in_top_k(shaped_probs, val_labels, k=k), tf.float32)) ema = tf.train.ExponentialMovingAverage(decay=0.99) top1, top2, top3, top5 = [get_top(k) for k in [1, 2, 3, 5]] maintain_averages = ema.apply([top1, top2, top3, top5, val_loss]) with tf.control_dependencies([self.val_targets]): # update only when validation targets passed self.update_averages = tf.group(maintain_averages) # TODO reset shadow variables between validation sessions self.val_loss = ema.average(val_loss) self.top1_av = ema.average(top1) self.top2_av = ema.average(top2) self.top3_av = ema.average(top3) self.top5_av = ema.average(top5) val_summary = tf.summary.merge([tf.summary.scalar("validation_loss", self.val_loss), tf.summary.scalar("top1", self.top1_av), tf.summary.scalar("top2", self.top2_av), tf.summary.scalar("top3", self.top3_av), tf.summary.scalar("top5", self.top5_av), tf.summary.histogram("correct_probs_distribution", shaped_probs[:, 0]), tf.summary.histogram("incorrect_probs_distribution", shaped_probs[:, 1:])]) return shaped_probs, val_summary def setSession(self, session): self._sess = session def save_model(self, saver, location, step): saver.save(self._sess, location, global_step=step) def load_model(self, saver, location): print("Variable initializaion") init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self._sess.run(init_op) ckpt = tf.train.get_checkpoint_state(location) if ckpt and ckpt.model_checkpoint_path: print('Restoring model') saver.restore(self._sess, ckpt.model_checkpoint_path) def batch_fit(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict["utterance"], self.utterance_len: batch_dict["utterance_len"], self.targets: batch_dict["label"]} train_summary, step, _, loss = self._sess.run([self.train_summaries, self.global_step, self.train_op, self.train_loss_op], feed_dict=feed_dict) return loss, step, train_summary def predict(self, batch_dict): feed_dict = {self.context: batch_dict["context"],
dct[word] = idx
conditional_block
dual_encoder.py
vectors = array.array('d') current_idx = 0 with codecs.open(filename, "r", encoding="utf-8") as f: for _, line in enumerate(f): tokens = line.split(" ") word = tokens[0] entries = tokens[1:] if not vocab or word in vocab: dct[word] = current_idx vectors.extend(float(x) for x in entries) current_idx += 1 word_dim = len(entries) num_vectors = len(dct) tf.logging.info("Found {} out of {} vectors in Glove".format(num_vectors, len(vocab))) return [np.array(vectors).reshape(num_vectors, word_dim), dct] def buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, embedding_dim): initial_embeddings = np.random.uniform(-0.25, 0.25, (len(vocab_dict), embedding_dim)).astype("float32") for word, glove_word_idx in glove_dict.items(): word_idx = vocab_dict.get(word) initial_embeddings[word_idx, :] = glove_vectors[glove_word_idx] return initial_embeddings FLAGS = tf.flags.FLAGS def get_embeddings(hparams): if hparams.glove_path and hparams.vocab_path: tf.logging.info("Loading Glove embeddings...") vocab_array, vocab_dict = loadVOCAB(hparams.vocab_path) glove_vectors, glove_dict = loadGLOVE(hparams.glove_path, vocab=set(vocab_array)) initializer = buildEMBMatrix(vocab_dict, glove_dict, glove_vectors, hparams.embedding_dim) else: tf.logging.info("No glove/vocab path specificed, starting with random embeddings.") initializer = tf.random_uniform_initializer(-0.25, 0.25) return tf.get_variable("word_embeddings", shape=[hparams.vocab_size, hparams.embedding_dim], initializer=initializer) class DualEncoders: def __init__(self, hparams): self.hparams = hparams self.global_step = tf.Variable(0, trainable=False, name='global_step') self.learning_rate = tf.train.exponential_decay( self.hparams.learning_rate, # Base learning rate. self.global_step, # Current index into the dataset. self.hparams.decay_step, # Decay step. self.hparams.decay_rate, # Decay rate. staircase=self.hparams.staircase, name="learning_rate_decay") self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate) self.context = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Context") self.context_len = tf.placeholder(tf.int64, [None], name="ContextLenValue") self.utterance = tf.placeholder(tf.int64, [None, hparams.max_context_len], name="Utterance") self.utterance_len = tf.placeholder(tf.int64, [None], name="UtteranceLenValue") self.targets = tf.placeholder(tf.int64, [None], name="TargetLabels") self.val_targets = tf.placeholder(tf.int64, [None], name="ValidationLabels") logits = self.inference() probs = tf.sigmoid(logits, name="probs_op") losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf.to_float(self.targets), name="CrossEntropy") mean_loss = tf.reduce_mean(losses, name="Mean_CE_Loss") train_op = tf.contrib.layers.optimize_loss(loss=mean_loss, global_step=self.global_step, learning_rate=self.learning_rate, clip_gradients=self.hparams.max_grad_norm, optimizer=hparams.optimizer) ema = tf.train.ExponentialMovingAverage(decay=0.99) mean_loss_ema_op = ema.apply([mean_loss]) with tf.control_dependencies([self.targets]): # update only when train targets passed train_op_group = tf.group(train_op, mean_loss_ema_op) self.probs_op = probs self.train_loss_op = ema.average(mean_loss) self.train_op = train_op_group self.train_summaries = tf.summary.merge([tf.summary.scalar("loss", mean_loss), tf.summary.scalar("learning_rate", self.learning_rate)]) self.val_probs, self.val_summary = self.validation_accuracy(probs, self.val_targets, mean_loss) def inference(self): W_emb = get_embeddings(self.hparams) context_emb = tf.nn.embedding_lookup(W_emb, self.context, name="ContextEmbedding") utterance_emb = tf.nn.embedding_lookup(W_emb, self.utterance, name="UtteranceEmbedding") with tf.variable_scope("BidirectionalLSTM"): argsdict = {"forget_bias": 2.0, "use_peepholes": True, "state_is_tuple": True} fw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) bw_cell = tf.contrib.rnn.LSTMCell(self.hparams.rnn_dim, **argsdict) seq = tf.concat([context_emb, utterance_emb],axis=0) seqlen = tf.concat([self.context_len,self.utterance_len], axis=0) _, rnn_states = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, inputs=seq, sequence_length=seqlen, dtype=tf.float32) fw_encoding_context, fw_encoding_utter = tf.split(rnn_states[0].h, 2, axis=0) bw_encoding_context, bw_encoding_utter = tf.split(rnn_states[1].h, 2, axis=0) encoding_context = tf.concat([fw_encoding_context, bw_encoding_context], axis=1) encoding_utterance = tf.concat([fw_encoding_utter, bw_encoding_utter], axis=1) with tf.variable_scope("Prediction"): M = tf.get_variable(name="M", shape=[2 * self.hparams.rnn_dim, 2 * self.hparams.rnn_dim], initializer=tf.random_uniform_initializer(-0.25, 0.25)) generated_response = tf.matmul(encoding_context, M) generated_response = tf.expand_dims(generated_response, 2) encoding_utterance = tf.expand_dims(encoding_utterance, 2) logits = tf.matmul(generated_response, encoding_utterance, True) logits = tf.reshape(logits, [-1]) return logits def validation_accuracy(self, pred_labels, val_labels, val_loss): shaped_probs = tf.reshape(pred_labels, [-1, 10]) def get_top(k): return tf.reduce_mean(tf.cast(tf.nn.in_top_k(shaped_probs, val_labels, k=k), tf.float32)) ema = tf.train.ExponentialMovingAverage(decay=0.99) top1, top2, top3, top5 = [get_top(k) for k in [1, 2, 3, 5]] maintain_averages = ema.apply([top1, top2, top3, top5, val_loss]) with tf.control_dependencies([self.val_targets]): # update only when validation targets passed self.update_averages = tf.group(maintain_averages) # TODO reset shadow variables between validation sessions self.val_loss = ema.average(val_loss) self.top1_av = ema.average(top1) self.top2_av = ema.average(top2) self.top3_av = ema.average(top3) self.top5_av = ema.average(top5) val_summary = tf.summary.merge([tf.summary.scalar("validation_loss", self.val_loss), tf.summary.scalar("top1", self.top1_av), tf.summary.scalar("top2", self.top2_av), tf.summary.scalar("top3", self.top3_av), tf.summary.scalar("top5", self.top5_av), tf.summary.histogram("correct_probs_distribution", shaped_probs[:, 0]), tf.summary.histogram("incorrect_probs_distribution", shaped_probs[:, 1:])]) return shaped_probs, val_summary def setSession(self, session): self._sess = session def save_model(self, saver, location, step): saver.save(self._sess, location, global_step=step) def load_model(self, saver, location): print("Variable initializaion") init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self._sess.run(init_op) ckpt = tf.train.get_checkpoint_state(location) if ckpt and ckpt.model_checkpoint_path: print('Restoring model') saver.restore(self._sess, ckpt.model_checkpoint_path) def batch_fit(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict["utterance"], self.utterance_len: batch_dict["utterance_len"], self.targets: batch_dict["label"]} train_summary, step, _, loss = self._sess.run([self.train_summaries, self.global_step, self.train_op, self.train_loss_op], feed_dict=feed_dict) return loss, step, train_summary def predict(self, batch_dict): feed_dict = {self.context: batch_dict["context"], self.context_len: batch_dict["context_len"], self.utterance: batch_dict["utterance"], self.utterance_len: batch_dict["utterance_len"]} return self._sess.run([self.probs_op], feed_dict=feed_dict) def
validate
identifier_name
list_view.rs
parent_base_ref = baseref_from_parent(parent); let opts = ListViewOpts::define_ctrl_id(opts); let ctrl_id = opts.ctrl_id; let context_menu = opts.context_menu; let new_self = Self( Arc::new( Obj { base: BaseNativeControl::new(parent_base_ref), opts_id: OptsId::Wnd(opts), events: ListViewEvents::new(parent_base_ref, ctrl_id), columns: ListViewColumns::new(), items: ListViewItems::new(), context_menu, }, ), ); new_self.0.columns.set_hwnd_ref(new_self.0.base.hwnd_ref()); new_self.0.items.set_hwnd_ref(new_self.0.base.hwnd_ref()); parent_base_ref.privileged_events_ref().wm(parent_base_ref.creation_wm(), { let me = new_self.clone(); move |_| { me.create(); 0 } }); new_self.handled_events(parent_base_ref, ctrl_id); new_self } /// Instantiates a new `ListView` object, to be loaded from a dialog /// resource with [`HWND::GetDlgItem`](crate::HWND::GetDlgItem). /// /// **Note:** The optional `context_menu` is shared: it must be destroyed /// manually after the control is destroyed. But note that menus loaded from /// resources don't need to be destroyed. pub fn new_dlg( parent: &dyn Parent, ctrl_id: u16, context_menu: Option<HMENU>) -> ListView { let parent_base_ref = baseref_from_parent(parent); let new_self = Self( Arc::new( Obj { base: BaseNativeControl::new(parent_base_ref), opts_id: OptsId::Dlg(ctrl_id), events: ListViewEvents::new(parent_base_ref, ctrl_id), columns: ListViewColumns::new(), items: ListViewItems::new(), context_menu, }, ), ); new_self.0.columns.set_hwnd_ref(new_self.0.base.hwnd_ref()); new_self.0.items.set_hwnd_ref(new_self.0.base.hwnd_ref()); parent_base_ref.privileged_events_ref().wm_init_dialog({ let me = new_self.clone(); move |_| { me.create(); true } }); new_self.handled_events(parent_base_ref, ctrl_id); new_self } fn create(&self) {
match &self.0.opts_id { OptsId::Wnd(opts) => { let mut pos = opts.position; let mut sz = opts.size; multiply_dpi(Some(&mut pos), Some(&mut sz))?; self.0.base.create_window( // may panic "SysListView32", None, pos, sz, opts.ctrl_id, opts.window_ex_style, opts.window_style | opts.list_view_style.into(), )?; if opts.list_view_ex_style != co::LVS_EX::NoValue { self.toggle_extended_style(true, opts.list_view_ex_style); } self.columns().add(&opts.columns)?; Ok(()) }, OptsId::Dlg(ctrl_id) => self.0.base.create_dlg(*ctrl_id).map(|_| ()), // may panic } }().unwrap_or_else(|err| PostQuitMessage(err)) } fn handled_events(&self, parent_base_ref: &Base, ctrl_id: u16) { parent_base_ref.privileged_events_ref().add_nfy(ctrl_id, co::LVN::KEYDOWN.into(), { let me = self.clone(); move |p| { let lvnk = unsafe { p.cast_nmhdr::<NMLVKEYDOWN>() }; let has_ctrl = GetAsyncKeyState(co::VK::CONTROL); let has_shift = GetAsyncKeyState(co::VK::SHIFT); if has_ctrl && lvnk.wVKey == co::VK('A' as _) { // Ctrl+A me.items().set_selected_all(true) .unwrap_or_else(|err| PostQuitMessage(err)); } else if lvnk.wVKey == co::VK::APPS { // context menu key me.show_context_menu(false, has_ctrl, has_shift).unwrap(); } None } }); parent_base_ref.privileged_events_ref().add_nfy(ctrl_id, co::NM::RCLICK.into(), { let me = self.clone(); move |p| { let nmia = unsafe { p.cast_nmhdr::<NMITEMACTIVATE>() }; let has_ctrl = nmia.uKeyFlags.has(co::LVKF::CONTROL); let has_shift = nmia.uKeyFlags.has(co::LVKF::SHIFT); me.show_context_menu(true, has_ctrl, has_shift).unwrap(); None } }); } pub_fn_ctrlid_hwnd_on_onsubclass!(ListViewEvents); /// Exposes the column methods. pub fn columns(&self) -> &ListViewColumns { &self.0.columns } /// Returns the context menu attached to this list view, if any. /// /// The context menu is attached when the list view is created, either by /// calling [`ListView::new`](crate::gui::ListView::new) or /// [`ListView::new_dlg`](crate::gui::ListView::new_dlg). pub fn context_menu(&self) -> Option<HMENU> { self.0.context_menu } /// Retrieves one of the associated image lists by sending an /// [`LVM_GETIMAGELIST`](crate::msg::lvm::GetImageList) message. pub fn image_list(&self, kind: co::LVSIL) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::GetImageList { kind }) } /// Exposes the item methods. pub fn items(&self) -> &ListViewItems { &self.0.items } /// Retrieves the current view by sending an /// [`LVM_GETVIEW`](crate::msg::lvm::GetView) message. pub fn current_view(&self) -> co::LV_VIEW { self.hwnd().SendMessage(lvm::GetView {}) } /// Sets the current view by sending an /// [`LVM_SETVIEW`](crate::msg::lvm::SetView) message. pub fn set_current_view(&self, view: co::LV_VIEW) -> WinResult<()> { self.hwnd().SendMessage(lvm::SetView { view }) } /// Sets the one of the associated image lists by sending an /// [`LVM_SETIMAGELIST`](crate::msg::lvm::SetImageList) message. /// /// Returns the previous image list, if any. pub fn set_image_list(&self, kind: co::LVSIL, himagelist: HIMAGELIST) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::SetImageList { kind, himagelist }) } /// Toggles the given extended list view styles by sending an /// [`LVM_SETEXTENDEDLISTVIEWSTYLE`](crate::msg::lvm::SetExtendedListViewStyle) /// message. pub fn toggle_extended_style(&self, set: bool, ex_style: co::LVS_EX) { self.hwnd().SendMessage(lvm::SetExtendedListViewStyle { mask: ex_style, style: if set { ex_style } else { co::LVS_EX::NoValue }, }); } fn show_context_menu(&self, follow_cursor: bool, has_ctrl: bool, has_shift: bool) -> WinResult<()> { let hmenu = match self.0.context_menu { Some(h) => h, None => return Ok(()), // no menu, nothing to do }; let menu_pos = if follow_cursor { // usually when fired by a right-click let mut menu_pos = GetCursorPos()?; // relative to screen self.hwnd().ScreenToClient(&mut menu_pos)?; // now relative to list view let mut lvhti = LVHITTESTINFO::default(); // find item below cursor, if any lvhti.pt = menu_pos; match self.items().hit_test(&mut lvhti) { Some(idx) => { // an item was right-clicked if !has_ctrl && !has_shift { if !self.items().is_selected(idx) { self.items().set_selected_all(false)?; self.items().set_selected(true, &[idx])?; } self.items().set_focused(idx)?; } }, None => { // no item was right-clicked self.items().set_selected_all(false)?; }, } self.hwnd().SetFocus(); // because a right-click won't set the focus by itself menu_pos } else { // usually fired by the context meny key let focused_idx_opt = self.items().focused(); if focused_idx_opt.is_some() && self.items().is_visible(focused_idx_opt.unwrap()) { let focused_idx = focused_idx_opt.unwrap(); let rc_item = self.items().rect(focused_idx, co::LVIR::BOUNDS)?; POINT::new(rc_item.left + 16, rc_item.top
|| -> WinResult<()> {
random_line_split
list_view.rs
let parent_base_ref = baseref_from_parent(parent); let opts = ListViewOpts::define_ctrl_id(opts); let ctrl_id = opts.ctrl_id; let context_menu = opts.context_menu; let new_self = Self( Arc::new( Obj { base: BaseNativeControl::new(parent_base_ref), opts_id: OptsId::Wnd(opts), events: ListViewEvents::new(parent_base_ref, ctrl_id), columns: ListViewColumns::new(), items: ListViewItems::new(), context_menu, }, ), ); new_self.0.columns.set_hwnd_ref(new_self.0.base.hwnd_ref()); new_self.0.items.set_hwnd_ref(new_self.0.base.hwnd_ref()); parent_base_ref.privileged_events_ref().wm(parent_base_ref.creation_wm(), { let me = new_self.clone(); move |_| { me.create(); 0 } }); new_self.handled_events(parent_base_ref, ctrl_id); new_self } /// Instantiates a new `ListView` object, to be loaded from a dialog /// resource with [`HWND::GetDlgItem`](crate::HWND::GetDlgItem). /// /// **Note:** The optional `context_menu` is shared: it must be destroyed /// manually after the control is destroyed. But note that menus loaded from /// resources don't need to be destroyed. pub fn new_dlg( parent: &dyn Parent, ctrl_id: u16, context_menu: Option<HMENU>) -> ListView { let parent_base_ref = baseref_from_parent(parent); let new_self = Self( Arc::new( Obj { base: BaseNativeControl::new(parent_base_ref), opts_id: OptsId::Dlg(ctrl_id), events: ListViewEvents::new(parent_base_ref, ctrl_id), columns: ListViewColumns::new(), items: ListViewItems::new(), context_menu, }, ), ); new_self.0.columns.set_hwnd_ref(new_self.0.base.hwnd_ref()); new_self.0.items.set_hwnd_ref(new_self.0.base.hwnd_ref()); parent_base_ref.privileged_events_ref().wm_init_dialog({ let me = new_self.clone(); move |_| { me.create(); true } }); new_self.handled_events(parent_base_ref, ctrl_id); new_self } fn create(&self) { || -> WinResult<()> { match &self.0.opts_id { OptsId::Wnd(opts) => { let mut pos = opts.position; let mut sz = opts.size; multiply_dpi(Some(&mut pos), Some(&mut sz))?; self.0.base.create_window( // may panic "SysListView32", None, pos, sz, opts.ctrl_id, opts.window_ex_style, opts.window_style | opts.list_view_style.into(), )?; if opts.list_view_ex_style != co::LVS_EX::NoValue { self.toggle_extended_style(true, opts.list_view_ex_style); } self.columns().add(&opts.columns)?; Ok(()) }, OptsId::Dlg(ctrl_id) => self.0.base.create_dlg(*ctrl_id).map(|_| ()), // may panic } }().unwrap_or_else(|err| PostQuitMessage(err)) } fn handled_events(&self, parent_base_ref: &Base, ctrl_id: u16) { parent_base_ref.privileged_events_ref().add_nfy(ctrl_id, co::LVN::KEYDOWN.into(), { let me = self.clone(); move |p| { let lvnk = unsafe { p.cast_nmhdr::<NMLVKEYDOWN>() }; let has_ctrl = GetAsyncKeyState(co::VK::CONTROL); let has_shift = GetAsyncKeyState(co::VK::SHIFT); if has_ctrl && lvnk.wVKey == co::VK('A' as _) { // Ctrl+A me.items().set_selected_all(true) .unwrap_or_else(|err| PostQuitMessage(err)); } else if lvnk.wVKey == co::VK::APPS { // context menu key me.show_context_menu(false, has_ctrl, has_shift).unwrap(); } None } }); parent_base_ref.privileged_events_ref().add_nfy(ctrl_id, co::NM::RCLICK.into(), { let me = self.clone(); move |p| { let nmia = unsafe { p.cast_nmhdr::<NMITEMACTIVATE>() }; let has_ctrl = nmia.uKeyFlags.has(co::LVKF::CONTROL); let has_shift = nmia.uKeyFlags.has(co::LVKF::SHIFT); me.show_context_menu(true, has_ctrl, has_shift).unwrap(); None } }); } pub_fn_ctrlid_hwnd_on_onsubclass!(ListViewEvents); /// Exposes the column methods. pub fn columns(&self) -> &ListViewColumns { &self.0.columns } /// Returns the context menu attached to this list view, if any. /// /// The context menu is attached when the list view is created, either by /// calling [`ListView::new`](crate::gui::ListView::new) or /// [`ListView::new_dlg`](crate::gui::ListView::new_dlg). pub fn context_menu(&self) -> Option<HMENU> { self.0.context_menu } /// Retrieves one of the associated image lists by sending an /// [`LVM_GETIMAGELIST`](crate::msg::lvm::GetImageList) message. pub fn image_list(&self, kind: co::LVSIL) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::GetImageList { kind }) } /// Exposes the item methods. pub fn items(&self) -> &ListViewItems { &self.0.items } /// Retrieves the current view by sending an /// [`LVM_GETVIEW`](crate::msg::lvm::GetView) message. pub fn current_view(&self) -> co::LV_VIEW { self.hwnd().SendMessage(lvm::GetView {}) } /// Sets the current view by sending an /// [`LVM_SETVIEW`](crate::msg::lvm::SetView) message. pub fn set_current_view(&self, view: co::LV_VIEW) -> WinResult<()> { self.hwnd().SendMessage(lvm::SetView { view }) } /// Sets the one of the associated image lists by sending an /// [`LVM_SETIMAGELIST`](crate::msg::lvm::SetImageList) message. /// /// Returns the previous image list, if any. pub fn set_image_list(&self, kind: co::LVSIL, himagelist: HIMAGELIST) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::SetImageList { kind, himagelist }) } /// Toggles the given extended list view styles by sending an /// [`LVM_SETEXTENDEDLISTVIEWSTYLE`](crate::msg::lvm::SetExtendedListViewStyle) /// message. pub fn
(&self, set: bool, ex_style: co::LVS_EX) { self.hwnd().SendMessage(lvm::SetExtendedListViewStyle { mask: ex_style, style: if set { ex_style } else { co::LVS_EX::NoValue }, }); } fn show_context_menu(&self, follow_cursor: bool, has_ctrl: bool, has_shift: bool) -> WinResult<()> { let hmenu = match self.0.context_menu { Some(h) => h, None => return Ok(()), // no menu, nothing to do }; let menu_pos = if follow_cursor { // usually when fired by a right-click let mut menu_pos = GetCursorPos()?; // relative to screen self.hwnd().ScreenToClient(&mut menu_pos)?; // now relative to list view let mut lvhti = LVHITTESTINFO::default(); // find item below cursor, if any lvhti.pt = menu_pos; match self.items().hit_test(&mut lvhti) { Some(idx) => { // an item was right-clicked if !has_ctrl && !has_shift { if !self.items().is_selected(idx) { self.items().set_selected_all(false)?; self.items().set_selected(true, &[idx])?; } self.items().set_focused(idx)?; } }, None => { // no item was right-clicked self.items().set_selected_all(false)?; }, } self.hwnd().SetFocus(); // because a right-click won't set the focus by itself menu_pos } else { // usually fired by the context meny key let focused_idx_opt = self.items().focused(); if focused_idx_opt.is_some() && self.items().is_visible(focused_idx_opt.unwrap()) { let focused_idx = focused_idx_opt.unwrap(); let rc_item = self.items().rect(focused_idx, co::LVIR::BOUNDS)?; POINT::new(rc_item.left + 16, rc_item.top
toggle_extended_style
identifier_name
list_view.rs
.unwrap_or_else(|err| PostQuitMessage(err)); } else if lvnk.wVKey == co::VK::APPS { // context menu key me.show_context_menu(false, has_ctrl, has_shift).unwrap(); } None } }); parent_base_ref.privileged_events_ref().add_nfy(ctrl_id, co::NM::RCLICK.into(), { let me = self.clone(); move |p| { let nmia = unsafe { p.cast_nmhdr::<NMITEMACTIVATE>() }; let has_ctrl = nmia.uKeyFlags.has(co::LVKF::CONTROL); let has_shift = nmia.uKeyFlags.has(co::LVKF::SHIFT); me.show_context_menu(true, has_ctrl, has_shift).unwrap(); None } }); } pub_fn_ctrlid_hwnd_on_onsubclass!(ListViewEvents); /// Exposes the column methods. pub fn columns(&self) -> &ListViewColumns { &self.0.columns } /// Returns the context menu attached to this list view, if any. /// /// The context menu is attached when the list view is created, either by /// calling [`ListView::new`](crate::gui::ListView::new) or /// [`ListView::new_dlg`](crate::gui::ListView::new_dlg). pub fn context_menu(&self) -> Option<HMENU> { self.0.context_menu } /// Retrieves one of the associated image lists by sending an /// [`LVM_GETIMAGELIST`](crate::msg::lvm::GetImageList) message. pub fn image_list(&self, kind: co::LVSIL) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::GetImageList { kind }) } /// Exposes the item methods. pub fn items(&self) -> &ListViewItems { &self.0.items } /// Retrieves the current view by sending an /// [`LVM_GETVIEW`](crate::msg::lvm::GetView) message. pub fn current_view(&self) -> co::LV_VIEW { self.hwnd().SendMessage(lvm::GetView {}) } /// Sets the current view by sending an /// [`LVM_SETVIEW`](crate::msg::lvm::SetView) message. pub fn set_current_view(&self, view: co::LV_VIEW) -> WinResult<()> { self.hwnd().SendMessage(lvm::SetView { view }) } /// Sets the one of the associated image lists by sending an /// [`LVM_SETIMAGELIST`](crate::msg::lvm::SetImageList) message. /// /// Returns the previous image list, if any. pub fn set_image_list(&self, kind: co::LVSIL, himagelist: HIMAGELIST) -> Option<HIMAGELIST> { self.hwnd().SendMessage(lvm::SetImageList { kind, himagelist }) } /// Toggles the given extended list view styles by sending an /// [`LVM_SETEXTENDEDLISTVIEWSTYLE`](crate::msg::lvm::SetExtendedListViewStyle) /// message. pub fn toggle_extended_style(&self, set: bool, ex_style: co::LVS_EX) { self.hwnd().SendMessage(lvm::SetExtendedListViewStyle { mask: ex_style, style: if set { ex_style } else { co::LVS_EX::NoValue }, }); } fn show_context_menu(&self, follow_cursor: bool, has_ctrl: bool, has_shift: bool) -> WinResult<()> { let hmenu = match self.0.context_menu { Some(h) => h, None => return Ok(()), // no menu, nothing to do }; let menu_pos = if follow_cursor { // usually when fired by a right-click let mut menu_pos = GetCursorPos()?; // relative to screen self.hwnd().ScreenToClient(&mut menu_pos)?; // now relative to list view let mut lvhti = LVHITTESTINFO::default(); // find item below cursor, if any lvhti.pt = menu_pos; match self.items().hit_test(&mut lvhti) { Some(idx) => { // an item was right-clicked if !has_ctrl && !has_shift { if !self.items().is_selected(idx) { self.items().set_selected_all(false)?; self.items().set_selected(true, &[idx])?; } self.items().set_focused(idx)?; } }, None => { // no item was right-clicked self.items().set_selected_all(false)?; }, } self.hwnd().SetFocus(); // because a right-click won't set the focus by itself menu_pos } else { // usually fired by the context meny key let focused_idx_opt = self.items().focused(); if focused_idx_opt.is_some() && self.items().is_visible(focused_idx_opt.unwrap()) { let focused_idx = focused_idx_opt.unwrap(); let rc_item = self.items().rect(focused_idx, co::LVIR::BOUNDS)?; POINT::new(rc_item.left + 16, rc_item.top + (rc_item.bottom - rc_item.top) / 2) } else { // no item is focused and visible POINT::new(6, 10) // arbitrary } }; hmenu.TrackPopupMenuAtPoint( menu_pos, self.hwnd().GetParent()?, self.hwnd()) } } //------------------------------------------------------------------------------ /// Options to create a [`ListView`](crate::gui::ListView) programmatically with /// [`ListView::new`](crate::gui::ListView::new). pub struct ListViewOpts { /// Control position within parent client area, in pixels, to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Will be adjusted to match current system DPI. /// /// Defaults to 0 x 0. pub position: POINT, /// Control size, in pixels, to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Will be adjusted to match current system DPI. /// /// Defaults to 50 x 50. pub size: SIZE, /// List view styles to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Defaults to `LVS::REPORT | LVS::NOSORTHEADER | LVS::SHOWSELALWAYS | LVS::SHAREIMAGELISTS`. pub list_view_style: co::LVS, /// Extended list view styles to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Defaults to `LVS_EX::NoValue`. pub list_view_ex_style: co::LVS_EX, /// Window styles to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Defaults to `WS::CHILD | WS::VISIBLE | WS::TABSTOP | WS::GROUP`. pub window_style: co::WS, /// Extended window styles to be /// [created](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw). /// /// Defaults to `WS_EX::LEFT | WS_EX::CLIENTEDGE`. pub window_ex_style: co::WS_EX, /// The control ID. /// /// Defaults to an auto-generated ID. pub ctrl_id: u16, /// Context popup menu. /// /// This menu is shared: it must be destroyed manually after the control is /// destroyed. But note that menus loaded from resources don't need to be /// destroyed. /// /// Defaults to `None`. pub context_menu: Option<HMENU>, /// Text and width of columns to be added right away. The columns only show /// in report mode. /// /// Defaults to none. pub columns: Vec<(String, u32)>, } impl Default for ListViewOpts { fn default() -> Self { Self { position: POINT::new(0, 0), size: SIZE::new(50, 50), list_view_style: co::LVS::REPORT | co::LVS::NOSORTHEADER | co::LVS::SHOWSELALWAYS | co::LVS::SHAREIMAGELISTS, list_view_ex_style: co::LVS_EX::NoValue, window_style: co::WS::CHILD | co::WS::VISIBLE | co::WS::TABSTOP | co::WS::GROUP, window_ex_style: co::WS_EX::LEFT | co::WS_EX::CLIENTEDGE, ctrl_id: 0, context_menu: None, columns: Vec::default(), } } } impl ListViewOpts { fn define_ctrl_id(mut self) -> Self { if self.ctrl_id == 0
{ self.ctrl_id = auto_ctrl_id(); }
conditional_block
main.rs
32, String) { let mut score = 0; let mut mutations = "".to_string(); let mut n = 1; for (i, j) in seq1.chars().zip(seq2.chars()) { if i != j { score += 1; if score == 1 { mutations = mutations + &format!("{}{}", n, i); } else { mutations = mutations + &format!(" {}{}", n, i); } } n += 1; } (score, mutations) } fn reverse_complement(seq: &str) -> String { seq.chars() .map(|t| match t { 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G', _ => 'N', }).rev().collect::<String>() } fn qual_check(a: &[u8], b: &[u8]) -> bool { for (i, j) in a.iter().zip(b.iter()) { if i < j { continue; } return false; } return true } fn data_stat(results: &HashMap<String, (String, Vec<u8>)>, output_file: &str) -> Result<String, Box<Error>> { // statistics on the datasets let wt_pac = "AGAGAAGATTTATCTGAAGTCGTTACGCGAG"; let mut diff_counts : [usize; 31] = [0; 31]; let mut diff_freq : [usize; 31] = [0; 31]; let mut output = try!(File::create(output_file)); let mut pac_stat = HashMap::new(); for (_, pac_info) in results { let ref pac = pac_info.0; let ref qual = pac_info.1; // mutation statistics let mut index = 0; let mut distance = 0; for (i, j) in pac.chars().zip(wt_pac.chars()) { if qual[index] > 63 && i != j { diff_freq[index] += 1; distance += 1; } index += 1; } diff_counts[distance] += 1; if distance > 8 { println!("# {} {}", distance, pac); } // pac sites statistics if pac_stat.contains_key(pac) { *pac_stat.get_mut(pac).unwrap() += 1; } else { pac_stat.insert(pac, 1); } } println!("# Overall statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_counts[i]); } println!("# Per-base statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_freq[i]); } //try!(write!(output, "{}", "# pac counts:\n")); for (pac, counts) in &pac_stat { try!(write!(output, "{} {} {}\n", pac, hamming(&pac, &wt_pac), counts)); } Ok("Done".into()) } fn
() { let args : Vec<String> = env::args().collect(); let file1 = fastq::Reader::from_file(&args[1]).unwrap(); let file2 = fastq::Reader::from_file(&args[2]).unwrap(); let mut num_records = 0; let mut num_duplicates = 0; let mut num_qual_skip = 0; let mut results : HashMap<String, (String, Vec<u8>)>= HashMap::new(); let wt_read1 = if &args[3] == "M" {b"ACTAAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAA"} else {b"AAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAATAC"}; for (record1, record2) in file1.records().zip(file2.records()) { // take read1, filter low quality reads let read1 = record1.unwrap(); let desc = read1.id().unwrap().split(":").skip(5).collect::<Vec<&str>>(); let description = desc[0].to_string() + ":" + desc[1]; let mut trim = 124; let mut am = " ".to_string(); for i in 0..120 { if qual_check(&read1.qual()[i .. i+5], &[63, 63, 63, 63, 63]) { trim = i+1; println!("# {} {}: Read 1 trimmed at {}.", num_records, description, trim); break; } } if trim < 18 { println!("# {}: Useful read too short. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // check if the read is the right read let seq1 = String::from_utf8_lossy(&read1.seq()[0 .. trim]); let score = |a: u8, b: u8| if a == b {1i32} else {-1i32}; let mut aligner = Aligner::with_capacity(seq1.len(), wt_read1.len(), -5, -1, &score); let alignment = aligner.global(&seq1[8..seq1.len()].as_bytes(), wt_read1); if alignment.score < (2 * trim as i32 - 133 - 30) { println!("# {} {}: wrong read 1 skipping", num_records, description); println!("# {} {}", &seq1[8..seq1.len()], alignment.score); num_records += 1; num_qual_skip += 1; continue; } // identifying AM/WT if &args[3] == "M" { if trim < 33 { println!("# {}: Useful read too short for M. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // Allowing 1 mismatch if hamming(&seq1[27 .. 32], "GCGGC") < 2 { match &seq1[32 .. 33] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 1 am_codon = {}", &seq1[27 .. 33]); } if am == " " { for i in 0 .. trim-6 { if &seq1[i .. i+5] == "GCGGC" { match &seq1[i+5 .. i+6] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 2 am_codon = {}", &seq1[i .. i+6]); break; } } } } // average quality filtering //let avg_qual = read1.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < (125 * 30) { // corresponding to an average quality of 20 // println!("# low quality read 1 skipping: {}", avg_qual); // continue; //} // now deal with read2 let read2 = record2.unwrap(); // average quality filtering //let avg_qual = read2.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < 125*30 { // println!("# {}: low quality read 2 skipping: {}", num_records, avg_qual); // num_qual_skip += 1; // continue; //} trim = 124; for i in 0..119 { if qual_check(&read2.qual()[i .. i+5], &[63, 63, 63, 63, 63]) { trim = i+1; println!("# {} {}: Read 2 trimmed at {}.", num_records, description, i); break; } } if trim < 80 { println!("# {}: Useful read too short. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } let seq2 =String::from_utf8_lossy(&read2.seq()[0 .. trim]); // extract barcodes let bc1 = &seq1[0
main
identifier_name
main.rs
// distance += 1; // } // } // else { // distance += 1; // } // index += 1; // } // (distance, dif) // } fn hamming(seq1: &str, seq2: &str) -> u32 { let mut score = 0; for (i, j) in seq1.chars().zip(seq2.chars()) { if i != j { score += 1; } } score } fn ham_mutations(seq1: &str, seq2: &str) -> (u32, String) { let mut score = 0; let mut mutations = "".to_string(); let mut n = 1; for (i, j) in seq1.chars().zip(seq2.chars()) { if i != j { score += 1; if score == 1 { mutations = mutations + &format!("{}{}", n, i); } else { mutations = mutations + &format!(" {}{}", n, i); } } n += 1; } (score, mutations) } fn reverse_complement(seq: &str) -> String { seq.chars() .map(|t| match t { 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G', _ => 'N', }).rev().collect::<String>() } fn qual_check(a: &[u8], b: &[u8]) -> bool { for (i, j) in a.iter().zip(b.iter()) { if i < j { continue; } return false; } return true } fn data_stat(results: &HashMap<String, (String, Vec<u8>)>, output_file: &str) -> Result<String, Box<Error>> { // statistics on the datasets let wt_pac = "AGAGAAGATTTATCTGAAGTCGTTACGCGAG"; let mut diff_counts : [usize; 31] = [0; 31]; let mut diff_freq : [usize; 31] = [0; 31]; let mut output = try!(File::create(output_file)); let mut pac_stat = HashMap::new(); for (_, pac_info) in results { let ref pac = pac_info.0; let ref qual = pac_info.1; // mutation statistics let mut index = 0; let mut distance = 0; for (i, j) in pac.chars().zip(wt_pac.chars()) { if qual[index] > 63 && i != j { diff_freq[index] += 1; distance += 1; } index += 1; } diff_counts[distance] += 1; if distance > 8 { println!("# {} {}", distance, pac); } // pac sites statistics if pac_stat.contains_key(pac) { *pac_stat.get_mut(pac).unwrap() += 1; } else { pac_stat.insert(pac, 1); } } println!("# Overall statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_counts[i]); } println!("# Per-base statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_freq[i]); } //try!(write!(output, "{}", "# pac counts:\n")); for (pac, counts) in &pac_stat { try!(write!(output, "{} {} {}\n", pac, hamming(&pac, &wt_pac), counts)); } Ok("Done".into()) } fn main() { let args : Vec<String> = env::args().collect(); let file1 = fastq::Reader::from_file(&args[1]).unwrap(); let file2 = fastq::Reader::from_file(&args[2]).unwrap(); let mut num_records = 0; let mut num_duplicates = 0; let mut num_qual_skip = 0; let mut results : HashMap<String, (String, Vec<u8>)>= HashMap::new(); let wt_read1 = if &args[3] == "M" {b"ACTAAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAA"} else {b"AAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAATAC"}; for (record1, record2) in file1.records().zip(file2.records()) { // take read1, filter low quality reads let read1 = record1.unwrap(); let desc = read1.id().unwrap().split(":").skip(5).collect::<Vec<&str>>(); let description = desc[0].to_string() + ":" + desc[1]; let mut trim = 124; let mut am = " ".to_string(); for i in 0..120 { if qual_check(&read1.qual()[i .. i+5], &[63, 63, 63, 63, 63]) { trim = i+1; println!("# {} {}: Read 1 trimmed at {}.", num_records, description, trim); break; } } if trim < 18 { println!("# {}: Useful read too short. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // check if the read is the right read let seq1 = String::from_utf8_lossy(&read1.seq()[0 .. trim]); let score = |a: u8, b: u8| if a == b {1i32} else {-1i32}; let mut aligner = Aligner::with_capacity(seq1.len(), wt_read1.len(), -5, -1, &score); let alignment = aligner.global(&seq1[8..seq1.len()].as_bytes(), wt_read1); if alignment.score < (2 * trim as i32 - 133 - 30) { println!("# {} {}: wrong read 1 skipping", num_records, description); println!("# {} {}", &seq1[8..seq1.len()], alignment.score); num_records += 1; num_qual_skip += 1; continue; } // identifying AM/WT if &args[3] == "M" { if trim < 33 { println!("# {}: Useful read too short for M. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // Allowing 1 mismatch if hamming(&seq1[27 .. 32], "GCGGC") < 2 { match &seq1[32 .. 33] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 1 am_codon = {}", &seq1[27 .. 33]); } if am == " " { for i in 0 .. trim-6 { if &seq1[i .. i+5] == "GCGGC" { match &seq1[i+5 .. i+6] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 2 am_codon = {}", &seq1[i .. i+6]); break; } } } } // average quality filtering //let avg_qual = read1.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < (125 * 30) { // corresponding to an average quality of 20 // println!("# low quality read 1 skipping: {}", avg_qual); // continue; //} // now deal with read2 let read2 = record2.unwrap(); // average quality filtering //let avg_qual = read2.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < 125*30 { // println!("# {}: low quality read 2 skipping: {}", num_records, avg_qual); // num_qual_skip += 1; // continue; //} trim = 124; for i in 0..119 { if qual_check(&read2.qual
// if i != j { // dif.push((index, i, j));
random_line_split
main.rs
fn ham_mutations(seq1: &str, seq2: &str) -> (u32, String) { let mut score = 0; let mut mutations = "".to_string(); let mut n = 1; for (i, j) in seq1.chars().zip(seq2.chars()) { if i != j { score += 1; if score == 1 { mutations = mutations + &format!("{}{}", n, i); } else { mutations = mutations + &format!(" {}{}", n, i); } } n += 1; } (score, mutations) } fn reverse_complement(seq: &str) -> String { seq.chars() .map(|t| match t { 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G', _ => 'N', }).rev().collect::<String>() } fn qual_check(a: &[u8], b: &[u8]) -> bool { for (i, j) in a.iter().zip(b.iter()) { if i < j { continue; } return false; } return true } fn data_stat(results: &HashMap<String, (String, Vec<u8>)>, output_file: &str) -> Result<String, Box<Error>> { // statistics on the datasets let wt_pac = "AGAGAAGATTTATCTGAAGTCGTTACGCGAG"; let mut diff_counts : [usize; 31] = [0; 31]; let mut diff_freq : [usize; 31] = [0; 31]; let mut output = try!(File::create(output_file)); let mut pac_stat = HashMap::new(); for (_, pac_info) in results { let ref pac = pac_info.0; let ref qual = pac_info.1; // mutation statistics let mut index = 0; let mut distance = 0; for (i, j) in pac.chars().zip(wt_pac.chars()) { if qual[index] > 63 && i != j { diff_freq[index] += 1; distance += 1; } index += 1; } diff_counts[distance] += 1; if distance > 8 { println!("# {} {}", distance, pac); } // pac sites statistics if pac_stat.contains_key(pac) { *pac_stat.get_mut(pac).unwrap() += 1; } else { pac_stat.insert(pac, 1); } } println!("# Overall statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_counts[i]); } println!("# Per-base statistics:"); for i in 0..31 { println!("# {}\t{}", i, diff_freq[i]); } //try!(write!(output, "{}", "# pac counts:\n")); for (pac, counts) in &pac_stat { try!(write!(output, "{} {} {}\n", pac, hamming(&pac, &wt_pac), counts)); } Ok("Done".into()) } fn main() { let args : Vec<String> = env::args().collect(); let file1 = fastq::Reader::from_file(&args[1]).unwrap(); let file2 = fastq::Reader::from_file(&args[2]).unwrap(); let mut num_records = 0; let mut num_duplicates = 0; let mut num_qual_skip = 0; let mut results : HashMap<String, (String, Vec<u8>)>= HashMap::new(); let wt_read1 = if &args[3] == "M" {b"ACTAAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAA"} else {b"AAGTGAGATGAATATGGCGGCACCAAAGGGCAACCGATTTTGGGAGGCCCGCAGTAGTCATGGGCGAAATCCTAAATTCGAATCGCCTGAGGCGCTGTGGGCTGCTTGTTGTGAATAC"}; for (record1, record2) in file1.records().zip(file2.records()) { // take read1, filter low quality reads let read1 = record1.unwrap(); let desc = read1.id().unwrap().split(":").skip(5).collect::<Vec<&str>>(); let description = desc[0].to_string() + ":" + desc[1]; let mut trim = 124; let mut am = " ".to_string(); for i in 0..120 { if qual_check(&read1.qual()[i .. i+5], &[63, 63, 63, 63, 63]) { trim = i+1; println!("# {} {}: Read 1 trimmed at {}.", num_records, description, trim); break; } } if trim < 18 { println!("# {}: Useful read too short. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // check if the read is the right read let seq1 = String::from_utf8_lossy(&read1.seq()[0 .. trim]); let score = |a: u8, b: u8| if a == b {1i32} else {-1i32}; let mut aligner = Aligner::with_capacity(seq1.len(), wt_read1.len(), -5, -1, &score); let alignment = aligner.global(&seq1[8..seq1.len()].as_bytes(), wt_read1); if alignment.score < (2 * trim as i32 - 133 - 30) { println!("# {} {}: wrong read 1 skipping", num_records, description); println!("# {} {}", &seq1[8..seq1.len()], alignment.score); num_records += 1; num_qual_skip += 1; continue; } // identifying AM/WT if &args[3] == "M" { if trim < 33 { println!("# {}: Useful read too short for M. Skipping. L = {}", num_records, trim); num_qual_skip += 1; num_records += 1; continue; } // Allowing 1 mismatch if hamming(&seq1[27 .. 32], "GCGGC") < 2 { match &seq1[32 .. 33] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 1 am_codon = {}", &seq1[27 .. 33]); } if am == " " { for i in 0 .. trim-6 { if &seq1[i .. i+5] == "GCGGC" { match &seq1[i+5 .. i+6] { "A" => am = "WT".to_string(), "G" => am = "AM".to_string(), _ => am = " ".to_string(), } println!("# 2 am_codon = {}", &seq1[i .. i+6]); break; } } } } // average quality filtering //let avg_qual = read1.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < (125 * 30) { // corresponding to an average quality of 20 // println!("# low quality read 1 skipping: {}", avg_qual); // continue; //} // now deal with read2 let read2 = record2.unwrap(); // average quality filtering //let avg_qual = read2.qual().iter().fold(0, |a, &b| a as u32 + b as u32); //if avg_qual < 125*30 { // println!("# {}: low quality read 2 skipping: {}", num_records, avg_qual); // num_qual_skip += 1; // continue; //} trim = 124; for i in 0..119 { if qual_check(&read2.qual()[i .. i+5], &[63, 63, 63, 63, 63]) { trim = i+1; println!("# {} {}: Read 2 trimmed at {}.", num_records, description, i); break; } } if trim < 80 { println!("# {}: Useful read too short. Skipping.
{ let mut score = 0; for (i, j) in seq1.chars().zip(seq2.chars()) { if i != j { score += 1; } } score }
identifier_body
get.go
]string Vars []string } // Clone clones the config options func (co *ConfigOptions) Clone() (*ConfigOptions, error) { out, err := yaml.Marshal(co) if err != nil { return nil, err } newCo := &ConfigOptions{} err = yaml.Unmarshal(out, newCo) if err != nil { return nil, err } return newCo, nil } // GetBaseConfig returns the config func GetBaseConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, false) } // GetConfig returns the config merged with all potential overwrite files func GetConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, true) } // GetRawConfig loads the raw config from a given path func GetRawConfig(configPath string) (map[interface{}]interface{}, error) { fileContent, err := ioutil.ReadFile(configPath) if err != nil { return nil, err } rawMap := map[interface{}]interface{}{} err = yaml.Unmarshal(fileContent, &rawMap) if err != nil { return nil, err } return rawMap, nil } // GetConfigFromPath loads the config from a given base path func GetConfigFromPath(generatedConfig *generated.Config, basePath string, options *ConfigOptions, log log.Logger) (*latest.Config, error) { if options == nil { options = &ConfigOptions{} } configPath := filepath.Join(basePath, constants.DefaultConfigPath) // Check devspace.yaml _, err := os.Stat(configPath) if err != nil { // Check for legacy devspace-configs.yaml _, configErr := os.Stat(filepath.Join(basePath, constants.DefaultConfigsPath)) if configErr == nil { return nil, errors.Errorf("devspace-configs.yaml is not supported anymore in devspace v4. Please use 'profiles' in 'devspace.yaml' instead") } return nil, errors.Errorf("Couldn't find '%s': %v", configPath, err) } rawMap, err := GetRawConfig(configPath) if err != nil { return nil, err } loadedConfig, err := ParseConfig(generatedConfig, rawMap, options, log) if err != nil { return nil, err } // Now we validate the config err = validate(loadedConfig) if err != nil { return nil, err } return loadedConfig, nil } // loadConfigOnce loads the config globally once func loadConfigOnce(options *ConfigOptions, allowProfile bool) (*latest.Config, error) { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { if options == nil { options = &ConfigOptions{} } // Get generated config generatedConfig, err := generated.LoadConfig(options.Profile) if err != nil { getConfigOnceErr = err return } // Check if we should load a specific config if allowProfile && generatedConfig.ActiveProfile != "" && options.Profile == "" { options.Profile = generatedConfig.ActiveProfile } else if !allowProfile { options.Profile = "" } // Set loaded vars for this options.LoadedVars = LoadedVars // Load base config config, err = GetConfigFromPath(generatedConfig, ".", options, log.GetInstance()) if err != nil { getConfigOnceErr = err return } // Save generated config err = generated.SaveConfig(generatedConfig) if err != nil { getConfigOnceErr = err return } }) return config, getConfigOnceErr } func validate(config *latest.Config) error { if config.Dev != nil { if config.Dev.Ports != nil { for index, port := range config.Dev.Ports { if port.ImageName == "" && port.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in port config at index %d", index) } if port.PortMappings == nil { return errors.Errorf("Error in config: portMappings is empty in port config at index %d", index) } } } if config.Dev.Sync != nil { for index, sync := range config.Dev.Sync { if sync.ImageName == "" && sync.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in sync config at index %d", index) } } } if config.Dev.Interactive != nil { for index, imageConf := range config.Dev.Interactive.Images { if imageConf.Name == "" { return errors.Errorf("Error in config: Unnamed interactive image config at index %d", index) } } } } if config.Commands != nil { for index, command := range config.Commands { if command.Name == "" { return errors.Errorf("commands[%d].name is required", index) } if command.Command == "" { return errors.Errorf("commands[%d].command is required", index) } } } if config.Hooks != nil { for index, hookConfig := range config.Hooks { if hookConfig.Command == "" { return errors.Errorf("hooks[%d].command is required", index) } } } if config.Images != nil { for imageConfigName, imageConf := range config.Images { if imageConfigName == "" { return errors.Errorf("images keys cannot be an empty string") } if imageConf.Image == "" { return errors.Errorf("images.%s.image is required", imageConfigName) } if imageConf.Build != nil && imageConf.Build.Custom != nil && imageConf.Build.Custom.Command == "" { return errors.Errorf("images.%s.build.custom.command is required", imageConfigName) } if imageConf.Image == "" { return fmt.Errorf("images.%s.image is required", imageConfigName) } } } if config.Deployments != nil { for index, deployConfig := range config.Deployments { if deployConfig.Name == "" { return errors.Errorf("deployments[%d].name is required", index) } if deployConfig.Helm == nil && deployConfig.Kubectl == nil { return errors.Errorf("Please specify either helm or kubectl as deployment type in deployment %s", deployConfig.Name) } if deployConfig.Helm != nil && (deployConfig.Helm.Chart == nil || deployConfig.Helm.Chart.Name == "") && (deployConfig.Helm.ComponentChart == nil || *deployConfig.Helm.ComponentChart == false) { return errors.Errorf("deployments[%d].helm.chart and deployments[%d].helm.chart.name or deployments[%d].helm.componentChart is required", index, index, index) } if deployConfig.Kubectl != nil && deployConfig.Kubectl.Manifests == nil { return errors.Errorf("deployments[%d].kubectl.manifests is required", index) } if deployConfig.Helm != nil && deployConfig.Helm.ComponentChart != nil && *deployConfig.Helm.ComponentChart == true { // Load override values from path overwriteValues := map[interface{}]interface{}{} if deployConfig.Helm.ValuesFiles != nil { for _, overridePath := range deployConfig.Helm.ValuesFiles { overwriteValuesPath, err := filepath.Abs(overridePath) if err != nil { return errors.Errorf("deployments[%d].helm.valuesFiles: Error retrieving absolute path from %s: %v", index, overridePath, err) } overwriteValuesFromPath := map[interface{}]interface{}{} err = yamlutil.ReadYamlFromFile(overwriteValuesPath, overwriteValuesFromPath) if err == nil { merge.Values(overwriteValues).MergeInto(overwriteValuesFromPath) } } } // Load override values from data and merge them if deployConfig.Helm.Values != nil { merge.Values(overwriteValues).MergeInto(deployConfig.Helm.Values) } bytes, err := yaml.Marshal(overwriteValues) if err != nil { return errors.Errorf("deployments[%d].helm: Error marshaling overwrite values: %v", index, err) } componentValues := &latest.ComponentConfig{} err = yaml.UnmarshalStrict(bytes, componentValues) if err != nil { return errors.Errorf("deployments[%d].helm.componentChart: component values are incorrect: %v", index, err) } } } } return nil } // SetDevSpaceRoot checks the current directory and all parent directories for a .devspace folder with a config and sets the current working directory accordingly func SetDevSpaceRoot(log log.Logger) (bool, error)
{ cwd, err := os.Getwd() if err != nil { return false, err } originalCwd := cwd homedir, err := homedir.Dir() if err != nil { return false, err } lastLength := 0 for len(cwd) != lastLength { if cwd != homedir { configExists := configExistsInPath(cwd) if configExists { // Change working directory err = os.Chdir(cwd) if err != nil {
identifier_body
get.go
, newCo) if err != nil { return nil, err } return newCo, nil } // GetBaseConfig returns the config func GetBaseConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, false) } // GetConfig returns the config merged with all potential overwrite files func GetConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, true) } // GetRawConfig loads the raw config from a given path func GetRawConfig(configPath string) (map[interface{}]interface{}, error) { fileContent, err := ioutil.ReadFile(configPath) if err != nil { return nil, err } rawMap := map[interface{}]interface{}{} err = yaml.Unmarshal(fileContent, &rawMap) if err != nil { return nil, err } return rawMap, nil } // GetConfigFromPath loads the config from a given base path func GetConfigFromPath(generatedConfig *generated.Config, basePath string, options *ConfigOptions, log log.Logger) (*latest.Config, error) { if options == nil { options = &ConfigOptions{} } configPath := filepath.Join(basePath, constants.DefaultConfigPath) // Check devspace.yaml _, err := os.Stat(configPath) if err != nil { // Check for legacy devspace-configs.yaml _, configErr := os.Stat(filepath.Join(basePath, constants.DefaultConfigsPath)) if configErr == nil { return nil, errors.Errorf("devspace-configs.yaml is not supported anymore in devspace v4. Please use 'profiles' in 'devspace.yaml' instead") } return nil, errors.Errorf("Couldn't find '%s': %v", configPath, err) } rawMap, err := GetRawConfig(configPath) if err != nil { return nil, err } loadedConfig, err := ParseConfig(generatedConfig, rawMap, options, log) if err != nil { return nil, err } // Now we validate the config err = validate(loadedConfig) if err != nil { return nil, err } return loadedConfig, nil } // loadConfigOnce loads the config globally once func loadConfigOnce(options *ConfigOptions, allowProfile bool) (*latest.Config, error) { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { if options == nil { options = &ConfigOptions{} } // Get generated config generatedConfig, err := generated.LoadConfig(options.Profile) if err != nil { getConfigOnceErr = err return } // Check if we should load a specific config if allowProfile && generatedConfig.ActiveProfile != "" && options.Profile == "" { options.Profile = generatedConfig.ActiveProfile } else if !allowProfile { options.Profile = "" } // Set loaded vars for this options.LoadedVars = LoadedVars // Load base config config, err = GetConfigFromPath(generatedConfig, ".", options, log.GetInstance()) if err != nil { getConfigOnceErr = err return } // Save generated config err = generated.SaveConfig(generatedConfig) if err != nil { getConfigOnceErr = err return } }) return config, getConfigOnceErr } func validate(config *latest.Config) error { if config.Dev != nil { if config.Dev.Ports != nil { for index, port := range config.Dev.Ports { if port.ImageName == "" && port.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in port config at index %d", index) } if port.PortMappings == nil { return errors.Errorf("Error in config: portMappings is empty in port config at index %d", index) } } } if config.Dev.Sync != nil { for index, sync := range config.Dev.Sync { if sync.ImageName == "" && sync.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in sync config at index %d", index) } } } if config.Dev.Interactive != nil { for index, imageConf := range config.Dev.Interactive.Images { if imageConf.Name == "" { return errors.Errorf("Error in config: Unnamed interactive image config at index %d", index) } } } } if config.Commands != nil { for index, command := range config.Commands { if command.Name == "" { return errors.Errorf("commands[%d].name is required", index) } if command.Command == "" { return errors.Errorf("commands[%d].command is required", index) } } } if config.Hooks != nil { for index, hookConfig := range config.Hooks { if hookConfig.Command == "" { return errors.Errorf("hooks[%d].command is required", index) } } } if config.Images != nil { for imageConfigName, imageConf := range config.Images { if imageConfigName == "" { return errors.Errorf("images keys cannot be an empty string") } if imageConf.Image == "" { return errors.Errorf("images.%s.image is required", imageConfigName) } if imageConf.Build != nil && imageConf.Build.Custom != nil && imageConf.Build.Custom.Command == "" { return errors.Errorf("images.%s.build.custom.command is required", imageConfigName) } if imageConf.Image == "" { return fmt.Errorf("images.%s.image is required", imageConfigName) } } } if config.Deployments != nil { for index, deployConfig := range config.Deployments { if deployConfig.Name == "" { return errors.Errorf("deployments[%d].name is required", index) } if deployConfig.Helm == nil && deployConfig.Kubectl == nil { return errors.Errorf("Please specify either helm or kubectl as deployment type in deployment %s", deployConfig.Name) } if deployConfig.Helm != nil && (deployConfig.Helm.Chart == nil || deployConfig.Helm.Chart.Name == "") && (deployConfig.Helm.ComponentChart == nil || *deployConfig.Helm.ComponentChart == false) { return errors.Errorf("deployments[%d].helm.chart and deployments[%d].helm.chart.name or deployments[%d].helm.componentChart is required", index, index, index) } if deployConfig.Kubectl != nil && deployConfig.Kubectl.Manifests == nil { return errors.Errorf("deployments[%d].kubectl.manifests is required", index) } if deployConfig.Helm != nil && deployConfig.Helm.ComponentChart != nil && *deployConfig.Helm.ComponentChart == true { // Load override values from path overwriteValues := map[interface{}]interface{}{} if deployConfig.Helm.ValuesFiles != nil { for _, overridePath := range deployConfig.Helm.ValuesFiles { overwriteValuesPath, err := filepath.Abs(overridePath) if err != nil { return errors.Errorf("deployments[%d].helm.valuesFiles: Error retrieving absolute path from %s: %v", index, overridePath, err) } overwriteValuesFromPath := map[interface{}]interface{}{} err = yamlutil.ReadYamlFromFile(overwriteValuesPath, overwriteValuesFromPath) if err == nil { merge.Values(overwriteValues).MergeInto(overwriteValuesFromPath) } } } // Load override values from data and merge them if deployConfig.Helm.Values != nil { merge.Values(overwriteValues).MergeInto(deployConfig.Helm.Values) } bytes, err := yaml.Marshal(overwriteValues) if err != nil { return errors.Errorf("deployments[%d].helm: Error marshaling overwrite values: %v", index, err) } componentValues := &latest.ComponentConfig{} err = yaml.UnmarshalStrict(bytes, componentValues) if err != nil { return errors.Errorf("deployments[%d].helm.componentChart: component values are incorrect: %v", index, err) } } } } return nil } // SetDevSpaceRoot checks the current directory and all parent directories for a .devspace folder with a config and sets the current working directory accordingly func SetDevSpaceRoot(log log.Logger) (bool, error) { cwd, err := os.Getwd() if err != nil { return false, err } originalCwd := cwd homedir, err := homedir.Dir() if err != nil { return false, err } lastLength := 0 for len(cwd) != lastLength { if cwd != homedir
{ configExists := configExistsInPath(cwd) if configExists { // Change working directory err = os.Chdir(cwd) if err != nil { return false, err } // Notify user that we are not using the current working directory if originalCwd != cwd { log.Infof("Using devspace config in %s", filepath.ToSlash(cwd)) } return true, nil } }
conditional_block
get.go
var getConfigOnce sync.Once var getConfigOnceErr error var getConfigOnceMutex sync.Mutex // ConfigExists checks whether the yaml file for the config exists or the configs.yaml exists func ConfigExists() bool { return configExistsInPath(".") } // configExistsInPath checks wheter a devspace configuration exists at a certain path func configExistsInPath(path string) bool { // Needed for testing if config != nil { return true } // Check devspace.yaml _, err := os.Stat(filepath.Join(path, constants.DefaultConfigPath)) if err == nil { return true } // Check devspace-configs.yaml _, err = os.Stat(filepath.Join(path, constants.DefaultConfigsPath)) if err == nil { return true } return false // Normal config file found } // ResetConfig resets the current config func ResetConfig() { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce = sync.Once{} getConfigOnceErr = nil } // InitConfig initializes the config objects func InitConfig() *latest.Config { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { config = latest.New().(*latest.Config) }) return config } // ConfigOptions defines options to load the config type ConfigOptions struct { Profile string KubeContext string LoadedVars map[string]string Vars []string } // Clone clones the config options func (co *ConfigOptions) Clone() (*ConfigOptions, error) { out, err := yaml.Marshal(co) if err != nil { return nil, err } newCo := &ConfigOptions{} err = yaml.Unmarshal(out, newCo) if err != nil { return nil, err } return newCo, nil } // GetBaseConfig returns the config func GetBaseConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, false) } // GetConfig returns the config merged with all potential overwrite files func GetConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, true) } // GetRawConfig loads the raw config from a given path func GetRawConfig(configPath string) (map[interface{}]interface{}, error) { fileContent, err := ioutil.ReadFile(configPath) if err != nil { return nil, err } rawMap := map[interface{}]interface{}{} err = yaml.Unmarshal(fileContent, &rawMap) if err != nil { return nil, err } return rawMap, nil } // GetConfigFromPath loads the config from a given base path func
(generatedConfig *generated.Config, basePath string, options *ConfigOptions, log log.Logger) (*latest.Config, error) { if options == nil { options = &ConfigOptions{} } configPath := filepath.Join(basePath, constants.DefaultConfigPath) // Check devspace.yaml _, err := os.Stat(configPath) if err != nil { // Check for legacy devspace-configs.yaml _, configErr := os.Stat(filepath.Join(basePath, constants.DefaultConfigsPath)) if configErr == nil { return nil, errors.Errorf("devspace-configs.yaml is not supported anymore in devspace v4. Please use 'profiles' in 'devspace.yaml' instead") } return nil, errors.Errorf("Couldn't find '%s': %v", configPath, err) } rawMap, err := GetRawConfig(configPath) if err != nil { return nil, err } loadedConfig, err := ParseConfig(generatedConfig, rawMap, options, log) if err != nil { return nil, err } // Now we validate the config err = validate(loadedConfig) if err != nil { return nil, err } return loadedConfig, nil } // loadConfigOnce loads the config globally once func loadConfigOnce(options *ConfigOptions, allowProfile bool) (*latest.Config, error) { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { if options == nil { options = &ConfigOptions{} } // Get generated config generatedConfig, err := generated.LoadConfig(options.Profile) if err != nil { getConfigOnceErr = err return } // Check if we should load a specific config if allowProfile && generatedConfig.ActiveProfile != "" && options.Profile == "" { options.Profile = generatedConfig.ActiveProfile } else if !allowProfile { options.Profile = "" } // Set loaded vars for this options.LoadedVars = LoadedVars // Load base config config, err = GetConfigFromPath(generatedConfig, ".", options, log.GetInstance()) if err != nil { getConfigOnceErr = err return } // Save generated config err = generated.SaveConfig(generatedConfig) if err != nil { getConfigOnceErr = err return } }) return config, getConfigOnceErr } func validate(config *latest.Config) error { if config.Dev != nil { if config.Dev.Ports != nil { for index, port := range config.Dev.Ports { if port.ImageName == "" && port.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in port config at index %d", index) } if port.PortMappings == nil { return errors.Errorf("Error in config: portMappings is empty in port config at index %d", index) } } } if config.Dev.Sync != nil { for index, sync := range config.Dev.Sync { if sync.ImageName == "" && sync.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in sync config at index %d", index) } } } if config.Dev.Interactive != nil { for index, imageConf := range config.Dev.Interactive.Images { if imageConf.Name == "" { return errors.Errorf("Error in config: Unnamed interactive image config at index %d", index) } } } } if config.Commands != nil { for index, command := range config.Commands { if command.Name == "" { return errors.Errorf("commands[%d].name is required", index) } if command.Command == "" { return errors.Errorf("commands[%d].command is required", index) } } } if config.Hooks != nil { for index, hookConfig := range config.Hooks { if hookConfig.Command == "" { return errors.Errorf("hooks[%d].command is required", index) } } } if config.Images != nil { for imageConfigName, imageConf := range config.Images { if imageConfigName == "" { return errors.Errorf("images keys cannot be an empty string") } if imageConf.Image == "" { return errors.Errorf("images.%s.image is required", imageConfigName) } if imageConf.Build != nil && imageConf.Build.Custom != nil && imageConf.Build.Custom.Command == "" { return errors.Errorf("images.%s.build.custom.command is required", imageConfigName) } if imageConf.Image == "" { return fmt.Errorf("images.%s.image is required", imageConfigName) } } } if config.Deployments != nil { for index, deployConfig := range config.Deployments { if deployConfig.Name == "" { return errors.Errorf("deployments[%d].name is required", index) } if deployConfig.Helm == nil && deployConfig.Kubectl == nil { return errors.Errorf("Please specify either helm or kubectl as deployment type in deployment %s", deployConfig.Name) } if deployConfig.Helm != nil && (deployConfig.Helm.Chart == nil || deployConfig.Helm.Chart.Name == "") && (deployConfig.Helm.ComponentChart == nil || *deployConfig.Helm.ComponentChart == false) { return errors.Errorf("deployments[%d].helm.chart and deployments[%d].helm.chart.name or deployments[%d].helm.componentChart is required", index, index, index) } if deployConfig.Kubectl != nil && deployConfig.Kubectl.Manifests == nil { return errors.Errorf("deployments[%d].kubectl.manifests is required", index) } if deployConfig.Helm != nil && deployConfig.Helm.ComponentChart != nil && *deployConfig.Helm.ComponentChart == true { // Load override values from path overwriteValues := map[interface{}]interface{}{} if deployConfig.Helm.ValuesFiles != nil { for _, overridePath := range deployConfig.Helm.ValuesFiles { overwriteValuesPath, err := filepath.Abs(overridePath) if err != nil { return errors.Errorf("deployments[%d].helm.valuesFiles: Error retrieving absolute path from %s: %v", index, overridePath, err) } overwriteValuesFromPath := map[interface{}]interface{}{} err = yamlutil.ReadYamlFromFile(overwriteValuesPath, overwriteValuesFromPath) if err == nil { merge.Values(overwriteValues).MergeInto(overwriteValuesFromPath) } } } // Load override values from data and merge them if deployConfig.Helm
GetConfigFromPath
identifier_name
get.go
var getConfigOnce sync.Once var getConfigOnceErr error var getConfigOnceMutex sync.Mutex // ConfigExists checks whether the yaml file for the config exists or the configs.yaml exists func ConfigExists() bool { return configExistsInPath(".") } // configExistsInPath checks wheter a devspace configuration exists at a certain path func configExistsInPath(path string) bool { // Needed for testing if config != nil { return true } // Check devspace.yaml _, err := os.Stat(filepath.Join(path, constants.DefaultConfigPath)) if err == nil { return true } // Check devspace-configs.yaml _, err = os.Stat(filepath.Join(path, constants.DefaultConfigsPath)) if err == nil { return true } return false // Normal config file found } // ResetConfig resets the current config func ResetConfig() { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce = sync.Once{} getConfigOnceErr = nil } // InitConfig initializes the config objects func InitConfig() *latest.Config { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { config = latest.New().(*latest.Config) }) return config } // ConfigOptions defines options to load the config type ConfigOptions struct { Profile string KubeContext string LoadedVars map[string]string Vars []string } // Clone clones the config options func (co *ConfigOptions) Clone() (*ConfigOptions, error) { out, err := yaml.Marshal(co) if err != nil { return nil, err } newCo := &ConfigOptions{} err = yaml.Unmarshal(out, newCo) if err != nil { return nil, err } return newCo, nil } // GetBaseConfig returns the config func GetBaseConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, false) } // GetConfig returns the config merged with all potential overwrite files func GetConfig(options *ConfigOptions) (*latest.Config, error) { return loadConfigOnce(options, true) } // GetRawConfig loads the raw config from a given path
if err != nil { return nil, err } rawMap := map[interface{}]interface{}{} err = yaml.Unmarshal(fileContent, &rawMap) if err != nil { return nil, err } return rawMap, nil } // GetConfigFromPath loads the config from a given base path func GetConfigFromPath(generatedConfig *generated.Config, basePath string, options *ConfigOptions, log log.Logger) (*latest.Config, error) { if options == nil { options = &ConfigOptions{} } configPath := filepath.Join(basePath, constants.DefaultConfigPath) // Check devspace.yaml _, err := os.Stat(configPath) if err != nil { // Check for legacy devspace-configs.yaml _, configErr := os.Stat(filepath.Join(basePath, constants.DefaultConfigsPath)) if configErr == nil { return nil, errors.Errorf("devspace-configs.yaml is not supported anymore in devspace v4. Please use 'profiles' in 'devspace.yaml' instead") } return nil, errors.Errorf("Couldn't find '%s': %v", configPath, err) } rawMap, err := GetRawConfig(configPath) if err != nil { return nil, err } loadedConfig, err := ParseConfig(generatedConfig, rawMap, options, log) if err != nil { return nil, err } // Now we validate the config err = validate(loadedConfig) if err != nil { return nil, err } return loadedConfig, nil } // loadConfigOnce loads the config globally once func loadConfigOnce(options *ConfigOptions, allowProfile bool) (*latest.Config, error) { getConfigOnceMutex.Lock() defer getConfigOnceMutex.Unlock() getConfigOnce.Do(func() { if options == nil { options = &ConfigOptions{} } // Get generated config generatedConfig, err := generated.LoadConfig(options.Profile) if err != nil { getConfigOnceErr = err return } // Check if we should load a specific config if allowProfile && generatedConfig.ActiveProfile != "" && options.Profile == "" { options.Profile = generatedConfig.ActiveProfile } else if !allowProfile { options.Profile = "" } // Set loaded vars for this options.LoadedVars = LoadedVars // Load base config config, err = GetConfigFromPath(generatedConfig, ".", options, log.GetInstance()) if err != nil { getConfigOnceErr = err return } // Save generated config err = generated.SaveConfig(generatedConfig) if err != nil { getConfigOnceErr = err return } }) return config, getConfigOnceErr } func validate(config *latest.Config) error { if config.Dev != nil { if config.Dev.Ports != nil { for index, port := range config.Dev.Ports { if port.ImageName == "" && port.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in port config at index %d", index) } if port.PortMappings == nil { return errors.Errorf("Error in config: portMappings is empty in port config at index %d", index) } } } if config.Dev.Sync != nil { for index, sync := range config.Dev.Sync { if sync.ImageName == "" && sync.LabelSelector == nil { return errors.Errorf("Error in config: imageName and label selector are nil in sync config at index %d", index) } } } if config.Dev.Interactive != nil { for index, imageConf := range config.Dev.Interactive.Images { if imageConf.Name == "" { return errors.Errorf("Error in config: Unnamed interactive image config at index %d", index) } } } } if config.Commands != nil { for index, command := range config.Commands { if command.Name == "" { return errors.Errorf("commands[%d].name is required", index) } if command.Command == "" { return errors.Errorf("commands[%d].command is required", index) } } } if config.Hooks != nil { for index, hookConfig := range config.Hooks { if hookConfig.Command == "" { return errors.Errorf("hooks[%d].command is required", index) } } } if config.Images != nil { for imageConfigName, imageConf := range config.Images { if imageConfigName == "" { return errors.Errorf("images keys cannot be an empty string") } if imageConf.Image == "" { return errors.Errorf("images.%s.image is required", imageConfigName) } if imageConf.Build != nil && imageConf.Build.Custom != nil && imageConf.Build.Custom.Command == "" { return errors.Errorf("images.%s.build.custom.command is required", imageConfigName) } if imageConf.Image == "" { return fmt.Errorf("images.%s.image is required", imageConfigName) } } } if config.Deployments != nil { for index, deployConfig := range config.Deployments { if deployConfig.Name == "" { return errors.Errorf("deployments[%d].name is required", index) } if deployConfig.Helm == nil && deployConfig.Kubectl == nil { return errors.Errorf("Please specify either helm or kubectl as deployment type in deployment %s", deployConfig.Name) } if deployConfig.Helm != nil && (deployConfig.Helm.Chart == nil || deployConfig.Helm.Chart.Name == "") && (deployConfig.Helm.ComponentChart == nil || *deployConfig.Helm.ComponentChart == false) { return errors.Errorf("deployments[%d].helm.chart and deployments[%d].helm.chart.name or deployments[%d].helm.componentChart is required", index, index, index) } if deployConfig.Kubectl != nil && deployConfig.Kubectl.Manifests == nil { return errors.Errorf("deployments[%d].kubectl.manifests is required", index) } if deployConfig.Helm != nil && deployConfig.Helm.ComponentChart != nil && *deployConfig.Helm.ComponentChart == true { // Load override values from path overwriteValues := map[interface{}]interface{}{} if deployConfig.Helm.ValuesFiles != nil { for _, overridePath := range deployConfig.Helm.ValuesFiles { overwriteValuesPath, err := filepath.Abs(overridePath) if err != nil { return errors.Errorf("deployments[%d].helm.valuesFiles: Error retrieving absolute path from %s: %v", index, overridePath, err) } overwriteValuesFromPath := map[interface{}]interface{}{} err = yamlutil.ReadYamlFromFile(overwriteValuesPath, overwriteValuesFromPath) if err == nil { merge.Values(overwriteValues).MergeInto(overwriteValuesFromPath) } } } // Load override values from data and merge them if deployConfig.Helm.Values !=
func GetRawConfig(configPath string) (map[interface{}]interface{}, error) { fileContent, err := ioutil.ReadFile(configPath)
random_line_split
charm.py
ATION_NAME = "legend-db" LEGEND_GITLAB_RELATION_NAME = "legend-sdlc-gitlab" LEGEND_STUDIO_RELATION_NAME = "legend-sdlc" SDLC_SERVICE_URL_FORMAT = "%(schema)s://%(host)s:%(port)s%(path)s" SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH = "/sdlc-config.yaml" SDLC_MAIN_GITLAB_REDIRECT_URL = "%(base_url)s/auth/callback" SDLC_GITLAB_REDIRECT_URI_FORMATS = [ SDLC_MAIN_GITLAB_REDIRECT_URL, "%(base_url)s/pac4j/login/callback", ] TRUSTSTORE_PASSPHRASE = "Legend SDLC" TRUSTSTORE_CONTAINER_LOCAL_PATH = "/truststore.jks" APPLICATION_CONNECTOR_PORT_HTTP = 7070 APPLICATION_ADMIN_CONNECTOR_PORT_HTTP = 7076 APPLICATION_ROOT_PATH = "/api" APPLICATION_LOGGING_FORMAT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c - %m%n" GITLAB_PROJECT_VISIBILITY_PUBLIC = "public" GITLAB_PROJECT_VISIBILITY_PRIVATE = "private" GITLAB_REQUIRED_SCOPES = ["openid", "profile", "api"] class LegendSDLCServerCharm(legend_operator_base.BaseFinosLegendCoreServiceCharm): """Charmed operator for the FINOS Legend SDLC Server.""" def __init__(self, *args): super().__init__(*args) # Studio relation events: self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_joined, self._on_studio_relation_joined ) self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_changed, self._on_studio_relation_changed ) @classmethod def _get_application_connector_port(cls): return APPLICATION_CONNECTOR_PORT_HTTP @classmethod def _get_workload_container_name(cls): return SDLC_CONTAINER_NAME @classmethod def _get_workload_service_names(cls): return [SDLC_SERVICE_NAME] @classmethod def _get_workload_pebble_layers(cls): return { "sdlc": { "summary": "SDLC layer.", "description": "Pebble config layer for FINOS Legend SDLC.", "services": { "sdlc": { "override": "replace", "summary": "sdlc", "command": ( # NOTE(aznashwan): starting through bash is needed # for the classpath glob (-cp ...) to be expanded: "/bin/sh -c 'java -XX:+ExitOnOutOfMemoryError " "-XX:MaxRAMPercentage=60 -Xss4M -cp /app/bin/*.jar" " -Dfile.encoding=UTF8 " '-Djavax.net.ssl.trustStore="%s" ' '-Djavax.net.ssl.trustStorePassword="%s" ' "org.finos.legend.sdlc.server.LegendSDLCServer " 'server "%s"\'' % ( TRUSTSTORE_CONTAINER_LOCAL_PATH, TRUSTSTORE_PASSPHRASE, SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH, ) ), # NOTE(aznashwan): considering the SDLC service expects # a singular config file which already contains all # relevant options in it (some of which will require # the relation with DB/Gitlab to have already been # established), we do not auto-start: "startup": "disabled", # TODO(aznashwan): determine any env vars we could pass # (most notably, things like the RAM percentage etc...) "environment": {}, } }, } } def _get_jks_truststore_preferences(self): jks_prefs = { "truststore_path": TRUSTSTORE_CONTAINER_LOCAL_PATH, "truststore_passphrase": TRUSTSTORE_PASSPHRASE, "trusted_certificates": {}, } cert = self._get_legend_gitlab_certificate() if cert: # NOTE(aznashwan): cert label 'gitlab-sdlc' is arbitrary: jks_prefs["trusted_certificates"]["gitlab-sdlc"] = cert return jks_prefs @classmethod def _get_legend_gitlab_relation_name(cls): return LEGEND_GITLAB_RELATION_NAME @classmethod def _get_legend_db_relation_name(cls): return LEGEND_DB_RELATION_NAME def _get_sdlc_service_url(self): ip_address = legend_operator_base.get_ip_address() return SDLC_SERVICE_URL_FORMAT % ( { # NOTE(aznashwan): we always return the plain HTTP endpoint: "schema": "http", "host": ip_address, "port": APPLICATION_CONNECTOR_PORT_HTTP, "path": APPLICATION_ROOT_PATH, } ) def _get_legend_gitlab_redirect_uris(self): base_url = self._get_sdlc_service_url() redirect_uris = [fmt % {"base_url": base_url} for fmt in SDLC_GITLAB_REDIRECT_URI_FORMATS] return redirect_uris def _get_core_legend_service_configs(self, legend_db_credentials, legend_gitlab_credentials): # Check DB-related options: if not legend_db_credentials:
legend_db_uri = legend_db_credentials["uri"] legend_db = legend_db_credentials["database"] # Check gitlab-related options: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PRIVATE if self.model.config["gitlab-create-new-projects-as-public"]: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PUBLIC if not legend_gitlab_credentials: return model.WaitingStatus("no legend gitlab info present in relation yet") gitlab_client_id = legend_gitlab_credentials["client_id"] gitlab_client_secret = legend_gitlab_credentials["client_secret"] gitlab_openid_discovery_url = legend_gitlab_credentials["openid_discovery_url"] gitlab_project_tag = self.model.config["gitlab-project-tag"] gitlab_project_creation_group_pattern = self.model.config[ "gitlab-project-creation-group-pattern" ] # Check Java logging options: request_logging_level = self._get_logging_level_from_config( "server-requests-logging-level" ) server_logging_level = self._get_logging_level_from_config("server-logging-level") if not all([server_logging_level, request_logging_level]): return model.BlockedStatus( "one or more logging config options are improperly formatted " "or missing, please review the debug-log for more details" ) # Compile base config: sdlc_config = { "applicationName": "Legend SDLC", "server": { "rootPath": APPLICATION_ROOT_PATH, "applicationConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_CONNECTOR_PORT_HTTP, "maxRequestHeaderSize": "128KiB", } ], "adminConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_ADMIN_CONNECTOR_PORT_HTTP, } ], "gzip": {"includedMethods": ["GET", "POST"]}, "requestLog": { "type": "classic", "level": request_logging_level, "appenders": [{"type": "console", "logFormat": "OFF"}], }, }, "filterPriorities": { "GitLab": 1, "org.pac4j.j2e.filter.CallbackFilter": 2, "org.pac4j.j2e.filter.SecurityFilter": 3, "CORS": 4, }, "pac4j": { "callbackPrefix": "/api/pac4j/login", "mongoUri": legend_db_uri, "mongoDb": legend_db, "clients": [ { "org.finos.legend.server.pac4j.gitlab.GitlabClient": { "name": "gitlab", "clientId": gitlab_client_id, "secret": gitlab_client_secret, "discoveryUri": gitlab_openid_discovery_url, # NOTE(aznashwan): needs to be a space-separated str: "scope": " ".join(GITLAB_REQUIRED_SCOPES), } } ], "mongoSession": {"enabled": True, "collection": "userSessions"}, "bypassPaths": ["/api/info"], }, "gitLab": { "newProjectVisibility": gitlab_project_visibility, "projectTag": gitlab_project_tag, "uat": { "server": { "scheme": legend_gitlab_credentials["gitlab_scheme"], "host": "%s:%s" % ( legend_gitlab_credentials["gitlab_host"], legend_gitlab_credentials["gitlab_port"], ), }, "app": { "id": gitlab_client_id, "secret": gitlab_client_secret, "redirectURI": ( SDLC_MAIN_GITLAB_REDIRECT_URL % {"base_url": self._get_sdlc_service_url()} ), }, }, }, "projectStructure": { "projectCreation": {"groupIdPattern": gitlab_project_creation_group_pattern}, "extensionProvider": { "org.finos.legend.sdlc.server.gitlab.finos." "FinosGitlabProjectStructureExtensionProvider": {}
return model.WaitingStatus("no legend db info present in relation yet")
conditional_block
charm.py
j/login/callback", ] TRUSTSTORE_PASSPHRASE = "Legend SDLC" TRUSTSTORE_CONTAINER_LOCAL_PATH = "/truststore.jks" APPLICATION_CONNECTOR_PORT_HTTP = 7070 APPLICATION_ADMIN_CONNECTOR_PORT_HTTP = 7076 APPLICATION_ROOT_PATH = "/api" APPLICATION_LOGGING_FORMAT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c - %m%n" GITLAB_PROJECT_VISIBILITY_PUBLIC = "public" GITLAB_PROJECT_VISIBILITY_PRIVATE = "private" GITLAB_REQUIRED_SCOPES = ["openid", "profile", "api"] class LegendSDLCServerCharm(legend_operator_base.BaseFinosLegendCoreServiceCharm): """Charmed operator for the FINOS Legend SDLC Server.""" def __init__(self, *args): super().__init__(*args) # Studio relation events: self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_joined, self._on_studio_relation_joined ) self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_changed, self._on_studio_relation_changed ) @classmethod def _get_application_connector_port(cls): return APPLICATION_CONNECTOR_PORT_HTTP @classmethod def _get_workload_container_name(cls): return SDLC_CONTAINER_NAME @classmethod def _get_workload_service_names(cls): return [SDLC_SERVICE_NAME] @classmethod def _get_workload_pebble_layers(cls): return { "sdlc": { "summary": "SDLC layer.", "description": "Pebble config layer for FINOS Legend SDLC.", "services": { "sdlc": { "override": "replace", "summary": "sdlc", "command": ( # NOTE(aznashwan): starting through bash is needed # for the classpath glob (-cp ...) to be expanded: "/bin/sh -c 'java -XX:+ExitOnOutOfMemoryError " "-XX:MaxRAMPercentage=60 -Xss4M -cp /app/bin/*.jar" " -Dfile.encoding=UTF8 " '-Djavax.net.ssl.trustStore="%s" ' '-Djavax.net.ssl.trustStorePassword="%s" ' "org.finos.legend.sdlc.server.LegendSDLCServer " 'server "%s"\'' % ( TRUSTSTORE_CONTAINER_LOCAL_PATH, TRUSTSTORE_PASSPHRASE, SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH, ) ), # NOTE(aznashwan): considering the SDLC service expects # a singular config file which already contains all # relevant options in it (some of which will require # the relation with DB/Gitlab to have already been # established), we do not auto-start: "startup": "disabled", # TODO(aznashwan): determine any env vars we could pass # (most notably, things like the RAM percentage etc...) "environment": {}, } }, } } def _get_jks_truststore_preferences(self): jks_prefs = { "truststore_path": TRUSTSTORE_CONTAINER_LOCAL_PATH, "truststore_passphrase": TRUSTSTORE_PASSPHRASE, "trusted_certificates": {}, } cert = self._get_legend_gitlab_certificate() if cert: # NOTE(aznashwan): cert label 'gitlab-sdlc' is arbitrary: jks_prefs["trusted_certificates"]["gitlab-sdlc"] = cert return jks_prefs @classmethod def _get_legend_gitlab_relation_name(cls): return LEGEND_GITLAB_RELATION_NAME @classmethod def _get_legend_db_relation_name(cls): return LEGEND_DB_RELATION_NAME def _get_sdlc_service_url(self): ip_address = legend_operator_base.get_ip_address() return SDLC_SERVICE_URL_FORMAT % ( { # NOTE(aznashwan): we always return the plain HTTP endpoint: "schema": "http", "host": ip_address, "port": APPLICATION_CONNECTOR_PORT_HTTP, "path": APPLICATION_ROOT_PATH, } ) def _get_legend_gitlab_redirect_uris(self): base_url = self._get_sdlc_service_url() redirect_uris = [fmt % {"base_url": base_url} for fmt in SDLC_GITLAB_REDIRECT_URI_FORMATS] return redirect_uris def _get_core_legend_service_configs(self, legend_db_credentials, legend_gitlab_credentials): # Check DB-related options: if not legend_db_credentials: return model.WaitingStatus("no legend db info present in relation yet") legend_db_uri = legend_db_credentials["uri"] legend_db = legend_db_credentials["database"] # Check gitlab-related options: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PRIVATE if self.model.config["gitlab-create-new-projects-as-public"]: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PUBLIC if not legend_gitlab_credentials: return model.WaitingStatus("no legend gitlab info present in relation yet") gitlab_client_id = legend_gitlab_credentials["client_id"] gitlab_client_secret = legend_gitlab_credentials["client_secret"] gitlab_openid_discovery_url = legend_gitlab_credentials["openid_discovery_url"] gitlab_project_tag = self.model.config["gitlab-project-tag"] gitlab_project_creation_group_pattern = self.model.config[ "gitlab-project-creation-group-pattern" ] # Check Java logging options: request_logging_level = self._get_logging_level_from_config( "server-requests-logging-level" ) server_logging_level = self._get_logging_level_from_config("server-logging-level") if not all([server_logging_level, request_logging_level]): return model.BlockedStatus( "one or more logging config options are improperly formatted " "or missing, please review the debug-log for more details" ) # Compile base config: sdlc_config = { "applicationName": "Legend SDLC", "server": { "rootPath": APPLICATION_ROOT_PATH, "applicationConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_CONNECTOR_PORT_HTTP, "maxRequestHeaderSize": "128KiB", } ], "adminConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_ADMIN_CONNECTOR_PORT_HTTP, } ], "gzip": {"includedMethods": ["GET", "POST"]}, "requestLog": { "type": "classic", "level": request_logging_level, "appenders": [{"type": "console", "logFormat": "OFF"}], }, }, "filterPriorities": { "GitLab": 1, "org.pac4j.j2e.filter.CallbackFilter": 2, "org.pac4j.j2e.filter.SecurityFilter": 3, "CORS": 4, }, "pac4j": { "callbackPrefix": "/api/pac4j/login", "mongoUri": legend_db_uri, "mongoDb": legend_db, "clients": [ { "org.finos.legend.server.pac4j.gitlab.GitlabClient": { "name": "gitlab", "clientId": gitlab_client_id, "secret": gitlab_client_secret, "discoveryUri": gitlab_openid_discovery_url, # NOTE(aznashwan): needs to be a space-separated str: "scope": " ".join(GITLAB_REQUIRED_SCOPES), } } ], "mongoSession": {"enabled": True, "collection": "userSessions"}, "bypassPaths": ["/api/info"], }, "gitLab": { "newProjectVisibility": gitlab_project_visibility, "projectTag": gitlab_project_tag, "uat": { "server": { "scheme": legend_gitlab_credentials["gitlab_scheme"], "host": "%s:%s" % ( legend_gitlab_credentials["gitlab_host"], legend_gitlab_credentials["gitlab_port"], ), }, "app": { "id": gitlab_client_id, "secret": gitlab_client_secret, "redirectURI": ( SDLC_MAIN_GITLAB_REDIRECT_URL % {"base_url": self._get_sdlc_service_url()} ), }, }, }, "projectStructure": { "projectCreation": {"groupIdPattern": gitlab_project_creation_group_pattern}, "extensionProvider": { "org.finos.legend.sdlc.server.gitlab.finos." "FinosGitlabProjectStructureExtensionProvider": {} }, }, "logging": { "level": server_logging_level, "appenders": [ { "type": "console", "logFormat": APPLICATION_LOGGING_FORMAT, } ], }, "swagger": { "title": "Legend SDLC", "resourcePackage": "org.finos.legend.sdlc.server.resources", "version": "local-snapshot", "schemes": [], }, } return {SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH: yaml.dump(sdlc_config)} def
_on_studio_relation_joined
identifier_name
charm.py
_RELATION_NAME = "legend-db" LEGEND_GITLAB_RELATION_NAME = "legend-sdlc-gitlab" LEGEND_STUDIO_RELATION_NAME = "legend-sdlc" SDLC_SERVICE_URL_FORMAT = "%(schema)s://%(host)s:%(port)s%(path)s" SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH = "/sdlc-config.yaml" SDLC_MAIN_GITLAB_REDIRECT_URL = "%(base_url)s/auth/callback" SDLC_GITLAB_REDIRECT_URI_FORMATS = [ SDLC_MAIN_GITLAB_REDIRECT_URL, "%(base_url)s/pac4j/login/callback", ] TRUSTSTORE_PASSPHRASE = "Legend SDLC" TRUSTSTORE_CONTAINER_LOCAL_PATH = "/truststore.jks" APPLICATION_CONNECTOR_PORT_HTTP = 7070 APPLICATION_ADMIN_CONNECTOR_PORT_HTTP = 7076 APPLICATION_ROOT_PATH = "/api" APPLICATION_LOGGING_FORMAT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c - %m%n" GITLAB_PROJECT_VISIBILITY_PUBLIC = "public" GITLAB_PROJECT_VISIBILITY_PRIVATE = "private" GITLAB_REQUIRED_SCOPES = ["openid", "profile", "api"] class LegendSDLCServerCharm(legend_operator_base.BaseFinosLegendCoreServiceCharm): """Charmed operator for the FINOS Legend SDLC Server.""" def __init__(self, *args): super().__init__(*args) # Studio relation events: self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_joined, self._on_studio_relation_joined ) self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_changed, self._on_studio_relation_changed ) @classmethod def _get_application_connector_port(cls): return APPLICATION_CONNECTOR_PORT_HTTP @classmethod def _get_workload_container_name(cls): return SDLC_CONTAINER_NAME @classmethod
def _get_workload_service_names(cls): return [SDLC_SERVICE_NAME] @classmethod def _get_workload_pebble_layers(cls): return { "sdlc": { "summary": "SDLC layer.", "description": "Pebble config layer for FINOS Legend SDLC.", "services": { "sdlc": { "override": "replace", "summary": "sdlc", "command": ( # NOTE(aznashwan): starting through bash is needed # for the classpath glob (-cp ...) to be expanded: "/bin/sh -c 'java -XX:+ExitOnOutOfMemoryError " "-XX:MaxRAMPercentage=60 -Xss4M -cp /app/bin/*.jar" " -Dfile.encoding=UTF8 " '-Djavax.net.ssl.trustStore="%s" ' '-Djavax.net.ssl.trustStorePassword="%s" ' "org.finos.legend.sdlc.server.LegendSDLCServer " 'server "%s"\'' % ( TRUSTSTORE_CONTAINER_LOCAL_PATH, TRUSTSTORE_PASSPHRASE, SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH, ) ), # NOTE(aznashwan): considering the SDLC service expects # a singular config file which already contains all # relevant options in it (some of which will require # the relation with DB/Gitlab to have already been # established), we do not auto-start: "startup": "disabled", # TODO(aznashwan): determine any env vars we could pass # (most notably, things like the RAM percentage etc...) "environment": {}, } }, } } def _get_jks_truststore_preferences(self): jks_prefs = { "truststore_path": TRUSTSTORE_CONTAINER_LOCAL_PATH, "truststore_passphrase": TRUSTSTORE_PASSPHRASE, "trusted_certificates": {}, } cert = self._get_legend_gitlab_certificate() if cert: # NOTE(aznashwan): cert label 'gitlab-sdlc' is arbitrary: jks_prefs["trusted_certificates"]["gitlab-sdlc"] = cert return jks_prefs @classmethod def _get_legend_gitlab_relation_name(cls): return LEGEND_GITLAB_RELATION_NAME @classmethod def _get_legend_db_relation_name(cls): return LEGEND_DB_RELATION_NAME def _get_sdlc_service_url(self): ip_address = legend_operator_base.get_ip_address() return SDLC_SERVICE_URL_FORMAT % ( { # NOTE(aznashwan): we always return the plain HTTP endpoint: "schema": "http", "host": ip_address, "port": APPLICATION_CONNECTOR_PORT_HTTP, "path": APPLICATION_ROOT_PATH, } ) def _get_legend_gitlab_redirect_uris(self): base_url = self._get_sdlc_service_url() redirect_uris = [fmt % {"base_url": base_url} for fmt in SDLC_GITLAB_REDIRECT_URI_FORMATS] return redirect_uris def _get_core_legend_service_configs(self, legend_db_credentials, legend_gitlab_credentials): # Check DB-related options: if not legend_db_credentials: return model.WaitingStatus("no legend db info present in relation yet") legend_db_uri = legend_db_credentials["uri"] legend_db = legend_db_credentials["database"] # Check gitlab-related options: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PRIVATE if self.model.config["gitlab-create-new-projects-as-public"]: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PUBLIC if not legend_gitlab_credentials: return model.WaitingStatus("no legend gitlab info present in relation yet") gitlab_client_id = legend_gitlab_credentials["client_id"] gitlab_client_secret = legend_gitlab_credentials["client_secret"] gitlab_openid_discovery_url = legend_gitlab_credentials["openid_discovery_url"] gitlab_project_tag = self.model.config["gitlab-project-tag"] gitlab_project_creation_group_pattern = self.model.config[ "gitlab-project-creation-group-pattern" ] # Check Java logging options: request_logging_level = self._get_logging_level_from_config( "server-requests-logging-level" ) server_logging_level = self._get_logging_level_from_config("server-logging-level") if not all([server_logging_level, request_logging_level]): return model.BlockedStatus( "one or more logging config options are improperly formatted " "or missing, please review the debug-log for more details" ) # Compile base config: sdlc_config = { "applicationName": "Legend SDLC", "server": { "rootPath": APPLICATION_ROOT_PATH, "applicationConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_CONNECTOR_PORT_HTTP, "maxRequestHeaderSize": "128KiB", } ], "adminConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_ADMIN_CONNECTOR_PORT_HTTP, } ], "gzip": {"includedMethods": ["GET", "POST"]}, "requestLog": { "type": "classic", "level": request_logging_level, "appenders": [{"type": "console", "logFormat": "OFF"}], }, }, "filterPriorities": { "GitLab": 1, "org.pac4j.j2e.filter.CallbackFilter": 2, "org.pac4j.j2e.filter.SecurityFilter": 3, "CORS": 4, }, "pac4j": { "callbackPrefix": "/api/pac4j/login", "mongoUri": legend_db_uri, "mongoDb": legend_db, "clients": [ { "org.finos.legend.server.pac4j.gitlab.GitlabClient": { "name": "gitlab", "clientId": gitlab_client_id, "secret": gitlab_client_secret, "discoveryUri": gitlab_openid_discovery_url, # NOTE(aznashwan): needs to be a space-separated str: "scope": " ".join(GITLAB_REQUIRED_SCOPES), } } ], "mongoSession": {"enabled": True, "collection": "userSessions"}, "bypassPaths": ["/api/info"], }, "gitLab": { "newProjectVisibility": gitlab_project_visibility, "projectTag": gitlab_project_tag, "uat": { "server": { "scheme": legend_gitlab_credentials["gitlab_scheme"], "host": "%s:%s" % ( legend_gitlab_credentials["gitlab_host"], legend_gitlab_credentials["gitlab_port"], ), }, "app": { "id": gitlab_client_id, "secret": gitlab_client_secret, "redirectURI": ( SDLC_MAIN_GITLAB_REDIRECT_URL % {"base_url": self._get_sdlc_service_url()} ), }, }, }, "projectStructure": { "projectCreation": {"groupIdPattern": gitlab_project_creation_group_pattern}, "extensionProvider": { "org.finos.legend.sdlc.server.gitlab.finos." "FinosGitlabProjectStructureExtensionProvider": {} },
random_line_split
charm.py
ATION_NAME = "legend-db" LEGEND_GITLAB_RELATION_NAME = "legend-sdlc-gitlab" LEGEND_STUDIO_RELATION_NAME = "legend-sdlc" SDLC_SERVICE_URL_FORMAT = "%(schema)s://%(host)s:%(port)s%(path)s" SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH = "/sdlc-config.yaml" SDLC_MAIN_GITLAB_REDIRECT_URL = "%(base_url)s/auth/callback" SDLC_GITLAB_REDIRECT_URI_FORMATS = [ SDLC_MAIN_GITLAB_REDIRECT_URL, "%(base_url)s/pac4j/login/callback", ] TRUSTSTORE_PASSPHRASE = "Legend SDLC" TRUSTSTORE_CONTAINER_LOCAL_PATH = "/truststore.jks" APPLICATION_CONNECTOR_PORT_HTTP = 7070 APPLICATION_ADMIN_CONNECTOR_PORT_HTTP = 7076 APPLICATION_ROOT_PATH = "/api" APPLICATION_LOGGING_FORMAT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c - %m%n" GITLAB_PROJECT_VISIBILITY_PUBLIC = "public" GITLAB_PROJECT_VISIBILITY_PRIVATE = "private" GITLAB_REQUIRED_SCOPES = ["openid", "profile", "api"] class LegendSDLCServerCharm(legend_operator_base.BaseFinosLegendCoreServiceCharm): """Charmed operator for the FINOS Legend SDLC Server.""" def __init__(self, *args): super().__init__(*args) # Studio relation events: self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_joined, self._on_studio_relation_joined ) self.framework.observe( self.on[LEGEND_STUDIO_RELATION_NAME].relation_changed, self._on_studio_relation_changed ) @classmethod def _get_application_connector_port(cls): return APPLICATION_CONNECTOR_PORT_HTTP @classmethod def _get_workload_container_name(cls): return SDLC_CONTAINER_NAME @classmethod def _get_workload_service_names(cls): return [SDLC_SERVICE_NAME] @classmethod def _get_workload_pebble_layers(cls): return { "sdlc": { "summary": "SDLC layer.", "description": "Pebble config layer for FINOS Legend SDLC.", "services": { "sdlc": { "override": "replace", "summary": "sdlc", "command": ( # NOTE(aznashwan): starting through bash is needed # for the classpath glob (-cp ...) to be expanded: "/bin/sh -c 'java -XX:+ExitOnOutOfMemoryError " "-XX:MaxRAMPercentage=60 -Xss4M -cp /app/bin/*.jar" " -Dfile.encoding=UTF8 " '-Djavax.net.ssl.trustStore="%s" ' '-Djavax.net.ssl.trustStorePassword="%s" ' "org.finos.legend.sdlc.server.LegendSDLCServer " 'server "%s"\'' % ( TRUSTSTORE_CONTAINER_LOCAL_PATH, TRUSTSTORE_PASSPHRASE, SDLC_CONFIG_FILE_CONTAINER_LOCAL_PATH, ) ), # NOTE(aznashwan): considering the SDLC service expects # a singular config file which already contains all # relevant options in it (some of which will require # the relation with DB/Gitlab to have already been # established), we do not auto-start: "startup": "disabled", # TODO(aznashwan): determine any env vars we could pass # (most notably, things like the RAM percentage etc...) "environment": {}, } }, } } def _get_jks_truststore_preferences(self): jks_prefs = { "truststore_path": TRUSTSTORE_CONTAINER_LOCAL_PATH, "truststore_passphrase": TRUSTSTORE_PASSPHRASE, "trusted_certificates": {}, } cert = self._get_legend_gitlab_certificate() if cert: # NOTE(aznashwan): cert label 'gitlab-sdlc' is arbitrary: jks_prefs["trusted_certificates"]["gitlab-sdlc"] = cert return jks_prefs @classmethod def _get_legend_gitlab_relation_name(cls): return LEGEND_GITLAB_RELATION_NAME @classmethod def _get_legend_db_relation_name(cls):
def _get_sdlc_service_url(self): ip_address = legend_operator_base.get_ip_address() return SDLC_SERVICE_URL_FORMAT % ( { # NOTE(aznashwan): we always return the plain HTTP endpoint: "schema": "http", "host": ip_address, "port": APPLICATION_CONNECTOR_PORT_HTTP, "path": APPLICATION_ROOT_PATH, } ) def _get_legend_gitlab_redirect_uris(self): base_url = self._get_sdlc_service_url() redirect_uris = [fmt % {"base_url": base_url} for fmt in SDLC_GITLAB_REDIRECT_URI_FORMATS] return redirect_uris def _get_core_legend_service_configs(self, legend_db_credentials, legend_gitlab_credentials): # Check DB-related options: if not legend_db_credentials: return model.WaitingStatus("no legend db info present in relation yet") legend_db_uri = legend_db_credentials["uri"] legend_db = legend_db_credentials["database"] # Check gitlab-related options: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PRIVATE if self.model.config["gitlab-create-new-projects-as-public"]: gitlab_project_visibility = GITLAB_PROJECT_VISIBILITY_PUBLIC if not legend_gitlab_credentials: return model.WaitingStatus("no legend gitlab info present in relation yet") gitlab_client_id = legend_gitlab_credentials["client_id"] gitlab_client_secret = legend_gitlab_credentials["client_secret"] gitlab_openid_discovery_url = legend_gitlab_credentials["openid_discovery_url"] gitlab_project_tag = self.model.config["gitlab-project-tag"] gitlab_project_creation_group_pattern = self.model.config[ "gitlab-project-creation-group-pattern" ] # Check Java logging options: request_logging_level = self._get_logging_level_from_config( "server-requests-logging-level" ) server_logging_level = self._get_logging_level_from_config("server-logging-level") if not all([server_logging_level, request_logging_level]): return model.BlockedStatus( "one or more logging config options are improperly formatted " "or missing, please review the debug-log for more details" ) # Compile base config: sdlc_config = { "applicationName": "Legend SDLC", "server": { "rootPath": APPLICATION_ROOT_PATH, "applicationConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_CONNECTOR_PORT_HTTP, "maxRequestHeaderSize": "128KiB", } ], "adminConnectors": [ { "type": legend_operator_base.APPLICATION_CONNECTOR_TYPE_HTTP, "port": APPLICATION_ADMIN_CONNECTOR_PORT_HTTP, } ], "gzip": {"includedMethods": ["GET", "POST"]}, "requestLog": { "type": "classic", "level": request_logging_level, "appenders": [{"type": "console", "logFormat": "OFF"}], }, }, "filterPriorities": { "GitLab": 1, "org.pac4j.j2e.filter.CallbackFilter": 2, "org.pac4j.j2e.filter.SecurityFilter": 3, "CORS": 4, }, "pac4j": { "callbackPrefix": "/api/pac4j/login", "mongoUri": legend_db_uri, "mongoDb": legend_db, "clients": [ { "org.finos.legend.server.pac4j.gitlab.GitlabClient": { "name": "gitlab", "clientId": gitlab_client_id, "secret": gitlab_client_secret, "discoveryUri": gitlab_openid_discovery_url, # NOTE(aznashwan): needs to be a space-separated str: "scope": " ".join(GITLAB_REQUIRED_SCOPES), } } ], "mongoSession": {"enabled": True, "collection": "userSessions"}, "bypassPaths": ["/api/info"], }, "gitLab": { "newProjectVisibility": gitlab_project_visibility, "projectTag": gitlab_project_tag, "uat": { "server": { "scheme": legend_gitlab_credentials["gitlab_scheme"], "host": "%s:%s" % ( legend_gitlab_credentials["gitlab_host"], legend_gitlab_credentials["gitlab_port"], ), }, "app": { "id": gitlab_client_id, "secret": gitlab_client_secret, "redirectURI": ( SDLC_MAIN_GITLAB_REDIRECT_URL % {"base_url": self._get_sdlc_service_url()} ), }, }, }, "projectStructure": { "projectCreation": {"groupIdPattern": gitlab_project_creation_group_pattern}, "extensionProvider": { "org.finos.legend.sdlc.server.gitlab.finos." "FinosGitlabProjectStructureExtensionProvider": {} },
return LEGEND_DB_RELATION_NAME
identifier_body
run_test.go
Conn) sshAgent.Add(key) } func removeKeyfromSSHAgent(key ssh.PublicKey) { aConn, _ := net.Dial("unix", sshAgentSocket) sshAgent := agent.NewClient(aConn) sshAgent.Remove(key) } func startSSHServer() { done := make(chan bool, 1) go func(done chan<- bool) { glssh.Handle(func(s glssh.Session) { authorizedKey := ssh.MarshalAuthorizedKey(s.PublicKey()) io.WriteString(s, fmt.Sprintf("public key used by %s:\n", s.User())) s.Write(authorizedKey) s.Exit(0) }) publicKeyOption := glssh.PublicKeyAuth(func(ctx glssh.Context, key glssh.PublicKey) bool { for _, pubk := range testPublicKeys { if glssh.KeysEqual(key, pubk) { return true } } return false }) fmt.Println("starting ssh server on port 2222...") done <- true panic(glssh.ListenAndServe(":2222", nil, publicKeyOption)) }(done) <-done } func TestMakeSigner(t *testing.T) { tests := []struct { name string key mockSSHKey expected ssh.Signer }{ {name: "Basic key signer with valid rsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: testSigners["rsa"], }, {name: "Basic key signer with valid dsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["dsa"], }, expected: testSigners["dsa"], }, {name: "Basic key signer with valid ecdsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["ecdsa"], }, expected: testSigners["ecdsa"], }, {name: "Basic key signer with valid user key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["user"], }, expected: testSigners["user"], }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Write content of the key to the keyname file ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) returned, _ := makeSigner(tt.key.keyname) if !reflect.DeepEqual(returned, tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } os.Remove(tt.key.keyname) }) } } func TestMakeKeyring(t *testing.T) { tests := []struct { name string useagent bool key mockSSHKey expected ssh.AuthMethod }{ {name: "Basic key ring with valid rsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring with valid dsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey11", content: testdata.PEMBytes["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring with valid ecdsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey12", content: testdata.PEMBytes["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, {name: "Basic key ring with valid user key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey13", content: testdata.PEMBytes["user"], }, expected: ssh.PublicKeys(testSigners["user"]), }, {name: "Basic key ring agent with valid rsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["rsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["rsa"]}, pubkey: testPublicKeys["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring agent with valid dsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["dsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}, pubkey: testPublicKeys["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring agent with valid ecdsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["ecdsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["ecdsa"]}, pubkey: testPublicKeys["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := makeKeyring(tt.key.keyname, sshAgentSocket, tt.useagent) // DeepEqual always returns false for functions unless nil // hence converting to string to compare check1 := reflect.ValueOf(returned).String() check2 := reflect.ValueOf(tt.expected).String() if !reflect.DeepEqual(check1, check2) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey) } if tt.key.keyname != "" { os.Remove(tt.key.keyname) } }) } } func TestRun(t *testing.T) { tests := []struct { name string machines []string user string cmd string key mockSSHKey port int useagent bool expected bool }{ {name: "Basic with valid rsa key", machines: []string{"localhost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey21", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: true, }, {name: "Basic with valid rsa key wrong hostname", machines: []string{"bogushost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey22", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key wrong port", machines: []string{"localhost"}, port: 2223, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey23", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key Google endpoint", machines: []string{"www.google.com"}, port: 22, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey24", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := Run(Machines(tt.machines), User(tt.user), Port(tt.port), Cmd(tt.cmd), Key(tt.key.keyname), UseAgent(tt.useagent), AgentSocket(sshAgentSocket)) if !(returned == tt.expected) {
t.Errorf("Value received: %v expected %v", returned, tt.expected) }
conditional_block
run_test.go
aConn, _ := net.Dial("unix", sshAgentSocket) sshAgent := agent.NewClient(aConn) sshAgent.Remove(key) } func startSSHServer() { done := make(chan bool, 1) go func(done chan<- bool) { glssh.Handle(func(s glssh.Session) { authorizedKey := ssh.MarshalAuthorizedKey(s.PublicKey()) io.WriteString(s, fmt.Sprintf("public key used by %s:\n", s.User())) s.Write(authorizedKey) s.Exit(0) }) publicKeyOption := glssh.PublicKeyAuth(func(ctx glssh.Context, key glssh.PublicKey) bool { for _, pubk := range testPublicKeys { if glssh.KeysEqual(key, pubk) { return true } } return false }) fmt.Println("starting ssh server on port 2222...") done <- true panic(glssh.ListenAndServe(":2222", nil, publicKeyOption)) }(done) <-done } func TestMakeSigner(t *testing.T) { tests := []struct { name string key mockSSHKey expected ssh.Signer }{ {name: "Basic key signer with valid rsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: testSigners["rsa"], }, {name: "Basic key signer with valid dsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["dsa"], }, expected: testSigners["dsa"], }, {name: "Basic key signer with valid ecdsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["ecdsa"], }, expected: testSigners["ecdsa"], }, {name: "Basic key signer with valid user key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["user"], }, expected: testSigners["user"], }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Write content of the key to the keyname file ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) returned, _ := makeSigner(tt.key.keyname) if !reflect.DeepEqual(returned, tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } os.Remove(tt.key.keyname) }) } } func TestMakeKeyring(t *testing.T) { tests := []struct { name string useagent bool key mockSSHKey expected ssh.AuthMethod }{ {name: "Basic key ring with valid rsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring with valid dsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey11", content: testdata.PEMBytes["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring with valid ecdsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey12", content: testdata.PEMBytes["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, {name: "Basic key ring with valid user key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey13", content: testdata.PEMBytes["user"], }, expected: ssh.PublicKeys(testSigners["user"]), }, {name: "Basic key ring agent with valid rsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["rsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["rsa"]}, pubkey: testPublicKeys["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring agent with valid dsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["dsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}, pubkey: testPublicKeys["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring agent with valid ecdsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["ecdsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["ecdsa"]}, pubkey: testPublicKeys["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := makeKeyring(tt.key.keyname, sshAgentSocket, tt.useagent) // DeepEqual always returns false for functions unless nil // hence converting to string to compare check1 := reflect.ValueOf(returned).String() check2 := reflect.ValueOf(tt.expected).String() if !reflect.DeepEqual(check1, check2) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey) } if tt.key.keyname != "" { os.Remove(tt.key.keyname) } }) } } func TestRun(t *testing.T) { tests := []struct { name string machines []string user string cmd string key mockSSHKey port int useagent bool expected bool }{ {name: "Basic with valid rsa key", machines: []string{"localhost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey21", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: true, }, {name: "Basic with valid rsa key wrong hostname", machines: []string{"bogushost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey22", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key wrong port", machines: []string{"localhost"}, port: 2223, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey23", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key Google endpoint", machines: []string{"www.google.com"}, port: 22, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey24", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := Run(Machines(tt.machines), User(tt.user), Port(tt.port), Cmd(tt.cmd), Key(tt.key.keyname), UseAgent(tt.useagent), AgentSocket(sshAgentSocket)) if !(returned == tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey)
}
random_line_split
run_test.go
[string]ssh.PublicKey sshAgentSocket string ) func init() { var err error n := len(testdata.PEMBytes) testSigners = make(map[string]ssh.Signer, n) testPrivateKeys = make(map[string]interface{}, n) testPublicKeys = make(map[string]ssh.PublicKey, n) for t, k := range testdata.PEMBytes { testPrivateKeys[t], err = ssh.ParseRawPrivateKey(k) if err != nil { panic(fmt.Sprintf("Unable to parse test key %s: %v", t, err)) } testSigners[t], err = ssh.NewSignerFromKey(testPrivateKeys[t]) if err != nil { panic(fmt.Sprintf("Unable to create signer for test key %s: %v", t, err)) } testPublicKeys[t] = testSigners[t].PublicKey() } randomStr := fmt.Sprintf("%v", rand.Intn(5000)) socketFile := "/tmp/gosocket" + randomStr + ".sock" setupSSHAgent(socketFile) time.Sleep(2 * time.Second) startSSHServer() } func setupSSHAgent(socketFile string) { done := make(chan string, 1) a := agent.NewKeyring() go func(done chan<- string) { ln, err := net.Listen("unix", socketFile) if err != nil { panic(fmt.Sprintf("Couldn't create socket for tests %v", err)) } defer ln.Close() // Need to wait until the socket is setup firstTime := true for { if firstTime == true { done <- socketFile firstTime = false } c, err := ln.Accept() if err != nil { panic(fmt.Sprintf("Couldn't accept connection to agent tests %v", err)) } defer c.Close() go func(c io.ReadWriter) { err = agent.ServeAgent(a, c) if err != nil { fmt.Sprintf("Couldn't serve ssh agent for tests %v", err) } }(c) } }(done) sshAgentSocket = <-done } func addKeytoSSHAgent(key agent.AddedKey) { aConn, _ := net.Dial("unix", sshAgentSocket) sshAgent := agent.NewClient(aConn) sshAgent.Add(key) } func removeKeyfromSSHAgent(key ssh.PublicKey) { aConn, _ := net.Dial("unix", sshAgentSocket) sshAgent := agent.NewClient(aConn) sshAgent.Remove(key) } func startSSHServer() { done := make(chan bool, 1) go func(done chan<- bool) { glssh.Handle(func(s glssh.Session) { authorizedKey := ssh.MarshalAuthorizedKey(s.PublicKey()) io.WriteString(s, fmt.Sprintf("public key used by %s:\n", s.User())) s.Write(authorizedKey) s.Exit(0) }) publicKeyOption := glssh.PublicKeyAuth(func(ctx glssh.Context, key glssh.PublicKey) bool { for _, pubk := range testPublicKeys { if glssh.KeysEqual(key, pubk) { return true } } return false }) fmt.Println("starting ssh server on port 2222...") done <- true panic(glssh.ListenAndServe(":2222", nil, publicKeyOption)) }(done) <-done } func T
t *testing.T) { tests := []struct { name string key mockSSHKey expected ssh.Signer }{ {name: "Basic key signer with valid rsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: testSigners["rsa"], }, {name: "Basic key signer with valid dsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["dsa"], }, expected: testSigners["dsa"], }, {name: "Basic key signer with valid ecdsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["ecdsa"], }, expected: testSigners["ecdsa"], }, {name: "Basic key signer with valid user key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["user"], }, expected: testSigners["user"], }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Write content of the key to the keyname file ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) returned, _ := makeSigner(tt.key.keyname) if !reflect.DeepEqual(returned, tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } os.Remove(tt.key.keyname) }) } } func TestMakeKeyring(t *testing.T) { tests := []struct { name string useagent bool key mockSSHKey expected ssh.AuthMethod }{ {name: "Basic key ring with valid rsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring with valid dsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey11", content: testdata.PEMBytes["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring with valid ecdsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey12", content: testdata.PEMBytes["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, {name: "Basic key ring with valid user key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey13", content: testdata.PEMBytes["user"], }, expected: ssh.PublicKeys(testSigners["user"]), }, {name: "Basic key ring agent with valid rsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["rsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["rsa"]}, pubkey: testPublicKeys["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring agent with valid dsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["dsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}, pubkey: testPublicKeys["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring agent with valid ecdsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["ecdsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["ecdsa"]}, pubkey: testPublicKeys["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := makeKeyring(tt.key.keyname, sshAgentSocket, tt.useagent) // DeepEqual always returns false for functions unless nil // hence converting to string to compare check1 := reflect.ValueOf(returned).String() check2 := reflect.ValueOf(tt.expected).String() if !reflect.DeepEqual(check1, check2) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey) } if tt.key.keyname != "" { os.Remove(tt.key.keyname) } }) } } func TestRun(t *testing.T) { tests := []struct { name string machines []string user string cmd string key mockSSHKey port int useagent bool expected bool }{ {name: "Basic with valid rsa key", machines: []string{"localhost"}, port: 2222, cmd: "ls", user:
estMakeSigner(
identifier_name
run_test.go
(func(ctx glssh.Context, key glssh.PublicKey) bool { for _, pubk := range testPublicKeys { if glssh.KeysEqual(key, pubk) { return true } } return false }) fmt.Println("starting ssh server on port 2222...") done <- true panic(glssh.ListenAndServe(":2222", nil, publicKeyOption)) }(done) <-done } func TestMakeSigner(t *testing.T) { tests := []struct { name string key mockSSHKey expected ssh.Signer }{ {name: "Basic key signer with valid rsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: testSigners["rsa"], }, {name: "Basic key signer with valid dsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["dsa"], }, expected: testSigners["dsa"], }, {name: "Basic key signer with valid ecdsa key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["ecdsa"], }, expected: testSigners["ecdsa"], }, {name: "Basic key signer with valid user key", key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["user"], }, expected: testSigners["user"], }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Write content of the key to the keyname file ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) returned, _ := makeSigner(tt.key.keyname) if !reflect.DeepEqual(returned, tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } os.Remove(tt.key.keyname) }) } } func TestMakeKeyring(t *testing.T) { tests := []struct { name string useagent bool key mockSSHKey expected ssh.AuthMethod }{ {name: "Basic key ring with valid rsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey", content: testdata.PEMBytes["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring with valid dsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey11", content: testdata.PEMBytes["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring with valid ecdsa key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey12", content: testdata.PEMBytes["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, {name: "Basic key ring with valid user key", useagent: false, key: mockSSHKey{ keyname: "/tmp/mockkey13", content: testdata.PEMBytes["user"], }, expected: ssh.PublicKeys(testSigners["user"]), }, {name: "Basic key ring agent with valid rsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["rsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["rsa"]}, pubkey: testPublicKeys["rsa"], }, expected: ssh.PublicKeys(testSigners["rsa"]), }, {name: "Basic key ring agent with valid dsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["dsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["dsa"]}, pubkey: testPublicKeys["dsa"], }, expected: ssh.PublicKeys(testSigners["dsa"]), }, {name: "Basic key ring agent with valid ecdsa key", useagent: true, key: mockSSHKey{ keyname: "", content: testdata.PEMBytes["ecdsa"], privkey: agent.AddedKey{PrivateKey: testPrivateKeys["ecdsa"]}, pubkey: testPublicKeys["ecdsa"], }, expected: ssh.PublicKeys(testSigners["ecdsa"]), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := makeKeyring(tt.key.keyname, sshAgentSocket, tt.useagent) // DeepEqual always returns false for functions unless nil // hence converting to string to compare check1 := reflect.ValueOf(returned).String() check2 := reflect.ValueOf(tt.expected).String() if !reflect.DeepEqual(check1, check2) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey) } if tt.key.keyname != "" { os.Remove(tt.key.keyname) } }) } } func TestRun(t *testing.T) { tests := []struct { name string machines []string user string cmd string key mockSSHKey port int useagent bool expected bool }{ {name: "Basic with valid rsa key", machines: []string{"localhost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey21", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: true, }, {name: "Basic with valid rsa key wrong hostname", machines: []string{"bogushost"}, port: 2222, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey22", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key wrong port", machines: []string{"localhost"}, port: 2223, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey23", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, {name: "Basic with valid rsa key Google endpoint", machines: []string{"www.google.com"}, port: 22, cmd: "ls", user: "testuser", key: mockSSHKey{ keyname: "/tmp/mockkey24", content: testdata.PEMBytes["rsa"], }, useagent: false, expected: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.useagent == true { addKeytoSSHAgent(tt.key.privkey) } // Write content of the key to the keyname file if tt.key.keyname != "" { ioutil.WriteFile(tt.key.keyname, tt.key.content, 0644) } returned := Run(Machines(tt.machines), User(tt.user), Port(tt.port), Cmd(tt.cmd), Key(tt.key.keyname), UseAgent(tt.useagent), AgentSocket(sshAgentSocket)) if !(returned == tt.expected) { t.Errorf("Value received: %v expected %v", returned, tt.expected) } if tt.useagent == true { removeKeyfromSSHAgent(tt.key.pubkey) } if tt.key.keyname != "" { os.Remove(tt.key.keyname) } }) } } func TestTearDown(t *testing.T) {
tests := []struct { name string id string }{ {name: "Teardown SSH Agent", id: "sshAgentTdown"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.id == "sshAgentTdown" { os.Remove(sshAgentSocket) } }) } }
identifier_body
main.rs
window::WindowBuilder::new() .with_title("starframe test") .with_inner_size(winit::dpi::LogicalSize { width: 800.0, height: 600.0, }), ); let state = State::init(&game.renderer.device); game.run(state); microprofile::shutdown!(); } // // Types // pub enum StateEnum { Playing, Paused, } pub struct State { scene: Scene, state: StateEnum, graph: MyGraph, player: player::PlayerController, mouse_mode: MouseMode, mouse_grabber: MouseGrabber, physics: phys::Physics, camera: gx::camera::MouseDragCamera, shape_renderer: gx::ShapeRenderer, } impl State { fn init(device: &wgpu::Device) -> Self { State { scene: Scene::default(), state: StateEnum::Playing, graph: MyGraph::new(), player: player::PlayerController::new(), mouse_mode: MouseMode::Grab, mouse_grabber: MouseGrabber::new(), physics: phys::Physics::with_substeps(10), camera: gx::camera::MouseDragCamera::new( gx::camera::ScalingStrategy::ConstantDisplayArea { width: 20.0, height: 10.0, }, ), shape_renderer: gx::ShapeRenderer::new(device), } } fn reset(&mut self) { self.physics.clear_constraints(); self.graph = MyGraph::new(); } fn read_scene(&mut self, file_idx: usize) { let dir = std::fs::read_dir("./examples/testgame/scenes"); match dir { Err(err) => eprintln!("Scenes dir not found: {}", err), Ok(mut dir) => { if let Some(Ok(entry)) = dir.nth(file_idx) { let file = std::fs::File::open(entry.path()); match file { Ok(file) => { let scene = Scene::read_from_file(file); match scene { Err(err) => eprintln!("Failed to parse file: {}", err), Ok(scene) => self.scene = scene, } } Err(err) => eprintln!("Failed to open file: {}", err), } } } } } fn instantiate_scene(&mut self) { self.scene.instantiate(&mut self.graph, &mut self.physics); } } #[derive(Clone, Copy, Debug)] pub enum MouseMode { /// Grab objects with the mouse Grab, /// Move the camera with the mouse Camera, } /// The recipes in a scene plus some adjustable parameters. #[derive(Clone, Debug, serde::Deserialize)] #[serde(default)] pub struct Scene { gravity: [f64; 2], recipes: Vec<Recipe>, } impl Default for Scene { fn default() -> Self { Self { gravity: [0.0, -9.81], recipes: vec![], } } } impl Scene { pub fn read_from_file(file: std::fs::File) -> Result<Self, ron::de::Error> { use serde::Deserialize; use std::io::Read; let mut reader = std::io::BufReader::new(file); let mut bytes = Vec::new(); reader.read_to_end(&mut bytes)?; let mut deser = ron::de::Deserializer::from_bytes(bytes.as_slice())?; Scene::deserialize(&mut deser) } pub fn instantiate(&self, graph: &mut crate::MyGraph, physics: &mut phys::Physics) { for recipe in &self.recipes { recipe.spawn(graph, physics); } } } /// The entity graph. pub struct
{ graph: graph::Graph, l_pose: graph::Layer<m::Pose>, l_collider: graph::Layer<phys::Collider>, l_body: graph::Layer<phys::RigidBody>, l_shape: graph::Layer<gx::Shape>, l_player: graph::Layer<player::Player>, l_evt_sink: sf::event::EventSinkLayer<MyGraph>, } impl MyGraph { pub fn new() -> Self { let mut graph = graph::Graph::new(); let l_pose = graph.create_layer(); let l_collider = graph.create_layer(); let l_body = graph.create_layer(); let l_shape = graph.create_layer(); let l_player = graph.create_layer(); let l_evt_sinks = graph.create_layer(); MyGraph { graph, l_pose, l_collider, l_body, l_shape, l_player, l_evt_sink: l_evt_sinks, } } } // // State updates // impl game::GameState for State { fn tick(&mut self, dt: f64, game: &Game) -> Option<()> { microprofile::flip(); microprofile::scope!("update", "all"); // // State-independent stuff // // exit on esc if game.input.is_key_pressed(Key::Escape, None) { return None; } // adjust physics substeps if game.input.is_key_pressed(Key::NumpadAdd, Some(0)) { self.physics.substeps += 1; println!("Substeps: {}", self.physics.substeps); } else if game.input.is_key_pressed(Key::NumpadSubtract, Some(0)) && self.physics.substeps > 1 { self.physics.substeps -= 1; println!("Substeps: {}", self.physics.substeps); } // mouse controls if game.input.is_key_pressed(Key::V, Some(0)) { self.mouse_mode = match self.mouse_mode { MouseMode::Grab => MouseMode::Camera, MouseMode::Camera => MouseMode::Grab, }; println!("Mouse mode: {:?}", self.mouse_mode); } match self.mouse_mode { MouseMode::Grab => { self.mouse_grabber.update( &game.input, &self.camera, game.renderer.window_size().into(), &mut self.physics, &self.graph, ); } MouseMode::Camera => { self.camera .update(&game.input, game.renderer.window_size().into()); if (game.input).is_mouse_button_pressed(MouseButton::Middle, Some(0)) { self.camera.pose = uv::DSimilarity2::identity(); } } } // reload for (idx, num_key) in [ Key::Key1, Key::Key2, Key::Key3, Key::Key4, Key::Key5, Key::Key6, Key::Key7, Key::Key8, Key::Key9, ] .iter() .enumerate() { if game.input.is_key_pressed(*num_key, Some(0)) { self.reset(); self.read_scene(idx); self.instantiate_scene(); } } // reload current scene if game.input.is_key_pressed(Key::Return, Some(0)) { self.reset(); self.instantiate_scene(); } // spawn stuff also when paused let random_pos = || { let mut rng = rand::thread_rng(); m::Vec2::new( distr::Uniform::from(-5.0..5.0).sample(&mut rng), distr::Uniform::from(1.0..4.0).sample(&mut rng), ) }; let random_angle = || m::Angle::Deg(distr::Uniform::from(0.0..360.0).sample(&mut rand::thread_rng())); let random_vel = || { let mut rng = rand::thread_rng(); [ distr::Uniform::from(-5.0..5.0).sample(&mut rng), distr::Uniform::from(-5.0..5.0).sample(&mut rng), ] }; let mut rng = rand::thread_rng(); if game.input.is_key_pressed(Key::S, Some(0)) { Recipe::DynamicBlock(recipes::Block { pose: m::IsometryBuilder::new() .with_position(random_pos()) .with_rotation(random_angle()), width: distr::Uniform::from(0.6..1.0).sample(&mut rng), height: distr::Uniform::from(0.5..0.8).sample(&mut rng), }) .spawn(&mut self.graph, &mut self.physics); } if game.input.is_key_pressed(Key::T, Some(0)) { Recipe::Ball(recipes::Ball { position: random_pos().into(), radius: distr::Uniform::from(0.1..0.4).sample(&mut rng), restitution: 1.0, start_velocity: random_vel(), }) .spawn(&mut self.graph, &mut self.physics); } match (&self.state, game.input.is_key_pressed(Key::Space, Some(0))) { // // Playing or stepping manually // (StateEnum::Playing, _) | (StateEnum::Paused, true) => { if game.input.is_key_pressed(Key::P, Some(0)) { self.state = StateEnum::Paused; return Some(()); } { microprofile::scope!("update", "physics"); let
MyGraph
identifier_name
main.rs
window::WindowBuilder::new() .with_title("starframe test") .with_inner_size(winit::dpi::LogicalSize { width: 800.0, height: 600.0, }), ); let state = State::init(&game.renderer.device); game.run(state); microprofile::shutdown!(); } // // Types // pub enum StateEnum { Playing, Paused, } pub struct State { scene: Scene, state: StateEnum, graph: MyGraph, player: player::PlayerController, mouse_mode: MouseMode, mouse_grabber: MouseGrabber, physics: phys::Physics, camera: gx::camera::MouseDragCamera, shape_renderer: gx::ShapeRenderer, } impl State { fn init(device: &wgpu::Device) -> Self { State { scene: Scene::default(), state: StateEnum::Playing, graph: MyGraph::new(), player: player::PlayerController::new(), mouse_mode: MouseMode::Grab, mouse_grabber: MouseGrabber::new(), physics: phys::Physics::with_substeps(10), camera: gx::camera::MouseDragCamera::new( gx::camera::ScalingStrategy::ConstantDisplayArea { width: 20.0, height: 10.0, }, ), shape_renderer: gx::ShapeRenderer::new(device), } } fn reset(&mut self) { self.physics.clear_constraints(); self.graph = MyGraph::new(); } fn read_scene(&mut self, file_idx: usize) { let dir = std::fs::read_dir("./examples/testgame/scenes"); match dir { Err(err) => eprintln!("Scenes dir not found: {}", err), Ok(mut dir) => { if let Some(Ok(entry)) = dir.nth(file_idx) { let file = std::fs::File::open(entry.path()); match file { Ok(file) => { let scene = Scene::read_from_file(file); match scene { Err(err) => eprintln!("Failed to parse file: {}", err), Ok(scene) => self.scene = scene, } } Err(err) => eprintln!("Failed to open file: {}", err), } } } } } fn instantiate_scene(&mut self) { self.scene.instantiate(&mut self.graph, &mut self.physics); } } #[derive(Clone, Copy, Debug)] pub enum MouseMode { /// Grab objects with the mouse Grab, /// Move the camera with the mouse Camera, } /// The recipes in a scene plus some adjustable parameters. #[derive(Clone, Debug, serde::Deserialize)] #[serde(default)] pub struct Scene { gravity: [f64; 2], recipes: Vec<Recipe>, } impl Default for Scene { fn default() -> Self { Self { gravity: [0.0, -9.81], recipes: vec![], } } } impl Scene { pub fn read_from_file(file: std::fs::File) -> Result<Self, ron::de::Error> { use serde::Deserialize; use std::io::Read; let mut reader = std::io::BufReader::new(file); let mut bytes = Vec::new(); reader.read_to_end(&mut bytes)?; let mut deser = ron::de::Deserializer::from_bytes(bytes.as_slice())?; Scene::deserialize(&mut deser) } pub fn instantiate(&self, graph: &mut crate::MyGraph, physics: &mut phys::Physics) { for recipe in &self.recipes { recipe.spawn(graph, physics); } } } /// The entity graph. pub struct MyGraph { graph: graph::Graph, l_pose: graph::Layer<m::Pose>, l_collider: graph::Layer<phys::Collider>, l_body: graph::Layer<phys::RigidBody>, l_shape: graph::Layer<gx::Shape>, l_player: graph::Layer<player::Player>, l_evt_sink: sf::event::EventSinkLayer<MyGraph>, } impl MyGraph { pub fn new() -> Self { let mut graph = graph::Graph::new(); let l_pose = graph.create_layer(); let l_collider = graph.create_layer(); let l_body = graph.create_layer(); let l_shape = graph.create_layer(); let l_player = graph.create_layer(); let l_evt_sinks = graph.create_layer(); MyGraph { graph, l_pose, l_collider, l_body, l_shape, l_player, l_evt_sink: l_evt_sinks, } } } // // State updates // impl game::GameState for State { fn tick(&mut self, dt: f64, game: &Game) -> Option<()> { microprofile::flip(); microprofile::scope!("update", "all"); // // State-independent stuff // // exit on esc if game.input.is_key_pressed(Key::Escape, None) { return None; } // adjust physics substeps if game.input.is_key_pressed(Key::NumpadAdd, Some(0)) { self.physics.substeps += 1; println!("Substeps: {}", self.physics.substeps); } else if game.input.is_key_pressed(Key::NumpadSubtract, Some(0)) && self.physics.substeps > 1 { self.physics.substeps -= 1; println!("Substeps: {}", self.physics.substeps); } // mouse controls if game.input.is_key_pressed(Key::V, Some(0)) { self.mouse_mode = match self.mouse_mode { MouseMode::Grab => MouseMode::Camera, MouseMode::Camera => MouseMode::Grab, }; println!("Mouse mode: {:?}", self.mouse_mode); } match self.mouse_mode { MouseMode::Grab => { self.mouse_grabber.update( &game.input, &self.camera, game.renderer.window_size().into(), &mut self.physics, &self.graph, ); } MouseMode::Camera => { self.camera .update(&game.input, game.renderer.window_size().into()); if (game.input).is_mouse_button_pressed(MouseButton::Middle, Some(0)) { self.camera.pose = uv::DSimilarity2::identity(); } } } // reload for (idx, num_key) in [ Key::Key1, Key::Key2, Key::Key3, Key::Key4, Key::Key5, Key::Key6, Key::Key7, Key::Key8, Key::Key9, ] .iter() .enumerate() { if game.input.is_key_pressed(*num_key, Some(0)) { self.reset(); self.read_scene(idx); self.instantiate_scene(); } } // reload current scene if game.input.is_key_pressed(Key::Return, Some(0)) { self.reset(); self.instantiate_scene(); } // spawn stuff also when paused let random_pos = || { let mut rng = rand::thread_rng(); m::Vec2::new( distr::Uniform::from(-5.0..5.0).sample(&mut rng), distr::Uniform::from(1.0..4.0).sample(&mut rng), ) }; let random_angle = || m::Angle::Deg(distr::Uniform::from(0.0..360.0).sample(&mut rand::thread_rng()));
distr::Uniform::from(-5.0..5.0).sample(&mut rng), distr::Uniform::from(-5.0..5.0).sample(&mut rng), ] }; let mut rng = rand::thread_rng(); if game.input.is_key_pressed(Key::S, Some(0)) { Recipe::DynamicBlock(recipes::Block { pose: m::IsometryBuilder::new() .with_position(random_pos()) .with_rotation(random_angle()), width: distr::Uniform::from(0.6..1.0).sample(&mut rng), height: distr::Uniform::from(0.5..0.8).sample(&mut rng), }) .spawn(&mut self.graph, &mut self.physics); } if game.input.is_key_pressed(Key::T, Some(0)) { Recipe::Ball(recipes::Ball { position: random_pos().into(), radius: distr::Uniform::from(0.1..0.4).sample(&mut rng), restitution: 1.0, start_velocity: random_vel(), }) .spawn(&mut self.graph, &mut self.physics); } match (&self.state, game.input.is_key_pressed(Key::Space, Some(0))) { // // Playing or stepping manually // (StateEnum::Playing, _) | (StateEnum::Paused, true) => { if game.input.is_key_pressed(Key::P, Some(0)) { self.state = StateEnum::Paused; return Some(()); } { microprofile::scope!("update", "physics"); let grav
let random_vel = || { let mut rng = rand::thread_rng(); [
random_line_split
main.rs
{ #[clap(long)] labels: bool, #[clap(long)] statement_counts: bool, #[clap(short, long, default_value = "0")] skip: u64, #[clap(short, long)] threads: Option<usize>, #[clap(required = true)] paths: Vec<String>, } #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum Extra<'a> { None, Type(&'a str), Lang(&'a str), } #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum Subject<'a> { IRI(&'a str), Blank(&'a str), } #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum Object<'a> { IRI(&'a str), Blank(&'a str), Literal(&'a str, Extra<'a>), } #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub struct Statement<'a> { subject: Subject<'a>, predicate: &'a str, object: Object<'a>, } pub enum Work { LINES(u64, Vec<String>), DONE, } pub struct WorkResult { statement_counts: Option<HashMap<String, u64>>, } lazy_static! { static ref RE: Regex = Regex::new( r#"(?x) ^ \s* # subject (?: # IRI (?:<([^>]*)>) | # Blank (?:_:([^\s]+)) ) \s* # predicate IRI <([^>]*)> \s* # object (?: # IRI (?:<([^>]*)>) | # Blank (?:_:([^\s]+)) | # literal (?: "([^"]*)" # optional extra (?: # language (?:@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*)) | # data type (?:\^\^<([^>]*)>) )? ) ) "# ) .unwrap(); } pub fn parse<'a>(line: u64, input: &'a str, regex: &Regex) -> Statement<'a> { let captures = regex .captures(input) .unwrap_or_else(|| panic!("Invalid line: {}: {:?}", line, input)); let subject = captures .get(1) .map(|object| Subject::IRI(object.as_str())) .or_else(|| captures.get(2).map(|blank| Subject::Blank(blank.as_str()))) .expect("failed to parse subject"); let predicate = captures.get(3).expect("failed to parse predicate").as_str(); let object = captures .get(4) .map(|object| Object::IRI(object.as_str())) .or_else(|| captures.get(5).map(|blank| Object::Blank(blank.as_str()))) .unwrap_or_else(|| { let literal = captures.get(6).expect("failed to parse object").as_str(); let extra = captures .get(7) .map(|lang| Extra::Lang(lang.as_str())) .or_else(|| { captures .get(8) .map(|data_type| Extra::Type(data_type.as_str())) }) .unwrap_or(Extra::None); Object::Literal(literal, extra) }); Statement { subject, predicate, object, } } lazy_static_include_str! { PROPERTIES_DATA => "properties", IDENTIFIER_PROPERTIES_DATA => "identifier-properties", LANGUAGES_DATA => "languages", LABELS_DATA => "labels", } lazy_static! { static ref PROPERTIES: HashSet<&'static str> = line_set(&PROPERTIES_DATA); } lazy_static! { static ref IDENTIFIER_PROPERTIES: HashSet<String> = line_set(&IDENTIFIER_PROPERTIES_DATA) .iter() .flat_map(|id| vec![ format!("http://www.wikidata.org/prop/direct/P{}", id), format!("http://www.wikidata.org/prop/direct-normalized/P{}", id) ]) .collect(); } lazy_static! { static ref LANGUAGES: HashSet<&'static str> = line_set(&LANGUAGES_DATA); } lazy_static! { static ref LABELS: HashSet<&'static str> = line_set(&LABELS_DATA); } fn line_set(data: &str) -> HashSet<&str> { data.lines().collect() } fn ignored_subject(iri: &str) -> bool { iri.starts_with("https://www.wikidata.org/wiki/Special:EntityData") } fn produce<T: Read>( running: Arc<AtomicBool>, skip: u64, reader: T, s: &Sender<Work>, ) -> (bool, u64) { let mut total = 0; let mut buf_reader = BufReader::new(reader); let mut lines = Vec::new(); if skip > 0 { eprintln!("# skipping {}", skip) } loop { if !running.load(Ordering::SeqCst) { eprintln!("# interrupted after {}", total); return (false, total); } let mut line = String::new(); if buf_reader.read_line(&mut line).unwrap() == 0 { break; } total += 1; let skipped = total < skip; if !skipped { lines.push(line); if total % BATCH_SIZE == 0 { s.send(Work::LINES(total, lines)).unwrap(); lines = Vec::new(); } } if total % PROGRESS_COUNT == 0 { let status = if skipped { "skipped" } else { "" }; eprintln!("# {} {}", status, total); } } if !lines.is_empty() { s.send(Work::LINES(total, lines)).unwrap(); } (true, total) } fn consume( name: String, work_receiver: Receiver<Work>, result_sender: Sender<WorkResult>, labels: bool, statement_counts: bool, ) { let regex = RE.clone(); let lines_path = format!("{}.nt.bz2", name); let lines_file = File::create(&lines_path) .unwrap_or_else(|_| panic!("unable to create file: {}", &lines_path)); let mut lines_encoder = BzEncoder::new(BufWriter::new(lines_file), Compression::best()); let mut labels_encoder = if labels { let labels_path = format!("labels_{}.bz2", name); let labels_file = File::create(&labels_path) .unwrap_or_else(|_| panic!("unable to create file: {}", &labels_path)); Some(BzEncoder::new( BufWriter::new(labels_file), Compression::best(), )) } else { None }; let mut statement_counter = if statement_counts { Some(HashMap::new()) } else { None }; loop { match work_receiver.recv().unwrap() { Work::LINES(number, lines) => { for line in lines { handle( &mut lines_encoder, labels_encoder.as_mut(), statement_counter.as_mut(), number, line, &regex, ); } lines_encoder.flush().unwrap(); if let Some(labels_encoder) = labels_encoder.as_mut() { labels_encoder.flush().unwrap() } } Work::DONE => { eprintln!("# stopping thread {}", name); lines_encoder.try_finish().unwrap(); if let Some(labels_encoder) = labels_encoder.as_mut() { labels_encoder.try_finish().unwrap() } result_sender .send(WorkResult { statement_counts: statement_counter, }) .unwrap(); return; } } } } fn handle<T: Write, U: Write>( lines_writer: &mut T, labels_writer: Option<&mut U>, statement_counter: Option<&mut HashMap<String, u64>>, number: u64, line: String, regex: &Regex, ) -> Option<()> { let statement = parse(number, &line, regex); maybe_write_line(lines_writer, &line, statement); let id = entity(statement.subject)?; maybe_count_statement(statement_counter, id, statement); maybe_write_label(labels_writer, id, statement); None } fn maybe_write_line<T: Write>(lines_writer: &mut T, line: &str, statement: Statement) { if !is_acceptable(statement) { return; } lines_writer.write_all(line.as_bytes()).unwrap(); } fn maybe_write_label<T: Write>( labels_writer: Option<&mut T>, id: &str, statement: Statement, ) -> Option<()> { let labels_writer = labels_writer?; let label = label(statement)?; labels_writer .write_fmt(format_args!("{} {}\n", id, label)) .unwrap(); None } fn maybe_count_statement( statement_counter: Option<&mut HashMap<String, u64>>, id: &str, statement: Statement, ) -> Option<()> { let statement_counter = statement_counter?; direct_property(statement.predicate)?; *statement_counter.entry(id.to_string()).or_insert(0) += 1; None } fn is_acceptable(statement: Statement) -> bool {
Opts
identifier_name
main.rs
.get(7) .map(|lang| Extra::Lang(lang.as_str())) .or_else(|| { captures .get(8) .map(|data_type| Extra::Type(data_type.as_str())) }) .unwrap_or(Extra::None); Object::Literal(literal, extra) }); Statement { subject, predicate, object, } } lazy_static_include_str! { PROPERTIES_DATA => "properties", IDENTIFIER_PROPERTIES_DATA => "identifier-properties", LANGUAGES_DATA => "languages", LABELS_DATA => "labels", } lazy_static! { static ref PROPERTIES: HashSet<&'static str> = line_set(&PROPERTIES_DATA); } lazy_static! { static ref IDENTIFIER_PROPERTIES: HashSet<String> = line_set(&IDENTIFIER_PROPERTIES_DATA) .iter() .flat_map(|id| vec![ format!("http://www.wikidata.org/prop/direct/P{}", id), format!("http://www.wikidata.org/prop/direct-normalized/P{}", id) ]) .collect(); } lazy_static! { static ref LANGUAGES: HashSet<&'static str> = line_set(&LANGUAGES_DATA); } lazy_static! { static ref LABELS: HashSet<&'static str> = line_set(&LABELS_DATA); } fn line_set(data: &str) -> HashSet<&str> { data.lines().collect() } fn ignored_subject(iri: &str) -> bool { iri.starts_with("https://www.wikidata.org/wiki/Special:EntityData") } fn produce<T: Read>( running: Arc<AtomicBool>, skip: u64, reader: T, s: &Sender<Work>, ) -> (bool, u64) { let mut total = 0; let mut buf_reader = BufReader::new(reader); let mut lines = Vec::new(); if skip > 0 { eprintln!("# skipping {}", skip) } loop { if !running.load(Ordering::SeqCst) { eprintln!("# interrupted after {}", total); return (false, total); } let mut line = String::new(); if buf_reader.read_line(&mut line).unwrap() == 0 { break; } total += 1; let skipped = total < skip; if !skipped { lines.push(line); if total % BATCH_SIZE == 0 { s.send(Work::LINES(total, lines)).unwrap(); lines = Vec::new(); } } if total % PROGRESS_COUNT == 0 { let status = if skipped { "skipped" } else { "" }; eprintln!("# {} {}", status, total); } } if !lines.is_empty() { s.send(Work::LINES(total, lines)).unwrap(); } (true, total) } fn consume( name: String, work_receiver: Receiver<Work>, result_sender: Sender<WorkResult>, labels: bool, statement_counts: bool, ) { let regex = RE.clone(); let lines_path = format!("{}.nt.bz2", name); let lines_file = File::create(&lines_path) .unwrap_or_else(|_| panic!("unable to create file: {}", &lines_path)); let mut lines_encoder = BzEncoder::new(BufWriter::new(lines_file), Compression::best()); let mut labels_encoder = if labels { let labels_path = format!("labels_{}.bz2", name); let labels_file = File::create(&labels_path) .unwrap_or_else(|_| panic!("unable to create file: {}", &labels_path)); Some(BzEncoder::new( BufWriter::new(labels_file), Compression::best(), )) } else { None }; let mut statement_counter = if statement_counts { Some(HashMap::new()) } else { None }; loop { match work_receiver.recv().unwrap() { Work::LINES(number, lines) => { for line in lines { handle( &mut lines_encoder, labels_encoder.as_mut(), statement_counter.as_mut(), number, line, &regex, ); } lines_encoder.flush().unwrap(); if let Some(labels_encoder) = labels_encoder.as_mut() { labels_encoder.flush().unwrap() } } Work::DONE => { eprintln!("# stopping thread {}", name); lines_encoder.try_finish().unwrap(); if let Some(labels_encoder) = labels_encoder.as_mut() { labels_encoder.try_finish().unwrap() } result_sender .send(WorkResult { statement_counts: statement_counter, }) .unwrap(); return; } } } } fn handle<T: Write, U: Write>( lines_writer: &mut T, labels_writer: Option<&mut U>, statement_counter: Option<&mut HashMap<String, u64>>, number: u64, line: String, regex: &Regex, ) -> Option<()> { let statement = parse(number, &line, regex); maybe_write_line(lines_writer, &line, statement); let id = entity(statement.subject)?; maybe_count_statement(statement_counter, id, statement); maybe_write_label(labels_writer, id, statement); None } fn maybe_write_line<T: Write>(lines_writer: &mut T, line: &str, statement: Statement) { if !is_acceptable(statement) { return; } lines_writer.write_all(line.as_bytes()).unwrap(); } fn maybe_write_label<T: Write>( labels_writer: Option<&mut T>, id: &str, statement: Statement, ) -> Option<()> { let labels_writer = labels_writer?; let label = label(statement)?; labels_writer .write_fmt(format_args!("{} {}\n", id, label)) .unwrap(); None } fn maybe_count_statement( statement_counter: Option<&mut HashMap<String, u64>>, id: &str, statement: Statement, ) -> Option<()> { let statement_counter = statement_counter?; direct_property(statement.predicate)?; *statement_counter.entry(id.to_string()).or_insert(0) += 1; None } fn is_acceptable(statement: Statement) -> bool { if PROPERTIES.contains(statement.predicate) || IDENTIFIER_PROPERTIES.contains(statement.predicate) { return false; } match statement.subject { Subject::Blank(_) => return false, Subject::IRI(iri) if ignored_subject(iri) => return false, _ => (), } match statement.object { Object::Blank(_) => return false, Object::Literal(_, Extra::Lang(lang)) if !LANGUAGES.contains(lang) => return false, // non-Earth geo coordinates are not supported by some triple stores Object::Literal( literal, Extra::Type("http://www.opengis.net/ont/geosparql#wktLiteral"), ) if literal.starts_with('<') => return false, _ => (), } true } fn label(statement: Statement) -> Option<String> { if !LABELS.contains(statement.predicate) { return None; } if let Object::Literal(label, Extra::Lang(lang)) = statement.object { if !LANGUAGES.contains(lang) { return None; } return Some(unescape(label)); } None } static ENTITY_IRI_PREFIX: &str = "http://www.wikidata.org/entity/Q"; fn entity(subject: Subject) -> Option<&str> { if let Subject::IRI(iri) = subject { iri.strip_prefix(ENTITY_IRI_PREFIX) } else { None } } static DIRECT_PROPERTY_IRI_PREFIX: &str = "http://www.wikidata.org/prop/direct/"; fn direct_property(predicate: &str) -> Option<&str> { predicate.strip_prefix(DIRECT_PROPERTY_IRI_PREFIX) } pub fn unescape(s: &str) -> String { let mut chars = s.chars().enumerate(); let mut res = String::with_capacity(s.len()); while let Some((idx, c)) = chars.next() { if c == '\\' { match chars.next() { None => { panic!("invalid escape at {} in {}", idx, s); } Some((idx, c2)) => { res.push(match c2 { 't' => '\t', 'b' => '\u{08}', 'n' => '\n', 'r' => '\r', 'f' => '\u{0C}', '\\' => '\\', 'u' => match parse_unicode(&mut chars, 4) { Ok(c3) => c3, Err(err) => { panic!("invalid escape {}{} at {} in {}: {}", c, c2, idx, s, err); }
} }, _ => { panic!("invalid escape {}{} at {} in {}", c, c2, idx, s); } }); continue; } }; } res.push(c); } res } fn parse_unicode<I>(chars: &mut I, count: usize) -> Result<char, String> where
}, 'U' => match parse_unicode(&mut chars, 8) { Ok(c3) => c3, Err(err) => { panic!("invalid escape {}{} at {} in {}: {}", c, c2, idx, s, err);
random_line_split
main.rs
continue; } }; } res.push(c); } res } fn parse_unicode<I>(chars: &mut I, count: usize) -> Result<char, String> where I: Iterator<Item = (usize, char)>, { let unicode_seq: String = chars.take(count).map(|(_, c)| c).collect(); u32::from_str_radix(&unicode_seq, 16) .map_err(|e| format!("could not parse {} as u32 hex: {}", unicode_seq, e)) .and_then(|u| { std::char::from_u32(u).ok_or_else(|| format!("could not parse {} as a unicode char", u)) }) } fn main() { let opts: Opts = Opts::parse(); let labels = opts.labels; let statement_counts = opts.statement_counts; let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); ctrlc::set_handler(move || { if r.load(Ordering::SeqCst) { exit(1); } r.store(false, Ordering::SeqCst); }) .expect("failed to set Ctrl-C handler"); let start = Instant::now(); let (work_sender, work_receiver) = bounded::<Work>(0); let (result_sender, result_receiver) = unbounded(); let mut threads = Vec::new(); let thread_count = opts.threads.unwrap_or_else(|| num_cpus::get() * 2); for id in 1..=thread_count { let work_receiver = work_receiver.clone(); let result_sender = result_sender.clone(); threads.push(thread::spawn(move || { consume( id.to_string(), work_receiver, result_sender, labels, statement_counts, ) })); } let mut exit_code = 0; for path in opts.paths { let file = File::open(&path).expect("can't open file"); let decoder = BzDecoder::new(BufReader::new(file)); eprintln!("# processing {}", path); let (finished, count) = produce(running.clone(), opts.skip, decoder, &work_sender); eprintln!("# processed {}: {}", path, count); if !finished { exit_code = 1; break; } } for _ in &threads { work_sender.send(Work::DONE).unwrap(); } let mut statement_counter = HashMap::new(); let mut result_count = 0; for result in result_receiver.iter() { if let Some(statement_counts) = result.statement_counts { for (id, count) in statement_counts.iter() { *statement_counter.entry(id.to_string()).or_insert(0) += count; } } result_count += 1; if result_count == thread_count { break; } } if statement_counts { eprintln!("# entities: {}", statement_counter.len()); let path = "statement_counts.bz2"; let file = File::create(path).unwrap_or_else(|_| panic!("unable to create file: {}", path)); let mut encoder = BzEncoder::new(BufWriter::new(file), Compression::best()); for (id, count) in statement_counter.iter() { encoder .write_fmt(format_args!("{} {}\n", id, count)) .unwrap(); } encoder.try_finish().unwrap(); } let duration = start.elapsed(); eprintln!("# took {:?}", duration); exit(exit_code); } #[cfg(test)] mod tests { use super::*; use pretty_assertions::assert_eq; use std::fs::read_to_string; use std::io::{self, Lines}; use std::path::{Path, PathBuf}; #[test] fn test_literal_with_type() { let line = r#"<http://www.wikidata.org/entity/Q1644> <http://www.wikidata.org/prop/direct/P2043> "+1094.26"^^<http://www.w3.org/2001/XMLSchema#decimal> ."#; assert_eq!( parse(1, line, &RE), Statement { subject: Subject::IRI("http://www.wikidata.org/entity/Q1644"), predicate: "http://www.wikidata.org/prop/direct/P2043", object: Object::Literal( "+1094.26", Extra::Type("http://www.w3.org/2001/XMLSchema#decimal") ) } ); } #[test] fn test_literal_with_lang() { let line = r#"<http://www.wikidata.org/entity/Q177> <http://schema.org/name> "pizza"@en ."#; assert_eq!( parse(1, line, &RE), Statement { subject: Subject::IRI("http://www.wikidata.org/entity/Q177"), predicate: "http://schema.org/name", object: Object::Literal("pizza", Extra::Lang("en")) } ); } #[test] fn test_literal() { let line = r#"<http://www.wikidata.org/entity/Q177> <http://www.wikidata.org/prop/direct/P373> "Pizzas" ."#; assert_eq!( parse(1, line, &RE), Statement { subject: Subject::IRI("http://www.wikidata.org/entity/Q177"), predicate: "http://www.wikidata.org/prop/direct/P373", object: Object::Literal("Pizzas", Extra::None) } ); } #[test] fn test_blank_subject() { let line = r#"_:foo <bar> <baz>"#; assert_eq!( parse(1, line, &RE), Statement { subject: Subject::Blank("foo"), predicate: "bar", object: Object::IRI("baz") } ); } #[test] fn test_blank_object() { let line = r#"<foo> <bar> _:baz"#; assert_eq!( parse(1, line, &RE), Statement { subject: Subject::IRI("foo"), predicate: "bar", object: Object::Blank("baz") } ); } #[test] fn test_statement_count() { let a = format!("{}a", ENTITY_IRI_PREFIX); let b = format!("{}b", ENTITY_IRI_PREFIX); let first_predicate = format!("{}first", DIRECT_PROPERTY_IRI_PREFIX); let second_predicate = "second"; let third_predicate = format!("{}third", DIRECT_PROPERTY_IRI_PREFIX); let first = Statement { subject: Subject::IRI(a.as_str()), predicate: first_predicate.as_str(), object: Object::IRI(""), }; let second = Statement { subject: Subject::IRI(b.as_str()), predicate: second_predicate, object: Object::IRI(""), }; let third = Statement { subject: Subject::IRI(a.as_str()), predicate: third_predicate.as_str(), object: Object::IRI(""), }; let mut counter = HashMap::new(); maybe_count_statement(Some(&mut counter), "a", first); maybe_count_statement(Some(&mut counter), "b", second); maybe_count_statement(Some(&mut counter), "a", third); assert_eq!(counter.len(), 1); assert_eq!(counter.get("a"), Some(&2)); assert_eq!(counter.get("b"), None); } #[test] fn test_geo_literals() { assert!(is_acceptable(parse( 1, r#"<foo> <bar> "Point(4.6681 50.6411)"^^<http://www.opengis.net/ont/geosparql#wktLiteral> ."#, &RE, ))); assert!(!is_acceptable(parse( 1, r#"<foo> <bar> "<http://www.wikidata.org/entity/Q405> Point(-141.6 42.6)"^^<http://www.opengis.net/ont/geosparql#wktLiteral> ."#, &RE, ))); } fn read_lines<P>(filename: P) -> io::Result<Lines<BufReader<File>>> where P: AsRef<Path>, { let file = File::open(filename)?; Ok(BufReader::new(file).lines()) } #[test] fn test_full() -> Result<(), ()>
{ let dir = env!("CARGO_MANIFEST_DIR"); let mut in_path = PathBuf::from(dir); in_path.push("test.in.rdf"); let in_path = in_path.as_os_str().to_str().unwrap(); let mut out_path = PathBuf::from(dir); out_path.push("test.out.rdf"); let out_path = out_path.as_os_str().to_str().unwrap(); let mut lines_writer = Vec::new(); let mut labels_writer = Vec::new(); for (line, number) in read_lines(in_path).unwrap().zip(1u64..) { let mut line = line.unwrap(); line.push('\n'); handle( &mut lines_writer, Some(&mut labels_writer),
identifier_body
lease_status.pb.go
i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Lease.Size())) n1, err := m.Lease.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n1 dAtA[i] = 0x12 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Timestamp.Size())) n2, err := m.Timestamp.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n2 if m.State != 0 { dAtA[i] = 0x18 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.State)) } if m.Liveness != nil { dAtA[i] = 0x22 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Liveness.Size())) n3, err := m.Liveness.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n3 } return i, nil } func encodeVarintLeaseStatus(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) return offset + 1 } func (m *LeaseStatus) Size() (n int) { var l int _ = l l = m.Lease.Size() n += 1 + l + sovLeaseStatus(uint64(l)) l = m.Timestamp.Size() n += 1 + l + sovLeaseStatus(uint64(l)) if m.State != 0 { n += 1 + sovLeaseStatus(uint64(m.State)) } if m.Liveness != nil { l = m.Liveness.Size() n += 1 + l + sovLeaseStatus(uint64(l)) } return n } func sovLeaseStatus(x uint64) (n int) { for { n++ x >>= 7 if x == 0 { break } } return n } func sozLeaseStatus(x uint64) (n int) { return sovLeaseStatus(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } func (m *LeaseStatus) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { preIndex := iNdEx var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { return fmt.Errorf("proto: LeaseStatus: wiretype end group for non-group") } if fieldNum <= 0 { return fmt.Errorf("proto: LeaseStatus: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Lease", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Lease.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Timestamp.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field State", wireType) } m.State = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ m.State |= (LeaseState(b) & 0x7F) << shift if b < 0x80 { break } } case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Liveness", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if m.Liveness == nil { m.Liveness = &Liveness{} } if err := m.Liveness.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipLeaseStatus(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthLeaseStatus } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } iNdEx += skippy } } if iNdEx > l { return io.ErrUnexpectedEOF } return nil } func skipLeaseStatus(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } wireType := int(wire & 0x7) switch wireType { case 0: for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } iNdEx++ if dAtA[iNdEx-1] < 0x80 { break } } return iNdEx, nil case 1: iNdEx += 8 return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if
var l int _ = l dAtA[i] = 0xa i++
random_line_split
lease_status.pb.go
() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalTo(dAtA) if err != nil { return nil, err } return dAtA[:n], nil } func (m *LeaseStatus) MarshalTo(dAtA []byte) (int, error) { var i int _ = i var l int _ = l dAtA[i] = 0xa i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Lease.Size())) n1, err := m.Lease.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n1 dAtA[i] = 0x12 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Timestamp.Size())) n2, err := m.Timestamp.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n2 if m.State != 0 { dAtA[i] = 0x18 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.State)) } if m.Liveness != nil { dAtA[i] = 0x22 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Liveness.Size())) n3, err := m.Liveness.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n3 } return i, nil } func encodeVarintLeaseStatus(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) return offset + 1 } func (m *LeaseStatus) Size() (n int) { var l int _ = l l = m.Lease.Size() n += 1 + l + sovLeaseStatus(uint64(l)) l = m.Timestamp.Size() n += 1 + l + sovLeaseStatus(uint64(l)) if m.State != 0 { n += 1 + sovLeaseStatus(uint64(m.State)) } if m.Liveness != nil { l = m.Liveness.Size() n += 1 + l + sovLeaseStatus(uint64(l)) } return n } func sovLeaseStatus(x uint64) (n int) { for { n++ x >>= 7 if x == 0 { break } } return n } func sozLeaseStatus(x uint64) (n int) { return sovLeaseStatus(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } func (m *LeaseStatus) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { preIndex := iNdEx var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { return fmt.Errorf("proto: LeaseStatus: wiretype end group for non-group") } if fieldNum <= 0 { return fmt.Errorf("proto: LeaseStatus: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Lease", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Lease.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Timestamp.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field State", wireType) } m.State = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ m.State |= (LeaseState(b) & 0x7F) << shift if b < 0x80 { break } } case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Liveness", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if m.Liveness == nil { m.Liveness = &Liveness{} } if err := m.Liveness.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipLeaseStatus(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthLeaseStatus } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } iNdEx += skippy } } if iNdEx > l { return io.ErrUnexpectedEOF } return nil } func skipLeaseStatus(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } wireType := int(wire & 0x7) switch wireType { case 0: for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } iNdEx++ if dAtA[iNdEx-1] <
Marshal
identifier_name
lease_status.pb.go
.Size())) n2, err := m.Timestamp.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n2 if m.State != 0 { dAtA[i] = 0x18 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.State)) } if m.Liveness != nil { dAtA[i] = 0x22 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Liveness.Size())) n3, err := m.Liveness.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n3 } return i, nil } func encodeVarintLeaseStatus(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) return offset + 1 } func (m *LeaseStatus) Size() (n int) { var l int _ = l l = m.Lease.Size() n += 1 + l + sovLeaseStatus(uint64(l)) l = m.Timestamp.Size() n += 1 + l + sovLeaseStatus(uint64(l)) if m.State != 0 { n += 1 + sovLeaseStatus(uint64(m.State)) } if m.Liveness != nil { l = m.Liveness.Size() n += 1 + l + sovLeaseStatus(uint64(l)) } return n } func sovLeaseStatus(x uint64) (n int) { for { n++ x >>= 7 if x == 0 { break } } return n } func sozLeaseStatus(x uint64) (n int) { return sovLeaseStatus(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } func (m *LeaseStatus) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { preIndex := iNdEx var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { return fmt.Errorf("proto: LeaseStatus: wiretype end group for non-group") } if fieldNum <= 0 { return fmt.Errorf("proto: LeaseStatus: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Lease", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Lease.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Timestamp.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field State", wireType) } m.State = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ m.State |= (LeaseState(b) & 0x7F) << shift if b < 0x80 { break } } case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Liveness", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if m.Liveness == nil { m.Liveness = &Liveness{} } if err := m.Liveness.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipLeaseStatus(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthLeaseStatus } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } iNdEx += skippy } } if iNdEx > l { return io.ErrUnexpectedEOF } return nil } func skipLeaseStatus(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } wireType := int(wire & 0x7) switch wireType { case 0: for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } iNdEx++ if dAtA[iNdEx-1] < 0x80 { break } } return iNdEx, nil case 1: iNdEx += 8 return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l
b := dAtA[iNdEx] iNdEx++ length |= (int(b) & 0x7F) << shift if b < 0x80 { break } } iNdEx += length if length < 0 { return 0, ErrInvalidLengthLeaseStatus } return iNdEx, nil case
{ return 0, io.ErrUnexpectedEOF }
conditional_block
lease_status.pb.go
.Size())) n2, err := m.Timestamp.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n2 if m.State != 0 { dAtA[i] = 0x18 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.State)) } if m.Liveness != nil { dAtA[i] = 0x22 i++ i = encodeVarintLeaseStatus(dAtA, i, uint64(m.Liveness.Size())) n3, err := m.Liveness.MarshalTo(dAtA[i:]) if err != nil { return 0, err } i += n3 } return i, nil } func encodeVarintLeaseStatus(dAtA []byte, offset int, v uint64) int { for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) return offset + 1 } func (m *LeaseStatus) Size() (n int) { var l int _ = l l = m.Lease.Size() n += 1 + l + sovLeaseStatus(uint64(l)) l = m.Timestamp.Size() n += 1 + l + sovLeaseStatus(uint64(l)) if m.State != 0 { n += 1 + sovLeaseStatus(uint64(m.State)) } if m.Liveness != nil { l = m.Liveness.Size() n += 1 + l + sovLeaseStatus(uint64(l)) } return n } func sovLeaseStatus(x uint64) (n int) { for { n++ x >>= 7 if x == 0 { break } } return n } func sozLeaseStatus(x uint64) (n int) { return sovLeaseStatus(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } func (m *LeaseStatus) Unmarshal(dAtA []byte) error
fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { return fmt.Errorf("proto: LeaseStatus: wiretype end group for non-group") } if fieldNum <= 0 { return fmt.Errorf("proto: LeaseStatus: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Lease", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Lease.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Timestamp.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field State", wireType) } m.State = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ m.State |= (LeaseState(b) & 0x7F) << shift if b < 0x80 { break } } case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Liveness", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthLeaseStatus } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if m.Liveness == nil { m.Liveness = &Liveness{} } if err := m.Liveness.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipLeaseStatus(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthLeaseStatus } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } iNdEx += skippy } } if iNdEx > l { return io.ErrUnexpectedEOF } return nil } func skipLeaseStatus(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } wireType := int(wire & 0x7) switch wireType { case 0: for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } iNdEx++ if dAtA[iNdEx-1] < 0x80 { break } } return iNdEx, nil case 1: iNdEx += 8 return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { if shift >= 64 { return 0, ErrIntOverflowLeaseStatus } if iNdEx >= l { return 0, io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ length |= (int(b) & 0x7F) << shift if b < 0x80 { break } } iNdEx += length if length < 0 { return 0, ErrInvalidLengthLeaseStatus } return iNdEx, nil case 3
{ l := len(dAtA) iNdEx := 0 for iNdEx < l { preIndex := iNdEx var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeaseStatus } if iNdEx >= l { return io.ErrUnexpectedEOF } b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } }
identifier_body
writer.rs
(&mut self, arch: Arch) { self.arch = arch; } /// Sets the debug identifier of this SymCache. pub fn set_debug_id(&mut self, debug_id: DebugId) { self.debug_id = debug_id; } // Methods processing symbolic-debuginfo [`ObjectLike`] below: // Feel free to move these to a separate file. /// This processes the given [`ObjectLike`] object, collecting all its functions and line /// information into the converter. #[tracing::instrument(skip_all, fields(object.debug_id = %object.debug_id().breakpad()))] pub fn process_object<'d, 'o, O>(&mut self, object: &'o O) -> Result<(), Error> where O: ObjectLike<'d, 'o>, O::Error: std::error::Error + Send + Sync + 'static, { let session = object .debug_session() .map_err(|e| Error::new(ErrorKind::BadDebugFile, e))?; self.set_arch(object.arch()); self.set_debug_id(object.debug_id()); self.is_windows_object = matches!(object.file_format(), FileFormat::Pe | FileFormat::Pdb); for function in session.functions() { let function = function.map_err(|e| Error::new(ErrorKind::BadDebugFile, e))?; self.process_symbolic_function(&function); } for symbol in object.symbols() { self.process_symbolic_symbol(&symbol); } self.is_windows_object = false; Ok(()) } /// Processes an individual [`Function`], adding its line information to the converter. pub fn process_symbolic_function(&mut self, function: &Function<'_>) { self.process_symbolic_function_recursive(function, &[(0x0, u32::MAX)]); } /// Processes an individual [`Function`], adding its line information to the converter. /// /// `call_locations` is a non-empty sorted list of `(address, call_location index)` pairs. fn process_symbolic_function_recursive( &mut self, function: &Function<'_>, call_locations: &[(u32, u32)], ) { let string_table = &mut self.string_table; // skip over empty functions or functions whose address is too large to fit in a u32 if function.size == 0 || function.address > u32::MAX as u64 { return; } let comp_dir = std::str::from_utf8(function.compilation_dir).ok(); let entry_pc = if function.inline { u32::MAX } else { function.address as u32 }; let function_idx = { let language = function.name.language(); let mut function = transform::Function { name: function.name.as_str().into(), comp_dir: comp_dir.map(Into::into), }; for transformer in &mut self.transformers.0 { function = transformer.transform_function(function); } let function_name = if self.is_windows_object { undecorate_win_symbol(&function.name) } else { &function.name }; let name_offset = string_table.insert(function_name) as u32; let lang = language as u32; let (fun_idx, _) = self.functions.insert_full(raw::Function { name_offset, _comp_dir_offset: u32::MAX, entry_pc, lang, }); fun_idx as u32 }; // We can divide the instructions in a function into two buckets: // (1) Instructions which are part of an inlined function call, and // (2) instructions which are *not* part of an inlined function call. // // Our incoming line records cover both (1) and (2) types of instructions. // // Let's call the address ranges of these instructions (1) inlinee ranges and (2) self ranges. // // We use the following strategy: For each function, only insert that function's "self ranges" // into `self.ranges`. Then recurse into the function's inlinees. Those will insert their // own "self ranges". Once the entire tree has been traversed, `self.ranges` will contain // entries from all levels. // // In order to compute this function's "self ranges", we first gather and sort its // "inlinee ranges". Later, when we iterate over this function's lines, we will compute the // "self ranges" from the gaps between the "inlinee ranges". let mut inlinee_ranges = Vec::new(); for inlinee in &function.inlinees { for line in &inlinee.lines { let start = line.address as u32; let end = (line.address + line.size.unwrap_or(1)) as u32; inlinee_ranges.push(start..end); } } inlinee_ranges.sort_unstable_by_key(|range| range.start); // Walk three iterators. All of these are already sorted by address. let mut line_iter = function.lines.iter(); let mut call_location_iter = call_locations.iter(); let mut inline_iter = inlinee_ranges.into_iter(); // call_locations is non-empty, so the first element always exists. let mut current_call_location = call_location_iter.next().unwrap(); let mut next_call_location = call_location_iter.next(); let mut next_line = line_iter.next(); let mut next_inline = inline_iter.next(); // This will be the list we pass to our inlinees as the call_locations argument. // This list is ordered by address by construction. let mut callee_call_locations = Vec::new(); // Iterate over the line records. while let Some(line) = next_line.take() { let line_range_start = line.address as u32; let line_range_end = (line.address + line.size.unwrap_or(1)) as u32; // Find the call location for this line. while next_call_location.is_some() && next_call_location.unwrap().0 <= line_range_start { current_call_location = next_call_location.unwrap(); next_call_location = call_location_iter.next(); } let inlined_into_idx = current_call_location.1; let mut location = transform::SourceLocation { file: transform::File { name: line.file.name_str(), directory: Some(line.file.dir_str()), comp_dir: comp_dir.map(Into::into), }, line: line.line as u32, }; for transformer in &mut self.transformers.0 { location = transformer.transform_source_location(location); } let name_offset = string_table.insert(&location.file.name) as u32; let directory_offset = location .file .directory .map_or(u32::MAX, |d| string_table.insert(&d) as u32); let comp_dir_offset = location .file .comp_dir .map_or(u32::MAX, |cd| string_table.insert(&cd) as u32); let (file_idx, _) = self.files.insert_full(raw::File { name_offset, directory_offset, comp_dir_offset, }); let source_location = raw::SourceLocation { file_idx: file_idx as u32, line: location.line, function_idx, inlined_into_idx, }; // The current line can be a "self line", or a "call line", or even a mixture. // // Examples: // // a) Just self line: // Line: |==============| // Inlinee ranges: (none) // // Effect: insert_range // // b) Just call line: // Line: |==============| // Inlinee ranges: |--------------| // // Effect: make_call_location // // c) Just call line, for multiple inlined calls: // Line: |==========================| // Inlinee ranges: |----------||--------------| // // Effect: make_call_location, make_call_location // // d) Call line and trailing self line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: make_call_location, insert_range // // e) Leading self line and also call line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: insert_range, make_call_location // // f) Interleaving // Line: |======================================| // Inlinee ranges: |-----------| |-------| // // Effect: insert_range, make_call_location, insert_range, make_call_location, insert_range // // g) Bad debug info // Line: |=======| // Inlinee ranges: |-------------| // // Effect: make_call_location let mut current_address = line_range_start; while current_address < line_range_end { // Emit our source location at current_address if current_address is not covered by an inlinee. if next_inline.is_none() || next_inline.as_ref().unwrap().start > current_address { // "insert_range" self.ranges.insert(current
set_arch
identifier_name
writer.rs
let mut callee_call_locations = Vec::new(); // Iterate over the line records. while let Some(line) = next_line.take() { let line_range_start = line.address as u32; let line_range_end = (line.address + line.size.unwrap_or(1)) as u32; // Find the call location for this line. while next_call_location.is_some() && next_call_location.unwrap().0 <= line_range_start { current_call_location = next_call_location.unwrap(); next_call_location = call_location_iter.next(); } let inlined_into_idx = current_call_location.1; let mut location = transform::SourceLocation { file: transform::File { name: line.file.name_str(), directory: Some(line.file.dir_str()), comp_dir: comp_dir.map(Into::into), }, line: line.line as u32, }; for transformer in &mut self.transformers.0 { location = transformer.transform_source_location(location); } let name_offset = string_table.insert(&location.file.name) as u32; let directory_offset = location .file .directory .map_or(u32::MAX, |d| string_table.insert(&d) as u32); let comp_dir_offset = location .file .comp_dir .map_or(u32::MAX, |cd| string_table.insert(&cd) as u32); let (file_idx, _) = self.files.insert_full(raw::File { name_offset, directory_offset, comp_dir_offset, }); let source_location = raw::SourceLocation { file_idx: file_idx as u32, line: location.line, function_idx, inlined_into_idx, }; // The current line can be a "self line", or a "call line", or even a mixture. // // Examples: // // a) Just self line: // Line: |==============| // Inlinee ranges: (none) // // Effect: insert_range // // b) Just call line: // Line: |==============| // Inlinee ranges: |--------------| // // Effect: make_call_location // // c) Just call line, for multiple inlined calls: // Line: |==========================| // Inlinee ranges: |----------||--------------| // // Effect: make_call_location, make_call_location // // d) Call line and trailing self line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: make_call_location, insert_range // // e) Leading self line and also call line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: insert_range, make_call_location // // f) Interleaving // Line: |======================================| // Inlinee ranges: |-----------| |-------| // // Effect: insert_range, make_call_location, insert_range, make_call_location, insert_range // // g) Bad debug info // Line: |=======| // Inlinee ranges: |-------------| // // Effect: make_call_location let mut current_address = line_range_start; while current_address < line_range_end { // Emit our source location at current_address if current_address is not covered by an inlinee. if next_inline.is_none() || next_inline.as_ref().unwrap().start > current_address { // "insert_range" self.ranges.insert(current_address, source_location.clone()); } // If there is an inlinee range covered by this line record, turn this line into that // call's "call line". Make a `call_location_idx` for it and store it in `callee_call_locations`. if next_inline.is_some() && next_inline.as_ref().unwrap().start < line_range_end { let inline_range = next_inline.take().unwrap(); // "make_call_location" let (call_location_idx, _) = self.call_locations.insert_full(source_location.clone()); callee_call_locations.push((inline_range.start, call_location_idx as u32)); // Advance current_address to the end of this inlinee range. current_address = inline_range.end; next_inline = inline_iter.next(); } else { // No further inlinee ranges are overlapping with this line record. Advance to the // end of the line record. current_address = line_range_end; } } // Advance the line iterator. next_line = line_iter.next(); // Skip any lines that start before current_address. // Such lines can exist if the debug information is faulty, or if the compiler created // multiple identical small "call line" records instead of one combined record // covering the entire inlinee range. We can't have different "call lines" for a single // inlinee range anyway, so it's fine to skip these. while next_line.is_some() && (next_line.as_ref().unwrap().address as u32) < current_address { next_line = line_iter.next(); } } if !function.inline { // add the bare minimum of information for the function if there isn't any. self.ranges.entry(entry_pc).or_insert(raw::SourceLocation { file_idx: u32::MAX, line: 0, function_idx, inlined_into_idx: u32::MAX, }); } // We've processed all address ranges which are *not* covered by inlinees. // Now it's time to recurse. // Process our inlinees. if !callee_call_locations.is_empty() { for inlinee in &function.inlinees { self.process_symbolic_function_recursive(inlinee, &callee_call_locations); } } let function_end = function.end_address() as u32; let last_addr = self.last_addr.get_or_insert(0); if function_end > *last_addr { *last_addr = function_end; } } /// Processes an individual [`Symbol`]. pub fn process_symbolic_symbol(&mut self, symbol: &Symbol<'_>) { let name_idx = { let mut function = transform::Function { name: match symbol.name { Some(ref name) => name.clone(), None => return, }, comp_dir: None, }; for transformer in &mut self.transformers.0 { function = transformer.transform_function(function); } let function_name = if self.is_windows_object { undecorate_win_symbol(&function.name) } else { &function.name }; self.string_table.insert(function_name) as u32 }; match self.ranges.entry(symbol.address as u32) { btree_map::Entry::Vacant(entry) => { let function = raw::Function { name_offset: name_idx, _comp_dir_offset: u32::MAX, entry_pc: symbol.address as u32, lang: u32::MAX, }; let function_idx = self.functions.insert_full(function).0 as u32; entry.insert(raw::SourceLocation { file_idx: u32::MAX, line: 0, function_idx, inlined_into_idx: u32::MAX, }); } btree_map::Entry::Occupied(entry) => { // ASSUMPTION: // the `functions` iterator has already filled in this addr via debug session. // we could trace the caller hierarchy up to the root, and assert that it is // indeed the same function, and maybe update its `entry_pc`, but we don’t do // that for now. let _function_idx = entry.get().function_idx as usize; } } let last_addr = self.last_addr.get_or_insert(0); if symbol.address as u32 >= *last_addr { self.last_addr = None; } } // Methods for serializing to a [`Write`] below: // Feel free to move these to a separate file. /// Serialize the converted data. /// /// This writes the SymCache binary format into the given [`Write`]. pub fn serialize<W: Write>(mut self, writer: &mut W) -> std::io::Result<()> {
let mut writer = Writer::new(writer); // Insert a trailing sentinel source location in case we have a definite end addr if let Some(last_addr) = self.last_addr { // TODO: to be extra safe, we might check that `last_addr` is indeed larger than // the largest range at some point. match self.ranges.entry(last_addr) { btree_map::Entry::Vacant(entry) => { entry.insert(raw::NO_SOURCE_LOCATION); } btree_map::Entry::Occupied(_entry) => { // BUG: // the last addr should not map to an already defined range } } } let num_files = self.files.len() as u32; let num_functions = self.functions.len() as u32; let num_source_locations = (self.call_locations.len() + self.ranges.len()) as u32;
identifier_body
writer.rs
#[tracing::instrument(skip_all, fields(object.debug_id = %object.debug_id().breakpad()))] pub fn process_object<'d, 'o, O>(&mut self, object: &'o O) -> Result<(), Error> where O: ObjectLike<'d, 'o>, O::Error: std::error::Error + Send + Sync + 'static, { let session = object .debug_session() .map_err(|e| Error::new(ErrorKind::BadDebugFile, e))?; self.set_arch(object.arch()); self.set_debug_id(object.debug_id()); self.is_windows_object = matches!(object.file_format(), FileFormat::Pe | FileFormat::Pdb); for function in session.functions() { let function = function.map_err(|e| Error::new(ErrorKind::BadDebugFile, e))?; self.process_symbolic_function(&function); } for symbol in object.symbols() { self.process_symbolic_symbol(&symbol); } self.is_windows_object = false; Ok(()) } /// Processes an individual [`Function`], adding its line information to the converter. pub fn process_symbolic_function(&mut self, function: &Function<'_>) { self.process_symbolic_function_recursive(function, &[(0x0, u32::MAX)]); } /// Processes an individual [`Function`], adding its line information to the converter. /// /// `call_locations` is a non-empty sorted list of `(address, call_location index)` pairs. fn process_symbolic_function_recursive( &mut self, function: &Function<'_>, call_locations: &[(u32, u32)], ) { let string_table = &mut self.string_table; // skip over empty functions or functions whose address is too large to fit in a u32 if function.size == 0 || function.address > u32::MAX as u64 { return; } let comp_dir = std::str::from_utf8(function.compilation_dir).ok(); let entry_pc = if function.inline { u32::MAX
let language = function.name.language(); let mut function = transform::Function { name: function.name.as_str().into(), comp_dir: comp_dir.map(Into::into), }; for transformer in &mut self.transformers.0 { function = transformer.transform_function(function); } let function_name = if self.is_windows_object { undecorate_win_symbol(&function.name) } else { &function.name }; let name_offset = string_table.insert(function_name) as u32; let lang = language as u32; let (fun_idx, _) = self.functions.insert_full(raw::Function { name_offset, _comp_dir_offset: u32::MAX, entry_pc, lang, }); fun_idx as u32 }; // We can divide the instructions in a function into two buckets: // (1) Instructions which are part of an inlined function call, and // (2) instructions which are *not* part of an inlined function call. // // Our incoming line records cover both (1) and (2) types of instructions. // // Let's call the address ranges of these instructions (1) inlinee ranges and (2) self ranges. // // We use the following strategy: For each function, only insert that function's "self ranges" // into `self.ranges`. Then recurse into the function's inlinees. Those will insert their // own "self ranges". Once the entire tree has been traversed, `self.ranges` will contain // entries from all levels. // // In order to compute this function's "self ranges", we first gather and sort its // "inlinee ranges". Later, when we iterate over this function's lines, we will compute the // "self ranges" from the gaps between the "inlinee ranges". let mut inlinee_ranges = Vec::new(); for inlinee in &function.inlinees { for line in &inlinee.lines { let start = line.address as u32; let end = (line.address + line.size.unwrap_or(1)) as u32; inlinee_ranges.push(start..end); } } inlinee_ranges.sort_unstable_by_key(|range| range.start); // Walk three iterators. All of these are already sorted by address. let mut line_iter = function.lines.iter(); let mut call_location_iter = call_locations.iter(); let mut inline_iter = inlinee_ranges.into_iter(); // call_locations is non-empty, so the first element always exists. let mut current_call_location = call_location_iter.next().unwrap(); let mut next_call_location = call_location_iter.next(); let mut next_line = line_iter.next(); let mut next_inline = inline_iter.next(); // This will be the list we pass to our inlinees as the call_locations argument. // This list is ordered by address by construction. let mut callee_call_locations = Vec::new(); // Iterate over the line records. while let Some(line) = next_line.take() { let line_range_start = line.address as u32; let line_range_end = (line.address + line.size.unwrap_or(1)) as u32; // Find the call location for this line. while next_call_location.is_some() && next_call_location.unwrap().0 <= line_range_start { current_call_location = next_call_location.unwrap(); next_call_location = call_location_iter.next(); } let inlined_into_idx = current_call_location.1; let mut location = transform::SourceLocation { file: transform::File { name: line.file.name_str(), directory: Some(line.file.dir_str()), comp_dir: comp_dir.map(Into::into), }, line: line.line as u32, }; for transformer in &mut self.transformers.0 { location = transformer.transform_source_location(location); } let name_offset = string_table.insert(&location.file.name) as u32; let directory_offset = location .file .directory .map_or(u32::MAX, |d| string_table.insert(&d) as u32); let comp_dir_offset = location .file .comp_dir .map_or(u32::MAX, |cd| string_table.insert(&cd) as u32); let (file_idx, _) = self.files.insert_full(raw::File { name_offset, directory_offset, comp_dir_offset, }); let source_location = raw::SourceLocation { file_idx: file_idx as u32, line: location.line, function_idx, inlined_into_idx, }; // The current line can be a "self line", or a "call line", or even a mixture. // // Examples: // // a) Just self line: // Line: |==============| // Inlinee ranges: (none) // // Effect: insert_range // // b) Just call line: // Line: |==============| // Inlinee ranges: |--------------| // // Effect: make_call_location // // c) Just call line, for multiple inlined calls: // Line: |==========================| // Inlinee ranges: |----------||--------------| // // Effect: make_call_location, make_call_location // // d) Call line and trailing self line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: make_call_location, insert_range // // e) Leading self line and also call line: // Line: |==================| // Inlinee ranges: |-----------| // // Effect: insert_range, make_call_location // // f) Interleaving // Line: |======================================| // Inlinee ranges: |-----------| |-------| // // Effect: insert_range, make_call_location, insert_range, make_call_location, insert_range // // g) Bad debug info // Line: |=======| // Inlinee ranges: |-------------| // // Effect: make_call_location let mut current_address = line_range_start; while current_address < line_range_end { // Emit our source location at current_address if current_address is not covered by an inlinee. if next_inline.is_none() || next_inline.as_ref().unwrap().start > current_address { // "insert_range" self.ranges.insert(current_address, source_location.clone()); } // If there is an inlinee range covered by this line record, turn this line into that // call's "call line". Make a `call_location_idx` for it and store it in `callee_call_locations`. if next_inline.is_some() && next_inline.as_ref().unwrap().start < line_range_end { let inline_range = next_inline.take().unwrap(); // "make_call_location" let (call_location_idx, _) = self.call_locations.insert_full
} else { function.address as u32 }; let function_idx = {
random_line_split
CAPM_ab.py
def get_indexReturns(self): index = self.Sindex # The index ind_ret = self.pf.symbols[index].TDs[self.period].get_timeSeriesReturn() return ind_ret def get_indexMeanReturn(self): ind_ret = self.get_indexReturns() ind_ret = np.mean(ind_ret) return ind_ret def get_symbol_ab(self, symbol): ## This function outputs the alpha beta a symbol index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # plt.scatter(ind_ret,sym_ret) coeff = bMl.get_linearRef(ind_ret, sym_ret) return coeff def get_all_symbols_ab (self): symbols = self.pf.symbols.keys() coeffs = [] for sym in symbols: coeffs.append(self.get_symbol_ab(sym)) return coeffs def get_portfolio_ab(self, mode = "normal"): ### This function gets the alpha beta for the portfolio index = self.Sindex if (mode == "normal"): # We calculate it in a gaussian way returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() coeff = bMl.get_linearRef(ind_ret, returns) if (mode == "gaussian"): # We calculate by calculating the individual ones first. # The total coefficient is the sum of all coefficients coeffs = np.array(self.get_all_symbols_ab()) coeff = coeffs.T.dot(self.allocation) return coeff def get_symbol_JensenAlpha(self, symbol, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_symbol_ab(symbol) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_SymbolReturn(symbol) ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def get_portfolio_JensenAlpha(self, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_portfolio_ab(mode = mode) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def test_Jensens_Alpha(self, nf = 1): # Test the gaussianity and confidence of the alpha. residual = self.get_portfolio_JensenAlpha() ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print "TESTING PORFOLIO" print np.mean(residual), np.std(residual) print ttest ## Fit a gaussian and plot it gl.histogram(residual) def test_symbol_ab(self,symbol, nf = 1): ## This function tests that the residuals behaves properly. ## That is, that the alpha (how we behave compared to the market) ## has a nice gaussian distribution. ## Slide 7 index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Get coefficients for the symbol coeffs = self.get_symbol_ab(symbol) ##### GET THE RESIDUAL X = np.concatenate((np.ones((sym_ret.shape[0],1)),sym_ret),axis = 1) pred = X.dot(np.array(coeffs)) # Pred = X * Phi pred = pred.reshape(pred.shape[0],1) residual = pred - ind_ret print "Mean of residual %f" % np.mean(residual) ### Now we test the residual print "Statistical test of residual" ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print ttest ######## DOUBLE REGRESSION OF PAGE 7. Early empirical test Xres = np.concatenate((ind_ret,np.power(residual,2)),axis = 1) coeff = bMl.get_linearRef(Xres, sym_ret) print "Early empirical test of CAPM is wrong" print coeff hist, bin_edges = np.histogram(residual, density=True) gl.bar(bin_edges[:-1], hist, labels = ["Distribution","Return", "Probability"], legend = [symbol], alpha = 0.5, nf = nf) ## Lets get some statistics using stats m, v, s, k = stats.t.stats(10, moments='mvsk') n, (smin, smax), sm, sv, ss, sk = stats.describe(residual) print "****** MORE STATISTIC ************" print "Mean " + str(sm) tt = (sm-m)/np.sqrt(sv/float(n)) # t-statistic for mean pval = stats.t.sf(np.abs(tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt) print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval) return coeff def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1 def get_residuals_ab(self): # For histogram import pylab import scipy.stats as stats measurements = np.random.normal(loc = 20, scale = 5, size=100) stats.probplot(measurements, dist="norm", plot=pylab) pylab.show() def plot_portfoliocorrab(self, nf = 1): # This function plots the returns of a symbol compared # to the index, and computes the regresion and correlation parameters. index = self.Sindex # The index sym_ret = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # Mean and covariance data = np.concatenate((sym_ret,ind_ret),axis = 1
if (type(symbol_index) == type(-1)): # If we are given nothing or a number # We just stablish the first one symbol_index = self.pf.symbols.keys()[0] self.Sindex = symbol_index
identifier_body
CAPM_ab.py
self.get_indexReturns() ind_ret = np.mean(ind_ret) return ind_ret def get_symbol_ab(self, symbol): ## This function outputs the alpha beta a symbol index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # plt.scatter(ind_ret,sym_ret) coeff = bMl.get_linearRef(ind_ret, sym_ret) return coeff def get_all_symbols_ab (self): symbols = self.pf.symbols.keys() coeffs = [] for sym in symbols: coeffs.append(self.get_symbol_ab(sym)) return coeffs def get_portfolio_ab(self, mode = "normal"): ### This function gets the alpha beta for the portfolio index = self.Sindex if (mode == "normal"): # We calculate it in a gaussian way returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() coeff = bMl.get_linearRef(ind_ret, returns) if (mode == "gaussian"): # We calculate by calculating the individual ones first. # The total coefficient is the sum of all coefficients coeffs = np.array(self.get_all_symbols_ab()) coeff = coeffs.T.dot(self.allocation) return coeff def get_symbol_JensenAlpha(self, symbol, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_symbol_ab(symbol) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_SymbolReturn(symbol) ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def get_portfolio_JensenAlpha(self, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_portfolio_ab(mode = mode) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def test_Jensens_Alpha(self, nf = 1): # Test the gaussianity and confidence of the alpha. residual = self.get_portfolio_JensenAlpha() ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print "TESTING PORFOLIO" print np.mean(residual), np.std(residual) print ttest ## Fit a gaussian and plot it gl.histogram(residual) def test_symbol_ab(self,symbol, nf = 1): ## This function tests that the residuals behaves properly. ## That is, that the alpha (how we behave compared to the market) ## has a nice gaussian distribution. ## Slide 7 index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Get coefficients for the symbol coeffs = self.get_symbol_ab(symbol) ##### GET THE RESIDUAL X = np.concatenate((np.ones((sym_ret.shape[0],1)),sym_ret),axis = 1) pred = X.dot(np.array(coeffs)) # Pred = X * Phi pred = pred.reshape(pred.shape[0],1) residual = pred - ind_ret print "Mean of residual %f" % np.mean(residual) ### Now we test the residual print "Statistical test of residual" ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print ttest ######## DOUBLE REGRESSION OF PAGE 7. Early empirical test Xres = np.concatenate((ind_ret,np.power(residual,2)),axis = 1) coeff = bMl.get_linearRef(Xres, sym_ret) print "Early empirical test of CAPM is wrong" print coeff
hist, bin_edges = np.histogram(residual, density=True) gl.bar(bin_edges[:-1], hist, labels = ["Distribution","Return", "Probability"], legend = [symbol], alpha = 0.5, nf = nf) ## Lets get some statistics using stats m, v, s, k = stats.t.stats(10, moments='mvsk') n, (smin, smax), sm, sv, ss, sk = stats.describe(residual) print "****** MORE STATISTIC ************" print "Mean " + str(sm) tt = (sm-m)/np.sqrt(sv/float(n)) # t-statistic for mean pval = stats.t.sf(np.abs(tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt) print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval) return coeff def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1 def get_residuals_ab(self): # For histogram import pylab import scipy.stats as stats measurements = np.random.normal(loc = 20, scale = 5, size=100) stats.probplot(measurements, dist="norm", plot=pylab) pylab.show() def plot_portfoliocorrab(self, nf = 1): # This function plots the returns of a symbol compared # to the index, and computes the regresion and correlation parameters. index = self.Sindex # The index sym_ret = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # Mean and covariance data = np.concatenate((sym_ret,ind_ret),axis = 1) means = np.mean(data, axis = 0) cov = np.cov(data) # Regression coeffs = bMl.get_linearRef(ind_ret, sym_ret) gl.scatter(ind_ret, sym_ret, labels = ["Gaussianity study", "Index: " + self.Sindex,"Porfolio"], legend = ["Returns"], nf = nf) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, sym
random_line_split
CAPM_ab.py
self.Sindex = symbol_index def get_indexReturns(self): index = self.Sindex # The index ind_ret = self.pf.symbols[index].TDs[self.period].get_timeSeriesReturn() return ind_ret def get_indexMeanReturn(self): ind_ret = self.get_indexReturns() ind_ret = np.mean(ind_ret) return ind_ret def get_symbol_ab(self, symbol): ## This function outputs the alpha beta a symbol index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # plt.scatter(ind_ret,sym_ret) coeff = bMl.get_linearRef(ind_ret, sym_ret) return coeff def get_all_symbols_ab (self): symbols = self.pf.symbols.keys() coeffs = [] for sym in symbols: coeffs.append(self.get_symbol_ab(sym)) return coeffs def get_portfolio_ab(self, mode = "normal"): ### This function gets the alpha beta for the portfolio index = self.Sindex if (mode == "normal"): # We calculate it in a gaussian way returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() coeff = bMl.get_linearRef(ind_ret, returns) if (mode == "gaussian"): # We calculate by calculating the individual ones first. # The total coefficient is the sum of all coefficients coeffs = np.array(self.get_all_symbols_ab()) coeff = coeffs.T.dot(self.allocation) return coeff def get_symbol_JensenAlpha(self, symbol, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_symbol_ab(symbol) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_SymbolReturn(symbol) ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def get_portfolio_JensenAlpha(self, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_portfolio_ab(mode = mode) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def test_Jensens_Alpha(self, nf = 1): # Test the gaussianity and confidence of the alpha. residual = self.get_portfolio_JensenAlpha() ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print "TESTING PORFOLIO" print np.mean(residual), np.std(residual) print ttest ## Fit a gaussian and plot it gl.histogram(residual) def test_symbol_ab(self,symbol, nf = 1): ## This function tests that the residuals behaves properly. ## That is, that the alpha (how we behave compared to the market) ## has a nice gaussian distribution. ## Slide 7 index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Get coefficients for the symbol coeffs = self.get_symbol_ab(symbol) ##### GET THE RESIDUAL X = np.concatenate((np.ones((sym_ret.shape[0],1)),sym_ret),axis = 1) pred = X.dot(np.array(coeffs)) # Pred = X * Phi pred = pred.reshape(pred.shape[0],1) residual = pred - ind_ret print "Mean of residual %f" % np.mean(residual) ### Now we test the residual print "Statistical test of residual" ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print ttest ######## DOUBLE REGRESSION OF PAGE 7. Early empirical test Xres = np.concatenate((ind_ret,np.power(residual,2)),axis = 1) coeff = bMl.get_linearRef(Xres, sym_ret) print "Early empirical test of CAPM is wrong" print coeff hist, bin_edges = np.histogram(residual, density=True) gl.bar(bin_edges[:-1], hist, labels = ["Distribution","Return", "Probability"], legend = [symbol], alpha = 0.5, nf = nf) ## Lets get some statistics using stats m, v, s, k = stats.t.stats(10, moments='mvsk') n, (smin, smax), sm, sv, ss, sk = stats.describe(residual) print "****** MORE STATISTIC ************" print "Mean " + str(sm) tt = (sm-m)/np.sqrt(sv/float(n)) # t-statistic for mean pval = stats.t.sf(np.abs(tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt) print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval) return coeff def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1 def get_residuals_ab(self): # For histogram import pylab import scipy.stats as stats measurements = np.random.normal(loc = 20, scale = 5, size=100) stats.probplot(measurements, dist="norm", plot=pylab) pylab.show() def plot_portfoliocorrab(self, nf = 1): # This function plots the returns of a symbol compared # to the index, and computes the regresion and correlation parameters. index = self.Sindex # The index sym_ret = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # Mean and covariance data = np.concatenate((sym_ret,ind_ret),axis = 1) means = np.mean(data, axis = 0) cov = np.cov(data) # Regression coeffs = bMl.get
symbol_index = self.pf.symbols.keys()[0]
conditional_block
CAPM_ab.py
self.get_indexReturns() ind_ret = np.mean(ind_ret) return ind_ret def get_symbol_ab(self, symbol): ## This function outputs the alpha beta a symbol index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # plt.scatter(ind_ret,sym_ret) coeff = bMl.get_linearRef(ind_ret, sym_ret) return coeff def get_all_symbols_ab (self): symbols = self.pf.symbols.keys() coeffs = [] for sym in symbols: coeffs.append(self.get_symbol_ab(sym)) return coeffs def get_portfolio_ab(self, mode = "normal"): ### This function gets the alpha beta for the portfolio index = self.Sindex if (mode == "normal"): # We calculate it in a gaussian way returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() coeff = bMl.get_linearRef(ind_ret, returns) if (mode == "gaussian"): # We calculate by calculating the individual ones first. # The total coefficient is the sum of all coefficients coeffs = np.array(self.get_all_symbols_ab()) coeff = coeffs.T.dot(self.allocation) return coeff def get_symbol_JensenAlpha(self, symbol, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_symbol_ab(symbol) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_SymbolReturn(symbol) ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def get_portfolio_JensenAlpha(self, mode = "normal"): ### This function gets the Jensens Alpha of the portolio. ## Which is the alpha of the portfolio, taking into account ## The risk-free rate. Which is what is everything expected to # Grow. index = self.Sindex coeff = self.get_portfolio_ab(mode = mode) beta = coeff[1] # print "beta = " + str(beta) returns = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # It is the difference between what we obtain and the index # Sum of weighted alphas, taking into account the Riskfree Rate JensenAlpha = (returns - self.Rf) - beta*(ind_ret - self.Rf) return JensenAlpha def test_Jensens_Alpha(self, nf = 1): # Test the gaussianity and confidence of the alpha. residual = self.get_portfolio_JensenAlpha() ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print "TESTING PORFOLIO" print np.mean(residual), np.std(residual) print ttest ## Fit a gaussian and plot it gl.histogram(residual) def
(self,symbol, nf = 1): ## This function tests that the residuals behaves properly. ## That is, that the alpha (how we behave compared to the market) ## has a nice gaussian distribution. ## Slide 7 index = self.Sindex # The index sym_ret = self.pf.symbols[symbol].TDs[self.period].get_timeSeriesReturn() ind_ret = self.get_indexReturns() # Get coefficients for the symbol coeffs = self.get_symbol_ab(symbol) ##### GET THE RESIDUAL X = np.concatenate((np.ones((sym_ret.shape[0],1)),sym_ret),axis = 1) pred = X.dot(np.array(coeffs)) # Pred = X * Phi pred = pred.reshape(pred.shape[0],1) residual = pred - ind_ret print "Mean of residual %f" % np.mean(residual) ### Now we test the residual print "Statistical test of residual" ttest = stats.ttest_1samp(a = residual, # Sample data popmean = 0) # Pop mean print ttest ######## DOUBLE REGRESSION OF PAGE 7. Early empirical test Xres = np.concatenate((ind_ret,np.power(residual,2)),axis = 1) coeff = bMl.get_linearRef(Xres, sym_ret) print "Early empirical test of CAPM is wrong" print coeff hist, bin_edges = np.histogram(residual, density=True) gl.bar(bin_edges[:-1], hist, labels = ["Distribution","Return", "Probability"], legend = [symbol], alpha = 0.5, nf = nf) ## Lets get some statistics using stats m, v, s, k = stats.t.stats(10, moments='mvsk') n, (smin, smax), sm, sv, ss, sk = stats.describe(residual) print "****** MORE STATISTIC ************" print "Mean " + str(sm) tt = (sm-m)/np.sqrt(sv/float(n)) # t-statistic for mean pval = stats.t.sf(np.abs(tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt) print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval) return coeff def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1 def get_residuals_ab(self): # For histogram import pylab import scipy.stats as stats measurements = np.random.normal(loc = 20, scale = 5, size=100) stats.probplot(measurements, dist="norm", plot=pylab) pylab.show() def plot_portfoliocorrab(self, nf = 1): # This function plots the returns of a symbol compared # to the index, and computes the regresion and correlation parameters. index = self.Sindex # The index sym_ret = self.get_PortfolioReturn() ind_ret = self.get_indexReturns() # Mean and covariance data = np.concatenate((sym_ret,ind_ret),axis = 1) means = np.mean(data, axis = 0) cov = np.cov(data) # Regression coeffs = bMl.get_linearRef(ind_ret, sym_ret) gl.scatter(ind_ret, sym_ret, labels = ["Gaussianity study", "Index: " + self.Sindex,"Porfolio"], legend = ["Returns"], nf = nf) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres
test_symbol_ab
identifier_name
TableauViz.js
url, options); } function exportPDF() { viz.showExportPDFDialog(); $('.tab-dialog')[0].animate({ 'marginLeft': "-=50px" }); } function exportData() { viz.showExportDataDialog(); } function resetViz() { viz.revertAllAsync(); } function showVizButtons() { var sheets = workbook.getPublishedSheetsInfo(); var divIndividualButtons = $('#vizButtons'); // First clear any buttons that may have been added on a previous load divIndividualButtons.html(""); // Show 'standard' controls, common to all vizzes divIndividualButtons.append('<button type="button" onclick="resetViz()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Reset Filters</button>'); divIndividualButtons.append('<button type="button" onclick="exportPDF()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export PDF</button>'); divIndividualButtons.append('<button type="button" onclick="exportData()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export Data</button>'); divIndividualButtons.append('<button type="button" onclick="launch_edit()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Edit</button>'); // Only show buttons to switch vizzes if there's more than one if (sheets.length > 1) { for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { var sheet = sheets[sheetIndex]; divIndividualButtons.append('<button type="button" onclick="switchToViz(\'' + sheet.getName() + '\')" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">See ' + sheet.getName() + '</button>') } } } function switchToViz(vizName) { workbook.activateSheetAsync(vizName).then(function (dashboard) { dashboard.changeSizeAsync({ behavior: tableau.SheetSizeBehavior.AUTOMATIC }); }); } function onMarksSelection(marksEvent) { //filter sheets of selected marks because we dont need to hear events on all of our sheets if (marksEvent.getWorksheet().getName() == nameOfVizToInteract) { //get,marksAsync() is a method in the API that will retun a set of the marks selected return marksEvent.getMarksAsync().then(handleSelectedMarks); } } function handleSelectedMarks(marks) {
var fixedCloseLabel = ""; var changeFromPriorClose = 0; var changeFromPriorCloseLabel = ""; var date = ""; for (var markIndex = 0; markIndex < marks.length; markIndex++) { //getPairs gets tuples of data for the mark. one mark has multiple tuples var pairs = marks[markIndex].getPairs(); for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) { switch (pairs[pairIndex].fieldName) { case "Company": company = pairs[pairIndex].value; break; case "Date": date = pairs[pairIndex].formattedValue; break; case "SUM(Fixed Close)": fixedClose += pairs[pairIndex].value; fixedCloseLabel = pairs[pairIndex].formattedValue; break; case "AGG(Change from Prior Close)": changeFromPriorClose += pairs[pairIndex].value; changeFromPriorCloseLabel = pairs[pairIndex].formattedValue; break; } } } // With all values in memory, let's produce the UI if (marks.length == 1) { // When we select a single mark, we can show the individual details $('#eventPanel').html("Submit <b>" + company + "</b>'s " + date + " trading period for research. The fixed close price was <b>$" + fixedCloseLabel + "</b> with a variance of <b>" + changeFromPriorCloseLabel + "</b>."); } else { // But if more that one mark is selected, we show a summary (average) var avgFixedClose = Number((fixedClose / marks.length).toFixed(2)); var avgChangeFromPriorCloseLabel = Number((changeFromPriorClose * 100 / marks.length).toFixed(2)); $('#eventPanel').html("Submit <b>" + marks.length + " " + company + "</b>'s trading periods for research. The average fixed close price was <b>$" + avgFixedClose + "</b> & the average variance of <b>" + avgChangeFromPriorCloseLabel + "%</b>."); } } else { // Save selection in memory and give the user the option to submit var plural = ((marks.length == 1) ? "it" : "them"); var pluralS = ((selectedMarks.length == 1) ? "" : "s"); $('#eventPanel').html("You've selected <b>" + marks.length + "</b> outlier" + pluralS + "." + " Would you like to submit " + plural + " them for research?"); } } function submitMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } tableauWriteBack(selectedMarks); //var plural = ((selectedMarks.length == 1) ? "" : "s"); //$('#eventPanel').html("Success! <b>" + selectedMarks.length + "</b> selection" + plural + " submitted for research."); //$('#eventBox').hide(2000); } function resetAllMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } $('#eventBox').hide(800); $('#eventPanel').html(""); } function launch_edit() { // Adjust UI: Hide Buttons & navigation menu, increase size for edit mode $('#VizToolbar').hide(); $('body').addClass("sidebar-collapse"); $(".content-wrapper").css("height","1200px"); $("#tableauViz").hide(); // If the URL happens to have a ticket on it, clean it up before loading the edit window var url_parts = url.split('/t/'); url = tableauServer + '/t/' + url_parts[1]; var edit_location = tableauServer + '/en/embed_wrapper.html?src=' + url + '?:embed=y'; edit_iframe = document.createElement('iframe'); edit_iframe.src = edit_location; // This makes it not look like an iframe edit_iframe.style.padding = '0px'; edit_iframe.style.border = 'none'; edit_iframe.style.margin = '0px'; // Also set these with the same values in the embed_wrapper.html page edit_iframe.style.width = '100%'; edit_iframe.style.height = '100%'; $('#editViz').html(edit_iframe); $('#editViz').show(); } function iframe_change(new_url) { console.log("Old URL received in iframe_change: " + url); console.log("New URL received in iframe_change: " + new_url); // Destroy the original edit_iframe so
// If selection has been cleared, no need to show a message if (marks.length == 0) { $('#eventBox').hide(600); return; } // Save selected marks in memory so they can be submitted later selectedMarks = marks; $('#eventPanel').html(""); $('#eventBox').show(600); // Logic for Equities Dashboard is specialized, any other scatterplots also are enabled but in a general sense if (workbook.getActiveSheet().getName() == 'Individual Equities Dashboard') { //loop through all the selected marks var noOrders = 0; var company = ""; var fixedClose = 0;
identifier_body
TableauViz.js
url, options); } function exportPDF() { viz.showExportPDFDialog(); $('.tab-dialog')[0].animate({ 'marginLeft': "-=50px" }); } function exportData() { viz.showExportDataDialog(); } function resetViz() { viz.revertAllAsync(); } function showVizButtons() { var sheets = workbook.getPublishedSheetsInfo(); var divIndividualButtons = $('#vizButtons'); // First clear any buttons that may have been added on a previous load divIndividualButtons.html(""); // Show 'standard' controls, common to all vizzes divIndividualButtons.append('<button type="button" onclick="resetViz()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Reset Filters</button>'); divIndividualButtons.append('<button type="button" onclick="exportPDF()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export PDF</button>'); divIndividualButtons.append('<button type="button" onclick="exportData()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export Data</button>'); divIndividualButtons.append('<button type="button" onclick="launch_edit()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Edit</button>'); // Only show buttons to switch vizzes if there's more than one if (sheets.length > 1) { for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { var sheet = sheets[sheetIndex]; divIndividualButtons.append('<button type="button" onclick="switchToViz(\'' + sheet.getName() + '\')" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">See ' + sheet.getName() + '</button>') } } } function switchToViz(vizName) { workbook.activateSheetAsync(vizName).then(function (dashboard) { dashboard.changeSizeAsync({ behavior: tableau.SheetSizeBehavior.AUTOMATIC }); }); } function onMarksSelection(marksEvent) { //filter sheets of selected marks because we dont need to hear events on all of our sheets if (marksEvent.getWorksheet().getName() == nameOfVizToInteract) { //get,marksAsync() is a method in the API that will retun a set of the marks selected return marksEvent.getMarksAsync().then(handleSelectedMarks); } } function handleSelectedMarks(marks) { // If selection has been cleared, no need to show a message if (marks.length == 0) { $('#eventBox').hide(600); return; } // Save selected marks in memory so they can be submitted later selectedMarks = marks; $('#eventPanel').html(""); $('#eventBox').show(600); // Logic for Equities Dashboard is specialized, any other scatterplots also are enabled but in a general sense if (workbook.getActiveSheet().getName() == 'Individual Equities Dashboard') { //loop through all the selected marks var noOrders = 0; var company = ""; var fixedClose = 0; var fixedCloseLabel = ""; var changeFromPriorClose = 0; var changeFromPriorCloseLabel = ""; var date = ""; for (var markIndex = 0; markIndex < marks.length; markIndex++) { //getPairs gets tuples of data for the mark. one mark has multiple tuples var pairs = marks[markIndex].getPairs(); for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) { switch (pairs[pairIndex].fieldName) { case "Company": company = pairs[pairIndex].value; break; case "Date": date = pairs[pairIndex].formattedValue; break; case "SUM(Fixed Close)": fixedClose += pairs[pairIndex].value; fixedCloseLabel = pairs[pairIndex].formattedValue; break; case "AGG(Change from Prior Close)": changeFromPriorClose += pairs[pairIndex].value; changeFromPriorCloseLabel = pairs[pairIndex].formattedValue; break; } } } // With all values in memory, let's produce the UI if (marks.length == 1) { // When we select a single mark, we can show the individual details $('#eventPanel').html("Submit <b>" + company + "</b>'s " + date + " trading period for research. The fixed close price was <b>$" + fixedCloseLabel + "</b> with a variance of <b>" + changeFromPriorCloseLabel + "</b>."); } else { // But if more that one mark is selected, we show a summary (average) var avgFixedClose = Number((fixedClose / marks.length).toFixed(2)); var avgChangeFromPriorCloseLabel = Number((changeFromPriorClose * 100 / marks.length).toFixed(2)); $('#eventPanel').html("Submit <b>" + marks.length + " " + company + "</b>'s trading periods for research. The average fixed close price was <b>$" + avgFixedClose + "</b> & the average variance of <b>" + avgChangeFromPriorCloseLabel + "%</b>."); } } else { // Save selection in memory and give the user the option to submit var plural = ((marks.length == 1) ? "it" : "them"); var pluralS = ((selectedMarks.length == 1) ? "" : "s"); $('#eventPanel').html("You've selected <b>" + marks.length + "</b> outlier" + pluralS + "." + " Would you like to submit " + plural + " them for research?"); } } function submitMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } tableauWriteBack(selectedMarks); //var plural = ((selectedMarks.length == 1) ? "" : "s"); //$('#eventPanel').html("Success! <b>" + selectedMarks.length + "</b> selection" + plural + " submitted for research."); //$('#eventBox').hide(2000); } function resetAllMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } $('#eventBox').hide(800); $('#eventPanel').html(""); } function la
{ // Adjust UI: Hide Buttons & navigation menu, increase size for edit mode $('#VizToolbar').hide(); $('body').addClass("sidebar-collapse"); $(".content-wrapper").css("height","1200px"); $("#tableauViz").hide(); // If the URL happens to have a ticket on it, clean it up before loading the edit window var url_parts = url.split('/t/'); url = tableauServer + '/t/' + url_parts[1]; var edit_location = tableauServer + '/en/embed_wrapper.html?src=' + url + '?:embed=y'; edit_iframe = document.createElement('iframe'); edit_iframe.src = edit_location; // This makes it not look like an iframe edit_iframe.style.padding = '0px'; edit_iframe.style.border = 'none'; edit_iframe.style.margin = '0px'; // Also set these with the same values in the embed_wrapper.html page edit_iframe.style.width = '100%'; edit_iframe.style.height = '100%'; $('#editViz').html(edit_iframe); $('#editViz').show(); } function iframe_change(new_url) { console.log("Old URL received in iframe_change: " + url); console.log("New URL received in iframe_change: " + new_url); // Destroy the original edit_iframe so
unch_edit()
identifier_name
TableauViz.js
Filters</button>'); divIndividualButtons.append('<button type="button" onclick="exportPDF()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export PDF</button>'); divIndividualButtons.append('<button type="button" onclick="exportData()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export Data</button>'); divIndividualButtons.append('<button type="button" onclick="launch_edit()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Edit</button>'); // Only show buttons to switch vizzes if there's more than one if (sheets.length > 1) { for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { var sheet = sheets[sheetIndex]; divIndividualButtons.append('<button type="button" onclick="switchToViz(\'' + sheet.getName() + '\')" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">See ' + sheet.getName() + '</button>') } } } function switchToViz(vizName) { workbook.activateSheetAsync(vizName).then(function (dashboard) { dashboard.changeSizeAsync({ behavior: tableau.SheetSizeBehavior.AUTOMATIC }); }); } function onMarksSelection(marksEvent) { //filter sheets of selected marks because we dont need to hear events on all of our sheets if (marksEvent.getWorksheet().getName() == nameOfVizToInteract) { //get,marksAsync() is a method in the API that will retun a set of the marks selected return marksEvent.getMarksAsync().then(handleSelectedMarks); } } function handleSelectedMarks(marks) { // If selection has been cleared, no need to show a message if (marks.length == 0) { $('#eventBox').hide(600); return; } // Save selected marks in memory so they can be submitted later selectedMarks = marks; $('#eventPanel').html(""); $('#eventBox').show(600); // Logic for Equities Dashboard is specialized, any other scatterplots also are enabled but in a general sense if (workbook.getActiveSheet().getName() == 'Individual Equities Dashboard') { //loop through all the selected marks var noOrders = 0; var company = ""; var fixedClose = 0; var fixedCloseLabel = ""; var changeFromPriorClose = 0; var changeFromPriorCloseLabel = ""; var date = ""; for (var markIndex = 0; markIndex < marks.length; markIndex++) { //getPairs gets tuples of data for the mark. one mark has multiple tuples var pairs = marks[markIndex].getPairs(); for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) { switch (pairs[pairIndex].fieldName) { case "Company": company = pairs[pairIndex].value; break; case "Date": date = pairs[pairIndex].formattedValue; break; case "SUM(Fixed Close)": fixedClose += pairs[pairIndex].value; fixedCloseLabel = pairs[pairIndex].formattedValue; break; case "AGG(Change from Prior Close)": changeFromPriorClose += pairs[pairIndex].value; changeFromPriorCloseLabel = pairs[pairIndex].formattedValue; break; } } } // With all values in memory, let's produce the UI if (marks.length == 1) { // When we select a single mark, we can show the individual details $('#eventPanel').html("Submit <b>" + company + "</b>'s " + date + " trading period for research. The fixed close price was <b>$" + fixedCloseLabel + "</b> with a variance of <b>" + changeFromPriorCloseLabel + "</b>."); } else { // But if more that one mark is selected, we show a summary (average) var avgFixedClose = Number((fixedClose / marks.length).toFixed(2)); var avgChangeFromPriorCloseLabel = Number((changeFromPriorClose * 100 / marks.length).toFixed(2)); $('#eventPanel').html("Submit <b>" + marks.length + " " + company + "</b>'s trading periods for research. The average fixed close price was <b>$" + avgFixedClose + "</b> & the average variance of <b>" + avgChangeFromPriorCloseLabel + "%</b>."); } } else { // Save selection in memory and give the user the option to submit var plural = ((marks.length == 1) ? "it" : "them"); var pluralS = ((selectedMarks.length == 1) ? "" : "s"); $('#eventPanel').html("You've selected <b>" + marks.length + "</b> outlier" + pluralS + "." + " Would you like to submit " + plural + " them for research?"); } } function submitMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } tableauWriteBack(selectedMarks); //var plural = ((selectedMarks.length == 1) ? "" : "s"); //$('#eventPanel').html("Success! <b>" + selectedMarks.length + "</b> selection" + plural + " submitted for research."); //$('#eventBox').hide(2000); } function resetAllMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } $('#eventBox').hide(800); $('#eventPanel').html(""); } function launch_edit() { // Adjust UI: Hide Buttons & navigation menu, increase size for edit mode $('#VizToolbar').hide(); $('body').addClass("sidebar-collapse"); $(".content-wrapper").css("height","1200px"); $("#tableauViz").hide(); // If the URL happens to have a ticket on it, clean it up before loading the edit window var url_parts = url.split('/t/'); url = tableauServer + '/t/' + url_parts[1]; var edit_location = tableauServer + '/en/embed_wrapper.html?src=' + url + '?:embed=y'; edit_iframe = document.createElement('iframe'); edit_iframe.src = edit_location; // This makes it not look like an iframe edit_iframe.style.padding = '0px'; edit_iframe.style.border = 'none'; edit_iframe.style.margin = '0px'; // Also set these with the same values in the embed_wrapper.html page edit_iframe.style.width = '100%'; edit_iframe.style.height = '100%'; $('#editViz').html(edit_iframe); $('#editViz').show(); } function iframe_change(new_url) { console.log("Old URL received in iframe_change: " + url); console.log("New URL received in iframe_change: " + new_url); // Destroy the original edit_iframe so you can build another one later if necessary $(edit_iframe).remove(); // Destroy the original Tableau Viz object so you can create new one with URL of the Save(d) As version viz.dispose(); // Reset the global vizURL at this point so that it all works circularly // But first remove any embed/authoring attributes from the URL var url_parts = new_url.split('?'); url = url_parts[0].replace('/authoring', '/views'); // Handle site if (url.search('/site/') !== -1) {
url_parts = url.split('#/site/'); url = url_parts[0] + "t/" + url_parts[1]; vizUrlForWebEdit = url; console.log("URL updated in iframe_change: " + url); }
conditional_block
TableauViz.js
function exportPDF() { viz.showExportPDFDialog(); $('.tab-dialog')[0].animate({ 'marginLeft': "-=50px" }); } function exportData() { viz.showExportDataDialog(); } function resetViz() { viz.revertAllAsync(); } function showVizButtons() { var sheets = workbook.getPublishedSheetsInfo(); var divIndividualButtons = $('#vizButtons'); // First clear any buttons that may have been added on a previous load divIndividualButtons.html(""); // Show 'standard' controls, common to all vizzes divIndividualButtons.append('<button type="button" onclick="resetViz()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Reset Filters</button>'); divIndividualButtons.append('<button type="button" onclick="exportPDF()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export PDF</button>'); divIndividualButtons.append('<button type="button" onclick="exportData()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Export Data</button>'); divIndividualButtons.append('<button type="button" onclick="launch_edit()" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">Edit</button>'); // Only show buttons to switch vizzes if there's more than one if (sheets.length > 1) { for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { var sheet = sheets[sheetIndex]; divIndividualButtons.append('<button type="button" onclick="switchToViz(\'' + sheet.getName() + '\')" class="btn btn-primary" style="min-width:135px; margin-right: 5px; margin-top: 5px;">See ' + sheet.getName() + '</button>') } } } function switchToViz(vizName) { workbook.activateSheetAsync(vizName).then(function (dashboard) { dashboard.changeSizeAsync({ behavior: tableau.SheetSizeBehavior.AUTOMATIC }); }); } function onMarksSelection(marksEvent) { //filter sheets of selected marks because we dont need to hear events on all of our sheets if (marksEvent.getWorksheet().getName() == nameOfVizToInteract) { //get,marksAsync() is a method in the API that will retun a set of the marks selected return marksEvent.getMarksAsync().then(handleSelectedMarks); } } function handleSelectedMarks(marks) { // If selection has been cleared, no need to show a message if (marks.length == 0) { $('#eventBox').hide(600); return; } // Save selected marks in memory so they can be submitted later selectedMarks = marks; $('#eventPanel').html(""); $('#eventBox').show(600); // Logic for Equities Dashboard is specialized, any other scatterplots also are enabled but in a general sense if (workbook.getActiveSheet().getName() == 'Individual Equities Dashboard') { //loop through all the selected marks var noOrders = 0; var company = ""; var fixedClose = 0; var fixedCloseLabel = ""; var changeFromPriorClose = 0; var changeFromPriorCloseLabel = ""; var date = ""; for (var markIndex = 0; markIndex < marks.length; markIndex++) { //getPairs gets tuples of data for the mark. one mark has multiple tuples var pairs = marks[markIndex].getPairs(); for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) { switch (pairs[pairIndex].fieldName) { case "Company": company = pairs[pairIndex].value; break; case "Date": date = pairs[pairIndex].formattedValue; break; case "SUM(Fixed Close)": fixedClose += pairs[pairIndex].value; fixedCloseLabel = pairs[pairIndex].formattedValue; break; case "AGG(Change from Prior Close)": changeFromPriorClose += pairs[pairIndex].value; changeFromPriorCloseLabel = pairs[pairIndex].formattedValue; break; } } } // With all values in memory, let's produce the UI if (marks.length == 1) { // When we select a single mark, we can show the individual details $('#eventPanel').html("Submit <b>" + company + "</b>'s " + date + " trading period for research. The fixed close price was <b>$" + fixedCloseLabel + "</b> with a variance of <b>" + changeFromPriorCloseLabel + "</b>."); } else { // But if more that one mark is selected, we show a summary (average) var avgFixedClose = Number((fixedClose / marks.length).toFixed(2)); var avgChangeFromPriorCloseLabel = Number((changeFromPriorClose * 100 / marks.length).toFixed(2)); $('#eventPanel').html("Submit <b>" + marks.length + " " + company + "</b>'s trading periods for research. The average fixed close price was <b>$" + avgFixedClose + "</b> & the average variance of <b>" + avgChangeFromPriorCloseLabel + "%</b>."); } } else { // Save selection in memory and give the user the option to submit var plural = ((marks.length == 1) ? "it" : "them"); var pluralS = ((selectedMarks.length == 1) ? "" : "s"); $('#eventPanel').html("You've selected <b>" + marks.length + "</b> outlier" + pluralS + "." + " Would you like to submit " + plural + " them for research?"); } } function submitMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } tableauWriteBack(selectedMarks); //var plural = ((selectedMarks.length == 1) ? "" : "s"); //$('#eventPanel').html("Success! <b>" + selectedMarks.length + "</b> selection" + plural + " submitted for research."); //$('#eventBox').hide(2000); } function resetAllMarks() { var referrer = viz.getWorkbook().getActiveSheet(); if (referrer.getSheetType() == "dashboard") { // The active sheets is a dashboard, which is made of several sheets var sheets = referrer.getWorksheets(); // Iterate over the sheets until we find the correct one and clear the marks for (var sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) { if (sheets[sheetIndex].getName() == nameOfVizToInteract) { sheets[sheetIndex].clearSelectedMarksAsync(); } } } else { // This is not a dashboard so just clear the sheet's selection referrer.clearSelectedMarksAsync(); } $('#eventBox').hide(800); $('#eventPanel').html(""); } function launch_edit() { // Adjust UI: Hide Buttons & navigation menu, increase size for edit mode $('#VizToolbar').hide(); $('body').addClass("sidebar-collapse"); $(".content-wrapper").css("height","1200px"); $("#tableauViz").hide(); // If the URL happens to have a ticket on it, clean it up before loading the edit window var url_parts = url.split('/t/'); url = tableauServer + '/t/' + url_parts[1]; var edit_location = tableauServer + '/en/embed_wrapper.html?src=' + url + '?:embed=y'; edit_iframe = document.createElement('iframe'); edit_iframe.src = edit_location; // This makes it not look like an iframe edit_iframe.style.padding = '0px'; edit_iframe.style.border = 'none'; edit_iframe.style.margin = '0px'; // Also set these with the same values in the embed_wrapper.html page edit_iframe.style.width = '100%'; edit_iframe.style.height = '100%'; $('#editViz').html(edit_iframe); $('#editViz').show(); } function iframe_change(new_url) { console.log("Old URL received in iframe_change: " + url); console.log("New URL received in iframe_change: " + new
viz = new tableau.Viz(placeholderDiv, url, options); }
random_line_split
__init__.py
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import import six __author__ = 'xepa4ep, a@toukmanov.ru' import re from ..exceptions import Error, RequiredArgumentIsNotPassed, MethodDoesNotExist from ..rules.contexts import Value, ContextNotFound from ..rules.case import DummyCase from tml.strings import to_string, suggest_string def need_to_escape(options): """ Need escape string Args: options (dict): translation options Returns: boolean """
return options['escape'] return True def escape_if_needed(text, options): """ Escape string if it needed Agrs: text (string): text to escape options (dict): tranlation options (if key safe is True - do not escape) Returns: text """ if hasattr(text, '__html__'): # Text has escape itself: return to_string(text.__html__()) if need_to_escape(options): return escape(to_string(text)) return to_string(text) ESCAPE_CHARS = (('&', '&amp;'), ('<', '&lt;'), ('>', '&gt;'), ('"', '&quot;'), ("'", '&#39;')) def escape(text): """ Escape text Args: text: input text Returns: (string): escaped HTML """ for find, replace in ESCAPE_CHARS: text = text.replace(find, replace) return text class AbstractToken(object): """ Base token class """ @classmethod def validate(self, text): """ Check that string is valid token Args: str (string): token string implimentation Returns: AbstractToken """ raise NotImplementedError() def execute(self, data, options): """ Execute token for data with options data (dict): data options (dict): execution options """ raise NotImplementedError() class TextToken(AbstractToken): """ Plain text """ def __init__(self, text): """ .ctor Args: text (string): token text """ self.text = text def execute(self, data, options): """ Execute token Returns: (string) """ return self.text @classmethod def validate(cls, text, language): """ Validate tokenized string Args: text(string): token text language (Language): token language Returns: TextToken|None """ if text == '': # Empty text return TextToken(text) if text[0] != '{': return TextToken(text) def __str__(self): return "TextToken[%s]" % self.text class AbstractVariableToken(AbstractToken): IS_VARIABLE = '([\$\d\w]+)' IS_METHOD = '([\$\d\w])' REGEXP_TOKEN = '^\{%s\}$' def __init__(self, name): self.name = name def fetch(self, data): try: if self.name == '$0': return data return data[self.name] except KeyError: raise RequiredArgumentIsNotPassed(self.name, data) class VariableToken(AbstractVariableToken): """ Token for variabel {name} """ # Regext to check objects IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % AbstractVariableToken.IS_VARIABLE) def __init__(self, name): """ Args: name (string): variable name """ self.name = name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ return escape_if_needed(self.fetch(data), options) def fetch(self, data): """ Fetch variable""" return suggest_string(super(VariableToken, self).fetch(data)) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return VariableToken(m.group(1)) def __str__(self): return 'VariableToken[%s]' % self.name class MethodToken(VariableToken): # Method Token Forms # # {user.name} # {user.name:gender} HAS_METHOD = '\.(\w*\s*)' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + HAS_METHOD)) def __init__(self, name, method_name): VariableToken.__init__(self, name) self.method_name = method_name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ obj = self.fetch(data) if isinstance(obj, dict) and self.method_name in obj: # if dict return obj[self.method_name] try: prop = getattr(obj, self.method_name) rv = callable(prop) and prop() or prop return escape_if_needed(rv, options) except AttributeError: raise MethodDoesNotExist(self.name, self.method_name) def fetch(self, data): """ Fetch variable""" return super(VariableToken, self).fetch(data) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return MethodToken(m.group(1), m.group(2)) class RulesToken(AbstractVariableToken): """ Token which execute some rules on variable {count|token, tokens}: count = 1 -> token count = 100 -> tokens """ def __init__(self, name, rules, language): """ .ctor Args: name (string): variable name rules (string): rules string language (Language): current language """ super(RulesToken, self).__init__(name) self.rules = rules self.language = language TOKEN_TYPE_REGEX = '\|([^\|]{1}(.*))' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + TOKEN_TYPE_REGEX,)) """ Compiler for rules """ @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), language) def execute(self, data, options): """ Execute token with var """ return self.language.contexts.execute(self.rules, self.fetch(data)).strip() def find_context(self, token=None): token = token or self.name try: return self.language.contexts.find_by_code(token) except ContextNotFound: return self.language.contexts.find_by_token_name(token) def __str__(self): return "RulesToken[%s, choices=%s]" % (self.name, self.rules) class RulesMethodToken(RulesToken): @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), m.group(3), language) class CaseToken(RulesToken): """ Language keys {name::nom} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\:\:(.*)',)) def __init__(self, name, case, language): super(RulesToken, self).__init__(name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( suggest_string(self.case.execute(self.fetch(data))), options) def __str__(self): return "CaseToken[%s, case=%s]" % (self.name, self.case) class CaseMethodToken(RulesMethodToken): IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % ( AbstractVariableToken.IS_VARIABLE + MethodToken.HAS_METHOD + '\:\:(.*)')) def __init__(self, name, method_name, case, language): self.token = MethodToken(name, method_name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( self.case.execute(self.token.execute(data, {'escape': False})), options) def __str__(self): return "CaseMethodToken[%s, case=%s]" % (self.name, self.case) class UnsupportedCase(Error): def __init__(self, language, case): self.language = language self.case = case def __str__(self): return 'Language does not support case %s for locale %s' % (self.case, self.language.locale) class PipeToken(RulesToken): """ Token which pipe rules and join it with variable {count||token, tokens}: count = 1 -> 1 token count = 100 -> 100 tokens works like {name||rules} == {name} {name|rules} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\|\|(.*)',)) def __init__(self, name, rules, language): self.token = VariableToken(name) self.rules = RulesToken(name, rules, language)
if 'escape' in options:
random_line_split
__init__.py
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import import six __author__ = 'xepa4ep, a@toukmanov.ru' import re from ..exceptions import Error, RequiredArgumentIsNotPassed, MethodDoesNotExist from ..rules.contexts import Value, ContextNotFound from ..rules.case import DummyCase from tml.strings import to_string, suggest_string def need_to_escape(options): """ Need escape string Args: options (dict): translation options Returns: boolean """ if 'escape' in options: return options['escape'] return True def escape_if_needed(text, options): """ Escape string if it needed Agrs: text (string): text to escape options (dict): tranlation options (if key safe is True - do not escape) Returns: text """ if hasattr(text, '__html__'): # Text has escape itself: return to_string(text.__html__()) if need_to_escape(options): return escape(to_string(text)) return to_string(text) ESCAPE_CHARS = (('&', '&amp;'), ('<', '&lt;'), ('>', '&gt;'), ('"', '&quot;'), ("'", '&#39;')) def escape(text): """ Escape text Args: text: input text Returns: (string): escaped HTML """ for find, replace in ESCAPE_CHARS: text = text.replace(find, replace) return text class AbstractToken(object): """ Base token class """ @classmethod def validate(self, text): """ Check that string is valid token Args: str (string): token string implimentation Returns: AbstractToken """ raise NotImplementedError() def execute(self, data, options): """ Execute token for data with options data (dict): data options (dict): execution options """ raise NotImplementedError() class TextToken(AbstractToken): """ Plain text """ def __init__(self, text): """ .ctor Args: text (string): token text """ self.text = text def execute(self, data, options): """ Execute token Returns: (string) """ return self.text @classmethod def validate(cls, text, language): """ Validate tokenized string Args: text(string): token text language (Language): token language Returns: TextToken|None """ if text == '': # Empty text return TextToken(text) if text[0] != '{': return TextToken(text) def __str__(self): return "TextToken[%s]" % self.text class AbstractVariableToken(AbstractToken): IS_VARIABLE = '([\$\d\w]+)' IS_METHOD = '([\$\d\w])' REGEXP_TOKEN = '^\{%s\}$' def __init__(self, name): self.name = name def fetch(self, data): try: if self.name == '$0': return data return data[self.name] except KeyError: raise RequiredArgumentIsNotPassed(self.name, data) class VariableToken(AbstractVariableToken): """ Token for variabel {name} """ # Regext to check objects IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % AbstractVariableToken.IS_VARIABLE) def __init__(self, name): """ Args: name (string): variable name """ self.name = name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ return escape_if_needed(self.fetch(data), options) def fetch(self, data): """ Fetch variable""" return suggest_string(super(VariableToken, self).fetch(data)) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return VariableToken(m.group(1)) def __str__(self): return 'VariableToken[%s]' % self.name class MethodToken(VariableToken): # Method Token Forms # # {user.name} # {user.name:gender} HAS_METHOD = '\.(\w*\s*)' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + HAS_METHOD)) def __init__(self, name, method_name): VariableToken.__init__(self, name) self.method_name = method_name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ obj = self.fetch(data) if isinstance(obj, dict) and self.method_name in obj: # if dict return obj[self.method_name] try: prop = getattr(obj, self.method_name) rv = callable(prop) and prop() or prop return escape_if_needed(rv, options) except AttributeError: raise MethodDoesNotExist(self.name, self.method_name) def fetch(self, data):
@classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return MethodToken(m.group(1), m.group(2)) class RulesToken(AbstractVariableToken): """ Token which execute some rules on variable {count|token, tokens}: count = 1 -> token count = 100 -> tokens """ def __init__(self, name, rules, language): """ .ctor Args: name (string): variable name rules (string): rules string language (Language): current language """ super(RulesToken, self).__init__(name) self.rules = rules self.language = language TOKEN_TYPE_REGEX = '\|([^\|]{1}(.*))' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + TOKEN_TYPE_REGEX,)) """ Compiler for rules """ @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), language) def execute(self, data, options): """ Execute token with var """ return self.language.contexts.execute(self.rules, self.fetch(data)).strip() def find_context(self, token=None): token = token or self.name try: return self.language.contexts.find_by_code(token) except ContextNotFound: return self.language.contexts.find_by_token_name(token) def __str__(self): return "RulesToken[%s, choices=%s]" % (self.name, self.rules) class RulesMethodToken(RulesToken): @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), m.group(3), language) class CaseToken(RulesToken): """ Language keys {name::nom} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\:\:(.*)',)) def __init__(self, name, case, language): super(RulesToken, self).__init__(name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( suggest_string(self.case.execute(self.fetch(data))), options) def __str__(self): return "CaseToken[%s, case=%s]" % (self.name, self.case) class CaseMethodToken(RulesMethodToken): IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % ( AbstractVariableToken.IS_VARIABLE + MethodToken.HAS_METHOD + '\:\:(.*)')) def __init__(self, name, method_name, case, language): self.token = MethodToken(name, method_name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( self.case.execute(self.token.execute(data, {'escape': False})), options) def __str__(self): return "CaseMethodToken[%s, case=%s]" % (self.name, self.case) class UnsupportedCase(Error): def __init__(self, language, case): self.language = language self.case = case def __str__(self): return 'Language does not support case %s for locale %s' % (self.case, self.language.locale) class PipeToken(RulesToken): """ Token which pipe rules and join it with variable {count||token, tokens}: count = 1 -> 1 token count = 100 -> 100 tokens works like {name||rules} == {name} {name|rules} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\|\|(.*)',)) def __init__(self, name, rules, language): self.token = VariableToken(name) self.rules = RulesToken(name, rules, language
""" Fetch variable""" return super(VariableToken, self).fetch(data)
identifier_body
__init__.py
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import import six __author__ = 'xepa4ep, a@toukmanov.ru' import re from ..exceptions import Error, RequiredArgumentIsNotPassed, MethodDoesNotExist from ..rules.contexts import Value, ContextNotFound from ..rules.case import DummyCase from tml.strings import to_string, suggest_string def need_to_escape(options): """ Need escape string Args: options (dict): translation options Returns: boolean """ if 'escape' in options: return options['escape'] return True def escape_if_needed(text, options): """ Escape string if it needed Agrs: text (string): text to escape options (dict): tranlation options (if key safe is True - do not escape) Returns: text """ if hasattr(text, '__html__'): # Text has escape itself: return to_string(text.__html__()) if need_to_escape(options): return escape(to_string(text)) return to_string(text) ESCAPE_CHARS = (('&', '&amp;'), ('<', '&lt;'), ('>', '&gt;'), ('"', '&quot;'), ("'", '&#39;')) def escape(text): """ Escape text Args: text: input text Returns: (string): escaped HTML """ for find, replace in ESCAPE_CHARS: text = text.replace(find, replace) return text class AbstractToken(object): """ Base token class """ @classmethod def validate(self, text): """ Check that string is valid token Args: str (string): token string implimentation Returns: AbstractToken """ raise NotImplementedError() def execute(self, data, options): """ Execute token for data with options data (dict): data options (dict): execution options """ raise NotImplementedError() class TextToken(AbstractToken): """ Plain text """ def __init__(self, text): """ .ctor Args: text (string): token text """ self.text = text def execute(self, data, options): """ Execute token Returns: (string) """ return self.text @classmethod def validate(cls, text, language): """ Validate tokenized string Args: text(string): token text language (Language): token language Returns: TextToken|None """ if text == '': # Empty text
if text[0] != '{': return TextToken(text) def __str__(self): return "TextToken[%s]" % self.text class AbstractVariableToken(AbstractToken): IS_VARIABLE = '([\$\d\w]+)' IS_METHOD = '([\$\d\w])' REGEXP_TOKEN = '^\{%s\}$' def __init__(self, name): self.name = name def fetch(self, data): try: if self.name == '$0': return data return data[self.name] except KeyError: raise RequiredArgumentIsNotPassed(self.name, data) class VariableToken(AbstractVariableToken): """ Token for variabel {name} """ # Regext to check objects IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % AbstractVariableToken.IS_VARIABLE) def __init__(self, name): """ Args: name (string): variable name """ self.name = name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ return escape_if_needed(self.fetch(data), options) def fetch(self, data): """ Fetch variable""" return suggest_string(super(VariableToken, self).fetch(data)) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return VariableToken(m.group(1)) def __str__(self): return 'VariableToken[%s]' % self.name class MethodToken(VariableToken): # Method Token Forms # # {user.name} # {user.name:gender} HAS_METHOD = '\.(\w*\s*)' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + HAS_METHOD)) def __init__(self, name, method_name): VariableToken.__init__(self, name) self.method_name = method_name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ obj = self.fetch(data) if isinstance(obj, dict) and self.method_name in obj: # if dict return obj[self.method_name] try: prop = getattr(obj, self.method_name) rv = callable(prop) and prop() or prop return escape_if_needed(rv, options) except AttributeError: raise MethodDoesNotExist(self.name, self.method_name) def fetch(self, data): """ Fetch variable""" return super(VariableToken, self).fetch(data) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return MethodToken(m.group(1), m.group(2)) class RulesToken(AbstractVariableToken): """ Token which execute some rules on variable {count|token, tokens}: count = 1 -> token count = 100 -> tokens """ def __init__(self, name, rules, language): """ .ctor Args: name (string): variable name rules (string): rules string language (Language): current language """ super(RulesToken, self).__init__(name) self.rules = rules self.language = language TOKEN_TYPE_REGEX = '\|([^\|]{1}(.*))' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + TOKEN_TYPE_REGEX,)) """ Compiler for rules """ @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), language) def execute(self, data, options): """ Execute token with var """ return self.language.contexts.execute(self.rules, self.fetch(data)).strip() def find_context(self, token=None): token = token or self.name try: return self.language.contexts.find_by_code(token) except ContextNotFound: return self.language.contexts.find_by_token_name(token) def __str__(self): return "RulesToken[%s, choices=%s]" % (self.name, self.rules) class RulesMethodToken(RulesToken): @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), m.group(3), language) class CaseToken(RulesToken): """ Language keys {name::nom} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\:\:(.*)',)) def __init__(self, name, case, language): super(RulesToken, self).__init__(name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( suggest_string(self.case.execute(self.fetch(data))), options) def __str__(self): return "CaseToken[%s, case=%s]" % (self.name, self.case) class CaseMethodToken(RulesMethodToken): IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % ( AbstractVariableToken.IS_VARIABLE + MethodToken.HAS_METHOD + '\:\:(.*)')) def __init__(self, name, method_name, case, language): self.token = MethodToken(name, method_name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( self.case.execute(self.token.execute(data, {'escape': False})), options) def __str__(self): return "CaseMethodToken[%s, case=%s]" % (self.name, self.case) class UnsupportedCase(Error): def __init__(self, language, case): self.language = language self.case = case def __str__(self): return 'Language does not support case %s for locale %s' % (self.case, self.language.locale) class PipeToken(RulesToken): """ Token which pipe rules and join it with variable {count||token, tokens}: count = 1 -> 1 token count = 100 -> 100 tokens works like {name||rules} == {name} {name|rules} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\|\|(.*)',)) def __init__(self, name, rules, language): self.token = VariableToken(name) self.rules = RulesToken(name, rules, language
return TextToken(text)
conditional_block
__init__.py
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import absolute_import import six __author__ = 'xepa4ep, a@toukmanov.ru' import re from ..exceptions import Error, RequiredArgumentIsNotPassed, MethodDoesNotExist from ..rules.contexts import Value, ContextNotFound from ..rules.case import DummyCase from tml.strings import to_string, suggest_string def need_to_escape(options): """ Need escape string Args: options (dict): translation options Returns: boolean """ if 'escape' in options: return options['escape'] return True def escape_if_needed(text, options): """ Escape string if it needed Agrs: text (string): text to escape options (dict): tranlation options (if key safe is True - do not escape) Returns: text """ if hasattr(text, '__html__'): # Text has escape itself: return to_string(text.__html__()) if need_to_escape(options): return escape(to_string(text)) return to_string(text) ESCAPE_CHARS = (('&', '&amp;'), ('<', '&lt;'), ('>', '&gt;'), ('"', '&quot;'), ("'", '&#39;')) def escape(text): """ Escape text Args: text: input text Returns: (string): escaped HTML """ for find, replace in ESCAPE_CHARS: text = text.replace(find, replace) return text class AbstractToken(object): """ Base token class """ @classmethod def validate(self, text): """ Check that string is valid token Args: str (string): token string implimentation Returns: AbstractToken """ raise NotImplementedError() def execute(self, data, options): """ Execute token for data with options data (dict): data options (dict): execution options """ raise NotImplementedError() class TextToken(AbstractToken): """ Plain text """ def __init__(self, text): """ .ctor Args: text (string): token text """ self.text = text def execute(self, data, options): """ Execute token Returns: (string) """ return self.text @classmethod def validate(cls, text, language): """ Validate tokenized string Args: text(string): token text language (Language): token language Returns: TextToken|None """ if text == '': # Empty text return TextToken(text) if text[0] != '{': return TextToken(text) def __str__(self): return "TextToken[%s]" % self.text class AbstractVariableToken(AbstractToken): IS_VARIABLE = '([\$\d\w]+)' IS_METHOD = '([\$\d\w])' REGEXP_TOKEN = '^\{%s\}$' def __init__(self, name): self.name = name def fetch(self, data): try: if self.name == '$0': return data return data[self.name] except KeyError: raise RequiredArgumentIsNotPassed(self.name, data) class VariableToken(AbstractVariableToken): """ Token for variabel {name} """ # Regext to check objects IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % AbstractVariableToken.IS_VARIABLE) def __init__(self, name): """ Args: name (string): variable name """ self.name = name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ return escape_if_needed(self.fetch(data), options) def fetch(self, data): """ Fetch variable""" return suggest_string(super(VariableToken, self).fetch(data)) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return VariableToken(m.group(1)) def __str__(self): return 'VariableToken[%s]' % self.name class MethodToken(VariableToken): # Method Token Forms # # {user.name} # {user.name:gender} HAS_METHOD = '\.(\w*\s*)' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + HAS_METHOD)) def __init__(self, name, method_name): VariableToken.__init__(self, name) self.method_name = method_name def execute(self, data, options): """ Fetch and escape variable from data Args: data (dict): input data options (dict): translation options Returns: string """ obj = self.fetch(data) if isinstance(obj, dict) and self.method_name in obj: # if dict return obj[self.method_name] try: prop = getattr(obj, self.method_name) rv = callable(prop) and prop() or prop return escape_if_needed(rv, options) except AttributeError: raise MethodDoesNotExist(self.name, self.method_name) def fetch(self, data): """ Fetch variable""" return super(VariableToken, self).fetch(data) @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return MethodToken(m.group(1), m.group(2)) class RulesToken(AbstractVariableToken): """ Token which execute some rules on variable {count|token, tokens}: count = 1 -> token count = 100 -> tokens """ def __init__(self, name, rules, language): """ .ctor Args: name (string): variable name rules (string): rules string language (Language): current language """ super(RulesToken, self).__init__(name) self.rules = rules self.language = language TOKEN_TYPE_REGEX = '\|([^\|]{1}(.*))' IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + TOKEN_TYPE_REGEX,)) """ Compiler for rules """ @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), language) def execute(self, data, options): """ Execute token with var """ return self.language.contexts.execute(self.rules, self.fetch(data)).strip() def find_context(self, token=None): token = token or self.name try: return self.language.contexts.find_by_code(token) except ContextNotFound: return self.language.contexts.find_by_token_name(token) def __str__(self): return "RulesToken[%s, choices=%s]" % (self.name, self.rules) class RulesMethodToken(RulesToken): @classmethod def validate(cls, text, language): m = cls.IS_TOKEN.match(text) if m: return cls(m.group(1), m.group(2), m.group(3), language) class CaseToken(RulesToken): """ Language keys {name::nom} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\:\:(.*)',)) def __init__(self, name, case, language): super(RulesToken, self).__init__(name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( suggest_string(self.case.execute(self.fetch(data))), options) def __str__(self): return "CaseToken[%s, case=%s]" % (self.name, self.case) class
(RulesMethodToken): IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % ( AbstractVariableToken.IS_VARIABLE + MethodToken.HAS_METHOD + '\:\:(.*)')) def __init__(self, name, method_name, case, language): self.token = MethodToken(name, method_name) self.case = language.cases.get(str(case), DummyCase()) def execute(self, data, options): """ Execute with rules options """ return escape_if_needed( self.case.execute(self.token.execute(data, {'escape': False})), options) def __str__(self): return "CaseMethodToken[%s, case=%s]" % (self.name, self.case) class UnsupportedCase(Error): def __init__(self, language, case): self.language = language self.case = case def __str__(self): return 'Language does not support case %s for locale %s' % (self.case, self.language.locale) class PipeToken(RulesToken): """ Token which pipe rules and join it with variable {count||token, tokens}: count = 1 -> 1 token count = 100 -> 100 tokens works like {name||rules} == {name} {name|rules} """ IS_TOKEN = re.compile(AbstractVariableToken.REGEXP_TOKEN % (AbstractVariableToken.IS_VARIABLE + '\|\|(.*)',)) def __init__(self, name, rules, language): self.token = VariableToken(name) self.rules = RulesToken(name, rules, language
CaseMethodToken
identifier_name
FindPoints.py
point[1]<150: continue if point[0]>(height-150): continue if point[1]>(width-150): continue temp_centers.append(point) min_distance = 300 cgroup = [] ## Average centers that are to close together. for tcenter in temp_centers: for tcenter2 in temp_centers: if tcenter==tcenter2: continue dist = distance(tcenter, tcenter2) if dist<min_distance: cgroup = [tcenter, tcenter2] min_distance = dist if cgroup==[]: for tcenter in temp_centers: centers.append(tcenter) else: ncenter = findCentre(cgroup) centers.append(ncenter) for tcenter in temp_centers: if tcenter not in cgroup: centers.append(tcenter) else: centers.append(center) return centers def groupCentres(centres): groups = [] for i in range(len(centres)): temp_group = [centres[i]] for j in range(i, len(centres)): if i==j: continue temp_group.append(centres[j]) n = len(temp_group) if n<3: continue if n>10: continue temp_group = orderGroup(temp_group) gheight, gwidth = dimension_of_group(temp_group, "dimension") if len(temp_group)!=n: continue if gheight>1300: continue if gwidth>2300: continue groups.append(orderGroup(temp_group)) return groups def closestGroupSignature(groups, signature, radius): number_of_matches = 0 bestgroup = [] i = 1 j = 0 for group in groups: psignature = overlaySignature(signature, group) match = [] for point in psignature.values(): for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): match.append(centre) continue matches = len(match) if number_of_matches<matches: bestgroup = group j = i number_of_matches = matches i += 1 return bestgroup def getPointdiff(point1, point2): return point1[0]-point2[0], point1[1]-point2[1] def dimension_of_group(group, return_value="centre"): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] if return_value=="centre" or return_value=="center": return [(max_y-min_y)/2+min_y, (max_x-min_x)/2+min_x] if return_value=="rectangle": return [[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]] if return_value=="dimension": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return height, width if return_value=="area": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return width*height return max_y, min_y, max_x, min_x def overlaySignature(signature, group): centre = dimension_of_group(group, "centre") sign_height, sign_width = dimension_of_group(signature.values(), "dimension") sign_height = int(sign_height/2) sign_width = int(sign_width/2) if (centre[0]-sign_height)<0: print "Above" centre[0] += abs(centre[0]-sign_height)+dim/2 if (centre[0]+sign_height)>height: print "Below" centre[0] -= abs(height-(centre[0]+sign_height))+dim/2 if (centre[1]-sign_width)<0: print "Left" centre[1] += abs(centre[1]-sign_width)+dim/2 if (centre[1]+sign_width)>width: print "Right" centre[1] -= abs(width-(centre[1]+sign_width))+dim/2 diff_y, diff_x = getPointdiff(signature['0'], centre) for point in signature: if point=='0': signature['0'] = centre continue signature[point] = [signature[point][0]-diff_y, signature[point][1]-diff_x] return signature def splitGroup(group): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] abovecentres = [] belowcentres = [] group = sorted(group, key=lambda x: x[1]) for centre in group: if abs(centre[0]-max_y)<abs(centre[0]-min_y): belowcentres.append(centre) if abs(centre[0]-max_y)>abs(centre[0]-min_y): abovecentres.append(centre) b_median = belowcentres[len(belowcentres)/2][0] a_median = abovecentres[len(abovecentres)/2][0] belowcentres = filter(lambda x: x[0]<(b_median+400) and x[0]>(b_median-400), belowcentres) abovecentres = filter(lambda x: x[0]<(a_median+400) and x[0]>(a_median-400), abovecentres) return abovecentres, belowcentres def orderGroup(group): abovecentres, belowcentres = splitGroup(group) return abovecentres+belowcentres def control_edge(point, cheight, cwidth): left = (point[1]-(cwidth/2)) right = (point[1]+(cwidth/2)) top = (point[0]-(cheight/2)) bottom = (point[0]+(cheight/2)) if left<0: diff = 0-left left += diff+1 right += diff+1 if right>width: diff = width-right left += diff-1 right += diff-1 if top<0: diff = 0-top top += diff+1 bottom += diff+1 if bottom>height: diff = height-bottom top += diff-1 bottom += diff-1 return top, bottom, left, right def local_Kmeans(mask, k=1): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) if len(blackpixels)==0: return [200, 200] centers = kmeans(blackpixels.T.astype(float), k, iter=20, thresh=1e-08)[0].astype(int) return centers def local_center_of_mass(mask): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) center = np.squeeze(np.asarray(blackpixels.mean(axis=1).astype(int).flatten('C'))) return center def claim_region(mask, top, bottom, left, right): mask[top:bottom, left:right] = 255 def testRadius(signature, group, radius, mask, dim): new_group = [] del signature['0'] for point in signature.values(): match = [] for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): if match==[]: match = centre if distance(match, point)>distance(centre, point):
if match==[]: #new_group.append(point) top, bottom, left, right = control_edge(point, dim, dim) try: npoint = local_Kmeans(mask[top:bottom,left:right])[0] npoint = local_center_of_mass(mask[top:bottom,left:right]) except: new_group.append(point) continue diff_x, diff_y = npoint[0]-(dim/2), npoint[1]-(dim/2) claim_region(mask, top, bottom, left, right) new_group.append([point[0]+diff_x, point[1]+diff_y]) else: top, bottom, left, right = control_edge(point, dim, dim) claim_region(mask, top, bottom, left,
match = centre
conditional_block
FindPoints.py
<min_distance: cgroup = [tcenter, tcenter2] min_distance = dist if cgroup==[]: for tcenter in temp_centers: centers.append(tcenter) else: ncenter = findCentre(cgroup) centers.append(ncenter) for tcenter in temp_centers: if tcenter not in cgroup: centers.append(tcenter) else: centers.append(center) return centers def groupCentres(centres): groups = [] for i in range(len(centres)): temp_group = [centres[i]] for j in range(i, len(centres)): if i==j: continue temp_group.append(centres[j]) n = len(temp_group) if n<3: continue if n>10: continue temp_group = orderGroup(temp_group) gheight, gwidth = dimension_of_group(temp_group, "dimension") if len(temp_group)!=n: continue if gheight>1300: continue if gwidth>2300: continue groups.append(orderGroup(temp_group)) return groups def closestGroupSignature(groups, signature, radius): number_of_matches = 0 bestgroup = [] i = 1 j = 0 for group in groups: psignature = overlaySignature(signature, group) match = [] for point in psignature.values(): for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): match.append(centre) continue matches = len(match) if number_of_matches<matches: bestgroup = group j = i number_of_matches = matches i += 1 return bestgroup def getPointdiff(point1, point2): return point1[0]-point2[0], point1[1]-point2[1] def dimension_of_group(group, return_value="centre"): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] if return_value=="centre" or return_value=="center": return [(max_y-min_y)/2+min_y, (max_x-min_x)/2+min_x] if return_value=="rectangle": return [[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]] if return_value=="dimension": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return height, width if return_value=="area": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return width*height return max_y, min_y, max_x, min_x def overlaySignature(signature, group): centre = dimension_of_group(group, "centre") sign_height, sign_width = dimension_of_group(signature.values(), "dimension") sign_height = int(sign_height/2) sign_width = int(sign_width/2) if (centre[0]-sign_height)<0: print "Above" centre[0] += abs(centre[0]-sign_height)+dim/2 if (centre[0]+sign_height)>height: print "Below" centre[0] -= abs(height-(centre[0]+sign_height))+dim/2 if (centre[1]-sign_width)<0: print "Left" centre[1] += abs(centre[1]-sign_width)+dim/2 if (centre[1]+sign_width)>width: print "Right" centre[1] -= abs(width-(centre[1]+sign_width))+dim/2 diff_y, diff_x = getPointdiff(signature['0'], centre) for point in signature: if point=='0': signature['0'] = centre continue signature[point] = [signature[point][0]-diff_y, signature[point][1]-diff_x] return signature def splitGroup(group): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] abovecentres = [] belowcentres = [] group = sorted(group, key=lambda x: x[1]) for centre in group: if abs(centre[0]-max_y)<abs(centre[0]-min_y): belowcentres.append(centre) if abs(centre[0]-max_y)>abs(centre[0]-min_y): abovecentres.append(centre) b_median = belowcentres[len(belowcentres)/2][0] a_median = abovecentres[len(abovecentres)/2][0] belowcentres = filter(lambda x: x[0]<(b_median+400) and x[0]>(b_median-400), belowcentres) abovecentres = filter(lambda x: x[0]<(a_median+400) and x[0]>(a_median-400), abovecentres) return abovecentres, belowcentres def orderGroup(group): abovecentres, belowcentres = splitGroup(group) return abovecentres+belowcentres def control_edge(point, cheight, cwidth): left = (point[1]-(cwidth/2)) right = (point[1]+(cwidth/2)) top = (point[0]-(cheight/2)) bottom = (point[0]+(cheight/2)) if left<0: diff = 0-left left += diff+1 right += diff+1 if right>width: diff = width-right left += diff-1 right += diff-1 if top<0: diff = 0-top top += diff+1 bottom += diff+1 if bottom>height: diff = height-bottom top += diff-1 bottom += diff-1 return top, bottom, left, right def local_Kmeans(mask, k=1): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) if len(blackpixels)==0: return [200, 200] centers = kmeans(blackpixels.T.astype(float), k, iter=20, thresh=1e-08)[0].astype(int) return centers def local_center_of_mass(mask): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) center = np.squeeze(np.asarray(blackpixels.mean(axis=1).astype(int).flatten('C'))) return center def claim_region(mask, top, bottom, left, right): mask[top:bottom, left:right] = 255 def testRadius(signature, group, radius, mask, dim): new_group = [] del signature['0'] for point in signature.values(): match = [] for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): if match==[]: match = centre if distance(match, point)>distance(centre, point): match = centre if match==[]: #new_group.append(point) top, bottom, left, right = control_edge(point, dim, dim) try: npoint = local_Kmeans(mask[top:bottom,left:right])[0] npoint = local_center_of_mass(mask[top:bottom,left:right]) except: new_group.append(point) continue diff_x, diff_y = npoint[0]-(dim/2), npoint[1]-(dim/2) claim_region(mask, top, bottom, left, right) new_group.append([point[0]+diff_x, point[1]+diff_y]) else: top, bottom, left, right = control_edge(point, dim, dim) claim_region(mask, top, bottom, left, right) new_group.append(match) return new_group def test_content(img, point, dim): #print img.shape
top, bottom, left, right = control_edge(point, dim, dim) img = img[top:bottom,left:right] labmask = LABMaskImage(img) hsvmask = HSVMaskImage(img) mask = combinemasks(labmask, hsvmask) blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) return blackpixels.shape[1]
identifier_body
FindPoints.py
(filename, mask, potsize=500): f = open(filename) centers = [] for line in f: info = line.split(" ")[2:] center = info[2] center = center.split(",") center[0] = int(round(float(center[0]))) center[1] = int(round(float(center[1]))) center = center[::-1] if center[0]<150 or center[1]<150: print "Centre on the edge, and will therefore be removed" continue if center[1]>(width-150) or center[0]>(height-150): print "Centre on the edge, and will therefore be removed" continue size = [int(i) for i in info[1].split("+")[0].split("x")] if size[0]>potsize: #centers.append(center) print "WARNING: Cluster size too big, potential merge of pots detected" print "Employing K-means on the merged blob to find the pots" c = (size[0]//potsize)+1 top, bottom, left, right = control_edge(center, potsize, size[0]+100) npoint = local_Kmeans(mask[top:bottom,left:right], c) temp_centers = [] for point in npoint: diff_x, diff_y = point[0]-int(potsize/2), point[1]-int((size[0]+100)/2) point = [center[0]+diff_x, center[1]+diff_y] if point[0]<150: continue if point[1]<150: continue if point[0]>(height-150): continue if point[1]>(width-150): continue temp_centers.append(point) min_distance = 300 cgroup = [] ## Average centers that are to close together. for tcenter in temp_centers: for tcenter2 in temp_centers: if tcenter==tcenter2: continue dist = distance(tcenter, tcenter2) if dist<min_distance: cgroup = [tcenter, tcenter2] min_distance = dist if cgroup==[]: for tcenter in temp_centers: centers.append(tcenter) else: ncenter = findCentre(cgroup) centers.append(ncenter) for tcenter in temp_centers: if tcenter not in cgroup: centers.append(tcenter) else: centers.append(center) return centers def groupCentres(centres): groups = [] for i in range(len(centres)): temp_group = [centres[i]] for j in range(i, len(centres)): if i==j: continue temp_group.append(centres[j]) n = len(temp_group) if n<3: continue if n>10: continue temp_group = orderGroup(temp_group) gheight, gwidth = dimension_of_group(temp_group, "dimension") if len(temp_group)!=n: continue if gheight>1300: continue if gwidth>2300: continue groups.append(orderGroup(temp_group)) return groups def closestGroupSignature(groups, signature, radius): number_of_matches = 0 bestgroup = [] i = 1 j = 0 for group in groups: psignature = overlaySignature(signature, group) match = [] for point in psignature.values(): for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): match.append(centre) continue matches = len(match) if number_of_matches<matches: bestgroup = group j = i number_of_matches = matches i += 1 return bestgroup def getPointdiff(point1, point2): return point1[0]-point2[0], point1[1]-point2[1] def dimension_of_group(group, return_value="centre"): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] if return_value=="centre" or return_value=="center": return [(max_y-min_y)/2+min_y, (max_x-min_x)/2+min_x] if return_value=="rectangle": return [[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]] if return_value=="dimension": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return height, width if return_value=="area": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return width*height return max_y, min_y, max_x, min_x def overlaySignature(signature, group): centre = dimension_of_group(group, "centre") sign_height, sign_width = dimension_of_group(signature.values(), "dimension") sign_height = int(sign_height/2) sign_width = int(sign_width/2) if (centre[0]-sign_height)<0: print "Above" centre[0] += abs(centre[0]-sign_height)+dim/2 if (centre[0]+sign_height)>height: print "Below" centre[0] -= abs(height-(centre[0]+sign_height))+dim/2 if (centre[1]-sign_width)<0: print "Left" centre[1] += abs(centre[1]-sign_width)+dim/2 if (centre[1]+sign_width)>width: print "Right" centre[1] -= abs(width-(centre[1]+sign_width))+dim/2 diff_y, diff_x = getPointdiff(signature['0'], centre) for point in signature: if point=='0': signature['0'] = centre continue signature[point] = [signature[point][0]-diff_y, signature[point][1]-diff_x] return signature def splitGroup(group): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] abovecentres = [] belowcentres = [] group = sorted(group, key=lambda x: x[1]) for centre in group: if abs(centre[0]-max_y)<abs(centre[0]-min_y): belowcentres.append(centre) if abs(centre[0]-max_y)>abs(centre[0]-min_y): abovecentres.append(centre) b_median = belowcentres[len(belowcentres)/2][0] a_median = abovecentres[len(abovecentres)/2][0] belowcentres = filter(lambda x: x[0]<(b_median+400) and x[0]>(b_median-400), belowcentres) abovecentres = filter(lambda x: x[0]<(a_median+400) and x[0]>(a_median-400), abovecentres) return abovecentres, belowcentres def orderGroup(group): abovecentres, belowcentres = splitGroup(group) return abovecentres+belowcentres def control_edge(point, cheight, cwidth): left = (point[1]-(cwidth/2)) right = (point[1]+(cwidth/2)) top = (point[0]-(cheight/2)) bottom = (point[0]+(cheight/2)) if left<0: diff = 0-left left += diff+1 right += diff+1 if right>width: diff = width-right left += diff-1 right += diff-1 if top<0: diff = 0-top top += diff+1 bottom += diff+1 if bottom>height: diff = height-bottom top += diff-1 bottom += diff-1 return top, bottom, left, right def local_Kmeans(mask, k=1): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) if len(blackpixels)==0: return [200, 200] centers = kmeans(blackpixels.T.astype(float), k, iter=20, thresh=1e-08)[0].astype(int) return centers
loadcenters
identifier_name
FindPoints.py
center = info[2] center = center.split(",") center[0] = int(round(float(center[0]))) center[1] = int(round(float(center[1]))) center = center[::-1] if center[0]<150 or center[1]<150: print "Centre on the edge, and will therefore be removed" continue if center[1]>(width-150) or center[0]>(height-150): print "Centre on the edge, and will therefore be removed" continue size = [int(i) for i in info[1].split("+")[0].split("x")] if size[0]>potsize: #centers.append(center) print "WARNING: Cluster size too big, potential merge of pots detected" print "Employing K-means on the merged blob to find the pots" c = (size[0]//potsize)+1 top, bottom, left, right = control_edge(center, potsize, size[0]+100) npoint = local_Kmeans(mask[top:bottom,left:right], c) temp_centers = [] for point in npoint: diff_x, diff_y = point[0]-int(potsize/2), point[1]-int((size[0]+100)/2) point = [center[0]+diff_x, center[1]+diff_y] if point[0]<150: continue if point[1]<150: continue if point[0]>(height-150): continue if point[1]>(width-150): continue temp_centers.append(point) min_distance = 300 cgroup = [] ## Average centers that are to close together. for tcenter in temp_centers: for tcenter2 in temp_centers: if tcenter==tcenter2: continue dist = distance(tcenter, tcenter2) if dist<min_distance: cgroup = [tcenter, tcenter2] min_distance = dist if cgroup==[]: for tcenter in temp_centers: centers.append(tcenter) else: ncenter = findCentre(cgroup) centers.append(ncenter) for tcenter in temp_centers: if tcenter not in cgroup: centers.append(tcenter) else: centers.append(center) return centers def groupCentres(centres): groups = [] for i in range(len(centres)): temp_group = [centres[i]] for j in range(i, len(centres)): if i==j: continue temp_group.append(centres[j]) n = len(temp_group) if n<3: continue if n>10: continue temp_group = orderGroup(temp_group) gheight, gwidth = dimension_of_group(temp_group, "dimension") if len(temp_group)!=n: continue if gheight>1300: continue if gwidth>2300: continue groups.append(orderGroup(temp_group)) return groups def closestGroupSignature(groups, signature, radius): number_of_matches = 0 bestgroup = [] i = 1 j = 0 for group in groups: psignature = overlaySignature(signature, group) match = [] for point in psignature.values(): for centre in group: if centre[0]<(point[0]+radius) and centre[0]>(point[0]-radius) and centre[1]<(point[1]+radius) and centre[1]>(point[1]-radius): match.append(centre) continue matches = len(match) if number_of_matches<matches: bestgroup = group j = i number_of_matches = matches i += 1 return bestgroup def getPointdiff(point1, point2): return point1[0]-point2[0], point1[1]-point2[1] def dimension_of_group(group, return_value="centre"): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] if return_value=="centre" or return_value=="center": return [(max_y-min_y)/2+min_y, (max_x-min_x)/2+min_x] if return_value=="rectangle": return [[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]] if return_value=="dimension": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return height, width if return_value=="area": width = distance([min_y, min_x], [min_y, max_x]) height = distance([min_y, min_x], [max_y, min_x]) return width*height return max_y, min_y, max_x, min_x def overlaySignature(signature, group): centre = dimension_of_group(group, "centre") sign_height, sign_width = dimension_of_group(signature.values(), "dimension") sign_height = int(sign_height/2) sign_width = int(sign_width/2) if (centre[0]-sign_height)<0: print "Above" centre[0] += abs(centre[0]-sign_height)+dim/2 if (centre[0]+sign_height)>height: print "Below" centre[0] -= abs(height-(centre[0]+sign_height))+dim/2 if (centre[1]-sign_width)<0: print "Left" centre[1] += abs(centre[1]-sign_width)+dim/2 if (centre[1]+sign_width)>width: print "Right" centre[1] -= abs(width-(centre[1]+sign_width))+dim/2 diff_y, diff_x = getPointdiff(signature['0'], centre) for point in signature: if point=='0': signature['0'] = centre continue signature[point] = [signature[point][0]-diff_y, signature[point][1]-diff_x] return signature def splitGroup(group): max_y = max(group, key=lambda x: x[0])[0] min_y = min(group, key=lambda x: x[0])[0] max_x = max(group, key=lambda x: x[1])[1] min_x = min(group, key=lambda x: x[1])[1] abovecentres = [] belowcentres = [] group = sorted(group, key=lambda x: x[1]) for centre in group: if abs(centre[0]-max_y)<abs(centre[0]-min_y): belowcentres.append(centre) if abs(centre[0]-max_y)>abs(centre[0]-min_y): abovecentres.append(centre) b_median = belowcentres[len(belowcentres)/2][0] a_median = abovecentres[len(abovecentres)/2][0] belowcentres = filter(lambda x: x[0]<(b_median+400) and x[0]>(b_median-400), belowcentres) abovecentres = filter(lambda x: x[0]<(a_median+400) and x[0]>(a_median-400), abovecentres) return abovecentres, belowcentres def orderGroup(group): abovecentres, belowcentres = splitGroup(group) return abovecentres+belowcentres def control_edge(point, cheight, cwidth): left = (point[1]-(cwidth/2)) right = (point[1]+(cwidth/2)) top = (point[0]-(cheight/2)) bottom = (point[0]+(cheight/2)) if left<0: diff = 0-left left += diff+1 right += diff+1 if right>width: diff = width-right left += diff-1 right += diff-1 if top<0: diff = 0-top top += diff+1 bottom += diff+1 if bottom>height: diff = height-bottom top += diff-1 bottom += diff-1 return top, bottom, left, right def local_Kmeans(mask, k=1): blackpixels = np.where(mask==0) blackpixels = np.matrix(blackpixels) if len(blackpixels)==0: return [200, 200] centers = kmeans(blackpixels.T.astype(float), k, iter=20, thresh=1e-08)[0].astype(int) return centers def local_center_of_mass(mask): blackpixels = np.where(mask==0) black
centers = [] for line in f: info = line.split(" ")[2:]
random_line_split
provider_validation.go
_, pc := range mod.ProviderConfigs { name := providerName(pc.Name, pc.Alias) // Validate the config against an empty schema to see if it's empty. _, pcConfigDiags := pc.Config.Content(&hcl.BodySchema{}) if pcConfigDiags.HasErrors() || pc.Version.Required != nil { configured[name] = pc.DeclRange } else { emptyConfigs[name] = pc.DeclRange } } if mod.ProviderRequirements != nil { for _, req := range mod.ProviderRequirements.RequiredProviders { localNames[req.Name] = req.Type for _, alias := range req.Aliases { addr := addrs.AbsProviderConfig{ Module: cfg.Path, Provider: req.Type, Alias: alias.Alias, } configAliases[providerName(alias.LocalName, alias.Alias)] = addr } } } // collect providers passed from the parent if parentCall != nil { for _, passed := range parentCall.Providers { name := providerName(passed.InChild.Name, passed.InChild.Alias) passedIn[name] = passed } } parentModuleText := "the root module" moduleText := "the root module" if !cfg.Path.IsRoot() { moduleText = cfg.Path.String() if parent := cfg.Path.Parent(); !parent.IsRoot() { // module address are prefixed with `module.` parentModuleText = parent.String() } } // Verify that any module calls only refer to named providers, and that // those providers will have a configuration at runtime. This way we can // direct users where to add the missing configuration, because the runtime // error is only "missing provider X". for _, modCall := range mod.ModuleCalls { for _, passed := range modCall.Providers { // aliased providers are handled more strictly, and are never // inherited, so they are validated within modules further down. // Skip these checks to prevent redundant diagnostics. if passed.InParent.Alias != "" { continue } name := passed.InParent.String() _, confOK := configured[name] _, localOK := localNames[name] _, passedOK := passedIn[name] // This name was not declared somewhere within in the // configuration. We ignore empty configs, because they will // already produce a warning. if !(confOK || localOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for provider %q.\n\nTo clarify your intent and silence this warning, add to %s a required_providers entry named %q with source = %q, or a different source address if appropriate.", name, moduleText, defAddr.ForDisplay(), parentModuleText, name, defAddr.ForDisplay(), ), Subject: &passed.InParent.NameRange, }) continue } // Now we may have named this provider within the module, but // there won't be a configuration available at runtime if the // parent module did not pass one in. if !cfg.Path.IsRoot() && !(confOK || passedOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The configuration for %s expects to inherit a configuration for provider %s with local name %q, but %s doesn't pass a configuration under that name.\n\nTo satisfy this requirement, add an entry for %q to the \"providers\" argument in the module %q block.", moduleText, defAddr.ForDisplay(), name, parentModuleText, name, parentCall.Name, ), Subject: parentCall.DeclRange.Ptr(), }) } } } if cfg.Path.IsRoot() { // nothing else to do in the root module return diags } // there cannot be any configurations if no provider config is allowed if len(configured) > 0 && noProviderConfigRange != nil { // We report this from the perspective of the use of count, for_each, // or depends_on rather than from inside the module, because the // recipient of this message is more likely to be the author of the // calling module (trying to use an older module that hasn't been // updated yet) than of the called module. diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Module is incompatible with count, for_each, and depends_on", Detail: fmt.Sprintf( "The module at %s is a legacy module which contains its own local provider configurations, and so calls to it may not use the count, for_each, or depends_on arguments.\n\nIf you also control the module %q, consider updating this module to instead expect provider configurations to be passed by its caller.", cfg.Path, cfg.SourceAddr, ), Subject: noProviderConfigRange, }) } // now check that the user is not attempting to override a config for name := range configured { if passed, ok := passedIn[name]; ok { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Cannot override provider configuration", Detail: fmt.Sprintf( "The configuration of %s has its own local configuration for %s, and so it cannot accept an overridden configuration provided by %s.", moduleText, name, parentModuleText, ), Subject: &passed.InChild.NameRange, }) } } // A declared alias requires either a matching configuration within the // module, or one must be passed in. for name, providerAddr := range configAliases { _, confOk := configured[name] _, passedOk := passedIn[name] if confOk || passedOk { continue } diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The child module requires an additional configuration for provider %s, with the local name %q.\n\nRefer to the module's documentation to understand the intended purpose of this additional provider configuration, and then add an entry for %s in the \"providers\" meta-argument in the module block to choose which provider configuration the module should use for that purpose.", providerAddr.Provider.ForDisplay(), name, name, ), Subject: &parentCall.DeclRange, }) } // You cannot pass in a provider that cannot be used for name, passed := range passedIn { childTy := passed.InChild.providerType // get a default type if there was none set if childTy.IsZero() { // This means the child module is only using an inferred // provider type. We allow this but will generate a warning to // declare provider_requirements below. childTy = addrs.NewDefaultProvider(passed.InChild.Name) } providerAddr := addrs.AbsProviderConfig{ Module: cfg.Path, Provider: childTy, Alias: passed.InChild.Alias, } localAddr, localName := localNames[name] if localName { providerAddr.Provider = localAddr } aliasAddr, configAlias := configAliases[name] if configAlias { providerAddr = aliasAddr }
if !(localName || configAlias || emptyConfig) { // we still allow default configs, so switch to a warning if the incoming provider is a default if providerAddr.Provider.IsDefault() { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for %q.\n\nIf you also control the child module, add a required_providers entry named %q with the source address %q.", name, moduleText, providerAddr.Provider.ForDisplay(), name, providerAddr.Provider.ForDisplay(), ), Subject: &passed.InChild.NameRange, }) } else { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "The child module does not declare any provider requirement with the local name %q.\n\nIf you also control the child
_, emptyConfig := emptyConfigs[name]
random_line_split
provider_validation.go
// those providers will have a configuration at runtime. This way we can // direct users where to add the missing configuration, because the runtime // error is only "missing provider X". for _, modCall := range mod.ModuleCalls { for _, passed := range modCall.Providers { // aliased providers are handled more strictly, and are never // inherited, so they are validated within modules further down. // Skip these checks to prevent redundant diagnostics. if passed.InParent.Alias != "" { continue } name := passed.InParent.String() _, confOK := configured[name] _, localOK := localNames[name] _, passedOK := passedIn[name] // This name was not declared somewhere within in the // configuration. We ignore empty configs, because they will // already produce a warning. if !(confOK || localOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for provider %q.\n\nTo clarify your intent and silence this warning, add to %s a required_providers entry named %q with source = %q, or a different source address if appropriate.", name, moduleText, defAddr.ForDisplay(), parentModuleText, name, defAddr.ForDisplay(), ), Subject: &passed.InParent.NameRange, }) continue } // Now we may have named this provider within the module, but // there won't be a configuration available at runtime if the // parent module did not pass one in. if !cfg.Path.IsRoot() && !(confOK || passedOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The configuration for %s expects to inherit a configuration for provider %s with local name %q, but %s doesn't pass a configuration under that name.\n\nTo satisfy this requirement, add an entry for %q to the \"providers\" argument in the module %q block.", moduleText, defAddr.ForDisplay(), name, parentModuleText, name, parentCall.Name, ), Subject: parentCall.DeclRange.Ptr(), }) } } } if cfg.Path.IsRoot() { // nothing else to do in the root module return diags } // there cannot be any configurations if no provider config is allowed if len(configured) > 0 && noProviderConfigRange != nil { // We report this from the perspective of the use of count, for_each, // or depends_on rather than from inside the module, because the // recipient of this message is more likely to be the author of the // calling module (trying to use an older module that hasn't been // updated yet) than of the called module. diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Module is incompatible with count, for_each, and depends_on", Detail: fmt.Sprintf( "The module at %s is a legacy module which contains its own local provider configurations, and so calls to it may not use the count, for_each, or depends_on arguments.\n\nIf you also control the module %q, consider updating this module to instead expect provider configurations to be passed by its caller.", cfg.Path, cfg.SourceAddr, ), Subject: noProviderConfigRange, }) } // now check that the user is not attempting to override a config for name := range configured { if passed, ok := passedIn[name]; ok { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Cannot override provider configuration", Detail: fmt.Sprintf( "The configuration of %s has its own local configuration for %s, and so it cannot accept an overridden configuration provided by %s.", moduleText, name, parentModuleText, ), Subject: &passed.InChild.NameRange, }) } } // A declared alias requires either a matching configuration within the // module, or one must be passed in. for name, providerAddr := range configAliases { _, confOk := configured[name] _, passedOk := passedIn[name] if confOk || passedOk { continue } diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The child module requires an additional configuration for provider %s, with the local name %q.\n\nRefer to the module's documentation to understand the intended purpose of this additional provider configuration, and then add an entry for %s in the \"providers\" meta-argument in the module block to choose which provider configuration the module should use for that purpose.", providerAddr.Provider.ForDisplay(), name, name, ), Subject: &parentCall.DeclRange, }) } // You cannot pass in a provider that cannot be used for name, passed := range passedIn { childTy := passed.InChild.providerType // get a default type if there was none set if childTy.IsZero() { // This means the child module is only using an inferred // provider type. We allow this but will generate a warning to // declare provider_requirements below. childTy = addrs.NewDefaultProvider(passed.InChild.Name) } providerAddr := addrs.AbsProviderConfig{ Module: cfg.Path, Provider: childTy, Alias: passed.InChild.Alias, } localAddr, localName := localNames[name] if localName { providerAddr.Provider = localAddr } aliasAddr, configAlias := configAliases[name] if configAlias { providerAddr = aliasAddr } _, emptyConfig := emptyConfigs[name] if !(localName || configAlias || emptyConfig) { // we still allow default configs, so switch to a warning if the incoming provider is a default if providerAddr.Provider.IsDefault() { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for %q.\n\nIf you also control the child module, add a required_providers entry named %q with the source address %q.", name, moduleText, providerAddr.Provider.ForDisplay(), name, providerAddr.Provider.ForDisplay(), ), Subject: &passed.InChild.NameRange, }) } else { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "The child module does not declare any provider requirement with the local name %q.\n\nIf you also control the child module, you can add a required_providers entry named %q with the source address %q to accept this provider configuration.", name, name, providerAddr.Provider.ForDisplay(), ), Subject: &passed.InChild.NameRange, }) } } // The provider being passed in must also be of the correct type. pTy := passed.InParent.providerType if pTy.IsZero() { // While we would like to ensure required_providers exists here, // implied default configuration is still allowed. pTy = addrs.NewDefaultProvider(passed.InParent.Name) } // use the full address for a nice diagnostic output parentAddr := addrs.AbsProviderConfig{ Module: cfg.Parent.Path, Provider: pTy, Alias: passed.InParent.Alias, } if cfg.Parent.Module.ProviderRequirements != nil { req, defined := cfg.Parent.Module.ProviderRequirements.RequiredProviders[name] if defined { parentAddr.Provider = req.Type } } if !providerAddr.Provider.Equals(parentAddr.Provider) { // If this module declares the same source address for a different // local name then we'll prefer to suggest changing to match // the child module's chosen name, assuming that it was the local // name that was wrong rather than the source address. var otherLocalName string for localName, sourceAddr := range localNames
{ if sourceAddr.Equals(parentAddr.Provider) { otherLocalName = localName break } }
conditional_block
provider_validation.go
providerAddr.Provider.ForDisplay(), name, name, ), Subject: &parentCall.DeclRange, }) } // You cannot pass in a provider that cannot be used for name, passed := range passedIn { childTy := passed.InChild.providerType // get a default type if there was none set if childTy.IsZero() { // This means the child module is only using an inferred // provider type. We allow this but will generate a warning to // declare provider_requirements below. childTy = addrs.NewDefaultProvider(passed.InChild.Name) } providerAddr := addrs.AbsProviderConfig{ Module: cfg.Path, Provider: childTy, Alias: passed.InChild.Alias, } localAddr, localName := localNames[name] if localName { providerAddr.Provider = localAddr } aliasAddr, configAlias := configAliases[name] if configAlias { providerAddr = aliasAddr } _, emptyConfig := emptyConfigs[name] if !(localName || configAlias || emptyConfig) { // we still allow default configs, so switch to a warning if the incoming provider is a default if providerAddr.Provider.IsDefault() { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for %q.\n\nIf you also control the child module, add a required_providers entry named %q with the source address %q.", name, moduleText, providerAddr.Provider.ForDisplay(), name, providerAddr.Provider.ForDisplay(), ), Subject: &passed.InChild.NameRange, }) } else { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "The child module does not declare any provider requirement with the local name %q.\n\nIf you also control the child module, you can add a required_providers entry named %q with the source address %q to accept this provider configuration.", name, name, providerAddr.Provider.ForDisplay(), ), Subject: &passed.InChild.NameRange, }) } } // The provider being passed in must also be of the correct type. pTy := passed.InParent.providerType if pTy.IsZero() { // While we would like to ensure required_providers exists here, // implied default configuration is still allowed. pTy = addrs.NewDefaultProvider(passed.InParent.Name) } // use the full address for a nice diagnostic output parentAddr := addrs.AbsProviderConfig{ Module: cfg.Parent.Path, Provider: pTy, Alias: passed.InParent.Alias, } if cfg.Parent.Module.ProviderRequirements != nil { req, defined := cfg.Parent.Module.ProviderRequirements.RequiredProviders[name] if defined { parentAddr.Provider = req.Type } } if !providerAddr.Provider.Equals(parentAddr.Provider) { // If this module declares the same source address for a different // local name then we'll prefer to suggest changing to match // the child module's chosen name, assuming that it was the local // name that was wrong rather than the source address. var otherLocalName string for localName, sourceAddr := range localNames { if sourceAddr.Equals(parentAddr.Provider) { otherLocalName = localName break } } const errSummary = "Provider type mismatch" if otherLocalName != "" { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: errSummary, Detail: fmt.Sprintf( "The assigned configuration is for provider %q, but local name %q in %s represents %q.\n\nTo pass this configuration to the child module, use the local name %q instead.", parentAddr.Provider.ForDisplay(), passed.InChild.Name, parentModuleText, providerAddr.Provider.ForDisplay(), otherLocalName, ), Subject: &passed.InChild.NameRange, }) } else { // If there is no declared requirement for the provider the // caller is trying to pass under any name then we'll instead // report it as an unsuitable configuration to pass into the // child module's provider configuration slot. diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: errSummary, Detail: fmt.Sprintf( "The local name %q in %s represents provider %q, but %q in %s represents %q.\n\nEach provider has its own distinct configuration schema and provider types, so this module's %q can be assigned only a configuration for %s, which is not required by %s.", passed.InParent, parentModuleText, parentAddr.Provider.ForDisplay(), passed.InChild, moduleText, providerAddr.Provider.ForDisplay(), passed.InChild, providerAddr.Provider.ForDisplay(), moduleText, ), Subject: passed.InParent.NameRange.Ptr(), }) } } } // Empty configurations are no longer needed. Since the replacement for // this calls for one entry per provider rather than one entry per // provider _configuration_, we'll first gather them up by provider // and then report a single warning for each, whereby we can show a direct // example of what the replacement should look like. type ProviderReqSuggestion struct { SourceAddr addrs.Provider SourceRanges []hcl.Range RequiredConfigs []string AliasCount int } providerReqSuggestions := make(map[string]*ProviderReqSuggestion) for name, src := range emptyConfigs { providerLocalName := name if idx := strings.IndexByte(providerLocalName, '.'); idx >= 0 { providerLocalName = providerLocalName[:idx] } sourceAddr, ok := localNames[name] if !ok { sourceAddr = addrs.NewDefaultProvider(providerLocalName) } suggestion := providerReqSuggestions[providerLocalName] if suggestion == nil { providerReqSuggestions[providerLocalName] = &ProviderReqSuggestion{ SourceAddr: sourceAddr, } suggestion = providerReqSuggestions[providerLocalName] } if providerLocalName != name { // It's an aliased provider config, then. suggestion.AliasCount++ } suggestion.RequiredConfigs = append(suggestion.RequiredConfigs, name) suggestion.SourceRanges = append(suggestion.SourceRanges, src) } for name, suggestion := range providerReqSuggestions { var buf strings.Builder fmt.Fprintf( &buf, "Earlier versions of Terraform used empty provider blocks (\"proxy provider configurations\") for child modules to declare their need to be passed a provider configuration by their callers. That approach was ambiguous and is now deprecated.\n\nIf you control this module, you can migrate to the new declaration syntax by removing all of the empty provider %q blocks and then adding or updating an entry like the following to the required_providers block of %s:\n", name, moduleText, ) fmt.Fprintf(&buf, " %s = {\n", name) fmt.Fprintf(&buf, " source = %q\n", suggestion.SourceAddr.ForDisplay()) if suggestion.AliasCount > 0 { // A lexical sort is fine because all of these strings are // guaranteed to start with the same provider local name, and // so we're only really sorting by the alias part. sort.Strings(suggestion.RequiredConfigs) fmt.Fprintln(&buf, " configuration_aliases = [") for _, addrStr := range suggestion.RequiredConfigs { fmt.Fprintf(&buf, " %s,\n", addrStr) } fmt.Fprintln(&buf, " ]") } fmt.Fprint(&buf, " }") // We're arbitrarily going to just take the one source range that // sorts earliest here. Multiple should be rare, so this is only to // ensure that we produce a deterministic result in the edge case. sort.Slice(suggestion.SourceRanges, func(i, j int) bool { return suggestion.SourceRanges[i].String() < suggestion.SourceRanges[j].String() }) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Redundant empty provider block", Detail: buf.String(), Subject: suggestion.SourceRanges[0].Ptr(), }) } return diags } func providerName(name, alias string) string
{ if alias != "" { name = name + "." + alias } return name }
identifier_body
provider_validation.go
(parentCall *ModuleCall, cfg *Config, noProviderConfigRange *hcl.Range) (diags hcl.Diagnostics) { mod := cfg.Module for name, child := range cfg.Children { mc := mod.ModuleCalls[name] childNoProviderConfigRange := noProviderConfigRange // if the module call has any of count, for_each or depends_on, // providers are prohibited from being configured in this module, or // any module beneath this module. switch { case mc.Count != nil: childNoProviderConfigRange = mc.Count.Range().Ptr() case mc.ForEach != nil: childNoProviderConfigRange = mc.ForEach.Range().Ptr() case mc.DependsOn != nil: if len(mc.DependsOn) > 0 { childNoProviderConfigRange = mc.DependsOn[0].SourceRange().Ptr() } else { // Weird! We'll just use the call itself, then. childNoProviderConfigRange = mc.DeclRange.Ptr() } } diags = append(diags, validateProviderConfigs(mc, child, childNoProviderConfigRange)...) } // the set of provider configuration names passed into the module, with the // source range of the provider assignment in the module call. passedIn := map[string]PassedProviderConfig{} // the set of empty configurations that could be proxy configurations, with // the source range of the empty configuration block. emptyConfigs := map[string]hcl.Range{} // the set of provider with a defined configuration, with the source range // of the configuration block declaration. configured := map[string]hcl.Range{} // the set of configuration_aliases defined in the required_providers // block, with the fully qualified provider type. configAliases := map[string]addrs.AbsProviderConfig{} // the set of provider names defined in the required_providers block, and // their provider types. localNames := map[string]addrs.Provider{} for _, pc := range mod.ProviderConfigs { name := providerName(pc.Name, pc.Alias) // Validate the config against an empty schema to see if it's empty. _, pcConfigDiags := pc.Config.Content(&hcl.BodySchema{}) if pcConfigDiags.HasErrors() || pc.Version.Required != nil { configured[name] = pc.DeclRange } else { emptyConfigs[name] = pc.DeclRange } } if mod.ProviderRequirements != nil { for _, req := range mod.ProviderRequirements.RequiredProviders { localNames[req.Name] = req.Type for _, alias := range req.Aliases { addr := addrs.AbsProviderConfig{ Module: cfg.Path, Provider: req.Type, Alias: alias.Alias, } configAliases[providerName(alias.LocalName, alias.Alias)] = addr } } } // collect providers passed from the parent if parentCall != nil { for _, passed := range parentCall.Providers { name := providerName(passed.InChild.Name, passed.InChild.Alias) passedIn[name] = passed } } parentModuleText := "the root module" moduleText := "the root module" if !cfg.Path.IsRoot() { moduleText = cfg.Path.String() if parent := cfg.Path.Parent(); !parent.IsRoot() { // module address are prefixed with `module.` parentModuleText = parent.String() } } // Verify that any module calls only refer to named providers, and that // those providers will have a configuration at runtime. This way we can // direct users where to add the missing configuration, because the runtime // error is only "missing provider X". for _, modCall := range mod.ModuleCalls { for _, passed := range modCall.Providers { // aliased providers are handled more strictly, and are never // inherited, so they are validated within modules further down. // Skip these checks to prevent redundant diagnostics. if passed.InParent.Alias != "" { continue } name := passed.InParent.String() _, confOK := configured[name] _, localOK := localNames[name] _, passedOK := passedIn[name] // This name was not declared somewhere within in the // configuration. We ignore empty configs, because they will // already produce a warning. if !(confOK || localOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Reference to undefined provider", Detail: fmt.Sprintf( "There is no explicit declaration for local provider name %q in %s, so Terraform is assuming you mean to pass a configuration for provider %q.\n\nTo clarify your intent and silence this warning, add to %s a required_providers entry named %q with source = %q, or a different source address if appropriate.", name, moduleText, defAddr.ForDisplay(), parentModuleText, name, defAddr.ForDisplay(), ), Subject: &passed.InParent.NameRange, }) continue } // Now we may have named this provider within the module, but // there won't be a configuration available at runtime if the // parent module did not pass one in. if !cfg.Path.IsRoot() && !(confOK || passedOK) { defAddr := addrs.NewDefaultProvider(name) diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The configuration for %s expects to inherit a configuration for provider %s with local name %q, but %s doesn't pass a configuration under that name.\n\nTo satisfy this requirement, add an entry for %q to the \"providers\" argument in the module %q block.", moduleText, defAddr.ForDisplay(), name, parentModuleText, name, parentCall.Name, ), Subject: parentCall.DeclRange.Ptr(), }) } } } if cfg.Path.IsRoot() { // nothing else to do in the root module return diags } // there cannot be any configurations if no provider config is allowed if len(configured) > 0 && noProviderConfigRange != nil { // We report this from the perspective of the use of count, for_each, // or depends_on rather than from inside the module, because the // recipient of this message is more likely to be the author of the // calling module (trying to use an older module that hasn't been // updated yet) than of the called module. diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Module is incompatible with count, for_each, and depends_on", Detail: fmt.Sprintf( "The module at %s is a legacy module which contains its own local provider configurations, and so calls to it may not use the count, for_each, or depends_on arguments.\n\nIf you also control the module %q, consider updating this module to instead expect provider configurations to be passed by its caller.", cfg.Path, cfg.SourceAddr, ), Subject: noProviderConfigRange, }) } // now check that the user is not attempting to override a config for name := range configured { if passed, ok := passedIn[name]; ok { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Cannot override provider configuration", Detail: fmt.Sprintf( "The configuration of %s has its own local configuration for %s, and so it cannot accept an overridden configuration provided by %s.", moduleText, name, parentModuleText, ), Subject: &passed.InChild.NameRange, }) } } // A declared alias requires either a matching configuration within the // module, or one must be passed in. for name, providerAddr := range configAliases { _, confOk := configured[name] _, passedOk := passedIn[name] if confOk || passedOk { continue } diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Missing required provider configuration", Detail: fmt.Sprintf( "The child module requires an additional configuration for provider %s, with the local name %q.\n\nRefer to the module's documentation to understand the intended purpose of this additional provider configuration, and then add an entry for %s in the \"providers\" meta-argument in the module block to choose which provider configuration the module should use for that purpose.", providerAddr.Provider.ForDisplay(), name, name, ), Subject: &parentCall.DeclRange, }) } // You cannot pass in a provider that cannot be used for name, passed := range passedIn { childTy := passed.InChild.providerType // get a default type
validateProviderConfigs
identifier_name
sup.rs
(no_version)] Status { /// A package identifier (ex: core/redis, core/busybox-static/1.42.2) #[structopt(name = "PKG_IDENT")] pkg_ident: Option<PackageIdent>, #[structopt(flatten)] remote_sup: RemoteSup, }, /// Gracefully terminate the Habitat Supervisor and all of its running services #[structopt(usage = "hab sup term [OPTIONS]", no_version)] Term, } // TODO (DM): This is unnecessarily difficult due to the orphan rule and the lack of specialization. // The `configopt` library could be improved to make this easier. #[derive(Deserialize, Serialize, Debug)] struct
(#[serde(with = "serde_string")] NatsAddress); impl fmt::Display for EventStreamAddress { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } impl FromStr for EventStreamAddress { type Err = RantsError; fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(EventStreamAddress(s.parse()?)) } } impl ConfigOptToString for EventStreamAddress {} #[derive(ConfigOptDefaults, Partial, StructOpt, Deserialize)] #[configopt_defaults(type = "PartialSupRun")] #[partial(derive(Debug, Default, Deserialize), attrs(serde))] #[serde(deny_unknown_fields)] #[structopt(name = "run", no_version, about = "Run the Habitat Supervisor", // set custom usage string, otherwise the binary // is displayed confusingly as `hab-sup` // see: https://github.com/kbknapp/clap-rs/blob/2724ec5399c500b12a1a24d356f4090f4816f5e2/src/app/mod.rs#L373-L394 usage = "hab sup run [FLAGS] [OPTIONS] [--] [PKG_IDENT_OR_ARTIFACT]" )] #[allow(dead_code)] pub struct SupRun { /// The listen address for the Gossip System Gateway #[structopt(name = "LISTEN_GOSSIP", long = "listen-gossip", env = GossipListenAddr::ENVVAR, default_value = GossipListenAddr::default_as_str())] listen_gossip: SocketAddr, /// Start the supervisor in local mode #[structopt(name = "LOCAL_GOSSIP_MODE", long = "local-gossip-mode", conflicts_with_all = &["LISTEN_GOSSIP", "PEER", "PEER_WATCH_FILE"])] local_gossip_mode: bool, /// The listen address for the HTTP Gateway #[structopt(name = "LISTEN_HTTP", long = "listen-http", env = HttpListenAddr::ENVVAR, default_value = HttpListenAddr::default_as_str())] listen_http: SocketAddr, /// Disable the HTTP Gateway completely #[structopt(name = "HTTP_DISABLE", long = "http-disable", short = "D")] http_disable: bool, /// The listen address for the Control Gateway. If not specified, the value will be taken from /// the HAB_LISTEN_CTL environment variable if defined #[structopt(name = "LISTEN_CTL", long = "listen-ctl", env = ListenCtlAddr::ENVVAR, default_value = ListenCtlAddr::default_as_str())] listen_ctl: SocketAddr, /// The organization that the Supervisor and its subsequent services are part of #[structopt(name = "ORGANIZATION", long = "org")] organization: Option<String>, /// The listen address of one or more initial peers (IP[:PORT]) #[structopt(name = "PEER", long = "peer")] // TODO (DM): This could probably be a different type for better validation (Vec<SockAddr>?) peer: Vec<String>, /// If this Supervisor is a permanent peer #[structopt(name = "PERMANENT_PEER", long = "permanent-peer", short = "I")] permanent_peer: bool, /// Watch this file for connecting to the ring #[structopt(name = "PEER_WATCH_FILE", long = "peer-watch-file", conflicts_with = "PEER")] peer_watch_file: PathBuf, #[structopt(flatten)] cache_key_path: CacheKeyPath, /// The name of the ring used by the Supervisor when running with wire encryption. (ex: hab sup /// run --ring myring) #[structopt(name = "RING", long = "ring", short = "r", env = RING_ENVVAR, conflicts_with = "RING_KEY")] ring: String, /// The contents of the ring key when running with wire encryption. (Note: This option is /// explicitly undocumented and for testing purposes only. Do not use it in a production /// system. Use the corresponding environment variable instead.) (ex: hab sup run --ring-key /// 'SYM-SEC-1 foo-20181113185935GCrBOW6CCN75LMl0j2V5QqQ6nNzWm6and9hkKBSUFPI=') #[structopt(name = "RING_KEY", long = "ring-key", env = RING_KEY_ENVVAR, hidden = true, conflicts_with = "RING")] ring_key: Option<String>, /// Receive Supervisor updates from the specified release channel #[structopt(name = "CHANNEL", long = "channel", default_value = "stable")] channel: String, /// Specify an alternate Builder endpoint. If not specified, the value will be taken from the /// HAB_BLDR_URL environment variable if defined (default: https://bldr.habitat.sh) #[structopt(name = "BLDR_URL", long = "url", short = "u", // TODO (DM): These fields are not actual set in the clap macro but I think they should // env = BLDR_URL_ENVVAR, // default_value = DEFAULT_BLDR_URL )] bldr_url: Url, /// Use package config from this path, rather than the package itself #[structopt(name = "CONFIG_DIR", long = "config-from")] config_dir: Option<PathBuf>, /// Enable automatic updates for the Supervisor itself #[structopt(name = "AUTO_UPDATE", long = "auto-update", short = "A")] auto_update: bool, /// Used for enabling TLS for the HTTP gateway. Read private key from KEY_FILE. This should be /// a RSA private key or PKCS8-encoded private key, in PEM format #[structopt(name = "KEY_FILE", long = "key", requires = "CERT_FILE")] key_file: Option<PathBuf>, /// Used for enabling TLS for the HTTP gateway. Read server certificates from CERT_FILE. This /// should contain PEM-format certificates in the right order (the first certificate should /// certify KEY_FILE, the last should be a root CA) #[structopt(name = "CERT_FILE", long = "certs", requires = "KEY_FILE")] cert_file: Option<PathBuf>, /// Used for enabling client-authentication with TLS for the HTTP gateway. Read CA certificate /// from CA_CERT_FILE. This should contain PEM-format certificate that can be used to validate /// client requests #[structopt(name = "CA_CERT_FILE", long = "ca-certs", requires_all = &["CERT_FILE", "KEY_FILE"])] ca_cert_file: Option<PathBuf>, /// Load the given Habitat package as part of the Supervisor startup specified by a package /// identifier (ex: core/redis) or filepath to a Habitat Artifact (ex: /// /home/core-redis-3.0.7-21120102031201-x86_64-linux.hart) // TODO (DM): We could probably do better validation here #[structopt(name = "PKG_IDENT_OR_ARTIFACT")] pkg_ident_or_artifact: Option<String>, // TODO (DM): This flag can eventually be removed. // See https://github.com/habitat-sh/habitat/issues/7339 #[structopt(name = "APPLICATION", long = "application", hidden = true)] application: Vec<String>, // TODO (DM): This flag can eventually be removed. // See https://github.com/habitat-sh/habitat/issues/7339 #[structopt(name = "ENVIRONMENT", long = "environment", hidden = true)] environment: Vec<String>, /// The service group; shared config and topology [default: default] // TODO (DM): This should set a default value #[structopt(name = "GROUP", long = "group")] group: String, /// Service topology; [default: none] // TODO (DM): I dont think saying the default is none makes sense here #[structopt(name = "TOPOLOGY", long = "topology", short = "t", possible_values = &["standalone", "leader"])] topology: Option<habitat_sup_protocol::types::Topology>, /// The update strategy; [default: none] [values: none, at-once, rolling] // TODO (DM): this should set a default_value and
EventStreamAddress
identifier_name
sup.rs
(no_version)] Status { /// A package identifier (ex: core/redis, core/busybox-static/1.42.2) #[structopt(name = "PKG_IDENT")] pkg_ident: Option<PackageIdent>, #[structopt(flatten)] remote_sup: RemoteSup, }, /// Gracefully terminate the Habitat Supervisor and all of its running services #[structopt(usage = "hab sup term [OPTIONS]", no_version)] Term, } // TODO (DM): This is unnecessarily difficult due to the orphan rule and the lack of specialization. // The `configopt` library could be improved to make this easier. #[derive(Deserialize, Serialize, Debug)] struct EventStreamAddress(#[serde(with = "serde_string")] NatsAddress); impl fmt::Display for EventStreamAddress { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } impl FromStr for EventStreamAddress { type Err = RantsError; fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(EventStreamAddress(s.parse()?)) } } impl ConfigOptToString for EventStreamAddress {} #[derive(ConfigOptDefaults, Partial, StructOpt, Deserialize)] #[configopt_defaults(type = "PartialSupRun")] #[partial(derive(Debug, Default, Deserialize), attrs(serde))] #[serde(deny_unknown_fields)] #[structopt(name = "run", no_version, about = "Run the Habitat Supervisor", // set custom usage string, otherwise the binary // is displayed confusingly as `hab-sup` // see: https://github.com/kbknapp/clap-rs/blob/2724ec5399c500b12a1a24d356f4090f4816f5e2/src/app/mod.rs#L373-L394 usage = "hab sup run [FLAGS] [OPTIONS] [--] [PKG_IDENT_OR_ARTIFACT]" )] #[allow(dead_code)] pub struct SupRun { /// The listen address for the Gossip System Gateway #[structopt(name = "LISTEN_GOSSIP", long = "listen-gossip", env = GossipListenAddr::ENVVAR, default_value = GossipListenAddr::default_as_str())] listen_gossip: SocketAddr, /// Start the supervisor in local mode #[structopt(name = "LOCAL_GOSSIP_MODE", long = "local-gossip-mode", conflicts_with_all = &["LISTEN_GOSSIP", "PEER", "PEER_WATCH_FILE"])] local_gossip_mode: bool, /// The listen address for the HTTP Gateway #[structopt(name = "LISTEN_HTTP", long = "listen-http", env = HttpListenAddr::ENVVAR, default_value = HttpListenAddr::default_as_str())] listen_http: SocketAddr, /// Disable the HTTP Gateway completely #[structopt(name = "HTTP_DISABLE", long = "http-disable", short = "D")] http_disable: bool, /// The listen address for the Control Gateway. If not specified, the value will be taken from /// the HAB_LISTEN_CTL environment variable if defined #[structopt(name = "LISTEN_CTL", long = "listen-ctl", env = ListenCtlAddr::ENVVAR, default_value = ListenCtlAddr::default_as_str())] listen_ctl: SocketAddr, /// The organization that the Supervisor and its subsequent services are part of #[structopt(name = "ORGANIZATION", long = "org")] organization: Option<String>, /// The listen address of one or more initial peers (IP[:PORT]) #[structopt(name = "PEER", long = "peer")] // TODO (DM): This could probably be a different type for better validation (Vec<SockAddr>?) peer: Vec<String>, /// If this Supervisor is a permanent peer #[structopt(name = "PERMANENT_PEER", long = "permanent-peer", short = "I")] permanent_peer: bool, /// Watch this file for connecting to the ring #[structopt(name = "PEER_WATCH_FILE", long = "peer-watch-file", conflicts_with = "PEER")] peer_watch_file: PathBuf,
/// run --ring myring) #[structopt(name = "RING", long = "ring", short = "r", env = RING_ENVVAR, conflicts_with = "RING_KEY")] ring: String, /// The contents of the ring key when running with wire encryption. (Note: This option is /// explicitly undocumented and for testing purposes only. Do not use it in a production /// system. Use the corresponding environment variable instead.) (ex: hab sup run --ring-key /// 'SYM-SEC-1 foo-20181113185935GCrBOW6CCN75LMl0j2V5QqQ6nNzWm6and9hkKBSUFPI=') #[structopt(name = "RING_KEY", long = "ring-key", env = RING_KEY_ENVVAR, hidden = true, conflicts_with = "RING")] ring_key: Option<String>, /// Receive Supervisor updates from the specified release channel #[structopt(name = "CHANNEL", long = "channel", default_value = "stable")] channel: String, /// Specify an alternate Builder endpoint. If not specified, the value will be taken from the /// HAB_BLDR_URL environment variable if defined (default: https://bldr.habitat.sh) #[structopt(name = "BLDR_URL", long = "url", short = "u", // TODO (DM): These fields are not actual set in the clap macro but I think they should // env = BLDR_URL_ENVVAR, // default_value = DEFAULT_BLDR_URL )] bldr_url: Url, /// Use package config from this path, rather than the package itself #[structopt(name = "CONFIG_DIR", long = "config-from")] config_dir: Option<PathBuf>, /// Enable automatic updates for the Supervisor itself #[structopt(name = "AUTO_UPDATE", long = "auto-update", short = "A")] auto_update: bool, /// Used for enabling TLS for the HTTP gateway. Read private key from KEY_FILE. This should be /// a RSA private key or PKCS8-encoded private key, in PEM format #[structopt(name = "KEY_FILE", long = "key", requires = "CERT_FILE")] key_file: Option<PathBuf>, /// Used for enabling TLS for the HTTP gateway. Read server certificates from CERT_FILE. This /// should contain PEM-format certificates in the right order (the first certificate should /// certify KEY_FILE, the last should be a root CA) #[structopt(name = "CERT_FILE", long = "certs", requires = "KEY_FILE")] cert_file: Option<PathBuf>, /// Used for enabling client-authentication with TLS for the HTTP gateway. Read CA certificate /// from CA_CERT_FILE. This should contain PEM-format certificate that can be used to validate /// client requests #[structopt(name = "CA_CERT_FILE", long = "ca-certs", requires_all = &["CERT_FILE", "KEY_FILE"])] ca_cert_file: Option<PathBuf>, /// Load the given Habitat package as part of the Supervisor startup specified by a package /// identifier (ex: core/redis) or filepath to a Habitat Artifact (ex: /// /home/core-redis-3.0.7-21120102031201-x86_64-linux.hart) // TODO (DM): We could probably do better validation here #[structopt(name = "PKG_IDENT_OR_ARTIFACT")] pkg_ident_or_artifact: Option<String>, // TODO (DM): This flag can eventually be removed. // See https://github.com/habitat-sh/habitat/issues/7339 #[structopt(name = "APPLICATION", long = "application", hidden = true)] application: Vec<String>, // TODO (DM): This flag can eventually be removed. // See https://github.com/habitat-sh/habitat/issues/7339 #[structopt(name = "ENVIRONMENT", long = "environment", hidden = true)] environment: Vec<String>, /// The service group; shared config and topology [default: default] // TODO (DM): This should set a default value #[structopt(name = "GROUP", long = "group")] group: String, /// Service topology; [default: none] // TODO (DM): I dont think saying the default is none makes sense here #[structopt(name = "TOPOLOGY", long = "topology", short = "t", possible_values = &["standalone", "leader"])] topology: Option<habitat_sup_protocol::types::Topology>, /// The update strategy; [default: none] [values: none, at-once, rolling] // TODO (DM): this should set a default_value and use possible
#[structopt(flatten)] cache_key_path: CacheKeyPath, /// The name of the ring used by the Supervisor when running with wire encryption. (ex: hab sup
random_line_split
texture.rs
Interaction, dstdx: &mut Vector2f, dstdy: &mut Vector2f) -> Point2f { let st = self.sphere(&si.p); // Compute texture coordinate differentials for sphere (u, v) mapping let delta = 0.1; let st_deltax = self.sphere(&(si.p + si.dpdx.get() * delta)); *dstdx = (st_deltax - st) / delta; let st_deltay = self.sphere(&(si.p + si.dpdy.get() * delta)); *dstdy = (st_deltay - st) / delta; // Handle sphere mapping discontinuity for coordinate differentials if dstdx[1] > 0.5 { dstdx[1] = 1.0 - dstdx[1]; } else if (*dstdx)[1] < -0.5 { (*dstdx)[1] = -((*dstdx)[1] + 1.0); } if dstdy[1] > 0.5 { dstdy[1] = 1.0 - dstdy[1]; } else if dstdy[1] < -0.5 { dstdy[1] = -(dstdy[1] + 1.0); } st } } pub struct CylindricalMapping2D { world_to_texture: Transform } impl CylindricalMapping2D { pub fn new(wtt: &Transform) -> Self { Self { world_to_texture: *wtt } } fn cylinder(&self, p: &Point3f) -> Point2f { let vec = ( self.world_to_texture.transform_point(p) - Point3f::new(0.0, 0.0, 0.0)) .normalize(); Point2f::new(PI + vec.y.atan2(vec.x) * INV2_PI, vec.z) } } impl TextureMapping2D for CylindricalMapping2D { fn map(&self, si: &SurfaceInteraction, dstdx: &mut Vector2f, dstdy: &mut Vector2f) -> Point2f { let st = self.cylinder(&si.p); // Compute texture coordinate differentials for cylinder (u, v) mapping let delta = 0.1; let st_deltax = self.cylinder(&(si.p + si.dpdx.get() * delta)); *dstdx = (st_deltax - st) / delta; let st_deltay = self.cylinder(&(si.p + si.dpdy.get() * delta)); *dstdy = (st_deltay - st) / delta; // Handle sphere mapping discontinuity for coordinate differentials if dstdx[1] > 0.5 { dstdx[1] = 1.0 - dstdx[1]; } else if (*dstdx)[1] < -0.5 { (*dstdx)[1] = -((*dstdx)[1] + 1.0); } if dstdy[1] > 0.5 { dstdy[1] = 1.0 - dstdy[1]; } else if dstdy[1] < -0.5 { dstdy[1] = -(dstdy[1] + 1.0); } st } } pub struct PlannarMapping2D { vs: Vector3f, vt: Vector3f, ds: Float, dt: Float } impl PlannarMapping2D { pub fn new(vs: &Vector3f, vt: &Vector3f, ds: Float, dt: Float) -> Self { Self { ds, dt, vs: *vs, vt: *vt } } } impl TextureMapping2D for PlannarMapping2D { fn map(&self, si: &SurfaceInteraction, dstdx: &mut Vector2f, dstdy: &mut Vector2f) -> Point2f { let vec = Vector3f::from(si.p); *dstdx = Vector2f::new(si.dpdx.get().dot(&self.vs), si.dpdx.get().dot(&self.vt)); *dstdy = Vector2f::new(si.dpdy.get().dot(&self.vs), si.dpdy.get().dot(&self.vt)); Point2f::new(self.ds + vec.dot(&self.vs), self.dt + vec.dot(&self.vt)) } } #[enum_dispatch] pub trait TextureMapping3D { fn map(&self, si: &SurfaceInteraction, dpdx: &mut Vector3f, dpdy: &mut Vector3f) -> Point3f; } #[enum_dispatch(TextureMapping3D)] pub enum TextureMapping3Ds { IdentityMapping3D } pub struct IdentityMapping3D { world_to_texture: Transform } impl IdentityMapping3D { pub fn new(w2t: &Transform) -> Self { Self { world_to_texture: *w2t } } } impl TextureMapping3D for IdentityMapping3D { fn map(&self, si: &SurfaceInteraction, dpdx: &mut Vector3f, dpdy: &mut Vector3f) -> Point3f { *dpdx = self.world_to_texture.transform_vector(&si.dpdx.get()); *dpdy = self.world_to_texture.transform_vector(&si.dpdy.get()); self.world_to_texture.transform_point(&si.p) } } pub fn lanczos(mut x: Float, tau: Float) -> Float { x = x.abs(); if x < 1.0e-5 { return 1.0; } if x > 1.0 { return 0.0; } x *= PI; let s = (x * tau).sin() / ( x * tau); let lanc = x.sin() / x; s * lanc } pub fn noise(x: Float, y: Float, z: Float) -> Float { let mut ix = x.floor() as usize; let mut iy = y.floor() as usize; let mut iz = z.floor() as usize; let dx = x - ix as Float; let dy = y - iy as Float; let dz = z - iz as Float; // Compute gradient weights ix &= NOISE_PERM_SIZE - 1; iy &= NOISE_PERM_SIZE - 1; iz &= NOISE_PERM_SIZE - 1; let w000 = grad(ix, iy, iz, dx, dy, dz); let w100 = grad(ix + 1, iy, iz, dx - 1.0, dy, dz); let w010 = grad(ix, iy + 1, iz, dx, dy - 1.0, dz); let w110 = grad(ix + 1, iy + 1, iz, dx - 1.0, dy - 1.0, dz); let w001 = grad(ix, iy, iz + 1, dx, dy, dz - 1.0); let w101 = grad(ix + 1, iy, iz + 1, dx - 1.0, dy, dz - 1.0); let w011 = grad(ix, iy + 1, iz + 1, dx, dy - 1.0, dz - 1.0); let w111 = grad(ix + 1, iy + 1, iz + 1, dx - 1.0, dy - 1.0, dz - 1.0); // Compute trilinear interpolation of weights let wx = noise_weight(dx); let wy = noise_weight(dy); let wz = noise_weight(dz); let x00 = lerp(wx, w000, w100); let x10 = lerp(wx, w010, w110); let x01 = lerp(wx, w001, w101); let x11 = lerp(wx, w011, w111); let y0 = lerp(wy, x00, x10); let y1 = lerp(wy, x01, x11); lerp(wz, y0, y1) } pub fn noisep(p: Point3f) -> Float { noise(p.x, p.y, p.z) } fn grad(x: usize, y: usize, z: usize, dx: Float, dy: Float, dz: Float) -> Float { let mut h = NOISE_PERM[NOISE_PERM[NOISE_PERM[x] + y] + z]; h &= 15; let u = if h < 8 || h == 12 || h == 13 { dx } else { dy }; let v = if h < 4 || h == 12 || h == 13 { dy } else { dz }; (if (h & 1) != 0 { -u } else { u }) + (if (h & 2) != 0
{ -v }
conditional_block
texture.rs
ay = self.cylinder(&(si.p + si.dpdy.get() * delta)); *dstdy = (st_deltay - st) / delta; // Handle sphere mapping discontinuity for coordinate differentials if dstdx[1] > 0.5 { dstdx[1] = 1.0 - dstdx[1]; } else if (*dstdx)[1] < -0.5 { (*dstdx)[1] = -((*dstdx)[1] + 1.0); } if dstdy[1] > 0.5 { dstdy[1] = 1.0 - dstdy[1]; } else if dstdy[1] < -0.5 { dstdy[1] = -(dstdy[1] + 1.0); } st } } pub struct PlannarMapping2D { vs: Vector3f, vt: Vector3f, ds: Float, dt: Float } impl PlannarMapping2D { pub fn new(vs: &Vector3f, vt: &Vector3f, ds: Float, dt: Float) -> Self { Self { ds, dt, vs: *vs, vt: *vt } } } impl TextureMapping2D for PlannarMapping2D { fn map(&self, si: &SurfaceInteraction, dstdx: &mut Vector2f, dstdy: &mut Vector2f) -> Point2f { let vec = Vector3f::from(si.p); *dstdx = Vector2f::new(si.dpdx.get().dot(&self.vs), si.dpdx.get().dot(&self.vt)); *dstdy = Vector2f::new(si.dpdy.get().dot(&self.vs), si.dpdy.get().dot(&self.vt)); Point2f::new(self.ds + vec.dot(&self.vs), self.dt + vec.dot(&self.vt)) } } #[enum_dispatch] pub trait TextureMapping3D { fn map(&self, si: &SurfaceInteraction, dpdx: &mut Vector3f, dpdy: &mut Vector3f) -> Point3f; } #[enum_dispatch(TextureMapping3D)] pub enum TextureMapping3Ds { IdentityMapping3D } pub struct IdentityMapping3D { world_to_texture: Transform } impl IdentityMapping3D { pub fn new(w2t: &Transform) -> Self { Self { world_to_texture: *w2t } } } impl TextureMapping3D for IdentityMapping3D { fn map(&self, si: &SurfaceInteraction, dpdx: &mut Vector3f, dpdy: &mut Vector3f) -> Point3f { *dpdx = self.world_to_texture.transform_vector(&si.dpdx.get()); *dpdy = self.world_to_texture.transform_vector(&si.dpdy.get()); self.world_to_texture.transform_point(&si.p) } } pub fn lanczos(mut x: Float, tau: Float) -> Float { x = x.abs(); if x < 1.0e-5 { return 1.0; } if x > 1.0 { return 0.0; } x *= PI; let s = (x * tau).sin() / ( x * tau); let lanc = x.sin() / x; s * lanc } pub fn noise(x: Float, y: Float, z: Float) -> Float { let mut ix = x.floor() as usize; let mut iy = y.floor() as usize; let mut iz = z.floor() as usize; let dx = x - ix as Float; let dy = y - iy as Float; let dz = z - iz as Float; // Compute gradient weights ix &= NOISE_PERM_SIZE - 1; iy &= NOISE_PERM_SIZE - 1; iz &= NOISE_PERM_SIZE - 1; let w000 = grad(ix, iy, iz, dx, dy, dz); let w100 = grad(ix + 1, iy, iz, dx - 1.0, dy, dz); let w010 = grad(ix, iy + 1, iz, dx, dy - 1.0, dz); let w110 = grad(ix + 1, iy + 1, iz, dx - 1.0, dy - 1.0, dz); let w001 = grad(ix, iy, iz + 1, dx, dy, dz - 1.0); let w101 = grad(ix + 1, iy, iz + 1, dx - 1.0, dy, dz - 1.0); let w011 = grad(ix, iy + 1, iz + 1, dx, dy - 1.0, dz - 1.0); let w111 = grad(ix + 1, iy + 1, iz + 1, dx - 1.0, dy - 1.0, dz - 1.0); // Compute trilinear interpolation of weights let wx = noise_weight(dx); let wy = noise_weight(dy); let wz = noise_weight(dz); let x00 = lerp(wx, w000, w100); let x10 = lerp(wx, w010, w110); let x01 = lerp(wx, w001, w101); let x11 = lerp(wx, w011, w111); let y0 = lerp(wy, x00, x10); let y1 = lerp(wy, x01, x11); lerp(wz, y0, y1) } pub fn noisep(p: Point3f) -> Float { noise(p.x, p.y, p.z) } fn grad(x: usize, y: usize, z: usize, dx: Float, dy: Float, dz: Float) -> Float { let mut h = NOISE_PERM[NOISE_PERM[NOISE_PERM[x] + y] + z]; h &= 15; let u = if h < 8 || h == 12 || h == 13 { dx } else { dy }; let v = if h < 4 || h == 12 || h == 13 { dy } else { dz }; (if (h & 1) != 0 { -u } else { u }) + (if (h & 2) != 0 { -v } else { v }) } fn noise_weight(t: Float) -> Float { let t3 = t * t * t; let t4 = t3 * t; 6.0 * t4 * t - 15.0 * t4 + 10.0 * t3 } pub fn fbm( p: &Point3f, dpdx: &Vector3f, dpdy: &Vector3f, omega: Float, max_octaves: usize) -> Float { // Compute number of octaves for antialiased FBm let len2 = dpdx.length_squared().max(dpdy.length_squared()); let n = clamp(-1.0 - 0.5 * log2(len2), 0.0, max_octaves as Float); let nint = n.floor() as usize; // Compute sum of octaves of noise for fbm let (mut sum, mut lambda, mut o) = (0.0, 1.0, 1.0); for _i in 0..nint { sum += o * noisep(*p * lambda); lambda *= 1.99; o *= omega; } let npartial = n - nint as Float; sum += o * smooth_step(0.3, 0.7, npartial) * noisep(*p * lambda); sum } pub fn turbulence( p: &Point3f, dpdx: &Vector3f, dpdy: &Vector3f, omega: Float, max_octaves: usize) -> Float { // Compute number of octaves for antialiased FBm let len2 = dpdx.length_squared().max(dpdy.length_squared()); let n = clamp(-1.0 - 0.5 * len2.log2(), 0.0, max_octaves as Float); let nint = n.floor() as usize; // Compute sum of octaves of noise for turbulence let (mut sum, mut lambda, mut o) = (0.0, 1.0, 1.0); for _i in 0..nint { sum += o + noisep(*p * lambda).abs(); lambda *= 1.99; o *= omega; } // Account for contributions of clamped octaves in turbulence let npartial = n - nint as Float; sum += o + lerp(
smooth_step(0.3, 0.7, npartial), 0.2, noisep(*p * lambda).abs());
random_line_split