Re: [chirp_devel] [developer] Control address format for full-file dump/diff - #1767
On Jul 15, 2014, at 6:26 PM, Dan Smith - dsmith@danplanet.com wrote:
The second thing is that my own preferred format is "both" which sort of breaks your model:
out + "%04i x%04X: " % ((i * line_sz), (i * line_sz))
I think you could do this:
result fmt % locals()
and then I could make a format like this to get what you want:
"%(line_sz)04i %(line_sz)04X"
Should be less code than what you have now and more flexible I think. Just catch ValueError, TypeError, and KeyError and you should be safe for error handling I think.
--Dan
I hadn't seen that form of string formatting before, and it makes this flexible approach much more reasonable. However, it doesn't _quite_ work because the config read seems to object to the "(name)" embedded in the format. Stack trace at the end if you're curious. I tried several forms of quoting and escaping without success.
But I've got a variant that does work, and I'm happier with it because it presents fewer ways for the user to do something strange.
The core of it is a config variable of the form hexdump_addrfmt = %04i x%04X and this code: addrfmt = addrfmt.replace ("%", "%(addr)") ... out += addrfmt % { "addr" : i * line_sz }
So far the only exceptions I've been able to throw are OverflowError for %c, and ValueError for %Q. Catching Overflow and replacing with %03i causes a very strange display, but a display results.
All the integer formats, as well as %s, %r, and even %E all do something reasonable.
Shall I submit a patch based on this approach?
-dan
===================================== Traceback (most recent call last): File "/Users/dan/chirpwork/dev_work/chirp.hg/chirpui/mainapp.py", line 1418, in mh self.do_diff_radio() File "/Users/dan/chirpwork/dev_work/chirp.hg/chirpui/mainapp.py", line 227, in do_diff_radio a = util.hexprint(eset_a.rthread.radio._mmap.get_packed()) File "/Users/dan/chirpwork/dev_work/chirp.hg/chirp/util.py", line 35, in hexprint addrfmt = CONF.get("hexdump_addrfmt", "developer") File "/Users/dan/chirpwork/dev_work/chirp.hg/chirpui/config.py", line 69, in get return self._config.get(key, section or self._section) File "/Users/dan/chirpwork/dev_work/chirp.hg/chirpui/config.py", line 46, in get return self.__config.get(section, key) File "/opt/kk7ds/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 615, in get return self._interpolate(section, option, value, d) File "/opt/kk7ds/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 661, in _interpolate option, section, rawval, e.args[0]) ConfigParser.InterpolationMissingOptionError: Bad value substitution: section: [developer] option : hexdump_addrfmt key : addr rawval : %(addr)04i x%(addr)04X =====================================
I hadn't seen that form of string formatting before, and it makes this flexible approach much more reasonable. However, it doesn't _quite_ work because the config read seems to object to the "(name)" embedded in the format. Stack trace at the end if you're curious. I tried several forms of quoting and escaping without success.
ConfigParser is trying to interpolate those as other configuration variables. You need to pass raw=True to the get() operation. The wrapping around ConfigParser makes that a little tough. Apply this (untested change) to your tree and then pass raw=True in the get call and see if that solves the problem. If so, then you can just include this in your patch:
--- a/chirpui/config.py Mon Jul 14 20:10:30 2014 -0400 +++ b/chirpui/config.py Thu Jul 17 20:04:23 2014 -0700 @@ -36,14 +36,14 @@ self.__config.write(cfg_file) cfg_file.close()
- def get(self, key, section): + def get(self, key, section, raw=False): if not self.__config.has_section(section): return None
if not self.__config.has_option(section, key): return None
- return self.__config.get(section, key) + return self.__config.get(section, key, raw=raw)
def set(self, key, value, section): if not self.__config.has_section(section): @@ -65,8 +65,9 @@ self._config = config self._section = section
- def get(self, key, section=None): - return self._config.get(key, section or self._section) + def get(self, key, section=None, raw=False): + return self._config.get(key, section or self._section, + raw=False)
def set(self, key, value, section=None): return self._config.set(key, value, section or self._section)
--Dan
participants (2)
-
chirp.cordless@xoxy.net
-
Dan Smith