tools/outputfile.py

Fri, 01 Jul 2022 16:46:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 16:46:43 +0300
changeset 312
2637134bc37c
parent 272
9d52b119b3f5
permissions
-rw-r--r--

Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay

1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 # coding: utf-8
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 # Copyright 2015 Teemu Piippo
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 # All rights reserved.
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 #
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 # Redistribution and use in source and binary forms, with or without
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 # modification, are permitted provided that the following conditions
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 # are met:
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 #
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 # 1. Redistributions of source code must retain the above copyright
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer.
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 # 2. Redistributions in binary form must reproduce the above copyright
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 # notice, this list of conditions and the following disclaimer in the
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 # documentation and/or other materials provided with the distribution.
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 # 3. Neither the name of the copyright holder nor the names of its
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 # contributors may be used to endorse or promote products derived from
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 # this software without specific prior written permission.
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 #
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 class OutputFile:
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
34 def __init__(self, filename, *, verbose = False):
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 self.filename = filename
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
36 self.verbose = verbose
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
37 self.body = ''
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
38 self.oldsum = ''
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
39 def __enter__(self):
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 try:
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
41 with open(self.filename, "r") as file:
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
42 self.oldsum = file.readline().strip().removeprefix('// ')
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 except IOError:
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
44 pass
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
45 return self
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 def write(self, text):
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 self.body += text
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
48 def __exit__(self, *args):
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 from hashlib import sha256
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 checksum = sha256(self.body.encode('utf-8')).hexdigest()
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 if checksum == self.oldsum:
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
52 if self.verbose:
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
53 print(f'{self.filename} is up to date')
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 pass
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 else:
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
56 with open(self.filename, "w") as file:
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 file.write('// %s\n' % checksum)
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 file.write('// This file has been automatically generated. Do not edit by hand\n')
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 file.write('\n')
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 file.write(self.body)
272
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
61 if self.verbose:
9d52b119b3f5 Sort out versions more, add about page
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 1
diff changeset
62 print(f'{self.filename} written ({checksum})')

mercurial