annotate Documentations/setGPLHeader.py @ 471:73da921869d9 fix-bat-2

bugfix: implement battery charge percentage in dive header This commit is (much) less trivial than the related 919e5cb51c92. First, rename the CCRmode attribute (corresponding to byte Ox59) of the SLogbookHeaderOSTC3. This byte (according to the hwOS interface document) does not contain any CCR related value, but it contains "battery information". Already since 2017, this byte is used from libdivecomputer to interface the charge percentage. So, its renamed from CCRmode to batteryCharge, to reflect its true purpose. Now, simply add a batteryCharge attribute to the SLogbookHeader (and see below why that is possible, without breaking things). The remaining changes are trivial to implement battery charge percentage in dive header. Caveat: do not get confused by the exact role of the individual logbook header types. SLogbookHeaderOSTC3 is the formal type of the logbook format that the OSTC4 produces. This format is supposed to identical to the format, as is used in hwOS for the series of small OSTCs. Only some values of attributes are different. For example, the OSTC4 supports VPM, so byte 0x79 (deco model used for this dive) also has a value for VPM. But the SLogbookHeader type, despite its name and structure, is *not* a true logbook header, as it includes attributes that are not available in the SLogbookHeaderOSTC3 formal header type. Signed-off-by: Jan Mulder <jan@jlmulder.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Wed, 22 Apr 2020 13:08:57 +0200
parents 6237372f76a4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
1 #! /usr/bin/env python3
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
2
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
3 from __future__ import print_function
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
4 import os, sys, glob,re, codecs
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
5
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
6 ##############################################################################
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
7
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
8 OSTC4=os.getcwd()
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
9 if not os.path.exists( os.path.join(OSTC4, 'Discovery') ):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
10 raise 'Wrong start directory...'
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
11
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
12 ##############################################################################
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
13
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
14 def walk(OSTC4):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
15 for dir in ['BootLoader', 'Common', 'Discovery', 'FontPack', 'Small_CPU']:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
16 # Make sure we have the top directory...
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
17 if not os.path.exists( os.path.join(OSTC4, dir) ):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
18 raise 'Missing ' + dir
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
19
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
20 # Then walk in all its existing source sub-directories...
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
21 for sub in ['.', 'Inc', 'Src']:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
22 path = os.path.join(OSTC4, dir, sub)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
23 if not os.path.exists(path):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
24 continue
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
25 print(path + ':')
35
6237372f76a4 ... more GPL License and general cleanups
jDG@sauge
parents: 29
diff changeset
26 for file in sorted( glob.iglob( os.path.join(path, '*.[chs]') ) ):
29
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
27 try:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
28 work(file)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
29 except Exception as e:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
30 raise RuntimeError('Cannot process ' + file + ':\n\t' + str(e))
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
31
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
32 ##############################################################################
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
33
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
34 with open(os.path.join(OSTC4, 'Documentations', 'GPL_template.txt')) as f:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
35 template = f.read()
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
36
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
37 def work(file):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
38
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
39 # Unclear what is done if encoding is not yet UTF-8...
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
40 with open(file, 'rt', encoding='cp1252') as f:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
41 lines = f.read().splitlines()
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
42
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
43 # Set defaults
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
44 kw = {}
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
45 kw['file'] = file.replace(OSTC4+'/', '')
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
46 kw['brief'] = ''
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
47 kw['date'] = '2018'
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
48
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
49 # Try to gather info from existing header, if any:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
50 for line in lines:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
51 # Skip files that already have a GNU license:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
52 if 'GNU General Public License' in line:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
53 print('(done)\t' + file)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
54 return
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
55
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
56 get(line, 'brief', kw)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
57 get(line, 'date', kw)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
58
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
59 # Replace kw in header
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
60 header = template
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
61 for k,v in kw.items():
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
62 header = header.replace('$'+k, v)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
63
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
64 # Overwrite resulting file, normalizing EOL
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
65 with open(file, 'wt', encoding='utf-8') as f:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
66 for line in header.splitlines():
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
67 f.write(line.rstrip() + '\n')
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
68 for line in lines:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
69 f.write(line.rstrip() + '\n')
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
70 print('+\t' + file)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
71
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
72 ##############################################################################
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
73
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
74 def get(line, word, kw):
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
75 m = re.search('(@|\\\\)' + word + '\s*(.*)', line)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
76 if m:
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
77 kw[word] = m.group(2)
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
78
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
79 ##############################################################################
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
80
3a98f9e7ca58 ADD script to set GPL headers
jDG
parents:
diff changeset
81 walk(OSTC4)