Skip to content

Commit

Permalink
Merge branch '1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
giphahne committed Nov 9, 2019
2 parents 565a662 + 8fbdc5f commit ac923b9
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='textgrid_utils',
version='1.0.0',
version='1.0.2',
description='utilities for working with textgrid files',
url='https://github.com/giphahne/textgrid-utils',
author='Dan Hahne',
Expand All @@ -31,6 +31,9 @@
entry_points={
'console_scripts': [
'merge-and-mark-textgrid-tiers=textgrid_utils:merge_main',
'tg-copy-tiers=textgrid_utils:copy_tiers_main',
'tg-remove-tiers=textgrid_utils:remove_tiers_main',
'tg-list-tiers=textgrid_utils:list_main',
],
},
)
124 changes: 124 additions & 0 deletions textgrid_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ def merge_and_mark_tiers(tg_file="", output_file="", tiers=()):
tg.write(f)


def copy_tiers(source_file="", target_file="", tiers=()):
"""
Copies given Tiers from source to target
"""
source_tg = textgrid.TextGrid()
source_tg.read(f=source_file)

target_tg = textgrid.TextGrid()
target_tg.read(f=target_file)

for tier in tiers:
target_tg.tiers.append(source_tg.getFirst(tier))

with open(target_file, "w") as f:
target_tg.write(f)


def remove_tiers(target_file="", tiers=()):
"""
Remove given Tiers from target
"""
target_tg = textgrid.TextGrid()
target_tg.read(f=target_file)

for tier in tiers:
try:
target_tg.pop(target_tg.getNames().index(tier))
except ValueError:
continue

with open(target_file, "w") as f:
target_tg.write(f)


def list_tiers(tg_file):
#print(tg_file)
target_tg = textgrid.TextGrid()
target_tg.read(f=tg_file)
print(target_tg.getNames())


def merge_main():
"""Entry point for the application script"""

Expand Down Expand Up @@ -78,5 +119,88 @@ def merge_main():
tiers=args.tiers)


def copy_tiers_main():
"""Entry point for the application script"""

import sys
import argparse
import json
from functools import partial

import argcomplete

description = ""
parser = argparse.ArgumentParser(usage=None, description=description)

parser.add_argument(
"-i",
"--source",
type=str,
help=("Search this Textgrid file for Tiers of the "
"given Name"))
parser.add_argument("-o", "--target", type=str, help=(""))
parser.add_argument(
"--tiers",
type=str,
nargs="+",
help=("tiers to copy from "
"source to target"))

argcomplete.autocomplete(parser)
args = parser.parse_args()

copy_tiers(
source_file=args.source, target_file=args.target, tiers=args.tiers)


def remove_tiers_main():
"""Entry point for the application script"""

import sys
import argparse
import json
from functools import partial

import argcomplete

description = ""
parser = argparse.ArgumentParser(usage=None, description=description)

parser.add_argument("-f", "--file", type=str, help=("textgrid file"))
parser.add_argument(
"-t",
"--tiers",
type=str,
nargs="+",
help=("tiers to copy from "
"source to target"))

argcomplete.autocomplete(parser)
args = parser.parse_args()

remove_tiers(target_file=args.file, tiers=args.tiers)


def list_main():
"""Entry point for the application script"""

import sys
import argparse
import json
from functools import partial

import argcomplete

description = ""
parser = argparse.ArgumentParser(usage=None, description=description)

parser.add_argument("file", type=str, help=("textgrid file"))

argcomplete.autocomplete(parser)
args = parser.parse_args()

list_tiers(tg_file=args.file)


if __name__ == '__main__':
merge_main()

0 comments on commit ac923b9

Please sign in to comment.