def execute_update_query(cnx, update_sql, sql_dict, label='execute_update_query', verbose_level_threshold=3): cursor = cnx.cursor() s = sqlparse.format(update_sql % sql_dict, reindent=True, encoding='utf8') GPR.print_it2( label, s, verbose_level_threshold) try: cursor.execute( update_sql % sql_dict) cnx.commit() except mysql.connector.Error as err: if err.errno == 1062 and err.sqlstate == '23000': if True or GPR.verbose_level >= n: n1 = err.msg.index('Duplicate entry') n2 = err.msg.index('for key ') msg2 = err.msg[n1:n2-1] print " "+repr(msg2) else: GPR.print_it2( label , "%r" % map(str , (err, err.errno , err.message , err.msg, err.sqlstate)), 0) # always print errors # Warning: It seems that you are trying to print a plain string containing unicode characters finally: cursor.close()
def vol_id_gen(cnx, in_gen): """processor for to find, store and then add vol_id to each item as it goes by""" # try: # select vol_id from volumes_uuid (based on volume uuid) # select vol_id from files (based on volume name and create date) # do a "test" insert of the volume entry just to get the autocreated vol_id back. (then act like it didn't happen :) local_vol_id = None for in_dict in in_gen: if local_vol_id == None: local_vol_id = db_get_vol_id(cnx, in_dict, local_vol_id) GPR.print_it2("vol_id_gen", "volume = %s, vol_id = %r" % (in_dict[NSURLNameKey], local_vol_id), verbose_level_threshold=2) in_dict['vol_id'] = local_vol_id yield in_dict GPR.print_it("end vol_id_gen.", verbose_level_threshold=3)
def main(): # some favorite testing files s = '/Volumes/Ulysses/bittorrent' s = u'/Users/donb/Downloads/incomplete' s = '/Volumes/Ulysses/TV Shows/Nikita/' # package s = u"/Users/donb/Documents/Installing Evernote v. 4.6.2—Windows Seven.rtfd" s = '/Volumes/Ulysses/bittorrent' s = u'/Users/donb/Downloads/incomplete' s = u'/Users/donb/Ashley+Roberts/' s = "/Volumes/Taos/videogame/" s = "/Volumes/Daytona/TV Series/Americas Next Top Model" s = '.' s = "/Volumes/Taos/videogame/Perfect Dark/Joanna Dark/" s = u'/Users/donb/Ashley+Roberts/' s = u"~/Catherine Video Review.mp4" s = "/Volumes/Brandywine/TV Show—single/" s = '/Volumes/Ulysses/TV Shows/Nikita/' s = '/Volumes/Ulysses/bittorrent/' s = "/Volumes/Corinna" s = "/Volumes/Corinna/Actress/Alison Armitage" s = "/Volumes/Corinna/Actress/Keira Knightley/Keira Knightley - Kenneth Willardt's GQ Photoshoot" s = "/Volumes/Dunharrow" # hack to have Textmate run with hardwired arguments while command line can be free… if os.getenv('TM_LINE_NUMBER' ): argv = [] argv += ["-v"] # verbose_level = 2 argv += ["-v"] # argv += ["-v"] # verbose_level = 4 # argv = ["-d 3"] argv += ["-f"] # force folder scan # argv += ["-p"] # scanning packages argv += [s] else: argv = sys.argv[1:] (options, args) = do_parse_args(argv) # no args means do the current directory if args == []: args = ["."] args2 = [] for a in args: try: unicode(a) except UnicodeDecodeError: a2 = a.decode('utf8') # print "arg [ %s ] is a unicode string" % (a2, ) GPR.print_it2("arg is a unicode string", a2, verbose_level_threshold=1) args2.append(a2) else: args2.append(a) args = args2 args = [os.path.abspath(os.path.expanduser(a)) for a in args] GPR.verbose_level = options.verbose_level GPR.print_list("sys.argv", sys.argv, verbose_level_threshold=3) # display list of timezones if options.verbose_level >= 4: print_timezones("time_zones") GPR.print_dict("options (after optparsing)", options.__dict__, left_col_width=24, verbose_level_threshold=2) GPR.print_list("args (after optparsing)", args, verbose_level_threshold=3) do_args(args, options)
def files_generator(basepath, options): """a generator which yields all files (as file_dicts) including volume, superfolder(s), basepath, and then all subfiles (subject to depth_limit and enumerator options). """ GPR.print_it25("files_generator", basepath, 2) superfolders_list = [] basepath_url = NSURL.fileURLWithPath_(basepath) # begin loop going upwards url = NSURL.fileURLWithPath_(basepath) while True: d1 = GetURLValues(url, enumeratorURLKeys) superfolders_list.insert(0,d1) if d1[NSURLIsVolumeKey]: break # go "upwards" one level (towards volume) url = url.URLByDeletingLastPathComponent() GPR.print_superfolders_list("volume, superfolder(s)", superfolders_list, 4) # now go back down, yielding dict objects at each step: n = len(superfolders_list) for i, superfolder_dict in enumerate(superfolders_list): superfolder_dict['depth'] = i+1-n yield superfolder_dict # last dict in superfolder list is the basepath_dict basepath_dict = superfolder_dict item_is_package = is_item_a_package(basepath_url) if basepath_dict[NSURLIsDirectoryKey] and item_is_package and not options.scan_packages: GPR.print_it("\nbasepath is a directory and a package but we're not scanning packages.\n", 1) return # we've yielded basepath above, don't enumerate if basepath is not a directory (or package and we want packages) if basepath_dict[NSURLIsDirectoryKey]: enumeratorOptionKeys = 0L if not options.scan_packages: enumeratorOptionKeys |= NSDirectoryEnumerationSkipsPackageDescendants if not options.scan_hidden_files: enumeratorOptionKeys |= NSDirectoryEnumerationSkipsHiddenFiles enumerator = sharedFM.enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_( basepath_url, enumeratorURLKeys, enumeratorOptionKeys, error_handler_for_enumerator ) for url in enumerator: item_dict = GetURLValues(url, enumeratorURLKeys) depth = enumerator.level() item_dict['depth'] = depth if options.depth_limit and (depth >= options.depth_limit-1): enumerator.skipDescendants() yield item_dict GPR.print_it2("end files_generator", basepath, verbose_level_threshold=3)
def db_execute_relation(cnx, sql_query, item_dict, required_fields, label, verbose_level_threshold=2): """db_execute is GetDR, sqlparse, cursor.execute, return rel.add( r ) for r in cursor and cursor.close """ sql_dict = GetDR(item_dict, required_fields, verbose_level_threshold=verbose_level_threshold) sql_query = sql_query % sql_dict s = sqlparse.format( sql_query, reindent=True, encoding='utf8') GPR.print_it2( label, s, verbose_level_threshold) # 4 try: cursor = cnx.cursor() cursor.execute( sql_query ) column_names = [column.name for column in cursor.description] rel = relation( column_names, [] , "pg_del_files") for r in cursor: l = list(r) l[2] = l[2].decode('utf8') # file_name l[4] = str(l[4]) rel.add( l ) cnx.commit() # r = [z for z in cursor] except cnx.ProgrammingError as err: if err.message == "no results to fetch": return None else: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.IntegrityError as err: if err.pgcode == "23505": # duplicate key GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors else: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.InternalError as err: #InternalError: current transaction is aborted, commands ignored until end of transaction block GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.Error as err: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0 ) # always print errors finally: cursor.close() return rel