public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/R_overlay:master commit in: roverlay/, roverlay/util/
@ 2014-02-22 14:56 André Erdmann
  0 siblings, 0 replies; 3+ messages in thread
From: André Erdmann @ 2014-02-22 14:56 UTC (permalink / raw
  To: gentoo-commits

commit:     b2d8535a2b905519d2b7d75c495135c7943edc99
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Fri Feb 21 18:28:08 2014 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Fri Feb 21 18:29:15 2014 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=b2d8535a

roverlay/util/common: get_home_dir()

---
 roverlay/core.py        |  3 ++-
 roverlay/util/common.py | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/roverlay/core.py b/roverlay/core.py
index b8fe6a5..d462f0f 100644
--- a/roverlay/core.py
+++ b/roverlay/core.py
@@ -22,6 +22,7 @@ import logging
 import roverlay.config
 import roverlay.recipe.easylogger
 import roverlay.tools.shenv
+import roverlay.util.common
 
 
 name        = "R_overlay"
@@ -45,7 +46,7 @@ DEFAULT_CONFIG_FILE_NAME = "R-overlay.conf"
 #
 CONFIG_DIRS = tuple ((
    (
-      ( os.getenv ( 'HOME' ) or os.path.expanduser ( '~' ) )
+      ( os.getenv ( 'HOME' ) or roverlay.util.common.get_home_dir ( None ) )
       + os.sep + 'roverlay'
    ),
    # os.sep is '/' if /etc exists, so don't care about that

diff --git a/roverlay/util/common.py b/roverlay/util/common.py
index 30dd310..92c0183 100644
--- a/roverlay/util/common.py
+++ b/roverlay/util/common.py
@@ -11,7 +11,7 @@ __all__= [
    'for_all_files_decorator', 'for_all_files',
    'get_dict_hash', 'keepenv', 'keepenv_v',
    'priosort', 'sysnop', 'getsize', 'is_vcs_dir', 'is_not_vcs_dir',
-    'headtail', 'try_unlink',
+    'headtail', 'try_unlink', 'get_pwd_info', 'get_home_dir',
 ]
 
 
@@ -19,6 +19,7 @@ import errno
 import os
 import sys
 import logging
+import pwd
 
 LOGGER = logging.getLogger ( 'util' )
 
@@ -322,3 +323,31 @@ def is_vcs_dir ( dirpath ):
 def is_not_vcs_dir ( dirpath ):
    return not is_vcs_dir ( dirpath )
 # --- end of is_not_vcs_dir (...) ---
+
+def get_pwd_info ( user=None ):
+   """Returns the passwd entry of the given user.
+
+   arguments:
+   * user -- name, uid or None (os.getuid()). Defaults to None.
+   """
+   if user is None:
+      return pwd.getpwuid ( os.getuid() )
+   elif isinstance ( user, int ):
+      return pwd.getpwuid ( user )
+   else:
+      try:
+         uid = int ( user, 10 )
+      except ValueError:
+         return pwd.getpwnam ( user )
+      else:
+         return pwd.getpwuid ( uid )
+# --- end of get_pwd_info (...) ---
+
+def get_home_dir ( user=None ):
+   """Returns a user's home directory.
+
+   arguments:
+   * user -- name, uid or None (os.getuid()). Defaults to None.
+   """
+   return get_pwd_info ( user ).pw_dir
+# --- end of get_home_dir (...) ---


^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [gentoo-commits] proj/R_overlay:master commit in: roverlay/, roverlay/util/
@ 2013-07-30 18:40 André Erdmann
  0 siblings, 0 replies; 3+ messages in thread
From: André Erdmann @ 2013-07-30 18:40 UTC (permalink / raw
  To: gentoo-commits

commit:     aa77ac2daaac2b4666fdecf574db00f2d5e1bba2
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Tue Jul 30 16:03:37 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Tue Jul 30 16:03:37 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=aa77ac2d

roverlay/util/fileio: detect filetype when reading

---
 roverlay/strutil.py     |  2 +-
 roverlay/util/common.py |  2 +-
 roverlay/util/fileio.py | 75 ++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/roverlay/strutil.py b/roverlay/strutil.py
index 755a191..a8e4361 100644
--- a/roverlay/strutil.py
+++ b/roverlay/strutil.py
@@ -145,7 +145,7 @@ def bytes_try_decode (
    * charwise_only -- do charwise conversion only
    * force_decode  -- decode byte_str even if it's already a str
    """
-   if not isinstance ( byte_str, str ):
+   if not isinstance ( byte_str, str ) or force_decode:
       if not charwise_only and encodings:
          ret = None
          if not isinstance ( encodings, str ):

diff --git a/roverlay/util/common.py b/roverlay/util/common.py
index 6053015..8449b2c 100644
--- a/roverlay/util/common.py
+++ b/roverlay/util/common.py
@@ -219,7 +219,7 @@ def dodir ( directory, mkdir_p=False, **makedirs_kw ):
 # --- end of dodir (...) ---
 
 def dodir_for_file ( filepath, mkdir_p=True, **kw ):
-   return dodir ( os.path.basename ( filepath ), mkdir_p=mkdir_p, **kw )
+   return dodir ( os.path.dirname ( filepath ), mkdir_p=mkdir_p, **kw )
 # --- end of dodir_for_file (...) ---
 
 def getsize ( filepath ):

diff --git a/roverlay/util/fileio.py b/roverlay/util/fileio.py
index 5d996f8..c6e33e5 100644
--- a/roverlay/util/fileio.py
+++ b/roverlay/util/fileio.py
@@ -10,14 +10,8 @@ import mimetypes
 import sys
 
 import roverlay.util.common
-
-if sys.hexversion >= 0x3000000:
-   def iter_decode ( lv ):
-      for l in lv:
-         yield l.decode()
-else:
-   def iter_decode ( lv ):
-      return lv
+import roverlay.strutil
+from roverlay.strutil import bytes_try_decode
 
 
 _MIME = mimetypes.MimeTypes()
@@ -37,19 +31,72 @@ SUPPORTED_COMPRESSION = {
 }
 
 
-def read_text_file ( filepath, preparse=None ):
+def read_compressed_file_handle ( CH, preparse=None ):
+   if preparse is None:
+      for line in CH.readlines():
+         yield bytes_try_decode ( line )
+   else:
+      yield preparse ( bytes_try_decode ( line ) )
+# --- end of read_compressed_file_handle (...) ---
+
+def read_text_file ( filepath, preparse=None, try_harder=True ):
+   """Generator that reads a compressed/uncompressed file and yields text
+   lines. Optionally preparses the rext lines.
+
+   arguments:
+   * filepath   -- file to read
+   * preparse   -- function for (pre-)parsing lines
+   * try_harder -- try known compression formats if file extension cannot
+                   be detected (defaults to True)
+   """
+
    ftype         = guess_filetype ( filepath )
    compress_open = SUPPORTED_COMPRESSION.get ( ftype[1], None )
 
    if compress_open is not None:
       with compress_open ( filepath, mode='r' ) as CH:
-         for line in iter_decode ( CH.readlines() ):
-            yield line if preparse is None else preparse ( line )
+         for line in read_compressed_file_handle ( CH, preparse ):
+            yield line
+
+   elif try_harder:
+      # guess_filetype detects file extensions only
+      #
+      #  try known compression formats
+      #
+      for comp in ( COMP_BZIP2, COMP_GZIP ):
+         CH = None
+         try:
+            CH = SUPPORTED_COMPRESSION [comp] ( filepath, mode='r' )
+            for line in read_compressed_file_handle ( CH, preparse ):
+               yield line
+            CH.close()
+         except IOError as ioerr:
+            if CH:
+               CH.close()
+            if ioerr.errno is not None:
+               raise
+         else:
+            break
+      else:
+         with open ( filepath, 'rt' ) as FH:
+            if preparse is None:
+               for line in FH.readlines():
+                  yield line
+            else:
+               for line in FH.readlines():
+                  yield preparse ( line )
+      # -- end for <comp>
    else:
       with open ( filepath, 'rt' ) as FH:
-         for line in FH.readlines():
-            yield line if preparse is None else preparse ( line )
+         if preparse is None:
+            for line in FH.readlines():
+               yield line
+         else:
+            for line in FH.readlines():
+               yield preparse ( line )
+   # -- end if <compress_open?, try_harder?>
 
+# --- end of read_text_file (...) ---
 
 def write_text_file (
    filepath, lines, compression=None, mode='wt',
@@ -64,7 +111,7 @@ def write_text_file (
       roverlay.util.common.dodir_for_file ( filepath )
 
    if compress_open:
-      NL = '\n'.encode()
+      NL = newline.encode()
       with compress_open ( filepath, mode.rstrip ( 'tu' ) ) as CH:
          for line in lines:
             CH.write ( str ( line ).encode() )


^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [gentoo-commits] proj/R_overlay:gsoc13/next commit in: roverlay/, roverlay/util/
@ 2013-06-19 18:58 André Erdmann
  2013-06-19 18:59 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
  0 siblings, 1 reply; 3+ messages in thread
From: André Erdmann @ 2013-06-19 18:58 UTC (permalink / raw
  To: gentoo-commits

commit:     8dfe62439f9e441b4eacceed058e3a4c86eb8610
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Wed Jun 19 18:45:52 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Wed Jun 19 18:45:52 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=8dfe6243

roverlay/: move util,py to util/common.py

The util module consists of a common part and (python-)version-specific code
now.

---
 roverlay/util/__init__.py            | 19 +++++++++++++++++++
 roverlay/{util.py => util/common.py} |  5 ++---
 roverlay/util/py2.py                 | 11 +++++++++++
 roverlay/util/py3.py                 | 12 ++++++++++++
 4 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/roverlay/util/__init__.py b/roverlay/util/__init__.py
new file mode 100644
index 0000000..77efd2a
--- /dev/null
+++ b/roverlay/util/__init__.py
@@ -0,0 +1,19 @@
+# R overlay -- roverlay package, util
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+from roverlay.util.common import *
+
+import sys
+
+#if sys.hexversion >= 0x4000000:
+#   raise NotImplementedError()
+
+if sys.hexversion >= 0x3000000:
+   from roverlay.util.py3 import *
+else:
+   from roverlay.util.py2 import *
+
+del sys

diff --git a/roverlay/util.py b/roverlay/util/common.py
similarity index 98%
rename from roverlay/util.py
rename to roverlay/util/common.py
index b9d083e..d8ad4b7 100644
--- a/roverlay/util.py
+++ b/roverlay/util/common.py
@@ -7,11 +7,11 @@
 """provides utility functions commonly used"""
 
 __all__= [
-   'dodir', 'keepenv', 'sysnop', 'get_dict_hash', 'priosort',
-   'for_all_files'
+   'dodir', 'for_all_files', 'get_dict_hash', 'keepenv', 'priosort', 'sysnop'
 ]
 
 import os
+import sys
 import logging
 
 LOGGER = logging.getLogger ( 'util' )
@@ -85,7 +85,6 @@ def get_dict_hash ( kwargs ):
    )
 # --- end of get_dict_hash (...) ---
 
-
 def keepenv ( *to_keep ):
    """Selectively imports os.environ.
 

diff --git a/roverlay/util/py2.py b/roverlay/util/py2.py
new file mode 100644
index 0000000..ef73684
--- /dev/null
+++ b/roverlay/util/py2.py
@@ -0,0 +1,11 @@
+# R overlay -- roverlay util package, python2-specific functions
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+__all__ = [ 'headtail', ]
+
+def headtail ( iterable ):
+   return ( iterable[0], iterable[1:] )
+# --- end of headtail #py2 (...) ---

diff --git a/roverlay/util/py3.py b/roverlay/util/py3.py
new file mode 100644
index 0000000..82083fb
--- /dev/null
+++ b/roverlay/util/py3.py
@@ -0,0 +1,12 @@
+# R overlay -- roverlay util package, python3-specific functions
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+__all__ = [ 'headtail', ]
+
+def headtail ( iterable ):
+   head, *tail = iterable
+   return ( head, tail )
+# --- end of headtail #py3 (...) ---


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-02-22 14:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-22 14:56 [gentoo-commits] proj/R_overlay:master commit in: roverlay/, roverlay/util/ André Erdmann
  -- strict thread matches above, loose matches on Subject: below --
2013-07-30 18:40 André Erdmann
2013-06-19 18:58 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-19 18:59 ` [gentoo-commits] proj/R_overlay:master " André Erdmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox