diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2014-02-21 04:46:49 +0000 |
---|---|---|
committer | <> | 2014-08-04 21:38:17 +0000 |
commit | f3765db04b903b3671733e07cf1541a51966dd14 (patch) | |
tree | defcc3c47d9b8bd78b97dcc04ee779a758d37b1c /demo/utils.py | |
download | posix-ipc-tarball-master.tar.gz |
Imported from /home/lorry/working-area/delta_python-packages_posix-ipc-tarball/posix_ipc-0.9.8.tar.gz.HEADposix_ipc-0.9.8master
Diffstat (limited to 'demo/utils.py')
-rw-r--r-- | demo/utils.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/demo/utils.py b/demo/utils.py new file mode 100644 index 0000000..03b1b3a --- /dev/null +++ b/demo/utils.py @@ -0,0 +1,90 @@ +import time +import sys + +PY_MAJOR_VERSION = sys.version_info[0] + +if PY_MAJOR_VERSION > 2: + NULL_CHAR = 0 +else: + NULL_CHAR = '\0' + +def raise_error(error, message): + # I have to exec() this code because the Python 2 syntax is invalid + # under Python 3 and vice-versa. + s = "raise " + s += "error, message" if (PY_MAJOR_VERSION == 2) else "error(message)" + + exec(s) + + +def say(s): + """Prints a timestamped, self-identified message""" + who = sys.argv[0] + if who.endswith(".py"): + who = who[:-3] + + s = "%s@%1.6f: %s" % (who, time.time(), s) + print (s) + + +def write_to_memory(mapfile, s): + """Writes the string s to the mapfile""" + say("writing %s " % s) + mapfile.seek(0) + # I append a trailing NULL in case I'm communicating with a C program. + s += '\0' + if PY_MAJOR_VERSION > 2: + s = s.encode() + mapfile.write(s) + + +def read_from_memory(mapfile): + """Reads a string from the mapfile and returns that string""" + mapfile.seek(0) + s = [ ] + c = mapfile.read_byte() + while c != NULL_CHAR: + s.append(c) + c = mapfile.read_byte() + + if PY_MAJOR_VERSION > 2: + s = [chr(c) for c in s] + s = ''.join(s) + + say("read %s" % s) + + return s + + +def read_params(): + """Reads the contents of params.txt and returns them as a dict""" + params = { } + + f = open("params.txt") + + for line in f: + line = line.strip() + if line: + if line.startswith('#'): + pass # comment in input, ignore + else: + name, value = line.split('=') + name = name.upper().strip() + + if name == "PERMISSIONS": + # Think octal, young man! + value = int(value, 8) + elif "NAME" in name: + # This is a string; leave it alone. + pass + else: + value = int(value) + + #print "name = %s, value = %d" % (name, value) + + params[name] = value + + f.close() + + return params + |