| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import os |
|---|
| 4 | import sys |
|---|
| 5 | import msync |
|---|
| 6 | import optparse |
|---|
| 7 | |
|---|
| 8 | def main(argv=None): |
|---|
| 9 | if argv is None or len(argv) == 1: |
|---|
| 10 | msync.help.print_help() |
|---|
| 11 | return |
|---|
| 12 | argv.pop(0) # pop the called filename |
|---|
| 13 | if argv[0] == 'help': |
|---|
| 14 | argv.pop(0) |
|---|
| 15 | msync.help.print_help(argv) |
|---|
| 16 | elif argv[0] in ('library', 'lib'): |
|---|
| 17 | argv.pop(0) |
|---|
| 18 | library(argv) |
|---|
| 19 | elif argv[0] in ('playlist', 'pl'): |
|---|
| 20 | argv.pop(0) |
|---|
| 21 | playlist(argv) |
|---|
| 22 | elif argv[0] in ('synclist', 'sl'): |
|---|
| 23 | argv.pop(0) |
|---|
| 24 | synclist(argv) |
|---|
| 25 | else: |
|---|
| 26 | msync.help.print_help(argv) |
|---|
| 27 | |
|---|
| 28 | def library_file(): |
|---|
| 29 | return os.path.normpath(os.path.expanduser('~/.config/msync.db')) |
|---|
| 30 | |
|---|
| 31 | def library(argv=None): |
|---|
| 32 | lib = msync.Library(msync.MsyncDB(library_file())) |
|---|
| 33 | if argv is None or len(argv) == 0: |
|---|
| 34 | msync.help.print_help(['lib']) |
|---|
| 35 | elif argv[0] == 'add': |
|---|
| 36 | if len(argv) == 1: |
|---|
| 37 | msync.help.print_library_help(argv) |
|---|
| 38 | return |
|---|
| 39 | try: |
|---|
| 40 | lib.add(argv[1]) |
|---|
| 41 | except msync.PathExistsInLibrary: |
|---|
| 42 | print "Path already exists in library" |
|---|
| 43 | elif argv[0] == 'check': |
|---|
| 44 | if len(argv) == 1: |
|---|
| 45 | argv.append(None) |
|---|
| 46 | print 'The following files can not be accessed:' |
|---|
| 47 | for file in sorted(lib.check(argv[1])): |
|---|
| 48 | print ' %s' % file |
|---|
| 49 | elif argv[0] in ('delete', 'del', 'remove', 'rm'): |
|---|
| 50 | if len(argv) == 1: |
|---|
| 51 | msync.help.print_library_help(argv) |
|---|
| 52 | return |
|---|
| 53 | if lib.remove(argv[1]) is False: |
|---|
| 54 | print "error removing path from library" |
|---|
| 55 | elif argv[0] in ('list', 'ls'): |
|---|
| 56 | if len(argv) == 1: |
|---|
| 57 | argv.append(None) |
|---|
| 58 | if argv[1] is not None and argv[1].startswith('--'): |
|---|
| 59 | if len(argv) == 2: |
|---|
| 60 | print 'You must specify an argument with the option' |
|---|
| 61 | return |
|---|
| 62 | try: |
|---|
| 63 | files = sorted(lib.list(option=argv[1].lstrip('-'), path=argv[2])) |
|---|
| 64 | except msync.InvalidOption: |
|---|
| 65 | print '%s is an invalid option' % argv[1] |
|---|
| 66 | return |
|---|
| 67 | else: |
|---|
| 68 | files = sorted(lib.list(path=argv[1])) |
|---|
| 69 | |
|---|
| 70 | for file in files: |
|---|
| 71 | print str(file.encode('utf-8')) |
|---|
| 72 | elif argv[0] == 'paths': |
|---|
| 73 | for path in sorted(lib.getLibraryPaths()): |
|---|
| 74 | print path |
|---|
| 75 | elif argv[0] in ('playlists', 'pls'): |
|---|
| 76 | for pls in sorted(lib.getPlaylistsNoFiles(), key=msync.Playlist.getName): |
|---|
| 77 | print pls.name |
|---|
| 78 | elif argv[0] in ('rescan', 'rs', 'update', 'up'): |
|---|
| 79 | if len(argv) == 1: |
|---|
| 80 | argv.append(None) |
|---|
| 81 | if argv[0] in ('rescan', 'rs'): |
|---|
| 82 | added, removed, updated = lib.update(argv[1], rescan=True) |
|---|
| 83 | else: |
|---|
| 84 | added, removed, updated = lib.update(argv[1]) |
|---|
| 85 | if len(added) != 0: |
|---|
| 86 | print 'Files added:' |
|---|
| 87 | for file in added: |
|---|
| 88 | print ' %s' % file |
|---|
| 89 | print |
|---|
| 90 | else: |
|---|
| 91 | print 'No files added.' |
|---|
| 92 | if len(removed) != 0: |
|---|
| 93 | print 'Files removed:' |
|---|
| 94 | for file in removed: |
|---|
| 95 | print ' %s' % file |
|---|
| 96 | print |
|---|
| 97 | else: |
|---|
| 98 | print 'No files removed.' |
|---|
| 99 | if len(updated) != 0: |
|---|
| 100 | print 'Files updated:' |
|---|
| 101 | for file in updated: |
|---|
| 102 | print ' %s' % file |
|---|
| 103 | print |
|---|
| 104 | else: |
|---|
| 105 | print 'No files updated.' |
|---|
| 106 | else: |
|---|
| 107 | msync.help.print_help(['lib']) |
|---|
| 108 | |
|---|
| 109 | def playlist(argv=None): |
|---|
| 110 | if argv is None or len(argv) < 2: |
|---|
| 111 | msync.help.print_help(['pl']) |
|---|
| 112 | return |
|---|
| 113 | db = msync.MsyncDB(library_file()) |
|---|
| 114 | pl = msync.Playlist(argv.pop(0)) |
|---|
| 115 | lib = msync.Library(db) |
|---|
| 116 | pl.setDB(db) |
|---|
| 117 | |
|---|
| 118 | if argv[0] in ('create','new'): |
|---|
| 119 | try: |
|---|
| 120 | pl.write() |
|---|
| 121 | except msync.PlaylistExists: |
|---|
| 122 | print 'Playlist %s already exists' % pl.name |
|---|
| 123 | return |
|---|
| 124 | elif argv[0] == 'sync' and len(argv) >= 3 and \ |
|---|
| 125 | ( argv[1] == '--remove' or argv[1] == '-r' ): |
|---|
| 126 | added, removed, updated = pl.sync(argv[2], remove=True) |
|---|
| 127 | print 'Playlist deleted and %d files removed' % removed |
|---|
| 128 | return |
|---|
| 129 | |
|---|
| 130 | try: |
|---|
| 131 | pl.get() |
|---|
| 132 | except msync.PlaylistDoesNotExist: |
|---|
| 133 | print '''Playlist does not exist. Try running with the arguments 'library playlists' |
|---|
| 134 | to see which playlists are available, or with 'create' to create a new playlist.''' |
|---|
| 135 | return |
|---|
| 136 | |
|---|
| 137 | if argv[0] == 'add': |
|---|
| 138 | if len(argv) == 1: |
|---|
| 139 | msync.help.print_playlist_help(argv) |
|---|
| 140 | return |
|---|
| 141 | |
|---|
| 142 | if argv[1].startswith('--'): |
|---|
| 143 | if len(argv) == 2: |
|---|
| 144 | print 'You must specify an argument with the option' |
|---|
| 145 | return |
|---|
| 146 | |
|---|
| 147 | try: |
|---|
| 148 | files_added = pl.addByTag(argv[1].lstrip('-'), argv[2]) |
|---|
| 149 | pl.write() |
|---|
| 150 | except msync.InvalidOption: |
|---|
| 151 | print '%s is an invalid option' % argv[1] |
|---|
| 152 | return |
|---|
| 153 | except msync.FileNotInLibrary: |
|---|
| 154 | print 'File does not exist in the library' |
|---|
| 155 | return |
|---|
| 156 | else: |
|---|
| 157 | try: |
|---|
| 158 | files_added = pl.addByFilename(argv[1]) |
|---|
| 159 | pl.write() |
|---|
| 160 | except msync.FileNotInLibrary: |
|---|
| 161 | print 'File does not exist in the library' |
|---|
| 162 | return |
|---|
| 163 | print '%d files added to the playlist' % files_added |
|---|
| 164 | elif argv[0] in ('copy', 'cp'): |
|---|
| 165 | if len(argv) == 1: |
|---|
| 166 | msync.help.print_playlist_help(argv) |
|---|
| 167 | return |
|---|
| 168 | files_copied = pl.copy(argv[1]) |
|---|
| 169 | print '%d files were copied to %s' % (files_copied, argv[1]) |
|---|
| 170 | elif argv[0] in ('delete', 'del', 'remove', 'rm'): |
|---|
| 171 | length = len(argv) |
|---|
| 172 | if length == 1: |
|---|
| 173 | if pl.delete(): |
|---|
| 174 | print 'Playlist has been deleted' |
|---|
| 175 | return |
|---|
| 176 | elif argv[1].startswith('--'): |
|---|
| 177 | if length == 2: |
|---|
| 178 | print 'You must specificy an argument with the option' |
|---|
| 179 | return |
|---|
| 180 | |
|---|
| 181 | if argv[1] == '--position': |
|---|
| 182 | pl.removeByPosition(int(argv[2]) - 1) |
|---|
| 183 | files_removed = 1 |
|---|
| 184 | else: |
|---|
| 185 | try: |
|---|
| 186 | files_removed = pl.removeByTag(argv[1].lstrip('-'), argv[2]) |
|---|
| 187 | except msync.InvalidOption: |
|---|
| 188 | print '%s is an invalid option' % argv[1] |
|---|
| 189 | return |
|---|
| 190 | pl.write() |
|---|
| 191 | else: |
|---|
| 192 | files_removed = pl.removeByFilename(argv[1]) |
|---|
| 193 | pl.write() |
|---|
| 194 | print '%d files removed from the playlist' % files_removed |
|---|
| 195 | elif argv[0] in ('export', 'ex'): |
|---|
| 196 | length = len(argv) |
|---|
| 197 | if length == 1: |
|---|
| 198 | msync.help.print_playlist_help(argv) |
|---|
| 199 | return |
|---|
| 200 | |
|---|
| 201 | pl.export(argv[1]) |
|---|
| 202 | elif argv[0] == 'info': |
|---|
| 203 | length = '%d:%02d' % (pl.length / 60, pl.length % 60) |
|---|
| 204 | size = '%.2f MB' % (float(pl.size) / 1048576) |
|---|
| 205 | print '''Name: %s |
|---|
| 206 | Length: %s |
|---|
| 207 | Size: %s |
|---|
| 208 | Tracks: %d''' % (pl.name, length, size, len(pl.playlist)) |
|---|
| 209 | elif argv[0] in ('list', 'ls'): |
|---|
| 210 | i = 1 |
|---|
| 211 | file_list = pl.list() |
|---|
| 212 | format_str = "(%%%dd) %%s" % len(str(len(file_list))) |
|---|
| 213 | for file in file_list: |
|---|
| 214 | print format_str % (i, file.filename) |
|---|
| 215 | i += 1 |
|---|
| 216 | elif argv[0] in ('move', 'mv', 'rename', 'ren'): |
|---|
| 217 | if len(argv) == 1: |
|---|
| 218 | msync.help.print_playlist_help(argv) |
|---|
| 219 | return |
|---|
| 220 | try: |
|---|
| 221 | pl.updateName(argv[1]) |
|---|
| 222 | except msync.PlaylistExists: |
|---|
| 223 | print 'Playlist %s already exists' % argv[1] |
|---|
| 224 | return |
|---|
| 225 | elif argv[0] in ('position', 'pos'): |
|---|
| 226 | if len(argv) < 3: |
|---|
| 227 | msync.help.print_playlist_help(argv) |
|---|
| 228 | return |
|---|
| 229 | try: |
|---|
| 230 | pl.changePosition(int(argv[1]) - 1, int(argv[2]) - 1) |
|---|
| 231 | except msync.InvalidPosition: |
|---|
| 232 | print 'One of these values was an invalid position in the list.' |
|---|
| 233 | elif argv[0] == 'sync': |
|---|
| 234 | length = len(argv) |
|---|
| 235 | if length == 1: |
|---|
| 236 | msync.help.print_playlist_help(argv) |
|---|
| 237 | return |
|---|
| 238 | |
|---|
| 239 | image = parse_sync_options(argv[2:]) |
|---|
| 240 | |
|---|
| 241 | added, removed, updated = pl.sync(argv[1], image_opts=image) |
|---|
| 242 | print '%d files were added to the library' % added |
|---|
| 243 | print '%d files were removed from the library' % removed |
|---|
| 244 | print '%d files were updated' % updated |
|---|
| 245 | else: |
|---|
| 246 | msync.help.print_help(['pl']) |
|---|
| 247 | |
|---|
| 248 | def parse_sync_options(argv): |
|---|
| 249 | parser = optparse.OptionParser() |
|---|
| 250 | parser.add_option('-s', '--size', action='store', type='int', dest='size', default=None) |
|---|
| 251 | parser.add_option('-f', '--format', action='store', dest='fmt', default=None) |
|---|
| 252 | |
|---|
| 253 | try: |
|---|
| 254 | options, args = parser.parse_args(argv) |
|---|
| 255 | images = {} |
|---|
| 256 | if options.size is not None: |
|---|
| 257 | images['size'] = options.size |
|---|
| 258 | if options.fmt is not None: |
|---|
| 259 | images['format'] = options.fmt |
|---|
| 260 | except Exception, strerr: |
|---|
| 261 | print 'Error parsing arguments: %s' % strerr |
|---|
| 262 | return {} |
|---|
| 263 | |
|---|
| 264 | return images |
|---|
| 265 | |
|---|
| 266 | def synclist(argv): |
|---|
| 267 | slist = msync.SyncList(msync.MsyncDB(library_file())) |
|---|
| 268 | length = len(argv) |
|---|
| 269 | |
|---|
| 270 | if argv is None or length == 0: |
|---|
| 271 | msync.help.print_help(['synclist']) |
|---|
| 272 | elif argv[0] in ('delete', 'del', 'remove', 'rm'): |
|---|
| 273 | if length == 1: |
|---|
| 274 | msync.help.print_synclist_help(argv) |
|---|
| 275 | return |
|---|
| 276 | if slist.delete(argv[1]) is True: |
|---|
| 277 | print 'SyncList \'%s\' has been deleted' % argv[1] |
|---|
| 278 | elif argv[0] == 'info': |
|---|
| 279 | (count, size, length) = slist.info() |
|---|
| 280 | length = '%d:%02d' % (length / 60, length % 60) |
|---|
| 281 | size = '%.2f MB' % (float(size) / 1048576) |
|---|
| 282 | print '''Length: %s |
|---|
| 283 | Size: %s |
|---|
| 284 | Tracks: %d''' % (length, size, count) |
|---|
| 285 | elif argv[0] in ('list', 'ls'): |
|---|
| 286 | if length == 2 and argv[1] == 'all': |
|---|
| 287 | all = True |
|---|
| 288 | else: |
|---|
| 289 | all = False |
|---|
| 290 | |
|---|
| 291 | print_sl_list(slist, all) |
|---|
| 292 | elif argv[0] == 'load': |
|---|
| 293 | if length == 1: |
|---|
| 294 | msync.help.print_synclist_help(argv) |
|---|
| 295 | return |
|---|
| 296 | if slist.load(argv[1]) is True: |
|---|
| 297 | print 'Loaded SyncList \'%s\'' % argv[1] |
|---|
| 298 | elif argv[0] == 'mark': |
|---|
| 299 | if length == 1: |
|---|
| 300 | msync.help.print_synclist_help(argv) |
|---|
| 301 | return |
|---|
| 302 | slist.mark(argv[1]) |
|---|
| 303 | print_sl_list(slist, True) |
|---|
| 304 | elif argv[0] == 'save': |
|---|
| 305 | if length == 1: |
|---|
| 306 | msync.help.print_synclist_help(argv) |
|---|
| 307 | return |
|---|
| 308 | if slist.save(argv[1]) is True: |
|---|
| 309 | print 'SyncList saved as \'%s\'' % argv[1] |
|---|
| 310 | elif argv[0] == 'show': |
|---|
| 311 | print 'Available SyncLists:' |
|---|
| 312 | for row in slist.show(): |
|---|
| 313 | print ' %s' % row |
|---|
| 314 | elif argv[0] == 'sync': |
|---|
| 315 | if length == 1: |
|---|
| 316 | msync.help.print_synclist_help(argv) |
|---|
| 317 | return |
|---|
| 318 | image = parse_sync_options(argv[2:]) |
|---|
| 319 | slist.sync(argv[1], image_opts=image) |
|---|
| 320 | elif argv[0] == 'unmark': |
|---|
| 321 | if length == 1: |
|---|
| 322 | msync.help.print_synclist_help(argv) |
|---|
| 323 | return |
|---|
| 324 | slist.unmark(argv[1]) |
|---|
| 325 | print_sl_list(slist, True) |
|---|
| 326 | else: |
|---|
| 327 | msync.help.print_help(['sl']) |
|---|
| 328 | |
|---|
| 329 | def print_sl_list(slist, all): |
|---|
| 330 | for plist in slist.list(all): |
|---|
| 331 | if all is False: |
|---|
| 332 | print plist.name |
|---|
| 333 | else: |
|---|
| 334 | if bool(plist.marked) is True: |
|---|
| 335 | ch = 'X' |
|---|
| 336 | else: |
|---|
| 337 | ch = ' ' |
|---|
| 338 | |
|---|
| 339 | print '(%s) %s' % (ch, plist.name) |
|---|
| 340 | |
|---|
| 341 | if __name__ == '__main__': |
|---|
| 342 | main(sys.argv) |
|---|