Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. scriptDir = os.getcwd()
  5. inputDir = scriptDir + "//provinces//"
  6.  
  7. def getFileAsString(filepath):
  8. with open(filepath, "rt", encoding='windows-1252') as sourceFile:
  9. text = sourceFile.readlines()
  10.  
  11. text_string = ""
  12.  
  13. for i in range(len(text)):
  14. text_string = text_string + text[i]
  15.  
  16. return text_string
  17.  
  18. def matchRegex(regex, line):
  19. lineMatches = re.search(regex, line)
  20.  
  21. if (lineMatches != None):
  22. return lineMatches
  23. else:
  24. return None
  25.  
  26. def compile_search( regex=None, province_data=None, datalist=None ):
  27. pattern = re.compile(regex)
  28. if datalist == None:
  29. datalist = []
  30.  
  31. for m in re.finditer(pattern, province_data):
  32. find = get_data_value(m.group(0)).lstrip().rstrip()
  33. datalist.append(find)
  34.  
  35. if len(datalist) >= 1:
  36. return datalist[0]
  37. else:
  38. return ""
  39.  
  40. def get_data_value( find ):
  41. return find.split( "=" )[1].lstrip()
  42.  
  43. def extractProvinceData( filename ):
  44. os.chdir(inputDir)
  45. file_contents = getFileAsString( filename )
  46.  
  47. os.chdir(inputDir)
  48.  
  49. file_info = filename.split( "-" )
  50. province_id = file_info[0].rstrip()
  51. file_info = file_info[1:]
  52. province_name = ""
  53. for i in range( 0, len(file_info) ):
  54. if i != 0 and i != len(file_info) - 1:
  55. province_name = province_name + file_info[i].lstrip().title()
  56. elif i == len(file_info) - 1:
  57. province_name = province_name + file_info[i].lstrip()
  58.  
  59. # Check for history, split if present
  60. dateMatches = matchRegex("\d*\.\d*\.\d*", file_contents)
  61.  
  62. if dateMatches != None:
  63. delimiter = dateMatches.group(0)
  64.  
  65. split_contents = file_contents.split(delimiter)
  66.  
  67. province_data = split_contents[0]
  68.  
  69. split_contents = split_contents.pop()
  70. history = delimiter
  71. for parts in split_contents:
  72. history = history + parts
  73. else:
  74. province_data = file_contents
  75. history = ""
  76.  
  77. # Remove comments
  78. province_data = re.sub( r'\#.*', '', province_data )
  79.  
  80. return Province( province_data, province_id, history )
  81.  
  82. class Province:
  83. def __init__(self, contents, id, history):
  84. self.id = id
  85. self.history = history
  86. self.contents = contents
  87.  
  88. # Generate lists
  89. self.owner = compile_search( "owner = .*", self.contents )
  90. self.controller = compile_search( "controller = .*", self.contents )
  91. self.core = compile_search( "add_core = .*", self.contents )
  92.  
  93. self.tax = compile_search( "base_tax = .*", self.contents )
  94. self.production = compile_search( "base_production = .*", self.contents )
  95. self.manpower = compile_search( "base_manpower = .*", self.contents )
  96.  
  97. self.religion = compile_search( "religion = .*", self.contents )
  98. self.culture = compile_search( "culture = .*", self.contents )
  99.  
  100. self.is_hre = compile_search( "hre = .*", self.contents )
  101. self.is_city = compile_search( "is_city = .*", self.contents )
  102. self.tradegood = compile_search( "trade_goods = .*", self.contents )
  103.  
  104. self.native_size = compile_search( "native_size = .*", self.contents )
  105. self.native_ferocity = compile_search( "native_ferocity = .*", self.contents )
  106. self.native_hostileness = compile_search( "native_hostileness = .*", self.contents )
  107.  
  108. def main():
  109. print( "Province Formatter" )
  110.  
  111. provinces = []
  112.  
  113. for (dirpath, dirnames, filenames) in os.walk(inputDir):
  114. for filename in filenames:
  115. provinces.append( extractProvinceData( filename ) )
  116.  
  117.  
  118. os.chdir(scriptDir)
  119. datafile = open( "province_data.csv", "wt", encoding='windows-1252')
  120. datafile.write( "ID,Owner,Controller,Core,Tax,Production,Manpower,Religion,Culture,Tradegood,Is HRE,Is City,Native Size,Native Ferocity,Native Hostileness\n")
  121.  
  122. for province in provinces:
  123. line = "{id},{owner},{controller},{core},{tax},{production},{manpower},{religion},{culture},{tradegood},{hrestatus},{citystatus},{nativesize},{nativeferocity},{nativehostileness}"
  124. line = line.format( id=province.id, owner=province.owner, controller=province.controller, core=province.core,
  125. tax=province.tax, production=province.production, manpower=province.manpower,
  126. religion=province.religion, culture=province.culture, tradegood=province.tradegood,
  127. hrestatus=province.is_hre, citystatus=province.is_city,
  128. nativesize=province.native_size, nativeferocity=province.native_ferocity, nativehostileness=province.native_hostileness
  129. )
  130. datafile.write( line + "\n" )
  131.  
  132. datafile.close()
  133.  
  134. if __name__ == "__main__":
  135. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement