******************* The locators Module ******************* .. current-library:: system .. current-module:: locators The Locators module provides Dylan programs with a portable, flexible, and uniform facility for locating files and other objects on the internet. The locators Module ------------------- .. class:: :open: :abstract: :superclasses: :drm:`` This is the base class for all locators. This is the usual locator class for coercion (using :drm:`as`) or instantiation (using :drm:`make`) of new locators. Situations where this class is not appropriate are ones where there is not enough information provided to select the appropriate concrete class. For example, it is not appropriate to coerce a string representing a portion of a URL without a scheme, such as ``as(, "toothpaste.html")``, because this would likely result in the instantiation of a native locator instead of the desired URL locator class. .. class:: :open: :abstract: :superclasses: :class:`` A physical locator is a locator which refers to an object (such as a file or directory) in a physical file system. This locator class is useful for coercing an abstract locator into its corresponding physical counterpart. .. note:: Locator classes representing file system objects are documented in :doc:`file-system`. .. class:: :open: :abstract: :superclasses: :class:`` :slot locator-relative?: #t if the locator is relative, #f if it is absolute. :slot locator-path: the path to the directory. A directory locator is a locator that refers to a directory as distinct from a file. This is important in file systems which can view a directory as either a file or a directory. This locator class is useful for coercing a file locator into a form where it can be manipulated as a directory (e.g. for constructing a locator to a file in a directory). .. constant:: This is bound to the native directory locator type for the host platform. On Windows, this is typically :class:`` while on POSIX platforms, it is :class:``. .. class:: :open: :abstract: :superclasses: :class:`` A file locator is a locator which refers to a file as distinct from a directory. This is important in file systems which can view a directory as either a file or a directory. This locator class is useful for coercing a directory locator into a form where it can be manipulated as a file. .. constant:: This is bound to the native file locator type for the host platform. On Windows, this is typically :class:`` while on POSIX platforms, it is :class:``. .. class:: All errors raised by the locator system should be instances of this error. :superclasses: :drm:`` .. class:: :open: :abstract: The abstract superclass of locators for servers. :superclasses: :class:`` :seealso: - :class:`` - :class:`` .. generic-function:: list-locator :open: Return a sequence of locators that are children of the given locator. :signature: list-locator (locator) => (locators) :parameter locator: An instance of :class:``. :value locators: An instance of :drm:``. :description: Return a sequence of locators that are children of the given locator. Note that this should only be called on a locator for which :gf:`supports-list-locator?` returns true. :seealso: - :gf:`supports-list-locator?` .. method:: list-locator :specializer: Returns a sequence of locators for the files and directories within the directory specified by the directory locator. :parameter locator: An instance of :class:``. :value locators: An instance of :drm:``. :description: Returns a sequence of locators for the files and directories within the directory specified by the directory locator. Instances of :class:`` for files and symbolic links. :gf:`subdirectory-locator` will be called to create locators for any directories. :seealso: - :meth:`supports-list-locator?()` - :gf:`do-directory` .. generic-function:: locator-address :signature: locator-address (mailto) => (address) :parameter mailto: An instance of :class:``. :value address: An instance of :drm:``. :description: Returns the email address specified by the mailto locator. .. generic-function:: locator-as-string :open: :signature: locator-as-string (class locator) => (string) :parameter class: An instance of :drm:`subclass() <>`. :parameter locator: An instance of :class:``. :value string: An instance of :drm:``. .. generic-function:: locator-base :open: :signature: locator-base (locator) => (base) :parameter locator: An instance of :class:``. :value base: An instance of :drm:`false-or() <>`. :description: Returns the locator name without extension. For example, if a file locator's path was ``a/b/c.txt``, the locator-base would be ``c``. .. generic-function:: locator-directory :open: :signature: locator-directory (locator) => (directory) :parameter locator: An instance of :class:``. :value directory: An instance of :class:`false-or() <`. :description: Returns the enclosing directory of a locator, or :drm:`#f` if it is not in a directory. .. function:: locator-error :signature: locator-error (format-string #rest format-arguments) => (#rest results) :parameter format-string: An instance of :drm:``. :parameter #rest format-arguments: An instance of :drm:``. :value #rest results: An instance of :drm:``. .. generic-function:: locator-extension :open: :signature: locator-extension (locator) => (extension) :parameter locator: An instance of :class:``. :value extension: An instance of :drm:`false-or() <>`. :description: Returns the extension part of the locator name. For example, if a file locator's path was ``a/b/c.txt``, the locator-extension would be ``txt``. Returns :drm:`#f` if the locator does not have an extension. .. generic-function:: locator-file :signature: locator-file (url) => (file) :parameter url: An instance of :class:`` or :class:``. :value file: An instance of :class:``. :description: Returns the file URL of a file index or CGI URL. For example, the locator-file of ``http://example.com/index.html#tag`` or ``http://example.com/index.html?q=text`` would be ``http://example.com/index.html`` .. generic-function:: locator-host :open: Returns the host name. :signature: locator-host (locator) => (host) :parameter locator: An instance of :class:``. :value host: An instance of :drm:`false-or() <>`. :description: Returns the computer host name of a :class:`` or :class:``. .. generic-function:: locator-name Returns the name of this locator. :signature: locator-name (locator) => (name) :parameter locator: An instance of :class:``. :value name: An instance of :drm:`false-or() <>`. :description: This is typically the last component of the locator's path but can be different for some specializations. .. method:: locator-name :specializer: Returns the email address of this locator. :parameter locator: an instance of :class:`` :value name: An instance of :drm:`` .. method:: locator-name :specializer: Returns the email address of this locator. :parameter locator: an instance of :class:`` :value name: An instance of :drm:`` .. method:: locator-name :specializer: Returns the drive letter of this locator. :parameter locator: an instance of :class:`` :value name: An instance of :drm:`` :description: The drive is returned as a single letter, for example, 'A' .. method:: locator-name :specializer: Returns the server name of this locator. :parameter locator: an instance of :class:`` :value name: An instance of :drm:`` .. generic-function:: locator-path :open: Returns the directory path of a locator. :signature: locator-path (locator) => (path) :parameter locator: An instance of :class:``. :value path: An instance of :drm:``. :description: Returns the directory path as a sequence of strings, each being the name of a path element. :example: .. code-block:: dylan locator-path(as(, "/a/b/c.d")) => #["a", "b"] .. generic-function:: locator-relative? :open: :signature: locator-relative? (locator) => (relative?) :parameter locator: An instance of :class:``. :value relative?: An instance of :drm:``. .. generic-function:: locator-server :open: :signature: locator-server (locator) => (server) :parameter locator: An instance of :class:``. :value server: An instance of :class:`false-or() <>`. .. generic-function:: locator-volume :open: :signature: locator-volume (locator) => (volume) :parameter locator: An instance of :class:``. :value volume: An instance of :drm:`false-or() <>`. .. generic-function:: merge-locators :open: :signature: merge-locators (locator from-locator) => (merged-locator) :parameter locator: An instance of :class:``. :parameter from-locator: An instance of :class:``. :value merged-locator: An instance of :class:``. .. generic-function:: open-locator :open: :signature: open-locator (locator #key #all-keys) => (stream) :parameter locator: An instance of :class:``. :value stream: An instance of :class:``. .. generic-function:: relative-locator :open: Returns a locator relative to another locator which references the same file as this locator. :signature: relative-locator (locator from-locator) => (relative-locator) :parameter locator: An instance of :class:``. :parameter from-locator: An instance of :class:``. :value relative-locator: An instance of :class:``. :example: If self is '/a/b/c/d.txt' and root is '/a/b' .. code-block:: dylan let rel = relative-locator(self, root); Then rel is 'c/d.txt' .. generic-function:: simplify-locator :open: Simplifies a locator by removing redundant elements from its path. :signature: simplify-locator (locator) => (simplified-locator) :parameter locator: An instance of :class:``. :value simplified-locator: An instance of :class:``. .. generic-function:: resolve-locator :open: Resolves all links, parent references (``..``), self references (``.``), and removes unnecessary path separators. Similar to :func:`simplify-locator` except that it consults the file system to resolve links. A :class:`` is signaled if for any reason the path can't be resolved. Examples include non-existent directory components, access denied, I/O error, etc. In short, this function follows the semantics of POSIX ``realpath(3)``. :signature: resolve-locator (locator) => (resolved-locator) :parameter locator: An instance of :class:``. :value simplified-locator: An instance of :class:``. .. generic-function:: string-as-locator :open: Parse a string and create a locator. :signature: string-as-locator (class string) => (locator) :parameter class: An instance of ``subclass()``. :parameter string: An instance of :drm:``. :value locator: An instance of :class:``. :description: This method should be specialized for each new locator class. It should return an instance of ``class``, or raise a condition of type :class:``. .. generic-function:: subdirectory-locator :open: Returns a directory locator for a subdirectory of a given directory. :signature: subdirectory-locator (locator #rest sub-path) => (subdirectory) :parameter locator: An instance of :class:``. :parameter #rest sub-path: An instance of :drm:``. :value subdirectory: An instance of :class:``. :example: .. code-block:: dylan let build-dir = subdirectory-locator(working-directory(), "_build"); .. generic-function:: file-locator :open: Returns a file locator for a file in a subdirectory of the given directory. :signature: file-locator (directory, name, #rest more-names) => (file) :parameter directory: An instance of :class:``. :parameter name: An instance of :drm:``. :parameter #rest more-names: Instances of :drm:``. :value file: An instance of :class:``. :example: .. code-block:: dylan let temp = file-locator(temp-directory(), "my-subdir", "my-test.json"); ensure-directories-exist(temp); // Create "my-subdir" directory. .. generic-function:: supports-list-locator? :open: Returns whether or not a given locator supports the :gf:`list-locator` operation. :signature: supports-list-locator? (locator) => (listable?) :parameter locator: An instance of :class:``. :value listable?: An instance of :drm:``. :seealso: - :gf:`list-locator` .. method:: supports-list-locator? :specializer: Returns true if the directory locator is not relative. :parameter locator: An instance of :class:``. :value listable?: An instance of :drm:``. :seealso: - :meth:`list-locator()` .. generic-function:: supports-open-locator? :open: Returns whether or not a given locator supports the :gf:`open-locator` operation. :signature: supports-open-locator? (locator) => (openable?) :parameter locator: An instance of :class:``. :value openable?: An instance of :drm:``. .. class:: :abstract: :superclasses: :class:`` The abstract superclass of locators that access a resource via web protocols, such as ftp or http. .. class:: :abstract: :sealed: :superclasses: :class:``, :class:`` The abstract superclass of web locators that reference a physical object. Use ``as(, "...")`` to create an appropriate concrete subclass. :seealso: :class:`` :class:`` :class:`` :class:`` .. class:: :superclasses: :class:``, :class:`` Represents directories that are accessible via web protocols. .. class:: :superclasses: :class:``, :class:`` Represents files that are accessible via web protocols. .. class:: :superclasses: :class:`` Represents a URL that has a fragment part, for example ``http://www.example.com/path/file.txt#fragment``. .. class:: :superclasses: :class:`` Represents a URL that has a query part, for example ``http://www.example.com/path/file.txt?query=text``. .. function:: locator-cgi-string Return the query part of a ````. :signature: locator-cgi-string(locator) => (string) :parameter locator: an instance of :class:`` :value string: an instance of :drm:`` .. function:: locator-index Return the fragment part of a :class:```` :signature: locator-index(locator) => (string) :parameter locator: an instance of :class:`` :value string: an instance of :drm:`` .. class:: :superclasses: :class:`` Represents a locator which is an email address. .. class:: :abstract: Represents a locator which is a machine accessible via web protocols. :superclasses: :class:``, :class:`` :slot locator-host: The computer host :slot locator-username: The user identifier :slot locator-password: The user password :operations: :gf:`locator-port`, :gf:`locator-default-port` The locator includes information on the protocol, host-name, port, user and password of the machine. :seealso: :class:`` :class:`` :class:`` :class:`` .. class:: :sealed: A server for the http protocol. :superclasses: :class:`` .. class:: :sealed: A server for the https protocol. :superclasses: :class:`` .. class:: :sealed: A server for the ftp protocol. :superclasses: :class:`` .. class:: :sealed: A locator using the file protocol. :superclasses: :class:`` .. generic-function:: locator-default-port Return the default port associated with the locator's protocol. :signature: locator-default-port(locator) => (port) :param locator: An instance of :class:`` :value port: An instance of :drm:`false-or() <>` :example: .. code-block:: dylan let locator = as(, "http://www.example.com"); let default-port = locator-default-port(locator); // Result: default-port = 80