Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- scriptDir = os.getcwd()
- inputDir = scriptDir + "//provinces//"
- def getFileAsString(filepath):
- with open(filepath, "rt", encoding='windows-1252') as sourceFile:
- text = sourceFile.readlines()
- text_string = ""
- for i in range(len(text)):
- text_string = text_string + text[i]
- return text_string
- def matchRegex(regex, line):
- lineMatches = re.search(regex, line)
- if (lineMatches != None):
- return lineMatches
- else:
- return None
- def compile_search( regex=None, province_data=None, datalist=None ):
- pattern = re.compile(regex)
- if datalist == None:
- datalist = []
- for m in re.finditer(pattern, province_data):
- find = get_data_value(m.group(0)).lstrip().rstrip()
- datalist.append(find)
- if len(datalist) >= 1:
- return datalist[0]
- else:
- return ""
- def get_data_value( find ):
- return find.split( "=" )[1].lstrip()
- def extractProvinceData( filename ):
- os.chdir(inputDir)
- file_contents = getFileAsString( filename )
- os.chdir(inputDir)
- file_info = filename.split( "-" )
- province_id = file_info[0].rstrip()
- file_info = file_info[1:]
- province_name = ""
- for i in range( 0, len(file_info) ):
- if i != 0 and i != len(file_info) - 1:
- province_name = province_name + file_info[i].lstrip().title()
- elif i == len(file_info) - 1:
- province_name = province_name + file_info[i].lstrip()
- # Check for history, split if present
- dateMatches = matchRegex("\d*\.\d*\.\d*", file_contents)
- if dateMatches != None:
- delimiter = dateMatches.group(0)
- split_contents = file_contents.split(delimiter)
- province_data = split_contents[0]
- split_contents = split_contents.pop()
- history = delimiter
- for parts in split_contents:
- history = history + parts
- else:
- province_data = file_contents
- history = ""
- # Remove comments
- province_data = re.sub( r'\#.*', '', province_data )
- return Province( province_data, province_id, history )
- class Province:
- def __init__(self, contents, id, history):
- self.id = id
- self.history = history
- self.contents = contents
- # Generate lists
- self.owner = compile_search( "owner = .*", self.contents )
- self.controller = compile_search( "controller = .*", self.contents )
- self.core = compile_search( "add_core = .*", self.contents )
- self.tax = compile_search( "base_tax = .*", self.contents )
- self.production = compile_search( "base_production = .*", self.contents )
- self.manpower = compile_search( "base_manpower = .*", self.contents )
- self.religion = compile_search( "religion = .*", self.contents )
- self.culture = compile_search( "culture = .*", self.contents )
- self.is_hre = compile_search( "hre = .*", self.contents )
- self.is_city = compile_search( "is_city = .*", self.contents )
- self.tradegood = compile_search( "trade_goods = .*", self.contents )
- self.native_size = compile_search( "native_size = .*", self.contents )
- self.native_ferocity = compile_search( "native_ferocity = .*", self.contents )
- self.native_hostileness = compile_search( "native_hostileness = .*", self.contents )
- def main():
- print( "Province Formatter" )
- provinces = []
- for (dirpath, dirnames, filenames) in os.walk(inputDir):
- for filename in filenames:
- provinces.append( extractProvinceData( filename ) )
- os.chdir(scriptDir)
- datafile = open( "province_data.csv", "wt", encoding='windows-1252')
- datafile.write( "ID,Owner,Controller,Core,Tax,Production,Manpower,Religion,Culture,Tradegood,Is HRE,Is City,Native Size,Native Ferocity,Native Hostileness\n")
- for province in provinces:
- line = "{id},{owner},{controller},{core},{tax},{production},{manpower},{religion},{culture},{tradegood},{hrestatus},{citystatus},{nativesize},{nativeferocity},{nativehostileness}"
- line = line.format( id=province.id, owner=province.owner, controller=province.controller, core=province.core,
- tax=province.tax, production=province.production, manpower=province.manpower,
- religion=province.religion, culture=province.culture, tradegood=province.tradegood,
- hrestatus=province.is_hre, citystatus=province.is_city,
- nativesize=province.native_size, nativeferocity=province.native_ferocity, nativehostileness=province.native_hostileness
- )
- datafile.write( line + "\n" )
- datafile.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement