예제 #1
0
def create_filename(basename, directory):
  """
   Create a full file path from a directory and filename + If a file with the
   specified name already exists, an alternative will be used.
  
   @param basename string filename
   @param directory string directory
   @return
  """
  dest = directory + '/' + basename
  if (php.file_exists(dest)):
    # Destination file already exists, generate an alternative.
    pos = strrpos(basename, '.')
    if (pos):
      name = php.substr(basename, 0, pos)
      ext = php.substr(basename, pos)
    else:
      name = basename
    counter = 0
    while True:
      dest = directory + '/' + name + '_' + counter + ext
      counter += 1
      if (not php.file_exists(dest)):
        break
  return dest
예제 #2
0
파일: file.py 프로젝트: vaibbhav/drupy
def create_filename(basename, directory):
    """
   Create a full file path from a directory and filename + If a file with the
   specified name already exists, an alternative will be used.
  
   @param basename string filename
   @param directory string directory
   @return
  """
    dest = directory + '/' + basename
    if (php.file_exists(dest)):
        # Destination file already exists, generate an alternative.
        pos = strrpos(basename, '.')
        if (pos):
            name = php.substr(basename, 0, pos)
            ext = php.substr(basename, pos)
        else:
            name = basename
        counter = 0
        while True:
            dest = directory + '/' + name + '_' + counter + ext
            counter += 1
            if (not php.file_exists(dest)):
                break
    return dest
예제 #3
0
파일: file.py 프로젝트: vaibbhav/drupy
def destination(destination, replace):
    """
   Determines the destination path for a file depending on how replacement of
   existing files should be handled.
  
   @param destination A string specifying the desired path.
   @param replace Replace behavior when the destination file already exists.
     - FILE_EXISTS_REPLACE - Replace the existing file
     - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
       unique
     - FILE_EXISTS_ERROR - Do nothing and return False.
   @return The destination file path or False if the file already exists and
     FILE_EXISTS_ERROR was specified.
  """
    if (php.file_exists(destination)):
        if replace == FILE_EXISTS_RENAME:
            basename = basename(destination)
            directory = php.dirname(destination)
            destination = file_create_filename(basename, directory)
        elif replace == FILE_EXISTS_ERROR:
            drupal_set_message(t('The selected file %file could not be copied, \
        because a file by that name already exists in the destination.'                                                                             , \
              {'%file' : destination}), 'error')
            return False
    return destination
예제 #4
0
def destination(destination, replace):
  """
   Determines the destination path for a file depending on how replacement of
   existing files should be handled.
  
   @param destination A string specifying the desired path.
   @param replace Replace behavior when the destination file already exists.
     - FILE_EXISTS_REPLACE - Replace the existing file
     - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
       unique
     - FILE_EXISTS_ERROR - Do nothing and return False.
   @return The destination file path or False if the file already exists and
     FILE_EXISTS_ERROR was specified.
  """
  if (php.file_exists(destination)):
    if replace == FILE_EXISTS_RENAME:
      basename = basename(destination)
      directory = php.dirname(destination)
      destination = file_create_filename(basename, directory)
    elif replace == FILE_EXISTS_ERROR:
      drupal_set_message(t('The selected file %file could not be copied, \
        because a file by that name already exists in the destination.', \
        {'%file' : destination}), 'error')
      return False
  return destination
예제 #5
0
파일: file.py 프로젝트: vaibbhav/drupy
def download():
    """
   Call plugins that implement hook_file_download() to find out if a file is
   accessible and what headers it should be transferred with + If a plugin
   returns -1 drupal_access_denied() will be returned + If one or more plugins
   returned headers the download will start with the returned headers + If no
   plugins respond drupal_not_found() will be returned.
  """
    # Merge remainder of arguments from php.GET['q'], into relative file path.
    args = func_get_args()
    filepath = php.implode('/', args)
    # Maintain compatibility with old ?file=paths saved in node bodies.
    if (php.isset(php.GET, 'file')):
        filepath = php.GET['file']
    if (php.file_exists(file_create_path(filepath))):
        headers = plugin_invoke_all('file_download', filepath)
        if (php.in_array(-1, headers)):
            return drupal_access_denied()
        if (php.count(headers)):
            file_transfer(filepath, headers)
    return drupal_not_found()
예제 #6
0
def download():
  """
   Call plugins that implement hook_file_download() to find out if a file is
   accessible and what headers it should be transferred with + If a plugin
   returns -1 drupal_access_denied() will be returned + If one or more plugins
   returned headers the download will start with the returned headers + If no
   plugins respond drupal_not_found() will be returned.
  """
  # Merge remainder of arguments from php.GET['q'], into relative file path.
  args = func_get_args()
  filepath = php.implode('/', args)
  # Maintain compatibility with old ?file=paths saved in node bodies.
  if (php.isset(php.GET, 'file')):
    filepath =  php.GET['file']
  if (php.file_exists(file_create_path(filepath))):
    headers = plugin_invoke_all('file_download', filepath)
    if (php.in_array(-1, headers)):
      return drupal_access_denied()
    if (php.count(headers)):
      file_transfer(filepath, headers)
  return drupal_not_found()
예제 #7
0
파일: bootstrap.py 프로젝트: vaibbhav/drupy
def drupal_get_filename(type_, name, filename=None):
    """
   Returns and optionally sets the filename for a system item (plugin,
   theme, etc.). The filename, whether provided, cached, or retrieved
   from the database, is only returned if the file exists.
  
   This def plays a key role in allowing Drupal's resources (plugins
   and themes) to be located in different places depending on a site's
   configuration. For example, a plugin 'foo' may legally be be located
   in any of these three places:
  
   plugins/foo/__init__.py
   sites/all/plugins/foo/__init__.py
   sites/example.com/plugins/foo/__init__.py
  
   Calling drupal_get_filename('plugin', 'foo') will give you one of
   the above, depending on where the plugin is located.
  
   @param type
     The type of the item (i.e. theme, theme_engine, plugin).
   @param name
     The name of the item for which the filename is requested.
   @param filename
     The filename of the item if it is to be set explicitly rather
     than by consulting the database.
  
   @return
     The filename of the requested item.
  """
    php.static(drupal_get_filename, 'files', {})
    file = lib_database.result(lib_database.query(\
      "SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", \
       name, type_))
    if (not php.isset(drupal_get_filename.files, type_)):
        drupal_get_filename.files[type_] = {}
    if (filename is not None and php.file_exists(filename)):
        drupal_get_filename.files[type_][name] = filename
    elif (php.isset(drupal_get_filename.files[type_], name)):
        # nothing
        pass
    # Verify that we have an active database connection, before querying
    # the database.  This is required because this def is called both
    # before we have a database connection (i.e. during installation) and
    # when a database connection fails.
    elif (lib_database.is_active() and (file and php.file_exists(file))):
        drupal_get_filename.files[type_][name] = file
    else:
        # Fallback to searching the filesystem if the database connection is
        # not established or the requested file is not found.
        config = conf_path()
        if type_ == 'theme_engine':
            dir_ = 'themes/engines'
            file = "%s.engine" % name
        else:
            dir_ = '%ss' % type_
            file = "__init__.py"
        fileVals = {
            'name': name,
            'file': file,
            'dir': dir_,
            'config': config
        }
        fileChecker = (
            # DRUPY: This is not used
            # "%(config)s/%(dir)s/%(file)s" % fileVals,
            "%(config)s/%(dir)s/%(name)s/%(file)s" % fileVals,
            # DRUPY: This is not used
            #"%(dir)s/%(file)s" % fileVals,
            "%(dir)s/%(name)s/%(file)s" % fileVals)
        for file_ in fileChecker:
            if (php.file_exists(file_)):
                drupal_get_filename.files[type_][name] = file_
                break
    if (php.isset(drupal_get_filename.files[type_], name)):
        return drupal_get_filename.files[type_][name]
