summaryrefslogtreecommitdiffstats
path: root/util.py
blob: 478d24f8beddf97d691ba3e217156da7b8ed2327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
#
# AWL simulator - utility functions
# Copyright 2012-2013 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2.
#

import sys
import os
import random
import struct


class AwlSimError(Exception):
	pass

class AwlParserError(Exception):
	pass

# isPyPy is True, if the interpreter is PyPy.
isPyPy = "PyPy" in sys.version

# isPy3Compat is True, if the interpreter is Python 3 compatible.
isPy3Compat = sys.version_info[0] == 3

# isPy2Compat is True, if the interpreter is Python 2 compatible.
isPy2Compat = sys.version_info[0] == 2

# Python 2/3 helper selection
def py23(py2, py3):
	if isPy3Compat:
		return py3
	if isPy2Compat:
		return py2
	raise AwlSimError("Failed to detect Python version")

# Always map input() to text input
if isPy2Compat:
	input = raw_input

# Info message helper
def printInfo(text):
	sys.stdout.write(text)
	sys.stdout.write("\n")
	sys.stdout.flush()

# Error message helper
def printError(text):
	sys.stderr.write(text)
	sys.stderr.write("\n")
	sys.stderr.flush()

def awlFileRead(filename):
	try:
		fd = open(filename, "rb")
		data = fd.read()
		data = data.decode("latin_1")
		fd.close()
	except (IOError, UnicodeError) as e:
		raise AwlParserError("Failed to read '%s': %s" %\
			(filename, str(e)))
	return data

def awlFileWrite(filename, data):
	data = "\r\n".join(data.splitlines()) + "\r\n"
	for count in range(1000):
		tmpFile = "%s-%d-%d.tmp" %\
			(filename, random.randint(0, 0xFFFF), count)
		if not os.path.exists(tmpFile):
			break
	else:
		raise AwlParserError("Could not create temporary file")
	try:
		fd = open(tmpFile, "wb")
		fd.write(data.encode("latin_1"))
		fd.flush()
		fd.close()
		if os.name.lower() != "posix":
			# Can't use safe rename on non-POSIX.
			# Must unlink first.
			os.unlink(filename)
		os.rename(tmpFile, filename)
	except (IOError, OSError, UnicodeError) as e:
		raise AwlParserError("Failed to write file:\n" + str(e))
	finally:
		try:
			os.unlink(tmpFile)
		except (IOError, OSError):
			pass

# Returns the index of a list element, or -1 if not found.
def listIndex(_list, value, start=0, stop=-1):
	if stop < 0:
		stop = len(_list)
	try:
		return _list.index(value, start, stop)
	except ValueError:
		return -1

def str2bool(string, default=False):
	s = string.lower()
	if s in ("true", "yes", "on"):
		return True
	if s in ("false", "no", "off"):
		return False
	try:
		return bool(int(s, 10))
	except ValueError:
		return default

class EnumerationHelper(object):
	"Enumeration helper"

	def __init__(self):
		self.__num = None

	@property
	def start(self):
		assert(self.__num is None)
		self.__num = 0
		return None

	@start.setter
	def start(self, startNumber):
		assert(self.__num is None)
		self.__num = startNumber

	@property
	def end(self):
		self.__num = None
		return None

	@property
	def item(self):
		number = self.itemNoInc
		self.__num += 1
		return number

	@property
	def itemNoInc(self):
		assert(self.__num is not None)
		return self.__num

	def itemAt(self, number):
		assert(self.__num is not None)
		self.__num = number + 1
		return number

	def __repr__(self):
		return "enum(%s)" % str(self.__num)

enum = EnumerationHelper()
bues.ch cgit interface