diff --git a/orvani/cmd.py b/orvani/cmd.py index 08d27dd..669f013 100644 --- a/orvani/cmd.py +++ b/orvani/cmd.py @@ -5,7 +5,7 @@ from pathlib import Path from rich.console import Console from rich.table import Table from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS -from plugins import frontmatter, hashslashslash +from plugins import frontmatter, hashdoubleslash, id3 def mktable(query: str, index: dict): console = Console() @@ -86,7 +86,8 @@ def main(): @orvani.metareader def reader(o): frontmatter.reader(o) - hashslashslash.reader(o) + hashdoubleslash.reader(o) + id3.reader(o) # orvani.metadata() diff --git a/orvani/orvani.py b/orvani/orvani.py index 53c6866..97ab182 100644 --- a/orvani/orvani.py +++ b/orvani/orvani.py @@ -393,6 +393,7 @@ class OrvaniTag: def metadata(self, filename: str): obj = { 'path' : filename, + 'config' : self.CONFIG, 'index' : self.INDEX, 'schema' : self.CTX, 'update' : self.__updateindex, diff --git a/orvani/plugins/frontmatter.py b/orvani/plugins/frontmatter.py index 3a033e7..ab72fbb 100644 --- a/orvani/plugins/frontmatter.py +++ b/orvani/plugins/frontmatter.py @@ -1,9 +1,16 @@ import frontmatter from pathlib import Path +NAME = 'frontmatter' +TYPES = ['.md', '.txt', '.markdown', '.rst'] + def reader(o): file = Path(o.path) - if file.suffix in ['.md', '.txt', '.markdown', '.rst']: + suffixes = TYPES + if NAME in o.config: + if 'types' in o.config[NAME]: + suffixes = o.config[NAME]['types'] + if file.suffix in suffixes: with open(file, 'r') as data: y = frontmatter.load(data) if y.metadata: diff --git a/orvani/plugins/hashslashslash.py b/orvani/plugins/hashdoubleslash.py similarity index 77% rename from orvani/plugins/hashslashslash.py rename to orvani/plugins/hashdoubleslash.py index 875ee61..193d3a1 100644 --- a/orvani/plugins/hashslashslash.py +++ b/orvani/plugins/hashdoubleslash.py @@ -4,11 +4,17 @@ import re from pathlib import Path +NAME = 'hash//' +TYPES = ['.md', '.txt', '.markdown', '.rst'] REGEX = r'\s*([@+]\w+|#\w+(? 5: diff --git a/orvani/plugins/id3.py b/orvani/plugins/id3.py new file mode 100644 index 0000000..27ffd16 --- /dev/null +++ b/orvani/plugins/id3.py @@ -0,0 +1,40 @@ +import re +from pathlib import Path +import taglib + +NAME = 'id3' +TYPES = ['.mp3', '.wav', '.flac', '.ogg'] +REGEX = r'\s*([@+]\w+|#\w+)' + +def reader(o): + file = Path(o.path) + suffixes = TYPES + if NAME in o.config: + if 'types' in o.config[NAME]: + suffixes = o.config[NAME]['types'] + if file.suffix in suffixes: + with taglib.File(o.path, save_on_exit=False) as song: + metadata = song.tags + id3map = {} + if 'id3' in o.config: + if 'map' in o.config['id3']: + id3map = o.config['id3']['map'].copy() + for context_key in id3map: + if context_key not in o.schema.keys(): + del id3map[context_key] + else: + ctag = str(id3map[context_key]).upper() + if ctag in metadata: + id3map[context_key] = metadata[ctag] + if id3map.keys(): + for context_key in id3map.keys(): + o.update(o.path, id3map, context_key) + if 'COMMENT' in metadata: + comment = metadata['COMMENT'] + tags = re.findall(REGEX, comment[0].strip()) + note = re.sub(REGEX, '', comment[0].strip()).lstrip() + argv = ' '.join(tags).split(' ') + [note] + argv = [x for x in argv if x != ''] + context = o.context(argv) + for context_key in context.keys(): + o.update(o.path, context, context_key) \ No newline at end of file diff --git a/test/.context.yaml b/test/.context.yaml index 9b122cd..db27134 100644 --- a/test/.context.yaml +++ b/test/.context.yaml @@ -1,3 +1,14 @@ +config: + hash//: + types: + - .py + id3: + types: + - .wav + map: + projects: artist + contexts: genre + note: title files: test.txt: contexts: diff --git a/test/chirpy.wav b/test/chirpy.wav new file mode 100644 index 0000000..53aba4a Binary files /dev/null and b/test/chirpy.wav differ