11#!@PYTHON_DEFAULT@
22# Copyright (C)
33# 2010 - Arnaud Quette <arnaud.quette@gmail.com>
4- # 2020 - 2024 Jim Klimov <jimklimov+nut@gmail.com>
4+ # 2020 - 2026 Jim Klimov <jimklimov+nut@gmail.com>
55#
66# This program is free software; you can redistribute it and/or modify
77# it under the terms of the GNU General Public License as published by
@@ -25,7 +25,23 @@ from __future__ import print_function
2525import sys
2626import re
2727import glob
28- import codecs
28+
29+ try:
30+ # NOTE: Deprecated as of python3.14
31+ import codecs
32+ HAVE_CODECS = True
33+ fd = None
34+ try:
35+ # Open self to test that it works
36+ fd = codecs.open(os.path.abspath(inspect.getfile(inspect.currentframe())), mode="r", encoding='utf-8')
37+ USE_CODECS = True
38+ except DeprecationWarning:
39+ USE_CODECS = False
40+ if fd is not None:
41+ close(fd)
42+ except:
43+ HAVE_CODECS = False
44+ USE_CODECS = False
2945
3046# Return a sorted list of unique entries, based on the input 'list'
3147def sortUnique(list):
@@ -80,7 +96,10 @@ if __name__ == '__main__':
8096 for filename in glob.glob('../../drivers/*.c'):
8197 # 1.2/ Exclude main.c, which defines addvar() and skel.c (example driver)
8298 if filename not in Exceptionlist:
83- fd = codecs.open(filename, encoding='utf-8')
99+ if USE_CODECS:
100+ fd = codecs.open(filename, encoding='utf-8')
101+ else:
102+ fd = open(filename, encoding='utf-8')
84103 # 1.3/ Grep for the "addvar(..." pattern
85104 matchResults = grep (r'.*addvar[\ ]*\(.*(VAR_FLAG|VAR_VALUE)*,.*', fd)
86105
@@ -95,7 +114,10 @@ if __name__ == '__main__':
95114 # Let's grep in .ch related files
96115 if (row[1].find('"') == -1):
97116 for defFilename in glob.glob(filename.replace('.c', '.[ch]')):
98- defFd = codecs.open(defFilename, encoding='utf-8')
117+ if USE_CODECS:
118+ defFd = codecs.open(defFilename, encoding='utf-8')
119+ else:
120+ defFd = open(defFilename, encoding='utf-8')
99121 matchString = '^#define.*' + row[1].replace('"', '').lstrip() + '.*'
100122 matchResult = grep (matchString, defFd)
101123 for varDefine in matchResult:
@@ -115,12 +137,18 @@ if __name__ == '__main__':
115137 specificVars += " | \"%s\"\n" %(name)
116138
117139 # 2/ Load the template lens
118- tplFd = codecs.open(dirPrefix + templateFilename, encoding='utf-8')
140+ if USE_CODECS:
141+ tplFd = codecs.open(dirPrefix + templateFilename, encoding='utf-8')
142+ else:
143+ tplFd = open(dirPrefix + templateFilename, encoding='utf-8')
119144
120145 # 2.1/ Search for the pattern to replace
121146 outputText = tplFd.read()
122147 outputText = outputText.replace('@SPECIFIC_DRV_VARS@', specificVars)
123148
124149 # 3/ Output final lens
125- outFd = codecs.open(outputFilename, mode='w', encoding='utf-8')
150+ if USE_CODECS:
151+ outFd = codecs.open(outputFilename, mode='w', encoding='utf-8')
152+ else:
153+ outFd = open(outputFilename, mode='w', encoding='utf-8')
126154 outFd.write(outputText)
0 commit comments