forked from Thooms/PPaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppaste_lib.py
140 lines (113 loc) · 3.71 KB
/
ppaste_lib.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
import datetime
import json
import os
import random
import string
import time
class PPasteException(Exception):
'''Custom exception'''
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class PasteManager:
NAME_LEN = 6
ALPHABET = list(string.ascii_uppercase) + list(string.digits)
PASTE_LOCATION = os.path.join(os.getcwd(), 'pastes')
@classmethod
def check_pastes_directory(cls):
'''
Check function that raises an exception if the pastes directory
doesn't exists
'''
if not os.path.isdir(cls.PASTE_LOCATION):
raise PPasteException(
'Pastes directory ({}) does not exist'.format(
cls.PASTE_LOCATION))
@classmethod
def get_rand_paste_name(cls):
return ''.join(
random.choice(cls.ALPHABET)
for _ in range(cls.NAME_LEN)
)
@classmethod
def craft_paste_path(cls, paste_name):
return os.path.join(cls.PASTE_LOCATION, paste_name)
@classmethod
def save_paste(cls, paste):
cls.check_pastes_directory()
path = cls.craft_paste_path(paste.name)
if os.path.exists(path):
raise PPasteException('Paste file {} already exists'.format(path))
try:
with open(path, 'w') as f:
json.dump(paste.get_dict(), f)
except OSError as e:
raise PPasteException('Cannot write file {} - {}'.format(
path,
e
))
@classmethod
def fetch_paste(cls, name):
cls.check_pastes_directory()
path = cls.craft_paste_path(name)
if not os.path.exists(path):
raise PPasteException(
'Paste file {} does not exists'.format(path))
try:
with open(path, 'r') as f:
d = json.load(f)
return Paste(
title=d['title'],
content=d['content'],
hl_alias=d['hl_alias'],
is_private=d['is_private'],
date=d['date'],
name=name,
)
except OSError as e:
raise PPasteException('Cannot load file {} - {}'.format(
path,
e
))
@classmethod
def fetch_public_pastes(cls):
cls.check_pastes_directory()
return sorted(
filter(
lambda p: not p.is_private,
(cls.fetch_paste(name)
for name
in os.listdir(cls.PASTE_LOCATION))
),
key=lambda p: -p.date
)
class Paste:
def __init__(self, title='', content='', hl_alias='',
is_private=False, name=None, date=None):
self.title = title or ''
self.content = content or ''
# When no highlighting is specified, we default it to text so that
# fragments can work properly
self.hl_alias = hl_alias or 'text'
self.is_private = is_private
self.name = PasteManager.get_rand_paste_name() \
if name is None else name
self.date = int(time.time()) \
if date is None else date
def get_dict(self):
return {
'title': self.title,
'content': self.content,
'hl_alias': self.hl_alias,
'is_private': self.is_private,
'name': self.name,
'date': self.date
}
def save(self):
PasteManager.check_pastes_directory()
PasteManager.save_paste(self)
def pprint_date(self):
return datetime.datetime.fromtimestamp(
int(self.date)
).strftime('%Y-%m-%d %H:%M')