def test_010_do_parse_args(self): s = u'/Users/donb/Ashley+Roberts/' argv = [] # argv = ["--help"]+[s] # argv = ["-rd 4"] argv = ["-d 2"] argv += ["-v"] # how tohave "-v" mean one and -v2, mean two? # argv += ["-v2"] # argv += ["-v"] # argv += ["-a"] argv += ["-p"] # argv += ["-f"] argv += [s] (options, args) = do_parse_args(argv) GPR.print_dict("options (after optparsing)", options.__dict__, left_col_width=24, verbose_level_threshold=2) self.assertEqual(options, Namespace(do_recursion=False, depth_limit=2, force_folder_scan=False, scan_hidden_files=False, scan_packages=True, verbose_level=2)) self.assertEqual( args, [u'/Users/donb/Ashley+Roberts/'] )
def db_connect(verbose_level_threshold=2): """open and return mysql connector object""" default_dict = { 'user': '******', 'password': '', 'host': '127.0.0.1', 'database': 'files' } try: config = ConfigParser.ConfigParser() config.readfp(open(CONFIG_FILE)) config_dict = dict(config.items('postgres')) default_dict.update(config_dict) except ConfigParser.NoSectionError as err: print err GPR.print_dict("postgres", default_dict, verbose_level_threshold=3) try: cnx = psycopg2.connect(**default_dict) # connection could have a cursor factory? GPR.print_attrs("connect", cnx, verbose_level_threshold=3) except psycopg2.OperationalError as err: if err.message.startswith( 'could not connect to server: Connection refused') : print "Is the postgres server running?" else: print err.message return return cnx # config.add_section('postgres') # for k,v in config_dict.items(): # self.__dict__.items(): # config.set('postgres', k, v) # with open(CONFIG_FILE, 'wb') as configfile: # config.write(configfile) # GPR.print_dict("db_connect(default_dict)", default_dict, verbose_level_threshold=1) try: cnx = mysql.connector.connect(**default_dict) except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Username or password %r and %r?" % (default_dict['user'], default_dict['password'])) elif err.errno == errorcode.ER_BAD_DB_ERROR: print "Database %r does not exist." % default_dict['database'] else: print 'err:', err GPR.print_attrs("mysql.connector", cnx, verbose_level_threshold=4) return cnx
def GetDR(item_dict, required_fields, quote_and_escape=True, verbose_level_threshold=3): """Convert from item_dict (Cocoa) forms to database-ready forms""" GPR.print_dict("GetDR(in)" , item_dict, 36, verbose_level_threshold) result_dict = {} for db_field_name in required_fields: if db_field_name in item_dict: dict_key_name = db_field_name else: try: db_field_name_index = map( lambda y: y[0], databaseAndURLKeys).index(db_field_name) dict_key_name = databaseAndURLKeys[db_field_name_index][1] except ValueError: dict_key_name = db_field_name # print "%16s => %-36s :" % (db_field_name, dict_key_name), # do special processing based on database field name, not on inherent type of argument? if db_field_name in ['vol_id', 'file_name', 'file_uti']: c = item_dict[dict_key_name].encode('utf8') if quote_and_escape: result_dict[db_field_name] = quote(escape(c)) else: result_dict[db_field_name] = c elif db_field_name in ['file_create_date', 'file_mod_date']: d = item_dict[dict_key_name] if isinstance( d, str): c = d else: c = str(db_df.stringFromDate_(d)) if quote_and_escape: result_dict[db_field_name] = quote(escape(c)) else: result_dict[db_field_name] = c elif db_field_name in ['file_size', 'file_id', 'folder_id']: # result_dict[db_field_name] = int(item_dict[dict_key_name]) else: result_dict[db_field_name] = item_dict[dict_key_name] GPR.print_dict_no_repr("GetDR(out)" + ( "(q+e)" if quote_and_escape else "(no q+e)"), result_dict, 36, verbose_level_threshold) return result_dict
def db_connect(): """open and return mysql connector object""" default_dict = { 'user': '******', 'password': '', 'host': '127.0.0.1', 'database': 'files', 'buffered': True } config = ConfigParser.ConfigParser() config.readfp(open(CONFIG_FILE)) config_dict = dict(config.items('dbstuff')) default_dict.update(config_dict) GPR.print_dict("db_connect(default_dict+config_dict)", default_dict, verbose_level_threshold=1) # config.add_section('dbstuff') # for k,v in config_dict.items(): # self.__dict__.items(): # config.set('dbstuff', k, v) # with open(CONFIG_FILE, 'wb') as configfile: # config.write(configfile) # GPR.print_dict("db_connect(default_dict)", default_dict, verbose_level_threshold=1) try: cnx = mysql.connector.connect(**default_dict) except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Username or password %r and %r?" % (default_dict['user'], default_dict['password'])) elif err.errno == errorcode.ER_BAD_DB_ERROR: print "Database %r does not exist." % default_dict['database'] else: print 'err:', err GPR.print_attrs("mysql.connector", cnx, verbose_level_threshold=4) return cnx
def X_GetD(item_dict): """Convert from item_dict (Cocoa) forms to something that the database DBI can convert from""" d = {} for dk, fk in databaseAndURLKeys: if dk: if fk in [NSURLNameKey, NSURLTypeIdentifierKey]: d[dk] = item_dict[fk].encode('utf8') elif dk in ['file_create_date', 'file_mod_date']: d[dk] = str(item_dict[fk])[:-len(" +0000")] # "2011-07-02 21:02:54 +0000" => "2011-07-02 21:02:54" elif type(item_dict[fk]) == objc._pythonify.OC_PythonLong: # print """"type(item_dict[fk]) = objc._pythonify.OC_PythonLong""", item_dict[fk], int(item_dict[fk]) d[dk] = int(item_dict[fk]) # elif 'objc._pythonify.OC_PythonLong' in str(type(item_dict[fk])): # print """"nscfnumber" in str(type(item_dict[fk]):""", item_dict[fk], int(item_dict[fk]), objc._pythonify.OC_PythonLong # d[dk] = int(item_dict[fk]) else: # print type(item_dict[fk]) d[dk] = item_dict[fk] GPR.print_dict("GetD result", d, 32, 4) return d
def db_insert_volume_data(cnx, vol_id, volume_url): """ insert/update volumes table with volume specific data, eg uuid, total capacity, available capacity """ # get volume info values, error = volume_url.resourceValuesForKeys_error_( ['NSURLVolumeUUIDStringKey', 'NSURLVolumeTotalCapacityKey', 'NSURLVolumeAvailableCapacityKey', 'NSURLVolumeSupportsPersistentIDsKey', 'NSURLVolumeSupportsVolumeSizesKey'] , None ) if error is not None: print print error # volume_dict.update(dict(values)) dv = dict(values) GPR.print_dict("volume info", values, 36, 1) # note: "on duplicate key update" of vol_total_capacity and vol_available_capacity. query = ("insert into volume_uuids " "(vol_id, vol_uuid, vol_total_capacity, vol_available_capacity) " "values ( %s, %s, %s, %s ) " "on duplicate key update " "vol_total_capacity = values(vol_total_capacity), " "vol_available_capacity = values(vol_available_capacity)" ) data = (vol_id, str(dv['NSURLVolumeUUIDStringKey']) , int(dv['NSURLVolumeTotalCapacityKey']), int(dv['NSURLVolumeAvailableCapacityKey']) ) (l , vol_id) = execute_insert_query(cnx, query, data, 1) GPR.pr4(l, vol_id, "", data[1], 1)
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)