-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for picasa ini files for metadata #430
Draft
lancerushing
wants to merge
1
commit into
simulot:main
Choose a base branch
from
lancerushing:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package picasa | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"github.com/spf13/afero" | ||
"log/slog" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type DirectoryData struct { | ||
Name string | ||
Description string | ||
Location string | ||
Files map[string]FileData | ||
Albums map[string]AlbumData | ||
} | ||
type AlbumData struct { | ||
Name string | ||
Description string | ||
Location string | ||
} | ||
type FileData struct { | ||
IsStar bool | ||
Caption string | ||
Albums []string | ||
} | ||
|
||
var appFS = afero.NewOsFs() | ||
|
||
var DirectoryCache = map[string]DirectoryData{} | ||
|
||
func CacheDirectory(dir string) { | ||
DirectoryCache[dir] = ParseDirectory(dir) | ||
} | ||
|
||
func HasPicasa(dir string) bool { | ||
fileName := path.Join(dir, ".picasa.ini") | ||
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func ParseDirectory(dir string) DirectoryData { | ||
directoryData := DirectoryData{ | ||
Files: map[string]FileData{}, | ||
Albums: map[string]AlbumData{}, | ||
} | ||
|
||
iniMap := parseFile(filepath.Join(dir, ".picasa.ini")) | ||
for sectionName, pairs := range iniMap { | ||
if sectionName == "Picasa" { | ||
if value, ok := pairs["name"]; ok { | ||
directoryData.Name = value | ||
} | ||
if value, ok := pairs["description"]; ok { | ||
directoryData.Description = value | ||
} | ||
if value, ok := pairs["location"]; ok { | ||
directoryData.Location = value | ||
} | ||
} else if strings.HasPrefix(sectionName, ".album:") { | ||
albumData := AlbumData{} | ||
token := sectionName[7:] | ||
if value, ok := pairs["name"]; ok { | ||
albumData.Name = value | ||
} | ||
if value, ok := pairs["description"]; ok { | ||
albumData.Description = value | ||
} | ||
if value, ok := pairs["location"]; ok { | ||
albumData.Location = value | ||
} | ||
directoryData.Albums[token] = albumData | ||
} else { | ||
fileData := FileData{} | ||
if value, ok := pairs["star"]; ok { | ||
fileData.IsStar = value == "yes" | ||
} | ||
if value, ok := pairs["caption"]; ok { | ||
fileData.Caption = value | ||
} | ||
if value, ok := pairs["albums"]; ok { | ||
fileData.Albums = strings.Split(value, ",") | ||
} | ||
directoryData.Files[sectionName] = fileData | ||
} | ||
} | ||
|
||
return directoryData | ||
} | ||
|
||
func parseFile(path string) (result map[string]map[string]string) { | ||
readFile, err := appFS.Open(path) | ||
if err != nil { | ||
slog.Error("could not open file", err) | ||
return | ||
} | ||
defer func() { | ||
err = readFile.Close() | ||
if err != nil { | ||
slog.Error("could not close file", err) | ||
} | ||
}() | ||
|
||
fileScanner := bufio.NewScanner(readFile) | ||
return parseScanner(fileScanner) | ||
} | ||
|
||
func parseScanner(fileScanner *bufio.Scanner) map[string]map[string]string { | ||
result := map[string]map[string]string{} | ||
fileScanner.Split(bufio.ScanLines) | ||
|
||
section := "" | ||
for fileScanner.Scan() { | ||
line := fileScanner.Text() | ||
|
||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
if line[0:1] == "[" { | ||
end := strings.Index(line, "]") | ||
section = line[1:end] | ||
if _, ok := result[section]; !ok { | ||
result[section] = map[string]string{} | ||
} else { | ||
panic("unexpected duplicate picasa.ini section. malformed .picasa.ini file?") | ||
} | ||
|
||
} | ||
if section != "" { | ||
if eqPos := strings.Index(line, "="); eqPos > 0 { | ||
key := line[0:eqPos] | ||
value := line[eqPos+1:] | ||
result[section][key] = value | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (p DirectoryData) DescriptionAndLocation() string { | ||
result := p.Description | ||
if strings.TrimSpace(p.Location) != "" { | ||
result += " Location: " + strings.TrimSpace(p.Location) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package picasa | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/psanford/memfs" | ||
"github.com/spf13/afero" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type inMemFS struct { | ||
*memfs.FS | ||
err error | ||
} | ||
|
||
func newInMemFS() *inMemFS { | ||
return &inMemFS{ | ||
FS: memfs.New(), | ||
} | ||
} | ||
|
||
func Test2(t *testing.T) { | ||
expected := DirectoryData{ | ||
Name: "A Name", | ||
Description: "A Description", | ||
Location: "A Location", | ||
Files: map[string]FileData{ | ||
"file-name-1.jpg": {}, | ||
"file-name-2.jpg": { | ||
IsStar: true, | ||
Caption: "A Caption", | ||
}, | ||
}, | ||
Albums: map[string]AlbumData{}, | ||
} | ||
|
||
sample := ` | ||
[Picasa] | ||
name=A Name | ||
description=A Description | ||
location=A Location | ||
|
||
[file-name-1.jpg] | ||
some_other_key=some value | ||
|
||
[file-name-2.jpg] | ||
star=yes | ||
caption=A Caption | ||
` | ||
|
||
appFS = afero.NewMemMapFs() | ||
// create test files and directories | ||
appFS.MkdirAll("sample", 0o755) | ||
afero.WriteFile(appFS, "sample/.picasa.ini", []byte(sample), 0o644) | ||
|
||
actual := ParseDirectory("sample") | ||
|
||
if !reflect.DeepEqual(expected, actual) { | ||
fmt.Printf("%+v\n", expected) | ||
fmt.Printf("%+v\n", actual) | ||
t.Error("ParseDirectory did not yield expected results") | ||
} | ||
} | ||
|
||
func TestReadLines(t *testing.T) { | ||
sample := ` | ||
[Picasa] | ||
key1=value1 | ||
key2=value2 | ||
|
||
[file-name.jpg] | ||
key3=value3 | ||
key4=value4 | ||
` | ||
buf := strings.NewReader(sample) | ||
s := bufio.NewScanner(buf) | ||
|
||
actual := parseScanner(s) | ||
expected := map[string]map[string]string{ | ||
"Picasa": { | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
"file-name.jpg": { | ||
"key3": "value3", | ||
"key4": "value4", | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Error("parsed ini did not match expected") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to figure this out.