| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# pylint: disable=invalid-name |
| 4 |
|
| 5 |
''' Simple tool to search and download remote configurations. ''' |
| 6 |
|
| 7 |
|
| 8 |
import fnmatch |
| 9 |
import os |
| 10 |
import os.path |
| 11 |
import sys |
| 12 |
import time |
| 13 |
import urllib.error # pylint: disable=no-name-in-module,F0401,E0611 |
| 14 |
import urllib.request # pylint: disable=no-name-in-module,F0401,E0611 |
| 15 |
|
| 16 |
if 'XDG_CACHE_HOME' in os.environ: |
| 17 |
REMOTES_LIST_DIR = os.environ['XDG_CACHE_HOME'] |
| 18 |
else: |
| 19 |
REMOTES_LIST_DIR = os.path.expanduser('~/.cache') |
| 20 |
REMOTES_LIST = os.path.join(REMOTES_LIST_DIR, 'remotes.list') |
| 21 |
|
| 22 |
if 'LIRC_REMOTES_URL' in os.environ: |
| 23 |
REMOTE_CODEBASE = os.environ['LIRC_REMOTES_URL'] |
| 24 |
else: |
| 25 |
REMOTE_CODEBASE = \ |
| 26 |
"https://sourceforge.net/p/lirc-remotes/code/ci/master/tree" |
| 27 |
|
| 28 |
if 'LIRC_IRDB_CACHE_ID' in os.environ: |
| 29 |
CACHE_ID = os.environ['LIRC_IRDB_CACHE_ID'] |
| 30 |
else: |
| 31 |
CACHE_ID = time.ctime() |
| 32 |
|
| 33 |
if 'LIRC_REMOTES_LIST' in os.environ: |
| 34 |
REMOTES_LIST_URL = os.environ['LIRC_REMOTES_LIST'] |
| 35 |
else: |
| 36 |
REMOTES_LIST_URL = "http://lirc-remotes.sourceforge.net/remotes.list" |
| 37 |
|
| 38 |
USAGE = """ |
| 39 |
Usage: |
| 40 |
irdb-get update |
| 41 |
irdb-get find <string> |
| 42 |
irdb-get info <id> |
| 43 |
irdb-get find-driver <string> |
| 44 |
irdb-get download <id> |
| 45 |
irdb-get list [pattern] |
| 46 |
irdb-get yaml-config |
| 47 |
irdb-get <-h | --help | -v | --version> |
| 48 |
""" |
| 49 |
|
| 50 |
|
| 51 |
def here(path): |
| 52 |
' Return path added to current dir for __file__. ' |
| 53 |
return os.path.join(os.path.dirname(os.path.abspath(__file__)), path) |
| 54 |
|
| 55 |
|
| 56 |
def download_file(url, path): |
| 57 |
''' Download url to a file. ''' |
| 58 |
if not os.path.exists(REMOTES_LIST_DIR): |
| 59 |
os.makedirs(REMOTES_LIST_DIR) |
| 60 |
try: |
| 61 |
urllib.request.urlretrieve(url, path) |
| 62 |
except urllib.error.HTTPError as ex: |
| 63 |
text = "Cannot download %s : %s" % (url, str(ex)) |
| 64 |
print(text) |
| 65 |
|
| 66 |
|
| 67 |
def get_list_lines(): |
| 68 |
''' Download remotes.list if required, return as list of lines. ''' |
| 69 |
if not os.path.exists(REMOTES_LIST): |
| 70 |
download_file(REMOTES_LIST_URL, REMOTES_LIST) |
| 71 |
with open(REMOTES_LIST) as f: |
| 72 |
list_ = f.read() |
| 73 |
return [l for l in list_.split('\n') if l] |
| 74 |
|
| 75 |
|
| 76 |
def do_download(): |
| 77 |
''' Download a file as given in sys.argv[2]. ''' |
| 78 |
if len(sys.argv) < 3: |
| 79 |
print("No remote to download") |
| 80 |
print(USAGE) |
| 81 |
sys.exit(1) |
| 82 |
remote = sys.argv[2] |
| 83 |
uri = os.path.join(REMOTE_CODEBASE, 'remotes', remote) |
| 84 |
path = os.path.basename(remote) |
| 85 |
download_file(uri + '?format=raw', path) |
| 86 |
print("Downloaded %s as %s" % (uri, path)) |
| 87 |
|
| 88 |
|
| 89 |
def print_info(l): |
| 90 |
''' Print some info for list line. ''' |
| 91 |
tokens = l.split(';') |
| 92 |
uri = os.path.join(REMOTE_CODEBASE, 'remotes', tokens[0]) |
| 93 |
print("Directory: " + tokens[0]) |
| 94 |
print("lircd.conf file: " + tokens[1]) |
| 95 |
print("lirmcd.conf file: " + tokens[2]) |
| 96 |
print("Photo: " + tokens[3]) |
| 97 |
print("Name: " + tokens[4]) |
| 98 |
print("Timing info: " + tokens[5]) |
| 99 |
print("Raw: " + tokens[6]) |
| 100 |
print("URI: " + uri) |
| 101 |
|
| 102 |
|
| 103 |
def do_show(): |
| 104 |
''' Show info for id in sys.argv[2]. ''' |
| 105 |
if len(sys.argv) < 3: |
| 106 |
print("No id string") |
| 107 |
print(USAGE) |
| 108 |
sys.exit(1) |
| 109 |
what = sys.argv[2] |
| 110 |
dir_, base = what.split("/") |
| 111 |
lines = get_list_lines() |
| 112 |
for l in lines: |
| 113 |
tokens = l.split(";") |
| 114 |
if tokens[0] == dir_ and tokens[1] == base: |
| 115 |
print_info(l) |
| 116 |
sys.exit(0) |
| 117 |
print("No line matching: " + what) |
| 118 |
sys.exit(1) |
| 119 |
|
| 120 |
|
| 121 |
def do_list(): |
| 122 |
''' List files matching optional pattern in sys.argv[2]. ''' |
| 123 |
if len(sys.argv) < 3: |
| 124 |
pattern = '*/*' |
| 125 |
elif len(sys.argv) == 3: |
| 126 |
pattern = sys.argv[2] |
| 127 |
else: |
| 128 |
print("Too many arguments") |
| 129 |
print(USAGE) |
| 130 |
sys.exit(1) |
| 131 |
lines = get_list_lines() |
| 132 |
for l in lines: |
| 133 |
tokens = l.split(';') |
| 134 |
if len(tokens) < 2: |
| 135 |
continue |
| 136 |
path = tokens[0] + "/" + tokens[1] |
| 137 |
if not fnmatch.fnmatch(path, pattern): |
| 138 |
continue |
| 139 |
lircmd = '-' if tokens[2].startswith('no') else 'L' |
| 140 |
photo = '-' if tokens[3].startswith('no') else 'P' |
| 141 |
timing = '-' if tokens[5].startswith('no') else 'T' |
| 142 |
raw = '-' if tokens[6].startswith('no') else 'R' |
| 143 |
print("%s%s%s%s %-32s %s" % |
| 144 |
(timing, raw, photo, lircmd, tokens[4], path)) |
| 145 |
|
| 146 |
|
| 147 |
def do_find_driver(): |
| 148 |
''' Find files having exactly the driver in sys.argv[2]. ''' |
| 149 |
if len(sys.argv) < 3: |
| 150 |
print("No driver specified") |
| 151 |
print(USAGE) |
| 152 |
sys.exit(1) |
| 153 |
driver = sys.argv[2] |
| 154 |
lines = get_list_lines() |
| 155 |
found = [] |
| 156 |
for l in lines: |
| 157 |
tokens = l.split(";") |
| 158 |
if tokens[7] == driver: |
| 159 |
found.append(l) |
| 160 |
for l in found: |
| 161 |
tokens = l.split(";") |
| 162 |
print(tokens[0] + "/" + tokens[1]) |
| 163 |
|
| 164 |
|
| 165 |
def print_yaml_dict(_dict): |
| 166 |
''' Print a single-level dictionary as a yaml snippet. ''' |
| 167 |
for key in sorted(_dict.keys()): |
| 168 |
print("\n %s:" % key, end='') |
| 169 |
to_print = ["\n" + (8 * ' ') + '- ' + p |
| 170 |
for p in sorted(list(set(_dict[key])))] |
| 171 |
print(" ".join(to_print)) |
| 172 |
|
| 173 |
|
| 174 |
def do_yaml_config(): |
| 175 |
''' Write a yaml file mappinng drivers to list of lircd.conf. ''' |
| 176 |
|
| 177 |
if len(sys.argv) != 2: |
| 178 |
print("yaml-config: No arguments are allowed.") |
| 179 |
print(USAGE) |
| 180 |
sys.exit(1) |
| 181 |
lines = get_list_lines() |
| 182 |
lircd_by_driver = {} |
| 183 |
lircmd_by_driver = {} |
| 184 |
for l in lines: |
| 185 |
tokens = l.split(";") |
| 186 |
if len(tokens) < 8: |
| 187 |
continue |
| 188 |
driver = tokens[7] |
| 189 |
if (not driver) or driver == "no_driver" or driver == 'unknown': |
| 190 |
continue |
| 191 |
if driver not in lircd_by_driver: |
| 192 |
lircd_by_driver[driver] = [] |
| 193 |
lircd_by_driver[driver].append("%s/%s" % (tokens[0], tokens[1])) |
| 194 |
if tokens[2] == 'no_lircmd': |
| 195 |
continue |
| 196 |
if driver not in lircmd_by_driver: |
| 197 |
lircmd_by_driver[driver] = [] |
| 198 |
lircmd_by_driver[driver].append("%s/%s" % (tokens[0], tokens[2])) |
| 199 |
|
| 200 |
print("#") |
| 201 |
print("# Created by 'irdb-get yaml-config' as of " + CACHE_ID) |
| 202 |
print("#") |
| 203 |
print("\nlircd_by_driver:") |
| 204 |
print_yaml_dict(lircd_by_driver) |
| 205 |
print("\nlircmd_by_driver:") |
| 206 |
print_yaml_dict(lircmd_by_driver) |
| 207 |
|
| 208 |
|
| 209 |
def do_find(): |
| 210 |
''' Find files containing pattern in sys.argv[2]. ''' |
| 211 |
if len(sys.argv) < 3: |
| 212 |
print("No search string") |
| 213 |
print(USAGE) |
| 214 |
sys.exit(1) |
| 215 |
what = sys.argv[2] |
| 216 |
lines = get_list_lines() |
| 217 |
found = [] |
| 218 |
for l in lines: |
| 219 |
if what in l: |
| 220 |
found.append(l) |
| 221 |
for l in found: |
| 222 |
tokens = l.split(";") |
| 223 |
print(tokens[0] + "/" + tokens[1]) |
| 224 |
|
| 225 |
|
| 226 |
def do_update(): |
| 227 |
''' Download the directory from website. ''' |
| 228 |
if os.path.exists(REMOTES_LIST): |
| 229 |
os.unlink(REMOTES_LIST) |
| 230 |
download_file(REMOTES_LIST_URL, REMOTES_LIST) |
| 231 |
|
| 232 |
|
| 233 |
def main(): |
| 234 |
''' Indeed: main program. ''' |
| 235 |
|
| 236 |
if len(sys.argv) < 2: |
| 237 |
print("No command given") |
| 238 |
print(USAGE) |
| 239 |
sys.exit(1) |
| 240 |
if sys.argv[1] == 'update': |
| 241 |
do_update() |
| 242 |
elif sys.argv[1] == 'download': |
| 243 |
do_download() |
| 244 |
elif sys.argv[1] == 'find': |
| 245 |
do_find() |
| 246 |
elif sys.argv[1] == 'find-driver': |
| 247 |
do_find_driver() |
| 248 |
elif sys.argv[1] == 'info': |
| 249 |
do_show() |
| 250 |
elif sys.argv[1] == 'list': |
| 251 |
do_list() |
| 252 |
elif sys.argv[1] == 'yaml-config': |
| 253 |
do_yaml_config() |
| 254 |
elif sys.argv[1] == '-h' or sys.argv[1] == '--help': |
| 255 |
print(USAGE) |
| 256 |
elif sys.argv[1] == '-v' or sys.argv[1] == '--version': |
| 257 |
print("irdb-get @version@") |
| 258 |
else: |
| 259 |
print("Illegal command: " + sys.argv[1]) |
| 260 |
print(USAGE) |
| 261 |
sys.exit(1) |
| 262 |
sys.exit(0) |
| 263 |
|
| 264 |
|
| 265 |
if __name__ == '__main__': |
| 266 |
main() |
| 267 |
|
| 268 |
|
| 269 |
# vim: set expandtab ts=4 sw=4: |