Skip to content

Commit

Permalink
[python] add context manager support for xbmcvfs.File
Browse files Browse the repository at this point in the history
This makes xbmcvfs.File usable in with statements.

with xbmcvfs.File(file) as file:
  pass
  • Loading branch information
Rechi committed Aug 16, 2019
1 parent 205a462 commit 0775d52
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions xbmc/interfaces/legacy/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ namespace XBMCAddon
///
///
///--------------------------------------------------------------------------
/// @python_v19 Added context manager support
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file, 'w')
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file, 'w') as f:
/// ..
/// ..
/// ~~~~~~~~~~~~~
//
class File : public AddonClass
{
Expand All @@ -61,6 +70,11 @@ namespace XBMCAddon

inline ~File() override { delete file; }

#if !defined(DOXYGEN_SHOULD_USE_THIS)
inline File* __enter__() { return this; };
inline void __exit__() { close(); };
#endif

#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
Expand All @@ -84,6 +98,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as file:
/// b = f.read()
/// ..
/// ~~~~~~~~~~~~~
///
read(...);
#else
inline String read(unsigned long numBytes = 0)
Expand Down Expand Up @@ -116,6 +138,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as f:
/// b = f.readBytes()
/// ..
/// ~~~~~~~~~~~~~
///
readBytes(...);
#else
XbmcCommons::Buffer readBytes(unsigned long numBytes = 0);
Expand Down Expand Up @@ -143,6 +173,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// whith xbmcvfs.File(file, 'w') as f:
/// result = f.write(buffer)
/// ..
/// ~~~~~~~~~~~~~
///
write(...);
#else
bool write(XbmcCommons::Buffer& buffer);
Expand All @@ -169,6 +207,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as f:
/// s = f.size()
/// ..
/// ~~~~~~~~~~~~~
///
size();
#else
inline long long size() { DelayedCallGuard dg(languageHook); return file->GetLength(); }
Expand Down Expand Up @@ -198,6 +244,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as f:
/// result = f.seek(8129, 0)
/// ..
/// ~~~~~~~~~~~~~
///
seek(...);
#else
inline long long seek(long long seekBytes, int iWhence = SEEK_SET) { DelayedCallGuard dg(languageHook); return file->Seek(seekBytes,iWhence); }
Expand Down Expand Up @@ -225,6 +279,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as f:
/// s = f.tell()
/// ..
/// ~~~~~~~~~~~~~
///
tell();
#else
inline long long tell() { DelayedCallGuard dg(languageHook); return file->GetPosition(); }
Expand All @@ -248,6 +310,14 @@ namespace XBMCAddon
/// ..
/// ~~~~~~~~~~~~~
///
/// **Example (v19 and up):**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// with xbmcvfs.File(file) as f:
/// ..
/// ..
/// ~~~~~~~~~~~~~
///
close();
#else
inline void close() { DelayedCallGuard dg(languageHook); file->Close(); }
Expand Down

0 comments on commit 0775d52

Please sign in to comment.