class Test_test_cached_data(unittest.TestCase): def setUp(self): self.cache_o = Cached_data('test.csv', carTags) self.cache_o.load() def test_get_values_empty(self): row = self.cache_o.get_values('New ID') assert row == dict() def test_set_value(self): self.cache_o.set_value('New ID', 'Type', '1') row = self.cache_o.get_values('New ID') assert row['DB file ID'] == 'New ID' assert row['Type'] == '1' def test_set_value_new(self): self.cache_o.set_value('New ID 2', 'Aero', '1') row = self.cache_o.get_values('New ID 2') assert row['DB file ID'] == 'New ID 2' assert row['Type'] == '' assert row['Aero'] == '1' def test_write(self): if os.path.isfile('test.csv'): os.remove('test.csv') self.test_set_value() self.cache_o.write() assert os.path.isfile('test.csv') self.cache2_o = Cached_data('test.csv', carTags) self.cache2_o.load() row = self.cache2_o.get_values('New ID') assert row['DB file ID'] == 'New ID' assert row['Type'] == '1' os.remove('test.csv')
def savePressed(self): # Write the data to a file named by the unique ID field messagebox.askokcancel( 'Sorry', 'Save not yet implemented' ) return if 'Track Name' in self.fields: _cd_o = Cached_data(trackCacheDataFile, trackTags) else: _cd_o = Cached_data(carCacheDataFile, carTags) _cd_o.load() for i, tag in enumerate(self.fields): if self.label[i]['text'] == 'DB file ID': ident = self.entry[i].get() _cd_o.delete_entry(ident) break for i, tag in enumerate(self.fields): _cd_o.set_value(ident, self.label[i]['text'], self.entry[i].get()) _cd_o.write() reloadAllData()
class TrackDataFiles(DataFiles): def __init__(self): self.installed_folder = 'locations' self.datafiles_folder = os.path.normpath(TrackDatafilesFolder) self.vehNames = None # (only used for car files) self.cache_o = Cached_data(trackCacheDataFile, trackTags + ['Longitude', 'Latitude']) self.cache_o.load() def _get_mft_tags(self, mft): """ Get the tags from the MFT file """ text, error = readFile(mft) _tags = getTags(text) for requiredTag in [ 'Name', 'Version', 'Type', 'Author', 'Origin', 'Category', 'ID', 'URL', 'Desc', 'Date', 'Flags', 'RefCount', '#Signature', '#MASFile', 'MinVersion', '#BaseSignature' ]: # MASFile, Signature and BaseSignature filtered out if requiredTag in _tags: """filter out boilerplate Author=Mod Team URL=www.YourModSite.com Desc=Your new mod. """ if _tags[requiredTag] in [ 'Mod Team', 'www.YourModSite.com', 'Your new mod.' ]: _tags[requiredTag] = '' #print('%s=%s' % (requiredTag, _tags[requiredTag])) if requiredTag == 'Name': _tags['strippedName'] = cleanTrackName(_tags['Name']) _tags['Year'], _tags['Decade'], _tags[ 'strippedName'] = extractYear(_tags['strippedName']) # Title Case The Name _tags['strippedName'] = _tags['strippedName'].title() # We need the original data folder to assemble the .SCN file path to put in # "Player.JSON" to force rF2 to switch tracks. We also need the .SCN # file names and that's a bit more difficult. # To select the track we also need the "Scene Description" _tags['originalFolder'], _ = os.path.split( mft[len(rF2root) + 1:]) # strip the root if 'Scene Description' not in _tags or _tags[ 'Scene Description'] == '': # if scn file name is available in scnNames.txt use it scnNames = getVehScnNames('scnNames.txt') if 'Name' in _tags and _tags['Name'] in scnNames: _tags['Scene Description'] = scnNames[_tags['Name']] else: _tags['Name'] = 'No track name' if 'Category' in _tags and _tags['Category'] in trackCategories: _tags['tType'] = trackCategories[_tags['Category']] tag = 'Track Name' if 'strippedName' in _tags: val = _tags['strippedName'].replace('_', ' ').strip() # default _tags[tag] = val else: _tags[tag] = 'No track name' return _tags def read_mas_files(self, tags, mas_dir): """ Scan track MAS files for: .scns: use the name .gdb files: add tags found in them """ _dir = self.dir_files_in_mas_files(mas_dir) for mas, files in _dir.items(): mas = os.path.join(mas_dir, mas) tags, found = self._read_mas_file(tags, mas, files) return tags def _read_mas_file(self, tags, mas, files): """ Open the track MAS files and look for *.scn - use the name *.gdb Latitude Longitude """ gdb_keywords = ['Latitude', 'Longitude'] found = False for _filename in files: _filename = _filename.lower().strip() if '.scn' in _filename: scn = os.path.splitext(_filename)[0] # Strip .scn tags['Scene Description'] = scn tags['Name'] = scn found = True if '.gdb' in _filename: tags = self.mas_file(mas, _filename, tags, gdb_keywords) # Only if cached_tags don't have it already? # (Only called if cached_tags is empty) if 'Latitude' in tags and 'Longitude' in tags: lat = float(tags['Latitude']) long = float(tags['Longitude']) address_o = google_address(lat, long) tags['Country'] = address_o.get_country() found = True if tags['Country'] == '': tags['Country'] = 'Lat ' + tags['Latitude'] tags['Continent'] = 'Long ' + tags['Longitude'] else: tags['Continent'] = country_to_continent( tags['Country']) else: if 'Latitude' in tags: tags['Country'] = 'Lat ' + tags['Latitude'] tags['Continent'] = '--No Long--' found = True if 'Longitude' in tags: tags['Country'] = '--No Lat--' tags['Continent'] = 'Long ' + tags['Longitude'] found = True return tags, found
class CarDataFiles(DataFiles): def __init__(self): self.installed_folder = 'vehicles' self.datafiles_folder = os.path.normpath(CarDatafilesFolder) self.vehNames = vehFiles( ) # Repeatedly reading this slows things down A LOT self.cache_o = Cached_data(carCacheDataFile, carTags) self.cache_o.load() def _get_mft_tags(self, mft): """ Get the tags from the MFT file """ text, error = readFile(mft) if error: return dict() _tags = getTags(text) for requiredTag in [ 'Name', 'Version', 'Type', 'Author', 'Origin', 'Category', 'ID', 'URL', 'Desc', 'Date', 'Flags', 'RefCount', '#Signature', '#MASFile', 'MinVersion', '#BaseSignature' ]: # MASFile, Signature and BaseSignature filtered out - NO THEY AREN'T, # _tags[] still contains them. tagsToBeWritten filters them out. # Not sure what this for loop is, er, for. if requiredTag in _tags: """filter out boilerplate Author=Mod Team URL=www.YourModSite.com Desc=Your new mod. """ if _tags[requiredTag] in [ 'Mod Team', 'www.YourModSite.com', 'Your new mod.' ]: _tags[requiredTag] = '' if _tags[requiredTag] in [ 'Slow Motion', 'Slow Motion Modding Group' ]: # make up your minds boys! _tags[requiredTag] = 'Slow Motion Group' if _tags[requiredTag] in ['Virtua_LM Modding Team' ]: # make up your minds boys! _tags[requiredTag] = 'Virtua_LM' if _tags['Category'] in carCategories: _tags['tType'] = carCategories[_tags['Category']] # We need the original data folder to assemble the .VEH file path to put in # "All Tracks & Cars.cch" to force rF2 to switch cars. We also need the .VEH # file names and that's a bit more difficult. # Not difficult, they're in all_vehicles.ini _tags['originalFolder'], _ = os.path.split(mft[len(rF2root) + 1:]) # strip the root # if veh file name is available in vehNames.txt use it _tags['vehFile'] = self.vehNames.veh(_tags['Name']) _tags = parse_name(_tags) _tags = parse_mfr_model(_tags) return _tags def _read_mas_file(self, tags, mas, files): """ Open the car mas files and look for *.hdv ForwardGears=6 WheelDrive=REAR // which wheels are driven: REAR, FOUR, or FRONT SemiAutomatic=0 // whether throttle and clutch are operated automatically (like an F1 car) maybe: TCSetting=0 ???? TractionControlGrip=(1.4, 0.2) // average driven wheel grip multiplied by 1st number, then added to 2nd TractionControlLevel=(0.33, 1.0) // effect of grip on throttle for low TC and high TC ABS4Wheel=0 // 0 = old-style single brake pulse, 1 = more effective 4-wheel ABS ABSGrip=(1.7, 0.0) // grip multiplied by 1st number and added to 2nd ABSLevel=(0.31, 0.92) // effect of grip on brakes for low ABS and high ABS Mass=828.0 Weight threshold FWRange=(0, 1, 1) // front wing range FWSetting=0 // front wing setting RWRange=(0, 1, 1) // rear wing range RWSetting=0 // rear wing setting (engine)*.ini BoostPower=0 no turbo? DumpValve= Turbo* *.tbc [COMPOUND] Name="Bias-Ply" but not [SLIPCURVE] Name="Lat" """ hdv_keywords = [ 'ForwardGears', 'WheelDrive', 'SemiAutomatic', 'Mass', 'FWSetting', 'RWSetting' ] ini_keywords = ['DumpValve', 'Turbo'] found = False for _filename in files: _filename = _filename.lower().strip() if '.hdv' in _filename: mas_tags = self.mas_file(mas, _filename, {}, hdv_keywords) if 'SemiAutomatic' in mas_tags: if mas_tags['SemiAutomatic'] == '0': tags['Gearshift'] = 'H' + mas_tags['ForwardGears'] found = True if 'WheelDrive' in mas_tags: tags['F/R/4WD'] = mas_tags['WheelDrive'] found = True if 'FWSetting' in mas_tags and 'RWSetting' in mas_tags: if mas_tags['FWSetting'] == '0' and mas_tags[ 'RWSetting'] == '0': tags['Aero'] = '0' found = True if 'Mass' in mas_tags: tags['Mass'] = str(int( mas_tags['Mass'].split('.')[0])) # May be float found = True if '.ini' in _filename: mas_tags = self.mas_file(mas, _filename, {}, ini_keywords) if 'DumpValve' in mas_tags: tags['Turbo'] = '1' found = True # Hacking to get an idea of tyre names: if '.tbc' in _filename: mas_tags = self.mas_file(mas, _filename, {}, ['Name']) if 'Mass' not in tags: # that PROBABLY indicates that mas was encrypted # which PROBABLY => S397 tags['Mass'] = '' if 'Author' not in tags or tags['Author'] == '': tags['Author'] = 'Studio 397?' return tags, found def read_mas_files(self, tags, mas_dir): """ Scan car MAS files for: .hdv files: add tags found in them (engine)*.ini DumpValve tag """ # defaults tags['Gearshift'] = 'Paddles' # Paddles or sequential tags['F/R/4WD'] = 'REAR' tags['Aero'] = '1' tags['Turbo'] = '0' _dir = self.dir_files_in_mas_files(mas_dir) for mas, files in _dir.items(): mas = os.path.join(mas_dir, mas) tags, found = self._read_mas_file(tags, mas, files) return tags