Wolfgang Scherer avatar Wolfgang Scherer committed 6ef6b40

adhoc.py: file inclusion consolidated. Preparations for file mode preservation.

Comments (0)

Files changed (1)

 
     # @:adhoc_run_time_section:@ on
     @classmethod
-    def unpack_(cls, ignored, file_=None, mtime=None,        # |:clm:|
+    def unpack_(cls, mod_name=None, file_=None, mtime=None, # |:clm:||:api_fi:|
                  zipped=True, flat=None, source64=None):
         # @:adhoc_run_time_section:@ off
         """Unpack adhoc'ed file, if it does not exist."""
         return strptime_(date_string, format_)
 
     @classmethod
-    def import_(cls, mod_name=None, file_=None, mtime=None,  # |:clm:|
+    def import_(cls, mod_name=None, file_=None, mtime=None, # |:clm:||:api_fi:|
                 zipped=True, flat=None, source64=None):
         # @:adhoc_run_time_section:@ off
         """Import adhoc'ed module."""
 
         # @:adhoc_run_time_section:@ on
         old_mtime = module.__adhoc__.mtime
-        module = cls.module_setup(mod_name, file_, mtime, source)
+        module = cls.module_setup(mod_name, file_, mtime, source, mode)
         if len(parent) > 0:
             setattr(sys.modules[parent], mod_child, module)
 
         # @:adhoc_run_time_section:@ on
 
     @classmethod
-    def module_setup(cls, module=None, file_=None, mtime=None, # |:clm:|
-                     source=None):
+    def module_setup(cls, module=None, file_=None, mtime=None, # |:clm:||:api_fi:|
+                     source=None, mode=0666):
         # @:adhoc_run_time_section:@ off
         '''Setup module for `AdHoc`.
         \\|:todo:| various modes are possible:
             setattr(adhoc, '__module__', module)
 
             mtime_set = None
+            mode_set = mode
             if hasattr(module, '__file__'):
                 module_file = module.__file__
                 if module_file.endswith('.pyc'):
                     stat = os.stat(module_file)
                     mtime_set = datetime.datetime.fromtimestamp(
                         stat.st_mtime)
+                    mode_set = stat.st_mode
             if mtime_set is None:
                 # the info is marked as very outdated
                 mtime_set = datetime.datetime.fromtimestamp(0)
             adhoc.mtime = mtime_set
+            adhoc.mode = mode_set
         else:
             adhoc = module.__adhoc__
 
             or not hasattr(module, '__file__')):
             if file_ is not None:
                 setattr(module, '__file__', file_)
-                if os.access(file_, os.R_OK):
+                if os.access(file_, os.R_OK):             # |:api_fi:|
                     stat = os.stat(file_)
                     adhoc.mtime = datetime.datetime.fromtimestamp(
                         stat.st_mtime)
+                    adhoc.mode = stat.st_mode
                     if adhoc.mtime > mtime:
                         # the file on disk is newer than the adhoc'ed source
                         try:
             if source is not None:
                 adhoc.source = source
                 adhoc.mtime = mtime
+                adhoc.mode = mode
 
         if not hasattr(adhoc, 'source'):
             try:
                 file_ = module.__file__
-                if file_.endswith('.pyc'):
-                    file_ = file_[:-1]
-                source = cls.read_source(file_)
+                file_, source = cls.std_source_param(file_, source)
                 adhoc.source = source
             except (AttributeError, IOError):
                 # @:adhoc_run_time_section:@ off
 
     # @:adhoc_run_time_section:@ on
     @classmethod
-    def std_source_param(cls, file_=None, source=None):      # |:clm:|
+    def std_source_param(cls, file_=None, source=None): # |:clm:||:api_fi:|
         # @:adhoc_run_time_section:@ off
         '''Setup standard source parameters.
 
+        :returns: tuple ``( file_, source )``
+
         :param file_: If None, `__file__` is used. If it ends with
           ``.pyc``, it is transformed to ``.py``.
         :param source: If None, the result of :meth:`read_source` is
 
     # @:adhoc_run_time_section:@ on
     @classmethod
-    def export__(cls, mod_name=None, file_=None, mtime=None, # |:clm:|
+    def export__(cls, mod_name=None, file_=None, mtime=None, # |:clm:||:api_fi:|
                 zipped=True, flat=None, source64=None):
         source = cls.unpack_file(source64, zipped=zipped, decode=False)
         # @:adhoc_run_time_section:@ off
 
     # @:adhoc_run_time_section:@ on
     @classmethod
-    def dump__(cls, module_name, file_=None, mtime=None,     # |:clm:|
+    def dump__(cls, mod_name=None, file_=None, mtime=None, # |:clm:||:api_fi:|
                zipped=True, flat=None, source64=None):
         if cls.verbose:
             list(map(sys.stderr.write,
     modules = {}
     compiling = []
 
+    file_include_template = (                             # |:api_fi:|
+        "{ind}"
+        "# {stg}\n{ind}"
+        "{rtp}{ahc}("
+        "{mod},"
+        " file_={fnm},\n{ina}"
+        " mtime={mtm},"
+        " zipped={zip},"
+        " flat={flt},"
+        " source64=\n"
+        "{src}"
+        ")\n{ind}"
+        "# {etg}\n"
+        )
+
     # --------------------------------------------------
     # ||:sec:|| Setup
     # --------------------------------------------------
                             stat = os.stat(lfile)
                             mtime = datetime.datetime.fromtimestamp(
                                 stat.st_mtime)
+                            mode = stat.st_mode
 
                             exp_source = self.read_source(lfile)
                             source_len = len(exp_source)
                             stat = os.stat(lfile)
                             mtime = datetime.datetime.fromtimestamp(
                                 stat.st_mtime)
+                            mode = stat.st_mode
 
                             exp_source = self.read_source(lfile, decode=False)
                             source64 = self.pack_file(exp_source, zipped)
                             output = self.strquote(source64, indent)
+                            file_include_args = dict([    # |:api_fi:|
+                                ('ind', indent),
+                                ('ina', ''.join((indent, "   "))),
+                                ('stg', ''.join((self.unpack_section_tag, ' !', ifile))),
+                                ('etg', self.unpack_section_tag),
+                                ('rtp', self.run_time_class_prefix),
+                                ('ahc', 'AdHoc.unpack_'),
+                                ('mod', 'None'),
+                                ('fnm', repr(str(ifile))),
+                                ('mtm', (((mtime is not None)
+                                          and repr(mtime.isoformat()))
+                                         or repr(mtime))),
+                                ('fmd', mode),
+                                ('zip', zipped),
+                                ('flt', flat),
+                                ('src', output.rstrip()),
+                            ])
                             output = sformat(
-                                "{0}# {1}\n{0}{9}{2}(None, file_={4},\n"
-                                "{0}    mtime={5}, zipped={6}, flat={10},"
-                                " source64=\n{7})\n{0}# {8}\n",
-                                indent,
-                                ''.join((self.unpack_section_tag, ' !', ifile)),
-                                'AdHoc.unpack_',
-                                None,
-                                repr(str(ifile)),
-                                (((mtime is not None)
-                                  and ((repr(mtime.isoformat())))
-                                  or (repr(mtime)))),
-                                zipped, output.rstrip(),
-                                self.unpack_section_tag,
-                                self.run_time_class_prefix,
-                                flat
+                                self.file_include_template,
+                                **file_include_args
                                 )
                             result.append(output)
                             found = True
         output = self.strquote(source64, indent)
 
         mtime = module.__adhoc__.mtime
+        mode = module.__adhoc__.mode
         # |:todo:| make Rt prefix configurable
+        file_include_args = dict([                        # |:api_fi:|
+            ('ind', indent),
+            ('ina', ''.join((indent, "   "))),
+            ('stg', ''.join((self.import_section_tag, ' !', module_name))),
+            ('etg', self.import_section_tag),
+            ('rtp', self.run_time_class_prefix),
+            ('ahc', for_),
+            ('mod', repr(module.__name__)),
+            ('fnm', (((module_file is not None)
+                      and repr(str(os.path.relpath(module_file))))
+                     or module_file)),
+            ('mtm', (((mtime is not None)
+                      and repr(mtime.isoformat()))
+                     or repr(mtime))),
+            ('fmd', mode),
+            ('zip', zipped),
+            ('src', output.rstrip()),
+            ('flt', flat),
+            ])
         output = sformat(
-            "{0}# {1}\n{0}{9}{2}({3}, file_={4},\n{0}    mtime={5}, zipped={6}, flat={10},"
-            " source64=\n{7})\n{0}# {8}\n",
-            indent,
-            ''.join((self.import_section_tag, ' !', module_name)),
-            for_,
-            repr(module.__name__),
-            (((module_file is not None) and (repr(str(os.path.relpath(module_file))))
-              or ((module_file)))),
-            #repr(mtime),
-            (((mtime is not None) and (repr(mtime.isoformat())))
-             or (repr(mtime))),
-            zipped, output.rstrip(),
-            self.import_section_tag,
-            self.run_time_class_prefix,
-            flat
+            self.file_include_template,
+            **file_include_args
             )
         result.append(output)
 
     #   :DBG:   __adhoc__              : ]...
     # ------------------------------
     #   :DBG:   __module__             : ]<module 'no:module:found' (built-in)>[
+    #   :DBG:   mode                   : ]...[
     #   :DBG:   mtime                  : ][
     # -----------------------------------
     # ||:SEC:|| adhoc_test.sub
     #   :DBG:   __adhoc__              : ]...
     # ------------------------------
     #   :DBG:   __module__             : ]<module 'adhoc' from '.../adhoc.py...'>[
+    #   :DBG:   mode                   : ]...[
     #   :DBG:   mtime                  : ][
     #   :DBG:   source                 : ]#!/usr/bin/env python\\n# -*- coding: utf-8 -*-\\n# Copyright (C) 2011, 2012, Wolfgang Scherer,
     ...
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.