Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, sys, base64, pickle, time, sqlite3
- class Pointer(object):
- def __init__(self, coords, direction):
- self.coords = coords
- self.direction = direction
- class Sample(object):
- def __init__(self, filepath, data_length):
- self.data_length = data_length
- with open(filepath, 'rb') as datafile:
- self.raw_data = datafile.read()
- #Building data_cube
- max_value = int(math.floor(len(self.raw_data) ** (1/3.0)))
- no_planes = 0
- data_cube = []
- pi_position = 0
- while no_planes < max_value:
- plane = []
- no_rows = 0
- while no_rows < max_value:
- row = []
- no_points = 0
- while no_points < max_value:
- row.append(self.raw_data[pi_position])
- no_points += 1
- pi_position += 1
- plane.append(row)
- no_rows += 1
- data_cube.append(plane)
- no_planes += 1
- self.data = data_cube
- def get_data_from_coordinates(self, coords, direction):
- x, y, z = coords
- plane = self.data[z]
- row = plane[y]
- point = row[x]
- if direction == 'x+':
- modifier = 0
- data = ''
- while modifier < self.data_length:
- if x+modifier > len(row) - 1:
- data = None
- break
- data += row[x+modifier]
- modifier += 1
- elif direction == 'x-':
- modifier = 0
- data = ''
- while modifier < self.data_length:
- if x - modifier < 0:
- data = None
- break
- data += row[x-modifier]
- modifier += 1
- elif direction == 'y+':
- selected_row = []
- for row in plane:
- selected_row.append(row[x])
- modifier = 0
- data = ''
- pos = y
- while modifier < self.data_length:
- if pos+modifier > len(selected_row) - 1:
- data = None
- break
- data += selected_row[pos + modifier]
- modifier += 1
- elif direction == 'y-':
- selected_row = []
- for row in plane:
- selected_row.append(row[x])
- modifier = 0
- data = ''
- pos = y
- while modifier < self.data_length:
- if pos - modifier < 0:
- data = None
- break
- data += selected_row[pos - modifier]
- modifier += 1
- elif direction == 'z+':
- selected_row = []
- for plane in self.data:
- row = plane[y]
- selected_row.append(row[x])
- modifier = 0
- data = ''
- pos = z
- while modifier < self.data_length:
- if pos+modifier > len(selected_row) - 1:
- data = None
- break
- data += selected_row[pos+modifier]
- modifier += 1
- elif direction == 'z-':
- selected_row = []
- for plane in self.data:
- row = plane[y]
- selected_row.append(row[x])
- modifier = 0
- data = ''
- pos = z
- while modifier < self.data_length:
- if pos - modifier < 0:
- data = None
- break
- data += selected_row[pos - modifier]
- modifier += 1
- return data
- def get_pointer_from_data(self, data):
- z = 0
- for plane in self.data:
- y = 0
- for row in plane:
- x = 0
- for point in row:
- x_pos_data = self.get_data_from_coordinates((x,y,z), 'x+')
- x_neg_data = self.get_data_from_coordinates((x,y,z), 'x-')
- y_pos_data = self.get_data_from_coordinates((x,y,z), 'y+')
- y_neg_data = self.get_data_from_coordinates((x,y,z), 'y-')
- z_pos_data = self.get_data_from_coordinates((x,y,z), 'z+')
- z_neg_data = self.get_data_from_coordinates((x,y,z), 'z-')
- dataArray = [x_pos_data, x_neg_data, y_pos_data, y_neg_data, z_pos_data, z_neg_data]
- if data in dataArray:
- data_index = dataArray.index(data)
- directionDict = {0:'x+',
- 1:'x-',
- 2:'y+',
- 3:'y-',
- 4:'z+',
- 5:'z-'}
- the_pointer = Pointer((x,y,z), directionDict[data_index])
- print("({0}, {1}, {2}) | {3}".format(x,y,z, directionDict[data_index]))
- return the_pointer
- x += 1
- y += 1
- z += 1
- print("NOT FOUND")
- class Cache(object):
- def __init__(self):
- self.con = sqlite3.connect('cache.db')
- self.cur = self.con.cursor()
- def init_cache(self):
- self.cur.execute("CREATE TABLE Cache (data TEXT, x INT, y INT, z INT, direction TEXT) ")
- def destroy_cache(self):
- self.cur.execute("DROP TABLE IF EXISTS Cache")
- def add_to_cache(self, pointer)
- class data_bundle(object):
- def __init__(self, data_sample, raw_data = ""):
- self.raw_data = raw_data
- self.chunk_size = 2
- self.sample = data_sample
- #table
- self.conversion_table = { 'a':'01',
- 'b':'02',
- 'c':'03',
- 'd':'04',
- 'e':'05',
- 'f':'06',
- 'g':'07',
- 'h':'08',
- 'i':'09',
- 'j':'10',
- 'k':'11',
- 'l':'12',
- 'm':'13',
- 'n':'14',
- 'o':'15',
- 'p':'16',
- 'q':'17',
- 'r':'18',
- 's':'19',
- 't':'20',
- 'u':'21',
- 'v':'22',
- 'w':'23',
- 'x':'24',
- 'y':'25',
- 'z':'26',
- 'A':'27',
- 'B':'28',
- 'C':'29',
- 'D':'30',
- 'E':'31',
- 'F':'32',
- 'G':'33',
- 'H':'34',
- 'I':'35',
- 'J':'36',
- 'K':'37',
- 'L':'38',
- 'M':'39',
- 'N':'40',
- 'O':'41',
- 'P':'42',
- 'Q':'43',
- 'R':'44',
- 'S':'45',
- 'T':'46',
- 'U':'47',
- 'V':'48',
- 'W':'49',
- 'X':'50',
- 'Y':'51',
- 'Z':'52',
- '0':'53',
- '1':'54',
- '2':'55',
- '3':'56',
- '4':'57',
- '5':'58',
- '6':'59',
- '7':'60',
- '8':'61',
- '9':'62',
- '+':'63',
- '/':'64' }
- #Future Attributes
- self.chunked_data = []
- self.pointers = []
- #Chunking
- self.chunk_data()
- #Getting Pointers
- self.get_pointers()
- def chunk_data(self):
- raw_chunks = [self.raw_data[i:i+self.chunk_size] for i in range(0, len(self.raw_data), self.chunk_size)]
- clean_chunks = []
- for chunk in raw_chunks:
- chunk_list = list(chunk)
- while len(chunk_list) < self.chunk_size:
- chunk_list.reverse()
- chunk_list.append('^')
- chunk_list.reverse()
- chunk = ''.join(chunk_list)
- clean_chunks.append(chunk)
- for chunk in clean_chunks:
- code = ""
- for char in chunk:
- if char in self.conversion_table.keys():
- char_symbol = self.conversion_table[char]
- else:
- char_symbol = '00'
- code += char_symbol
- self.chunked_data.append(code)
- def get_pointers(self):
- mycache = Cache()
- mycache.init_cache()
- max_length = len(self.chunked_data)
- counter = 0
- for chunk in self.chunked_data:
- new_pointer = self.sample.get_pointer_from_data(chunk)
- self.pointers.append(new_pointer)
- counter+=1
- print("{0}/{1}".format(counter, max_length))
- mycache.destroy_cache()
- def dump(self, filename):
- with open(filename, 'wb') as opened_file:
- pointer_array = []
- for pointer in self.pointers:
- x = pointer.coords[0]
- y = pointer.coords[1]
- z = pointer.coords[2]
- direction = pointer.direction
- data = [x,y,z,direction]
- pointer_array.append(data)
- opened_file.write(pickle.dumps(pointer_array))
- def reload(self, filename):
- #Resetting Attributes
- self.pointers = []
- self.chunked_data = []
- self.raw_data = ""
- pointers_array = pickle.loads(filename)
- #Rebuilding pointers...
- for pointer_data in pointers_array:
- pointer = Pointer((pointer_data[0], pointer_data[1], pointer_data[2]), pointer_data[3])
- self.pointers.append(pointer)
- #Recovering data from pi...
- for pointer in self.pointers:
- coords = pointer.coords
- direction = pointer.direction
- data = self.sample.get_data_from_coordinates(coords, direction)
- self.chunked_data.append(data)
- #De-Chunking Data
- print self.chunked_data
- for chunk in self.chunked_data:
- one_found = False
- two_found = False
- char_one = chunk[0] + chunk[1]
- char_two = chunk[2] + chunk[3]
- for key in self.conversion_table.keys():
- if char_one == self.conversion_table[key] and not one_found:
- char_one = key
- one_found = True
- if char_two == self.conversion_table[key] and not two_found:
- char_two = key
- two_found = True
- if not one_found:
- char_one = ''
- if not two_found:
- char_two = ''
- self.raw_data += char_one + char_two
- def init():
- #Removing useless argument
- arguments = sys.argv
- arguments.reverse()
- arguments.pop()
- arguments.reverse()
- if len(arguments) < 2 or len(arguments) > 3:
- sys.exit("USAGE: pienc [action] [filename] {output}")
- #Getting file names
- action = arguments[0]
- filename = arguments[1]
- if len(arguments) == 3:
- outfile = arguments[3]
- else:
- outfile = filename+".out"
- input_file = ""
- #Reading file
- try:
- with open(filename, 'rb') as infile:
- input_file = infile.read()
- except:
- sys.exit("An error occured while reading the file")
- #Action
- if action == 'e':
- encode(input_file, outfile)
- elif action == 'd':
- decode(input_file, outfile)
- else:
- sys.exit("Invalid command: '{0}'".format(action))
- def encode(infile, outfile):
- data = base64.b64encode(infile)
- data = data.replace("=", "") #Cleaning up data
- data_sample = Sample('pi.dat', 4)
- bundled_data = data_bundle(data_sample, data)
- bundled_data.dump(outfile)
- def decode(infile, outfile):
- data_sample = Sample('pi.dat', 4)
- bundled_data = data_bundle(data_sample)
- bundled_data.reload(infile)
- data = bundled_data.raw_data
- missing_padding = 4 - len(data) % 4
- if missing_padding:
- data += b'='* missing_padding
- clean_data = base64.decodestring(data)
- print(outfile)
- with open(outfile, 'a+') as file_out:
- file_out.write(clean_data)
- start_time = time.time()
- init()
- print("--- %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement