forked from six-pack/Axis-Mundi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
175 lines (144 loc) · 5.08 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from datetime import datetime
from PIL import Image
import cStringIO
import base64
import re
import json
import random
import string
import itertools
import os
import sys
from platform import system as get_os
class Listing(object):
# Listing class
def __init__(self):
# This will form part of btc address generation
self.id = str(random.SystemRandom().randint(1000000000, 2000000000))
self.title = ''
self.description = ''
self.unitprice = ''
self.currency_code = ''
self.categories = ''
self.is_public = True
self.image_str = ''
self.quantity_available = 0
self.order_max_qty = 0
self.seller_key= ''
self.ships_from = ''
self.ships_to = []
self.shipping_options = ''
class full_name(object):
def __init__(self, display_name, pgpkey_id):
self.display_name = display_name
self.pgpkey_id = pgpkey_id
class queue_task(object): # Tasks passed from the front end to the main thread for processing
def __init__(self, id, command, data):
self.id = id
self.command = command
self.data = data
def current_time():
utc_datetime = datetime.utcnow()
# always zero seconds to reduce impact of clock time skew leakage
return utc_datetime.strftime("%Y-%m-%d %H:%M") + ":00"
def get_age(when):
age = datetime.utcnow() - when
return age.seconds
def encode_image(buf, size):
im = Image.open(cStringIO.StringIO(buf))
im.thumbnail(size, Image.ANTIALIAS)
image_buffer = cStringIO.StringIO()
im.save(image_buffer, "PNG")
image_buffer.getvalue()
b64_image = base64.b64encode(image_buffer.getvalue())
return b64_image
def got_pgpkey(orm, key_id):
if key_id == '':
return True
session = orm.DBSession()
key_present = session.query(
orm.cachePGPKeys).filter_by(key_id=key_id).first()
if key_present:
return True
else:
return False
def parse_user_name(name):
if not name:
return None
key_only = re.compile('^[0-9a-fA-F]{16}$')
if key_only.match(name):
pgp_keyid = name
fullname = full_name
fullname.display_name = ''
fullname.pgpkey_id = name
return fullname
elif re.match('.*\([0-9a-fA-F]{16}\)(?!.*\([0-9a-fA-F]{16}\))$', name):
display_name = name[0:str(name).rindex('(')].strip()
pgp_keyid = name[str(name).rindex('(') + 1:len(name) - 1]
fullname = full_name
fullname.display_name = display_name
fullname.pgpkey_id = pgp_keyid
return fullname
else:
print "Unable to extract display name/key if from recipient"
return None
# display_name =
def generate_seed(wordlist_path, words=12):
seed = ''
# TODO: allow user specified wordlist AND handle different
# locations/non-existant words
wordlist = getWords(wordlist_path)
if words < 12:
words = 12
for x in range(0, words - 1):
seed = ' '.join(random.SystemRandom().choice(wordlist)
for _ in range(words))
return seed
def getWords(filepath):
with open(filepath) as f:
words = []
pos = {}
position = itertools.count()
for line in f:
for word in line.split():
if word not in pos:
pos[word] = position.next()
words.append(word)
return sorted(words, key=pos.__getitem__)
def os_is_tails():
# is this OS Tails
try:
with open('/etc/os-release') as f:
if re.match('TAILS_PRODUCT_NAME',f.readline()):
return True
except:
pass
return False
def replace_in_file(path,text,subs,flags=0):
with open (path,"r+") as file:
file_contents=file.read()
text_pattern = re.compile(re.escape(text), flags)
file_contents = text_pattern.sub(subs,file_contents)
file.seek(0)
file.truncate()
file.write(file_contents)
######## Pyinstaller onefile need paths translation #####################
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def json_from_clearsigned(clearsigned_rawmessage):
if clearsigned_rawmessage.startswith('-----BEGIN PGP SIGNED MESSAGE-----'):
nearly_stripped_message = clearsigned_rawmessage[
clearsigned_rawmessage.index('{'):clearsigned_rawmessage.rindex('}') + 1]
# Now replace and '- ' if at start of line with nothing to mirror pgp escaping of dashes
current_stage_signed = re.sub('(?m)^- ',"",nearly_stripped_message) # pgp wont do this for us
# Now strip the line breaks that were added prior to signing
stripped_message = current_stage_signed.replace('\n', '') # strip out all those newlines we added pre-signing
return stripped_message
else:
return False