From 46d16b70e87aa2c7acb1786480ed2bf0a4c09772 Mon Sep 17 00:00:00 2001 From: zyp Date: Fri, 6 Oct 2006 16:53:10 +0000 Subject: [project @ zyp-20061006165310-7aac92411fd55372] [project @ 58] Added colors and counter. Using python-2.5 dependant syntax. --- anidb.py | 37 ++++++++++++++++++++++++------------- pyanidb/__init__.py | 4 ++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/anidb.py b/anidb.py index 3cd8cac..a4ce618 100644 --- a/anidb.py +++ b/anidb.py @@ -3,6 +3,13 @@ import pyanidb, pyanidb.hash import ConfigParser, optparse, os, sys, getpass, multihash +# Colors. + +red = lambda x: '\x1b[1;31m' + x + '\x1b[0m' +green = lambda x: '\x1b[1;32m' + x + '\x1b[0m' +yellow = lambda x: '\x1b[1;33m' + x + '\x1b[0m' +blue = lambda x: '\x1b[1;34m' + x + '\x1b[0m' + # Config. config = {} @@ -59,12 +66,12 @@ if options.login: files = [] for name in args: if not os.access(name, os.R_OK): - print 'Invalid file: %s' % (name) + print red('Invalid file:'), name elif os.path.isfile(name): files.append(name) elif os.path.isdir(name): if not options.recursive: - print 'Is a directory: %s' % (name) + print red('Is a directory:'), name else: for root, subdirs, subfiles in os.walk(name): subdirs.sort() @@ -72,7 +79,7 @@ for name in args: files += [os.path.join(root, file) for file in subfiles if sum([file.endswith('.' + suffix) for suffix in options.suffix])] if not files: - print 'All operations finished.' + print blue('Nothing to do.') sys.exit(0) # Authorization. @@ -81,23 +88,26 @@ if options.login: a = pyanidb.AniDB(options.username, options.password) try: a.auth() - print 'Logged in as user %s.' % (options.username) + print blue('Logged in as user:'), options.username except pyanidb.AniDBUserError: - print 'Invalid username/password.' + print red('Invalid username/password.') sys.exit(1) except pyanidb.AniDBTimeout: - print 'Connection timed out.' + print red('Connection timed out.') sys.exit(1) except pyanidb.AniDBError, e: - print 'Fatal error:', e + print red('Fatal error:'), e sys.exit(1) # Hashing. +hashed = unknown = 0 + for filename, hash in pyanidb.hash.hash_files(files): size = os.path.getsize(filename) - print 'Hashed: ed2k://|file|%s|%d|%s|' % (filename, size, hash.ed2k()) + print blue('Hashed:'), 'ed2k://|file|%s|%d|%s|' % (filename, size, hash.ed2k()) fid = (size, hash.ed2k()) + hashed += 1 try: @@ -106,7 +116,7 @@ for filename, hash in pyanidb.hash.hash_files(files): if options.identify: info = a.get_file(fid, ('gtag', 'romaji', 'epno', 'state', 'epromaji', 'crc32', 'filetype'), True) fid = int(info['fid']) - print 'Identified: [%s] %s - %s - %s' % (info['gtag'], info['romaji'], info['epno'], info['epromaji']) + print green('Identified:'), '[%s] %s - %s - %s' % (info['gtag'], info['romaji'], info['epno'], info['epromaji']) # Renaming. @@ -126,18 +136,19 @@ for filename, hash in pyanidb.hash.hash_files(files): s = s[1:].replace(' ', '_') s = s.replace('/', '_') - print 'Renaming to: %s' % (s) + print yellow('Renaming to:'), s os.rename(filename, os.path.join(os.path.split(filename)[0], s)) # Adding. if options.add: a.add_file(fid, retry = True) - print 'Added to mylist.' + print green('Added to mylist.') except pyanidb.AniDBUnknownFile: - print 'Unknown file.' + print red('Unknown file.') + unknown += 1 # Finished. -print 'All operations finished.' +print blue('Hashed %d files%s.' % (hashed, (', %d unknown' % unknown) if unknown else '')) diff --git a/pyanidb/__init__.py b/pyanidb/__init__.py index adc7bfe..ab8db14 100644 --- a/pyanidb/__init__.py +++ b/pyanidb/__init__.py @@ -25,7 +25,7 @@ acode = ( 'short', 'synonym', 'category', '', '', '', '', '') info = fcode + acode -info = dict([(info[i], 1 << i) for i in xrange(len(info)) if info[i]]) +info = dict([(info[i], 1L << i) for i in xrange(len(info)) if info[i]]) class AniDBError(Exception): pass @@ -121,7 +121,7 @@ class AniDB: info_codes = list(info_codes) info_codes.sort(lambda x, y: cmp(info[x], info[y])) info_code = sum([info[code] for code in info_codes]) - code, text, data = self.execute('FILE s=%s&%s&fcode=%d&acode=%d' % (self.session, fid, info_code & 0xffffffff, info_code >> 32), retry) + code, text, data = self.execute('FILE s=%s&%s&fcode=%d&acode=%d' % (self.session, fid, info_code & 0xffffffffL, info_code >> 32), retry) if code == 220: return dict([(name, data[0].pop(0)) for name in ['fid'] + info_codes]) elif code == 320: -- cgit v1.2.3