truncateTrp.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #coding=utf-8
  2. # _ _ _______ _____ _____
  3. # | | | | |__ __| __ \| __ \
  4. # | |_ _ __ _ _ _ __ ___ __ _| |_ ___| | | |__) | |__) |
  5. # | __| '__| | | | '_ \ / __/ _` | __/ _ \ | | _ /| ___/
  6. # | |_| | | |_| | | | | (_| (_| | || __/ | | | \ \| |
  7. # \__|_| \__,_|_| |_|\___\__,_|\__\___|_| |_| \_\_|
  8. # Python tool that 'truncates' CoPilot™ GPS trip files (.trp). Essentialy making a smaller, self-contained trip out of a larger one.
  9. # No breach of any copyright was intended.
  10. # Made by Rodrigo Rosmaninho (2018)
  11. # Github Repository: https://github.com/RodrigoRosmaninho/truncate-copilot-trips
  12. import traceback, argparse, sys, os.path, subprocess
  13. # 'argparser' manages arguments and the 'help' message in the command line
  14. global parser
  15. # Instantiate the parser
  16. parser = argparse.ArgumentParser(description='Python tool that \'truncates\' CoPilot trip files (.trp). Essentialy making a smaller, self-contained trip out of a larger one.')
  17. # Specifies input file
  18. parser.add_argument('-in', dest='fileIN',
  19. help='Specifies the name of your input .trp file. The file you want to read from.')
  20. # Specifies output file
  21. parser.add_argument('-out', dest='fileOUT',
  22. help='Specifies the name of your output .trp file. The file you want to write to.')
  23. # Specifies start stop number
  24. parser.add_argument('-start', dest='startStop',
  25. help='Specifies the number of the stop you want to start at. This is optional.')
  26. # Specifies end stop number
  27. parser.add_argument('-end', dest='endStop',
  28. help='Specifies the number of the stop you want to end at. This is optional.')
  29. data = []
  30. def main():
  31. global fileIN, fileOUT, start, end, args, data
  32. # Get arguments
  33. args = parser.parse_args()
  34. # If the value wasn't given as an argument, ask the user for it
  35. if args.fileIN == None:
  36. fileIN = input("Input .trp file: ")
  37. else:
  38. fileIN = args.fileIN
  39. # If the value wasn't given as an argument, ask the user for it
  40. if args.fileOUT == None:
  41. fileOUT = input("Output .trp file: ")
  42. else:
  43. fileOUT = args.fileOUT
  44. validate_file(fileIN)
  45. # CoPilot .trp files are encoded in UTF-8. Therefore, a temporary copy of the input file is created and converted to UTF-8 so that it can be read by this script without using '.decode()'
  46. run_bash_command("iconv -f UTF-16 -t UTF-8 " + fileIN + " -o temp.trp")
  47. fileIN = open("temp.trp","r")
  48. # A temporary output file, encoded in UTF-8, is created. Later, it'll be converted to UTF-16
  49. tempOUT = open("temp2.trp", "w")
  50. fileOUT = open(fileOUT, "w")
  51. read_file(fileIN)
  52. # If the value wasn't given as an argument, ask the user for it
  53. if args.startStop == None:
  54. start = int(input("Choose start point: "))
  55. else:
  56. start = args.startStop
  57. # If the value wasn't given as an argument, ask the user for it
  58. if args.endStop == None:
  59. end = int(input("Choose end point: "))
  60. else:
  61. end = args.endStop
  62. write_file(start, end, tempOUT, fileOUT.name)
  63. tempOUT.close()
  64. # Generate the final output file, encoded in UTF-16, as a copy of the temporary output file
  65. run_bash_command("iconv -f UTF-8 -t UTF-16 temp2.trp -o " + fileOUT.name)
  66. # Delete both temporary files
  67. run_bash_command("rm temp.trp temp2.trp")
  68. def validate_file(fname):
  69. if not os.path.exists(fname):
  70. sys.exit("Error! " + fname + " does not exist!")
  71. if os.path.isdir(fname):
  72. sys.exit("Error! " + fname + " is a directory!")
  73. if not os.path.isfile(fname):
  74. sys.exit("Error! " + fname + " is not a file!")
  75. def run_bash_command(cmd):
  76. process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
  77. output, error = process.communicate()
  78. # Reads the file line by line saving the data for each stop in an object of class 'Stop'. All stops are saved in 'data', a list of 'Stop' objects
  79. def read_file(file):
  80. global data
  81. current = -1
  82. stop = Stop()
  83. data=[]
  84. for line in file:
  85. if "Start Stop=Stop" in line:
  86. current += 1
  87. stop = Stop()
  88. stop.sequence = current
  89. elif "Name=" in line:
  90. stop.name = line[5:]
  91. elif "Address=" in line:
  92. stop.address = line[8:]
  93. elif "City=" in line:
  94. stop.city = line[5:]
  95. elif "State=" in line:
  96. stop.state = line[6:]
  97. elif "Country=" in line:
  98. stop.country = line[8:]
  99. elif "Longitude=" in line:
  100. stop.lng = line[10:]
  101. elif "Latitude=" in line:
  102. stop.lat = line[9:]
  103. elif "Show=" in line:
  104. stop.show = line[5:]
  105. elif ("End Stop" in line) and ("Opt" not in line):
  106. print("Stop " + str(stop.sequence), end="")
  107. if hasattr(stop, "name"):
  108. print(" -> " + stop.name, end="")
  109. else:
  110. print()
  111. data.append(stop)
  112. # Accesses the list of stops, 'data', and writes only the stops in between the 'start' and 'end' values to a new file
  113. def write_file(start, end, file, name):
  114. file.write("Data Version:2.14.6.1\n")
  115. file.write("Start Trip=" + name[:5] + "\n")
  116. file.write("Creator=Made with the truncateTrp tool by RodrigoRosmaninho\n")
  117. file.write("Memo=\n")
  118. file.write("End Trip\n\n")
  119. global data
  120. x = 0
  121. for i in range(int(start), int(end) + 1):
  122. file.write("Start Stop=Stop " + str(x) + "\n")
  123. if hasattr(data[i], "name"):
  124. file.write("Name=" + data[i].name)
  125. if hasattr(data[i], "address"):
  126. file.write("Address=" + data[i].address)
  127. if hasattr(data[i], "city"):
  128. file.write("City=" + data[i].city)
  129. if hasattr(data[i], "state"):
  130. file.write("State=" + data[i].state)
  131. if hasattr(data[i], "country"):
  132. file.write("Country=" + data[i].country)
  133. if hasattr(data[i], "lng"):
  134. file.write("Longitude=" + data[i].lng)
  135. if hasattr(data[i], "lat"):
  136. file.write("Latitude=" + data[i].lat)
  137. if hasattr(data[i], "show"):
  138. file.write("Show=" + data[i].show)
  139. file.write("Sequence=" + str(x) + "\n")
  140. file.write("End Stop\n")
  141. file.write("\n")
  142. file.write("Start StopOpt=Stop " + str(x) + "\n")
  143. file.write("Loaded=1\n")
  144. file.write("End StopOpt\n")
  145. file.write("\n")
  146. x += 1
  147. class Stop:
  148. global name, address, city, state, country, lng, lat, show, sequence
  149. if __name__ == "__main__":
  150. main()