Last active
December 11, 2015 02:58
-
-
Save davidchambers/4534417 to your computer and use it in GitHub Desktop.
Print known Uniform Type Identifiers as JSON
This file contains hidden or 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
#!/usr/bin/env python | |
import json | |
import re | |
import subprocess | |
import sys | |
if len(sys.argv) > 1: | |
lsregister = sys.argv[1] | |
else: | |
lsregister = ('/System/Library/Frameworks/CoreServices.framework' | |
'/Versions/A/Frameworks/LaunchServices.framework' | |
'/Versions/A/Support/lsregister') | |
p = subprocess.Popen([lsregister, '-dump'], stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = p.communicate() | |
chunks = [''] | |
for line in out.splitlines(): | |
if line == '-' * 80: | |
chunks.append('') | |
else: | |
chunks[-1] += line + '\n' | |
kvsplit = re.compile(r'^\s*(?P<key>.*?):\s*(?P<value>.*)$').search | |
def parse(chunk): | |
entry = {} | |
for line in chunk.splitlines(): | |
match = kvsplit(line) | |
if match is not None: | |
entry[match.group('key')] = match.group('value') | |
return entry | |
entries = {} | |
csvsplit = re.compile(r'\s*,\s*').split | |
for entry in map(parse, chunks): | |
if 'uti' in entry: | |
content_types, extensions = [], [] | |
for tag in csvsplit(entry['tags']): | |
if tag.startswith('.'): | |
extensions.append(tag) | |
elif '/' in tag: | |
content_types.append(tag) | |
entries[entry['uti']] = { | |
'conforms_to': csvsplit(entry['conforms to']), | |
'content_types': content_types, | |
'description': entry['description'], | |
'extensions': extensions, | |
} | |
print json.dumps(entries) |
Author
davidchambers
commented
Jan 14, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment