<commit_before>/*
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
	"flag"
	"json"
	"log"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"syscall"

	"camli/blobref"
	"camli/osutil"
)

// These override the JSON config file ~/.camli/config "server" and
// "password" keys
var flagServer *string = flag.String("blobserver", "", "camlistore blob server")
var flagPassword *string = flag.String("password", "", "password for blob server")

func ConfigFilePath() string {
	return filepath.Join(osutil.CamliConfigDir(), "config")
}

var configOnce sync.Once
var config = make(map[string]interface{})

func parseConfig() {
	f, err := os.Open(ConfigFilePath())
	switch {
	case err != nil && err.(*os.PathError).Error.(os.Errno) == syscall.ENOENT:
		// TODO: write empty file?
		return
	case err != nil:
		log.Printf("Error opening config file %q: %v", ConfigFilePath(), err)
		return
	default:
		defer f.Close()
		dj := json.NewDecoder(f)
		if err := dj.Decode(&config); err != nil {
			log.Printf("Error parsing JSON in config file %q: %v", ConfigFilePath(), err)
		}
	}
}

func cleanServer(server string) string {
	// Remove trailing slash if provided.
	if strings.HasSuffix(server, "/") {
		server = server[0 : len(server)-1]
	}
	// Add "http://" prefix if not present:
	if !strings.HasPrefix(server, "http") {
		server = "http://" + server
	}
	return server
}
<commit_msg>client config: use JSON errorutil.HighlightBytePosition helper<commit_after>/*
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
	"flag"
	"fmt"
	"json"
	"log"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"syscall"

	"camli/blobref"
	"camli/errorutil"
	"camli/osutil"
)

// These override the JSON config file ~/.camli/config "server" and
// "password" keys
var flagServer *string = flag.String("blobserver", "", "camlistore blob server")
var flagPassword *string = flag.String("password", "", "password for blob server")

func ConfigFilePath() string {
	return filepath.Join(osutil.CamliConfigDir(), "config")
}

var configOnce sync.Once
var config = make(map[string]interface{})

func parseConfig() {
	f, err := os.Open(ConfigFilePath())
	switch {
	case err != nil && err.(*os.PathError).Error.(os.Errno) == syscall.ENOENT:
		// TODO: write empty file?
		return
	case err != nil:
		log.Printf("Error opening config file %q: %v", ConfigFilePath(), err)
		return
	default:
		defer f.Close()
		dj := json.NewDecoder(f)
		if err := dj.Decode(&config); err != nil {
			extra := ""
			if serr, ok := err.(*json.SyntaxError); ok {
				if _, serr := f.Seek(0, os.SEEK_SET); serr != nil {
					log.Fatalf("seek error: %v", serr)
				}
				line, col, highlight := errorutil.HighlightBytePosition(f, serr.Offset)
				extra = fmt.Sprintf(":\nError at line %d, column %d (file offset %d):\n%s",
					line, col, serr.Offset, highlight)
			}
			log.Printf("error parsing JSON object in config file %s%s\n%v",
				ConfigFilePath(), extra, err)
		}
	}
}

func cleanServer(server string) string {
	// Remove trailing slash if provided.
	if strings.HasSuffix(server, "/") {
		server = server[0 : len(server)-1]
	}
	// Add "http://" prefix if not present:
	if !strings.HasPrefix(server, "http") {
		server = "http://" + server
	}
	return server
}

{'fraction_non_alphanumeric': 0.13349001175088132, 'fraction_numerical': 0.004935370152761457, 'mean_word_length': 4.0011750881316095, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3370670', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1348, 'n_words': 463}