tools/outputfile.py

Thu, 21 Jun 2018 18:46:03 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 21 Jun 2018 18:46:03 +0300
changeset 1417
ed39bfca7a67
parent 1337
d4f71a6b9e40
permissions
-rw-r--r--

refactored the segments/divisions editor in MainWindow to a new widget

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

mercurial