예제 #8
0
파일: file.py 프로젝트: vaibbhav/drupy
def copy(source, dest=0, replace=FILE_EXISTS_RENAME):
    """
   Copies a file to a new location.
   This is a powerful function that in many ways
   performs like an advanced version of copy().
   - Checks if source and dest are valid and readable/writable.
   - Performs a file copy if source is not equal to dest.
   - If file already exists in dest either the call will
     error out, replace the
     file or rename the file based on the replace parameter.
  
   @param source A string specifying the file location of the original file.
     This parameter will contain the resulting destination filename in case of
     success.
   @param dest A string containing the directory source should be copied to.
     If this value is omitted, Drupal's 'files' directory will be used.
   @param replace Replace behavior when the destination file already exists.
     - FILE_EXISTS_REPLACE - Replace the existing file
     - FILE_EXISTS_RENAME - Append _{incrementing number} until
       the filename is unique
     - FILE_EXISTS_ERROR - Do nothing and return False.
   @return True for success, False for failure.
  """
    php.Reference.check(source)
    dest = file_create_path(dest)
    directory = dest
    basename = file_check_path(directory)
    # Make sure we at least have a valid directory.
    if (basename == False):
        if hasattr(source, 'filepath'):
            source._ = source.filepath
        drupal_set_message(t('The selected file %file could not be ' + \
          'uploaded, because the destination %directory is not ' + \
          'properly configured.', \
          {'%file' : source._, '%directory' : dest}), 'error')
        watchdog('file system', 'The selected file %file could not ' + \
          'be uploaded, because the destination %directory could ' + \
          'not be found, or because its permissions do ' + \
          'not allow the file to be written.', \
          {'%file' : source._, '%directory' : dest}, WATCHDOG_ERROR)
        return False
    # Process a file upload object.
    if (php.is_object(source._)):
        file = source._
        source._ = file.filepath
        if (not basename):
            basename = file.filename
    source._ = php.realpath(source._)
    if (not php.file_exists(source._)):
        drupal_set_message(t('The selected file %file could not be copied, ' + \
          'because no file by that name exists. ' + \
          'Please check that you supplied the correct filename.', \
          {'%file' : source._}), 'error')
        return False
    # If the destination file is not specified then use the filename
    # of the source file.
    basename = (basename if basename else basename(source._))
    dest._ = directory + '/' + basename
    # Make sure source and destination filenames are not the same, makes no sense
    # to copy it if they are + In fact copying the file will most
    # likely result in
    # a 0 byte file + Which is bad. Real bad.
    if (source._ != php.realpath(dest._)):
        dest._ = file_destination(dest._, replace)
        if (not dest._):
            drupal_set_message(t('The selected file %file could not be copied, ' + \
              'because a file by that name already exists in the destination.', \
              {'%file' : source._}), 'error')
            return False
        if (not copy(source._, dest._)):
            drupal_set_message(t('The selected file %file could not be copied.', \
              {'%file' : source._}), 'error')
            return False
        # Give everyone read access so that FTP'd users or
        # non-webserver users can see/read these files,
        # and give group write permissions so group members
        # can alter files uploaded by the webserver.
        chmod(dest._, 0664)
    if (php.isset(file) and php.is_object(file)):
        file.filename = basename
        file.filepath = dest._
        source._ = file
    else:
        source._ = dest
    return True  # Everything went ok.
예제 #9
0
파일: bootstrap.py 프로젝트: sabren/drupy
def drupal_get_filename(type_, name, filename=None):
    """
   Returns and optionally sets the filename for a system item (plugin,
   theme, etc.). The filename, whether provided, cached, or retrieved
   from the database, is only returned if the file exists.
  
   This def plays a key role in allowing Drupal's resources (plugins
   and themes) to be located in different places depending on a site's
   configuration. For example, a plugin 'foo' may legally be be located
   in any of these three places:
  
   plugins/foo/__init__.py
   sites/all/plugins/foo/__init__.py
   sites/example.com/plugins/foo/__init__.py
  
   Calling drupal_get_filename('plugin', 'foo') will give you one of
   the above, depending on where the plugin is located.
  
   @param type
     The type of the item (i.e. theme, theme_engine, plugin).
   @param name
     The name of the item for which the filename is requested.
   @param filename
     The filename of the item if it is to be set explicitly rather
     than by consulting the database.
  
   @return
     The filename of the requested item.
  """
    php.static(drupal_get_filename, "files", {})
    file = lib_database.result(
        lib_database.query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", name, type_)
    )
    if not php.isset(drupal_get_filename.files, type_):
        drupal_get_filename.files[type_] = {}
    if filename is not None and php.file_exists(filename):
        drupal_get_filename.files[type_][name] = filename
    elif php.isset(drupal_get_filename.files[type_], name):
        # nothing
        pass
    # Verify that we have an active database connection, before querying
    # the database.  This is required because this def is called both
    # before we have a database connection (i.e. during installation) and
    # when a database connection fails.
    elif lib_database.is_active() and (file and php.file_exists(file)):
        drupal_get_filename.files[type_][name] = file
    else:
        # Fallback to searching the filesystem if the database connection is
        # not established or the requested file is not found.
        config = conf_path()
        if type_ == "theme_engine":
            dir_ = "themes/engines"
            file = "%s.engine" % name
        else:
            dir_ = "%ss" % type_
            file = "__init__.py"
        fileVals = {"name": name, "file": file, "dir": dir_, "config": config}
        fileChecker = (
            # DRUPY: This is not used
            # "%(config)s/%(dir)s/%(file)s" % fileVals,
            "%(config)s/%(dir)s/%(name)s/%(file)s" % fileVals,
            # DRUPY: This is not used
            # "%(dir)s/%(file)s" % fileVals,
            "%(dir)s/%(name)s/%(file)s" % fileVals,
        )
        for file_ in fileChecker:
            if php.file_exists(file_):
                drupal_get_filename.files[type_][name] = file_
                break
    if php.isset(drupal_get_filename.files[type_], name):
        return drupal_get_filename.files[type_][name]
예제 #10
0
def copy(source, dest = 0, replace = FILE_EXISTS_RENAME):
  """
   Copies a file to a new location.
   This is a powerful function that in many ways
   performs like an advanced version of copy().
   - Checks if source and dest are valid and readable/writable.
   - Performs a file copy if source is not equal to dest.
   - If file already exists in dest either the call will
     error out, replace the
     file or rename the file based on the replace parameter.
  
   @param source A string specifying the file location of the original file.
     This parameter will contain the resulting destination filename in case of
     success.
   @param dest A string containing the directory source should be copied to.
     If this value is omitted, Drupal's 'files' directory will be used.
   @param replace Replace behavior when the destination file already exists.
     - FILE_EXISTS_REPLACE - Replace the existing file
     - FILE_EXISTS_RENAME - Append _{incrementing number} until
       the filename is unique
     - FILE_EXISTS_ERROR - Do nothing and return False.
   @return True for success, False for failure.
  """
  php.Reference.check(source)
  dest = file_create_path(dest)
  directory = dest
  basename = file_check_path(directory)
  # Make sure we at least have a valid directory.
  if (basename == False):
    if hasattr(source, 'filepath'):
      source._ = source.filepath
    drupal_set_message(t('The selected file %file could not be ' + \
      'uploaded, because the destination %directory is not ' + \
      'properly configured.', \
      {'%file' : source._, '%directory' : dest}), 'error')
    watchdog('file system', 'The selected file %file could not ' + \
      'be uploaded, because the destination %directory could ' + \
      'not be found, or because its permissions do ' + \
      'not allow the file to be written.', \
      {'%file' : source._, '%directory' : dest}, WATCHDOG_ERROR)
    return False
  # Process a file upload object.
  if (php.is_object(source._)):
    file = source._
    source._ = file.filepath
    if (not basename):
      basename = file.filename
  source._ = php.realpath(source._)
  if (not php.file_exists(source._)):
    drupal_set_message(t('The selected file %file could not be copied, ' + \
      'because no file by that name exists. ' + \
      'Please check that you supplied the correct filename.', \
      {'%file' : source._}), 'error')
    return False
  # If the destination file is not specified then use the filename
  # of the source file.
  basename = (basename if basename else basename(source._))
  dest._ = directory + '/' + basename
  # Make sure source and destination filenames are not the same, makes no sense
  # to copy it if they are + In fact copying the file will most
  # likely result in
  # a 0 byte file + Which is bad. Real bad.
  if (source._ != php.realpath(dest._)):
    dest._ = file_destination(dest._, replace)
    if (not dest._):
      drupal_set_message(t('The selected file %file could not be copied, ' + \
        'because a file by that name already exists in the destination.', \
        {'%file' : source._}), 'error')
      return False
    if (not copy(source._, dest._)):
      drupal_set_message(t('The selected file %file could not be copied.', \
        {'%file' : source._}), 'error')
      return False
    # Give everyone read access so that FTP'd users or
    # non-webserver users can see/read these files,
    # and give group write permissions so group members
    # can alter files uploaded by the webserver.
    chmod(dest._, 0664)
  if (php.isset(file) and php.is_object(file)):
    file.filename = basename
    file.filepath = dest._
    source._ = file
  else:
    source._ = dest
  return True # Everything went ok.
예제 #11
0
def hook_init(template):
  file = php.dirname(template.filename) + '/template.py'
  if (php.file_exists(file)):
    lib_theme.processors['template'] = DrupyImport.import_file(file)