Created
January 14, 2013 22:31
-
-
Save juntalis/4534140 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| """ | |
| __init__.py | |
| TODO: Description | |
| This program is free software. It comes without any warranty, to | |
| the extent permitted by applicable law. You can redistribute it | |
| and/or modify it under the terms of the Do What The Fuck You Want | |
| To Public License, Version 2, as published by Sam Hocevar. See | |
| http://sam.zoy.org/wtfpl/COPYING for more details. | |
| """ | |
| import copy, string as _string | |
| ws = _string.whitespace | |
| class ICloneable(object): | |
| def Clone(self): | |
| return copy.deepcopy(self) | |
| class IEnumerator(object): | |
| """ | |
| Not quite the same as the .NET IEnumerator, but oh | |
| well. Our aim isn't for full interpretation - just | |
| enough to interpret your average msbuild project | |
| file. """ | |
| def __init__(self, obj, index=0): | |
| self._idx = index | |
| self._obj = obj | |
| @property | |
| def Current(self): | |
| return self._obj[self._idx] | |
| def Reset(self): | |
| return IEnumerator(self._obj) | |
| def Next(self): | |
| idx = self._idx + 1 | |
| if idx >= len(self._obj): | |
| return None | |
| else: | |
| return IEnumerator(self._obj, idx) | |
| class IEnumerable(object): | |
| def GetEnumerator(self): | |
| return IEnumerator(self) | |
| class DotNetBaseClass(object): | |
| Overloads = object() | |
| def MemberWiseClone(self): return copy.copy(self) | |
| def GetType(self): return type(self) | |
| def ToString(self): return self.__str__() | |
| def Equals(self, other): return self.__eq__(other) | |
| def GetHashCode(self): return self.__hash__() | |
| #noinspection PyMethodFirstArgAssignment | |
| def Finalize(self): self = None | |
| def ReferenceEquals(self, other): return id(self) == id(other) | |
| @staticmethod | |
| def Parse(self, other): | |
| return type(self)(other) | |
| class DotNetAbstractClass(DotNetBaseClass): | |
| _abstract = True | |
| def __init__(self): | |
| raise TypeError('cannot instantiate abstract class') | |
| class DotNetInstClass(DotNetBaseClass): pass | |
| class DotNetNumClass(DotNetInstClass): | |
| MaxValue = 0 | |
| MinValue = 0 | |
| def __init__(self, val, *args, **kwargs): | |
| if val > self.MaxValue: | |
| raise TypeError('Value exceeds maximum value.') | |
| elif val < self.MinValue: | |
| raise TypeError('Value is smaller than minimum value.') | |
| args = tuple([val] + args) | |
| super(DotNetNumClass, self).__init__(*args, **kwargs) | |
| class TypeCode(object): | |
| Empty = 0 | |
| Object = 1 | |
| DBNull = 2 | |
| Boolean = 3 | |
| Char = 4 | |
| SByte = 5 | |
| Byte = 6 | |
| Int16 = 7 | |
| UInt16 = 8 | |
| Int32 = 9 | |
| UInt32 = 10 | |
| Int64 = 11 | |
| UInt64 = 12 | |
| Single = 13 | |
| Double = 14 | |
| Decimal = 15 | |
| DateTime = 16 | |
| String = 18 | |
| TypeCode = TypeCode() | |
| class String(str, DotNetInstClass, IEnumerable, ICloneable): | |
| def __init__(self, *args, **kwargs): | |
| super(String, self).__init__(*args, **kwargs) | |
| self.Split = self.split | |
| self.ToUpper = self.upper | |
| self.ToUpperInvariant = self.upper | |
| self.ToLower = self.upper | |
| self.ToLowerInvariant = self.upper | |
| self.PadLeft = self.ljust | |
| self.PadRight = self.rjust | |
| self.LastIndexOf = self.rfind | |
| self.LastIndexOfAny = self.rfind | |
| self.Join = self.join | |
| self.IndexOf = self.find | |
| self.IndexOfAny = self.find | |
| self.Normalize = lambda: None | |
| self.IsNormalized = lambda: True | |
| self.Contains = lambda s: s in self | |
| self.IsInterned = lambda: True | |
| self.Intern = lambda: self | |
| self.Concat = lambda s: self + s | |
| self.Insert = lambda i, s: self[:i] + s + self[i:] | |
| self.Copy = lambda: copy.copy(self) | |
| self.Trim = self.strip | |
| self.TrimStart = self.lstrip | |
| self.TrimEnd = self.rstrip | |
| self.StartsWith = self.startswith | |
| self.Replace = self.replace | |
| self.IsNullOrWhiteSpace = lambda: len(self.strip()) == 0 | |
| self.IsNullOrEmpty = lambda: len(self) == 0 | |
| self.ToCharArray = lambda: self.Chars | |
| self.Remove = lambda i: self[:i] | |
| self.GetTypeCode = lambda: TypeCode.String | |
| # Not implementing the following: | |
| self.CopyTo = lambda: self | |
| self.Compare = lambda o: self.Equals(o) | |
| self.CompareOrdinal = lambda o: self.Equals(o) | |
| self.Format = self.format | |
| @property | |
| def Length(self): return len(self) | |
| @property | |
| def Chars(self): return list(self) | |
| def Substring(self, i, length=None): | |
| if length is None: | |
| return self[i:] | |
| else: | |
| length += i | |
| if length > len(self): | |
| length = len(self) - 1 | |
| return self[i:length] | |
| class SByte(int, DotNetNumClass): | |
| MaxValue = 127 | |
| MinValue = -128 | |
| Byte = SByte | |
| class Int16(int, DotNetNumClass): | |
| MaxValue = 32767 | |
| MinValue = -32768 | |
| class UInt16(int, DotNetNumClass): | |
| MaxValue = 65535 | |
| MinValue = 0 | |
| class Int32(int, DotNetNumClass): | |
| MaxValue = 2147483647 | |
| MinValue = -2147483648 | |
| class UInt32(long, DotNetNumClass): | |
| MaxValue = 0xFFFFFFFFL | |
| MinValue = 0x00000L | |
| class Int64(long, DotNetNumClass): | |
| MaxValue = 0x7FFFFFFFFFFFFFFFL | |
| MinValue = -0x8000000000000000L | |
| class UInt64(long, DotNetNumClass): | |
| MaxValue = 0xFFFFFFFFFFFFFFFFL | |
| MinValue = 0x00000L | |
| class IntPtr(long, DotNetInstClass): | |
| Size = 4 | |
| class UIntPtr(long, DotNetInstClass): | |
| Size = 4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment