PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/IronPython.Modules/bz2/BZ2File.cs

http://github.com/IronLanguages/main
C# | 228 lines | 175 code | 31 blank | 22 comment | 8 complexity | 47556969473459f2bedbbccfd333beb7 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* **************************************************************************
  2. *
  3. * Copyright 2012 Jeff Hardy
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * *************************************************************************/
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Runtime.InteropServices;
  22. using Ionic.BZip2;
  23. using IronPython.Runtime;
  24. using Microsoft.Scripting.Runtime;
  25. using IronPython.Runtime.Operations;
  26. using Microsoft.Scripting.Utils;
  27. namespace IronPython.Modules.Bz2 {
  28. public static partial class Bz2Module {
  29. [PythonType]
  30. public class BZ2File : PythonFile {
  31. public const string __doc__ =
  32. @"BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object
  33. Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or
  34. writing. When opened for writing, the file will be created if it doesn't
  35. exist, and truncated otherwise. If the buffering argument is given, 0 means
  36. unbuffered, and larger numbers specify the buffer size. If compresslevel
  37. is given, must be a number between 1 and 9.
  38. ";
  39. public int buffering { get; private set; }
  40. public int compresslevel { get; private set; }
  41. private Stream bz2Stream;
  42. public BZ2File(CodeContext context) : base(context) { }
  43. public void __init__(CodeContext context,
  44. string filename,
  45. [DefaultParameterValue("r")]string mode,
  46. [DefaultParameterValue(0)]int buffering,
  47. [DefaultParameterValue(DEFAULT_COMPRESSLEVEL)]int compresslevel) {
  48. var pythonContext = PythonContext.GetContext(context);
  49. this.buffering = buffering;
  50. this.compresslevel = compresslevel;
  51. if (!mode.Contains("b") && !mode.Contains("U")) {
  52. // bz2 files are always written in binary mode, unless they are in univeral newline mode
  53. mode = mode + 'b';
  54. }
  55. if (mode.Contains("w")) {
  56. var underlyingStream = File.Open(filename, FileMode.Create, FileAccess.Write);
  57. if (mode.Contains("p")) {
  58. this.bz2Stream = new ParallelBZip2OutputStream(underlyingStream);
  59. } else {
  60. this.bz2Stream = new BZip2OutputStream(underlyingStream);
  61. }
  62. } else {
  63. this.bz2Stream = new BZip2InputStream(File.OpenRead(filename));
  64. }
  65. this.__init__(bz2Stream, pythonContext.DefaultEncoding, filename, mode);
  66. }
  67. [Documentation(@"close() -> None or (perhaps) an integer
  68. Close the file. Sets data attribute .closed to true. A closed file
  69. cannot be used for further I/O operations. close() may be called more
  70. than once without error.
  71. ")]
  72. public new void close() {
  73. base.close();
  74. }
  75. [Documentation(@"read([size]) -> string
  76. Read at most size uncompressed bytes, returned as a string. If the size
  77. argument is negative or omitted, read until EOF is reached.
  78. ")]
  79. public new string read() {
  80. ThrowIfClosed();
  81. return base.read();
  82. }
  83. public new string read(int size) {
  84. ThrowIfClosed();
  85. return base.read(size);
  86. }
  87. [Documentation(@"readline([size]) -> string
  88. Return the next line from the file, as a string, retaining newline.
  89. A non-negative size argument will limit the maximum number of bytes to
  90. return (an incomplete line may be returned then). Return an empty
  91. string at EOF.
  92. ")]
  93. public new string readline() {
  94. ThrowIfClosed();
  95. return base.readline();
  96. }
  97. public new string readline(int sizehint) {
  98. ThrowIfClosed();
  99. return base.readline(sizehint);
  100. }
  101. [Documentation(@"readlines([size]) -> list
  102. Call readline() repeatedly and return a list of lines read.
  103. The optional size argument, if given, is an approximate bound on the
  104. total number of bytes in the lines returned.
  105. ")]
  106. public new List readlines() {
  107. if (this.closed) throw PythonOps.ValueError("I/O operation on closed file");
  108. return base.readlines();
  109. }
  110. public new List readlines(int sizehint) {
  111. if (this.closed) throw PythonOps.ValueError("I/O operation on closed file");
  112. return base.readlines(sizehint);
  113. }
  114. [Documentation(@"xreadlines() -> self
  115. For backward compatibility. BZ2File objects now include the performance
  116. optimizations previously implemented in the xreadlines module.
  117. ")]
  118. public new BZ2File xreadlines() {
  119. return this;
  120. }
  121. [Documentation(@"seek(offset [, whence]) -> None
  122. Move to new file position. Argument offset is a byte count. Optional
  123. argument whence defaults to 0 (offset from start of file, offset
  124. should be >= 0); other values are 1 (move relative to current position,
  125. positive or negative), and 2 (move relative to end of file, usually
  126. negative, although many platforms allow seeking beyond the end of a file).
  127. Note that seeking of bz2 files is emulated, and depending on the parameters
  128. the operation may be extremely slow.
  129. ")]
  130. public new void seek(long offset, [DefaultParameterValue(0)]int whence) {
  131. throw new NotImplementedException();
  132. //if (this.closed) throw PythonOps.ValueError("I/O operation on closed file");
  133. //base.seek(offset, whence);
  134. }
  135. [Documentation(@"tell() -> int
  136. Return the current file position, an integer (may be a long integer).
  137. ")]
  138. public new object tell() {
  139. throw new NotImplementedException();
  140. //if (this.closed) throw PythonOps.ValueError("I/O operation on closed file");
  141. //return base.tell();
  142. }
  143. [Documentation(@"write(data) -> None
  144. Write the 'data' string to file. Note that due to buffering, close() may
  145. be needed before the file on disk reflects the data written.
  146. ")]
  147. public new void write([BytesConversion]IList<byte> data) {
  148. ThrowIfClosed();
  149. base.write(data);
  150. }
  151. public new void write(object data) {
  152. ThrowIfClosed();
  153. base.write(data);
  154. }
  155. public new void write(string data) {
  156. ThrowIfClosed();
  157. base.write(data);
  158. }
  159. public new void write(PythonBuffer data) {
  160. ThrowIfClosed();
  161. base.write(data);
  162. }
  163. [Documentation(@"writelines(sequence_of_strings) -> None
  164. Write the sequence of strings to the file. Note that newlines are not
  165. added. The sequence can be any iterable object producing strings. This is
  166. equivalent to calling write() for each string.
  167. ")]
  168. public new void writelines(object sequence_of_strings) {
  169. ThrowIfClosed();
  170. base.writelines(sequence_of_strings);
  171. }
  172. public void __del__() {
  173. this.close();
  174. }
  175. [Documentation("__enter__() -> self.")]
  176. public new object __enter__() {
  177. ThrowIfClosed();
  178. return this;
  179. }
  180. [Documentation("__exit__(*excinfo) -> None. Closes the file.")]
  181. public new void __exit__(params object[] excinfo) {
  182. this.close();
  183. }
  184. }
  185. }
  186. }