예제 #1
1
파일: gajim.go 프로젝트: VKCheung/coyim
func (g *gajimImporter) importOTRSettings(f string) (map[string]gajimOTRSettings, map[gajimAccountAndPeer]gajimOTRSettings, bool) {
	file, err := os.Open(f)
	if err != nil {
		return nil, nil, false
	}
	defer file.Close()
	res, err := stalecucumber.DictString(stalecucumber.Unpickle(file))
	if err != nil {
		return nil, nil, false
	}

	resAccount := make(map[string]gajimOTRSettings)
	resAccountToPeer := make(map[gajimAccountAndPeer]gajimOTRSettings)

	for k, v := range res {
		vv, err := stalecucumber.Dict(v, nil)
		if err == nil {
			for k2, v2 := range vv {
				vv2, _ := stalecucumber.DictString(v2, nil)
				settings := intoGajimOTRSettings(vv2)
				switch k2.(type) {
				case string:
					resAccountToPeer[gajimAccountAndPeer{k, k2.(string)}] = settings
				default:
					resAccount[k] = settings
				}
			}
		}
	}

	return resAccount, resAccountToPeer, true
}
예제 #2
0
파일: services.go 프로젝트: tgres/tgres
func handleGraphitePickleProtocol(rcvr *receiver.Receiver, conn net.Conn, timeout int) {

	defer conn.Close() // decrements graceful.TcpWg

	if timeout != 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
	}

	// We use the Scanner, becase it has a MaxScanTokenSize of 64K

	var (
		name                 string
		tstamp               int64
		int_value            int64
		value                float64
		err                  error
		item                 interface{}
		items, itemSlice, dp []interface{}
	)

	items, err = pickle.ListOrTuple(pickle.Unpickle(conn))
	if err == nil {
		for _, item = range items {
			itemSlice, err = pickle.ListOrTuple(item, err)
			if len(itemSlice) == 2 {
				name, err = pickle.String(itemSlice[0], err)
				dp, err = pickle.ListOrTuple(itemSlice[1], err)
				if len(dp) == 2 {
					tstamp, err = pickle.Int(dp[0], err)
					if value, err = pickle.Float(dp[1], err); err != nil {
						if _, ok := err.(pickle.WrongTypeError); ok {
							if int_value, err = pickle.Int(dp[1], nil); err == nil {
								value = float64(int_value)
							}
						}
					}
					rcvr.QueueDataPoint(name, time.Unix(tstamp, 0), value)
				} else {
					err = fmt.Errorf("dp wrong length: %d", len(dp))
					break
				}
			} else {
				err = fmt.Errorf("item wrong length: %d", len(itemSlice))
				break
			}
		}
	}

	if timeout != 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
	}

	if err != nil {
		if !strings.Contains(err.Error(), "use of closed") {
			log.Println("handleGraphitePickleProtocol(): Error reading: %v", err.Error())
		}
	}
}
예제 #3
0
파일: pickle.go 프로젝트: adfin/gocelery
func (p *PickleSerializer) Deserialize(b []byte, o interface{}) (err error) {
	unpacker := stalecucumber.UnpackInto(o)
	//BUG: this doens't work as pickle cannot deserialize kwargs!!
	unpacker.AllowMismatchedFields = true
	unpacker.AllowMissingFields = true
	err = unpacker.From(stalecucumber.Unpickle(bytes.NewReader(b)))
	//log.Debugf("Pickled: [%s] [%s]", task.ID, task.Task)
	return
}
예제 #4
0
/*
Python object file protocol.
Super specific to the format found in banner.p.
*/
func unpickle(p io.ReadCloser) [][][]interface{} {
	defer p.Close()
	result := make([][][]interface{}, 0)
	err := stalecucumber.UnpackInto(&result).From(stalecucumber.Unpickle(p))
	if err != nil {
		panic(err)
	}
	return result
}
예제 #5
0
// ParseCarbonlinkRequest from pickle encoded data
func ParseCarbonlinkRequest(data []byte) (*CarbonlinkRequest, error) {
	reader := bytes.NewReader(data)
	req := NewCarbonlinkRequest()

	if err := stalecucumber.UnpackInto(req).From(stalecucumber.Unpickle(reader)); err != nil {
		return nil, err
	}

	return req, nil
}
예제 #6
0
파일: bafelbish.go 프로젝트: moul/bafelbish
func Unmarshal(input []byte, inputFormat format) (interface{}, error) {
	var data interface{}
	var err error

	switch inputFormat {
	case formatJSON:
		decoder := json.NewDecoder(bytes.NewReader(input))
		decoder.UseNumber()
		err = decoder.Decode(&data)
		// FIXME: convert numbers to int64
	case formatTOML:
		_, err = toml.Decode(string(input), &data)
		// FIXME: use effective bytes to string instead whole copy
	case formatXML:
		err = xml.Unmarshal(input, &data)
	case formatMsgpack:
	case formatPickle:
		buf := new(bytes.Buffer)
		buf.Write(input)
		err = stalecucumber.UnpackInto(&data).From(stalecucumber.Unpickle(buf))
	case formatBson:
		err = bson.Unmarshal(input, &data)
	case formatPlist:
		input := bytes.NewReader(input)
		decoder := plist.NewDecoder(input)
		err = decoder.Decode(data)
	case formatYAML:
		err = yaml.Unmarshal(input, &data)
		if err == nil {
			data, err = convertMapsToStringMaps(data)
		}
	default:
		err = fmt.Errorf("unsupported input format")
	}

	return data, err
}
예제 #7
0
func (p *PickleSerializer) Loads(reader *strings.Reader) (map[string]interface{}, error) {
	return stalecucumber.DictString(stalecucumber.Unpickle(reader))
}
예제 #8
0
파일: points.go 프로젝트: Gibheer/go-carbon
// ParsePickle ...
func ParsePickle(pkt []byte) ([]*Points, error) {
	result, err := stalecucumber.Unpickle(bytes.NewReader(pkt))

	list, err := stalecucumber.ListOrTuple(result, err)
	if err != nil {
		return nil, err
	}

	msgs := []*Points{}
	for i := 0; i < len(list); i++ {
		metric, err := stalecucumber.ListOrTuple(list[i], nil)
		if err != nil {
			return nil, err
		}

		if len(metric) < 2 {
			return nil, errors.New("Unexpected array length while unpickling metric")
		}

		name, err := stalecucumber.String(metric[0], nil)
		if err != nil {
			return nil, err
		}

		msg := New()
		msg.Metric = name

		for j := 1; j < len(metric); j++ {
			v, err := stalecucumber.ListOrTuple(metric[j], nil)
			if err != nil {
				return nil, err
			}
			if len(v) != 2 {
				return nil, errors.New("Unexpected array length while unpickling data point")
			}
			timestamp, err := stalecucumber.Int(v[0], nil)
			if err != nil {
				timestampFloat, err := stalecucumber.Float(v[0], nil)
				if err != nil {
					return nil, err
				}
				timestamp = int64(timestampFloat)
			}
			if timestamp > math.MaxUint32 || timestamp < 0 {
				err = errors.New("Unexpected value for timestamp, cannot be cast to uint32")
				return nil, err
			}

			value, err := stalecucumber.Float(v[1], nil)
			if err != nil {
				valueInt, err := stalecucumber.Int(v[1], nil)
				if err != nil {
					return nil, err
				}
				value = float64(valueInt)
			}

			msg.Add(value, timestamp)
		}
		msgs = append(msgs, msg)
	}
	return msgs, nil
}
예제 #9
0
func (reply *CarbonlinkReply) LoadBytes(replyBytes []byte) {
	stalecucumber.UnpackInto(reply).From(stalecucumber.Unpickle(bytes.NewReader(replyBytes)))
}