You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 KiB
95 KiB
<html>
<head>
</head>
</html>
In [1]:
!python3 -m pip install pyyaml minidb requests keyring appdirs lxml cssselect urlwatch
Requirement already satisfied: pyyaml in /opt/conda/lib/python3.9/site-packages (6.0) Requirement already satisfied: minidb in /opt/conda/lib/python3.9/site-packages (2.0.6) Requirement already satisfied: requests in /opt/conda/lib/python3.9/site-packages (2.27.1) Requirement already satisfied: keyring in /opt/conda/lib/python3.9/site-packages (23.5.0) Requirement already satisfied: appdirs in /opt/conda/lib/python3.9/site-packages (1.4.4) Requirement already satisfied: lxml in /opt/conda/lib/python3.9/site-packages (4.8.0) Requirement already satisfied: cssselect in /opt/conda/lib/python3.9/site-packages (1.1.0) Requirement already satisfied: urlwatch in /opt/conda/lib/python3.9/site-packages (2.25) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.9/site-packages (from requests) (1.26.9) Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/lib/python3.9/site-packages (from requests) (2.0.12) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.9/site-packages (from requests) (2021.10.8) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.9/site-packages (from requests) (3.3) Requirement already satisfied: jeepney>=0.4.2 in /opt/conda/lib/python3.9/site-packages (from keyring) (0.8.0) Requirement already satisfied: SecretStorage>=3.2 in /opt/conda/lib/python3.9/site-packages (from keyring) (3.3.2) Requirement already satisfied: importlib-metadata>=3.6 in /opt/conda/lib/python3.9/site-packages (from keyring) (4.11.3) Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.9/site-packages (from importlib-metadata>=3.6->keyring) (3.8.0) Requirement already satisfied: cryptography>=2.0 in /opt/conda/lib/python3.9/site-packages (from SecretStorage>=3.2->keyring) (37.0.1) Requirement already satisfied: cffi>=1.12 in /opt/conda/lib/python3.9/site-packages (from cryptography>=2.0->SecretStorage>=3.2->keyring) (1.15.0) Requirement already satisfied: pycparser in /opt/conda/lib/python3.9/site-packages (from cffi>=1.12->cryptography>=2.0->SecretStorage>=3.2->keyring) (2.21)
Settings¶
In [10]:
# import sys
# !{sys.executable} -m pip install urlwatch
import os
# stream = os.popen('urlwatch --config "/home/jovyan/code/Unfamiliar tools/urlmonitor/config.yml"')
stream = os.popen('urlwatch --edit')
# stream = os.popen('echo $PWD')
output = stream.read()
print(output)
Please set $VISUAL or $EDITOR.
In [ ]:
<div class="nav_wrap">
<div id="resizer">
</div>
</div>
<div id="main" tabindex="-1">
<div id="header">
<div id="menu">
<a href="/gems" target="_top">Libraries</a> » <span class="title">maid
<span class="small">(0.7.0)</span></span> » [Index
(T)](/gems/maid/0.7.0/index) » <span class="title">Maid</span> » <span
class="title">Tools</span>
</div>
<div id="search">
<a href="/list/gems/maid/0.7.0/class" id="class_list_link"
class="full_list_link"><img
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiPgo8cmVjdCB4PSIwIiB5PSI0IiB3aWR0aD0iMjQiIGhlaWdodD0iNCIgcng9IjEiIHJ5PSIxIj48L3JlY3Q+CjxyZWN0IHg9IjAiIHk9IjEyIiB3aWR0aD0iMjQiIGhlaWdodD0iNCIgcng9IjEiIHJ5PSIxIj48L3JlY3Q+CjxyZWN0IHg9IjAiIHk9IjIwIiB3aWR0aD0iMjQiIGhlaWdodD0iNCIgcng9IjEiIHJ5PSIxIj48L3JlY3Q+Cjwvc3ZnPg==" /></a>
</div>
<div class="clear">
</div>
</div>
<div id="content">
# Module: Maid::Tools
<div class="box_info">
Includes:
Deprecated
<!-- -->
Defined in:
lib/maid/tools.rb
</div>
## Overview
<div class="docstring">
<div class="discussion">
These "tools" are methods available in the Maid DSL.
In general, methods are expected to:
- Automatically expand paths (that is, `'~/Downloads/foo.zip'` becomes
`'/home/username/Downloads/foo.zip'`)
- Respect the `noop` (`dry-run`) option if it is set
Some methods are not available on all platforms. An `ArgumentError` is
raised when a command is not available. See tags such as: \[Mac OS X\]
</div>
</div>
<div class="tags">
</div>
## Instance Method Summary <span class="small"><a href="#" class="summary_toggle">collapse</a></span>
- <span
class="summary_signature">[accessed_at](/gems/maid/0.7.0/Maid/Tools#accessed_at-instance_method "#accessed_at (instance method)")</span>
- <span
class="summary_signature">[checksum_of](/gems/maid/0.7.0/Maid/Tools#checksum_of-instance_method "#checksum_of (instance method)")</span>
- <span
class="summary_signature">[content_types](/gems/maid/0.7.0/Maid/Tools#content_types-instance_method "#content_types (instance method)")</span>
- <span
class="summary_signature">[copy](/gems/maid/0.7.0/Maid/Tools#copy-instance_method "#copy (instance method)")</span>
- <span
class="summary_signature">[created_at](/gems/maid/0.7.0/Maid/Tools#created_at-instance_method "#created_at (instance method)")</span>
- <span
class="summary_signature">[dimensions_px](/gems/maid/0.7.0/Maid/Tools#dimensions_px-instance_method "#dimensions_px (instance method)")</span>
- <span
class="summary_signature">[dir](/gems/maid/0.7.0/Maid/Tools#dir-instance_method "#dir (instance method)")</span>
- <span
class="summary_signature">[dir_safe](/gems/maid/0.7.0/Maid/Tools#dir_safe-instance_method "#dir_safe (instance method)")</span>
- <span
class="summary_signature">[disk_usage](/gems/maid/0.7.0/Maid/Tools#disk_usage-instance_method "#disk_usage (instance method)")</span>
- <span
class="summary_signature">[downloaded_from](/gems/maid/0.7.0/Maid/Tools#downloaded_from-instance_method "#downloaded_from (instance method)")</span>
- <span
class="summary_signature">[downloading?](/gems/maid/0.7.0/Maid/Tools#downloading%3F-instance_method "#downloading? (instance method)")</span>
- <span
class="summary_signature">[dupes_in](/gems/maid/0.7.0/Maid/Tools#dupes_in-instance_method "#dupes_in (instance method)")</span>
- <span
class="summary_signature">[duration_s](/gems/maid/0.7.0/Maid/Tools#duration_s-instance_method "#duration_s (instance method)")</span>
- <span
class="summary_signature">[escape_glob](/gems/maid/0.7.0/Maid/Tools#escape_glob-instance_method "#escape_glob (instance method)")</span>
- <span
class="summary_signature">[files](/gems/maid/0.7.0/Maid/Tools#files-instance_method "#files (instance method)")</span>
- <span
class="summary_signature">[find](/gems/maid/0.7.0/Maid/Tools#find-instance_method "#find (instance method)")</span>
- <span
class="summary_signature">[git_piston](/gems/maid/0.7.0/Maid/Tools#git_piston-instance_method "#git_piston (instance method)")</span>
- <span
class="summary_signature">[ignore_child_dirs](/gems/maid/0.7.0/Maid/Tools#ignore_child_dirs-instance_method "#ignore_child_dirs (instance method)")</span>
- <span
class="summary_signature">[last_accessed](/gems/maid/0.7.0/Maid/Tools#last_accessed-instance_method "#last_accessed (instance method)")</span>
- <span
class="summary_signature">[locate](/gems/maid/0.7.0/Maid/Tools#locate-instance_method "#locate (instance method)")</span>
- <span
class="summary_signature">[location_city](/gems/maid/0.7.0/Maid/Tools#location_city-instance_method "#location_city (instance method)")</span>
- <span
class="summary_signature">[media_type](/gems/maid/0.7.0/Maid/Tools#media_type-instance_method "#media_type (instance method)")</span>
- <span
class="summary_signature">[mime_type](/gems/maid/0.7.0/Maid/Tools#mime_type-instance_method "#mime_type (instance method)")</span>
- <span
class="summary_signature">[mkdir](/gems/maid/0.7.0/Maid/Tools#mkdir-instance_method "#mkdir (instance method)")</span>
- <span
class="summary_signature">[modified_at](/gems/maid/0.7.0/Maid/Tools#modified_at-instance_method "#modified_at (instance method)")</span>
- <span
class="summary_signature">[move](/gems/maid/0.7.0/Maid/Tools#move-instance_method "#move (instance method)")</span>
- <span
class="summary_signature">[newest_dupes_in](/gems/maid/0.7.0/Maid/Tools#newest_dupes_in-instance_method "#newest_dupes_in (instance method)")</span>
- <span
class="summary_signature">[remove](/gems/maid/0.7.0/Maid/Tools#remove-instance_method "#remove (instance method)")</span>
- <span
class="summary_signature">[rename](/gems/maid/0.7.0/Maid/Tools#rename-instance_method "#rename (instance method)")</span>
- <span
class="summary_signature">[size_of](/gems/maid/0.7.0/Maid/Tools#size_of-instance_method "#size_of (instance method)")</span>
- <span
class="summary_signature">[spotlight_content_types](/gems/maid/0.7.0/Maid/Tools#spotlight_content_types-instance_method "#spotlight_content_types (instance method)")</span>
- <span
class="summary_signature">[sync](/gems/maid/0.7.0/Maid/Tools#sync-instance_method "#sync (instance method)")</span>
- <span
class="summary_signature">[trash](/gems/maid/0.7.0/Maid/Tools#trash-instance_method "#trash (instance method)")</span>
- <span
class="summary_signature">[tree_empty?](/gems/maid/0.7.0/Maid/Tools#tree_empty%3F-instance_method "#tree_empty? (instance method)")</span>
- <span
class="summary_signature">[verbose_dupes_in](/gems/maid/0.7.0/Maid/Tools#verbose_dupes_in-instance_method "#verbose_dupes_in (instance method)")</span>
- <span
class="summary_signature">[where_content_type](/gems/maid/0.7.0/Maid/Tools#where_content_type-instance_method "#where_content_type (instance method)")</span>
- <span
class="summary_signature">[zipfile_contents](/gems/maid/0.7.0/Maid/Tools#zipfile_contents-instance_method "#zipfile_contents (instance method)")</span>
<!-- -->
- <span class="summary_signature"> [\#**accessed_at**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#accessed_at-instance_method "#accessed_at (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the time that a file was last accessed.
</div>
- <span class="summary_signature"> [\#**checksum_of**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#checksum_of-instance_method "#checksum_of (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get a checksum for a file.
</div>
- <span class="summary_signature"> [\#**content_types**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#content_types-instance_method "#content_types (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the content types of a path.
</div>
- <span class="summary_signature"> [\#**copy**(sources, destination) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#copy-instance_method "#copy (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Copy from `sources` to `destination`.
</div>
- <span class="summary_signature"> [\#**created_at**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#created_at-instance_method "#created_at (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the creation time of a file.
</div>
- <span class="summary_signature"> [\#**dimensions_px**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#dimensions_px-instance_method "#dimensions_px (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Determine the dimensions of GIF, PNG, JPEG, or TIFF images.
</div>
- <span class="summary_signature"> [\#**dir**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#dir-instance_method "#dir (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Give all files matching the given glob.
</div>
- <span class="summary_signature"> [\#**dir_safe**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#dir_safe-instance_method "#dir_safe (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Same as `dir`, but excludes files that are (possibly) being
downloaded.
</div>
- <span class="summary_signature"> [\#**disk_usage**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#disk_usage-instance_method "#disk_usage (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Calculate disk usage of a given path in kilobytes.
</div>
- <span class="summary_signature"> [\#**downloaded_from**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#downloaded_from-instance_method "#downloaded_from (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
\[Mac OS X\] Use Spotlight metadata to determine the site from which
a file was downloaded.
</div>
- <span class="summary_signature"> [\#**downloading?**(path) ⇒
Boolean](/gems/maid/0.7.0/Maid/Tools#downloading%3F-instance_method "#downloading? (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Detect whether the path is currently being downloaded in Chrome,
Firefox or Safari.
</div>
- <span class="summary_signature"> [\#**dupes_in**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#dupes_in-instance_method "#dupes_in (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Find all duplicate files in the given globs.
</div>
- <span class="summary_signature"> [\#**duration_s**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#duration_s-instance_method "#duration_s (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
\[Mac OS X\] Use Spotlight metadata to determine audio length.
</div>
- <span class="summary_signature"> [\#**escape_glob**(glob) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#escape_glob-instance_method "#escape_glob (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Escape characters that have special meaning as a part of path global
patterns.
</div>
- <span class="summary_signature"> [\#**files**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#files-instance_method "#files (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Give only files matching the given glob.
</div>
- <span class="summary_signature"> [\#**find**(path, &block) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#find-instance_method "#find (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Find matching files, akin to the Unix utility `find`.
</div>
- <span class="summary_signature"> [\#**git_piston**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#git_piston-instance_method "#git_piston (instance method)")
</span> <span class="deprecated note title">deprecated</span> <span
class="summary_desc">**Deprecated.** </span>
<div class="inline">
</div>
- <span class="summary_signature"> [\#**ignore_child_dirs**(arr) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#ignore_child_dirs-instance_method "#ignore_child_dirs (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Given an array of directories, return a new array without any child
directories whose parent is already present in that array.
</div>
- <span class="summary_signature"> [\#**last_accessed**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#last_accessed-instance_method "#last_accessed (instance method)")
</span> <span class="deprecated note title">deprecated</span> <span
class="summary_desc">**Deprecated.** </span>
<div class="inline">
</div>
- <span class="summary_signature"> [\#**locate**(name) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#locate-instance_method "#locate (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
\[Mac OS X\] Use Spotlight to locate all files matching the given
filename.
</div>
- <span class="summary_signature"> [\#**location_city**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#location_city-instance_method "#location_city (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Determine the city of the given JPEG image.
</div>
- <span class="summary_signature"> [\#**media_type**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#media_type-instance_method "#media_type (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the Internet media type of the file.
</div>
- <span class="summary_signature"> [\#**mime_type**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#mime_type-instance_method "#mime_type (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the MIME type of the file.
</div>
- <span class="summary_signature"> [\#**mkdir**(path, options = {}) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#mkdir-instance_method "#mkdir (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Create a directory and all of its parent directories.
</div>
- <span class="summary_signature"> [\#**modified_at**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#modified_at-instance_method "#modified_at (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the modification time of a file.
</div>
- <span class="summary_signature"> [\#**move**(sources, destination) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#move-instance_method "#move (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Move `sources` to a `destination` directory.
</div>
- <span class="summary_signature"> [\#**newest_dupes_in**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#newest_dupes_in-instance_method "#newest_dupes_in (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Convenience method that is like `dupes_in` but excludes the oldest
dupe.
</div>
- <span class="summary_signature"> [\#**remove**(paths, options = {})
⇒
Object](/gems/maid/0.7.0/Maid/Tools#remove-instance_method "#remove (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Delete the files at the given path recursively.
</div>
- <span class="summary_signature"> [\#**rename**(source, destination)
⇒
Object](/gems/maid/0.7.0/Maid/Tools#rename-instance_method "#rename (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Rename a single file.
</div>
- <span class="summary_signature"> [\#**size_of**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#size_of-instance_method "#size_of (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Get the size of a file.
</div>
- <span class="summary_signature">
[\#**spotlight_content_types**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#spotlight_content_types-instance_method "#spotlight_content_types (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
\[Mac OS X\] Use Spotlight metadata to determine which content types
a file has.
</div>
- <span class="summary_signature"> [\#**sync**(from, to, options = {})
⇒
Object](/gems/maid/0.7.0/Maid/Tools#sync-instance_method "#sync (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Simple sync two files/folders using `rsync`.
</div>
- <span class="summary_signature"> [\#**trash**(paths, options = {}) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#trash-instance_method "#trash (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Move the given paths to the user's trash.
</div>
- <span class="summary_signature"> [\#**tree_empty?**(root) ⇒
Boolean](/gems/maid/0.7.0/Maid/Tools#tree_empty%3F-instance_method "#tree_empty? (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Test whether a directory is either empty, or contains only empty
directories/subdirectories.
</div>
- <span class="summary_signature"> [\#**verbose_dupes_in**(globs) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#verbose_dupes_in-instance_method "#verbose_dupes_in (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Convenience method for `dupes_in` that excludes the dupe with the
shortest name.
</div>
- <span class="summary_signature"> [\#**where_content_type**(paths,
filter_types) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#where_content_type-instance_method "#where_content_type (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
Filter an array by content types.
</div>
- <span class="summary_signature"> [\#**zipfile_contents**(path) ⇒
Object](/gems/maid/0.7.0/Maid/Tools#zipfile_contents-instance_method "#zipfile_contents (instance method)")
</span> <span class="summary_desc"></span>
<div class="inline">
List the contents of a zip file.
</div>
<div id="instance_method_details" class="method_details_list">
## Instance Method Details
<div class="method_details first">
### <a href="/gems/maid/0.7.0/Maid%2FTools:accessed_at"
class="permalink">permalink</a> \#**accessed_at**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the time that a file was last accessed.
In Unix speak, `atime`.
## Examples
``` code
accessed_at('foo.zip') # => Sat Apr 09 10:50:01 -0400 2011
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>519
520
521</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 519
def accessed_at(path)
File.atime(expand(path))
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:checksum_of"
class="permalink">permalink</a> \#**checksum_of**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get a checksum for a file.
## Examples
``` code
checksum_of('foo.zip') # => "67258d750ca654d5d3c7b06bd2a1c792ced2003e"
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>557
558
559</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 557
def checksum_of(path)
Digest::SHA1.hexdigest(File.read(path))
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:content_types"
class="permalink">permalink</a> \#**content_types**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the content types of a path.
Content types can be MIME types, Internet media types or Spotlight
content types (OS X only).
## Examples
``` code
content_types('foo.zip') # => ["public.zip-archive", "com.pkware.zip-archive", "public.archive", "application/zip", "application"]
content_types('bar.jpg') # => ["public.jpeg", "public.image", "image/jpeg", "image"]
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>647
648
649</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 647
def content_types(path)
[spotlight_content_types(path), mime_type(path), media_type(path)].flatten
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:copy"
class="permalink">permalink</a> \#**copy**(sources, destination) ⇒ Object
<div class="docstring">
<div class="discussion">
Copy from `sources` to `destination`
The path is not copied if a file already exists at the destination with
the same name. A warning is logged instead. Note: Similar functionality
is provided by the sync tool, but this requires installation of the
`rsync` binary
## Examples
Single path:
``` code
copy('~/Downloads/foo.zip', '~/Archive/Software/Mac OS X/')
```
Multiple paths:
``` code
copy(['~/Downloads/foo.zip', '~/Downloads/bar.zip'], '~/Archive/Software/Mac OS X/')
copy(dir('~/Downloads/*.zip'), '~/Archive/Software/Mac OS X/')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>163
164
165
166
167
168
169
170
171
172
173
174
175
176</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 163
def copy(sources, destination)
destination = expand(destination)
expand_all(sources).each do |source|
target = File.join(destination, File.basename(source))
unless File.exist?(target)
log("cp #{ sh_escape(source) } #{ sh_escape(destination) }")
FileUtils.cp(source, destination, @file_options)
else
warn("skipping copy because #{ sh_escape(source) } because #{ sh_escape(target) } already exists")
end
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:created_at"
class="permalink">permalink</a> \#**created_at**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the creation time of a file.
In Unix speak, `ctime`.
## Examples
``` code
created_at('foo.zip') # => Sat Apr 09 10:50:01 -0400 2011
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>508
509
510</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 508
def created_at(path)
File.ctime(expand(path))
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:dimensions_px"
class="permalink">permalink</a> \#**dimensions_px**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Determine the dimensions of GIF, PNG, JPEG, or TIFF images.
Value returned is \[width, height\].
## Examples
``` code
dimensions_px('image.jpg') # => [1024, 768]
width, height = dimensions_px('image.jpg')
dimensions_px('image.jpg').join('x') # => "1024x768"
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>442
443
444</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 442
def dimensions_px(path)
Dimensions.dimensions(path)
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:dir"
class="permalink">permalink</a> \#**dir**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Give all files matching the given glob.
Note that the globs are *not* regexps (they're closer to shell globs).
However, some regexp-like notation can be used, e.g. `?`, `[a-z]`,
`{tgz,zip}`. For more details, see Ruby's documentation on `Dir.glob`.
The matches are sorted lexically to aid in readability when using
`--dry-run`.
## Examples
Single glob:
``` code
dir('~/Downloads/*.zip')
```
Specifying multiple extensions succinctly:
``` code
dir('~/Downloads/*.{exe,deb,dmg,pkg,rpm}')
```
Multiple glob (all are equivalent):
``` code
dir(['~/Downloads/*.zip', '~/Dropbox/*.zip'])
dir(%w(~/Downloads/*.zip ~/Dropbox/*.zip))
dir('~/{Downloads,Dropbox}/*.zip')
```
Recursing into subdirectories (see also: `find`):
``` code
dir('~/Music/**/*.m4a')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>240
241
242
243
244
245</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 240
def dir(globs)
expand_all(globs).
map { |glob| Dir.glob(glob) }.
flatten.
sort
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:dir_safe"
class="permalink">permalink</a> \#**dir_safe**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Same as `dir`, but excludes files that are (possibly) being downloaded.
## Example
Move Debian/Ubuntu packages that are finished downloading into a
software directory.
``` code
move dir_safe('~/Downloads/*.deb'), '~/Archive/Software'
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>256
257
258
259</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 256
def dir_safe(globs)
dir(globs).
reject { |path| downloading?(path) }
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:disk_usage"
class="permalink">permalink</a> \#**disk_usage**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Calculate disk usage of a given path in kilobytes.
See also: `Maid::NumericExtensions::SizeToKb`.
## Examples
``` code
disk_usage('foo.zip') # => 136
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>489
490
491
492
493
494
495
496
497
498
499</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 489
def disk_usage(path)
raw = cmd("du -s #{ sh_escape(path) }")
# FIXME: This reports in kilobytes, but should probably report in bytes.
usage_kb = raw.split(/\s+/).first.to_i
if usage_kb.zero?
raise "Stopping pessimistically because of unexpected value from du (#{ raw.inspect })"
else
usage_kb
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:downloaded_from"
class="permalink">permalink</a> \#**downloaded_from**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
\[Mac OS X\] Use Spotlight metadata to determine the site from which a
file was downloaded.
## Examples
``` code
downloaded_from('foo.zip') # => ['http://www.site.com/foo.zip', 'http://www.site.com/']
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>355
356
357</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 355
def downloaded_from(path)
mdls_to_array(path, 'kMDItemWhereFroms')
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:downloading%3F"
class="permalink">permalink</a> \#**downloading?**(path) ⇒ Boolean
<div class="docstring">
<div class="discussion">
Detect whether the path is currently being downloaded in Chrome, Firefox
or Safari.
See also: `dir_safe`
</div>
</div>
<div class="tags">
Returns:
- <span class="type">(Boolean)</span>
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>362
363
364</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 362
def downloading?(path)
!!(chrome_downloading?(path) || firefox_downloading?(path) || safari_downloading?(path))
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:dupes_in"
class="permalink">permalink</a> \#**dupes_in**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Find all duplicate files in the given globs.
More often than not, you'll want to use `newest_dupes_in` or
`verbose_dupes_in` instead of using this method directly.
Globs are expanded as in `dir`, then all non-files are filtered out. The
remaining files are compared by size, and non-dupes are filtered out.
The remaining candidates are then compared by checksum. Dupes are
returned as an array of arrays.
## Examples
``` code
dupes_in('~/{Downloads,Desktop}/*') # => [
['~/Downloads/foo.zip', '~/Downloads/foo (1).zip'],
['~/Desktop/bar.txt', '~/Desktop/bar copy.txt']
]
```
Keep the newest dupe:
``` code
dupes_in('~/Desktop/*', '~/Downloads/*').each do |dupes|
trash dupes.sort_by { |p| File.mtime(p) }[0..-2]
end
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>389
390
391
392
393
394
395
396
397
398
399
400
401</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 389
def dupes_in(globs)
dupes = []
files(globs). # Start by filtering out non-files
group_by { |f| size_of(f) }. # ... then grouping by size, since that's fast
reject { |s, p| p.length < 2 }. # ... and filter out any non-dupes
map do |size, candidates|
dupes += candidates.
group_by { |p| checksum_of(p) }. # Now group our candidates by a slower checksum calculation
reject { |c, p| p.length < 2 }. # ... and filter out any non-dupes
values
end
dupes
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:duration_s"
class="permalink">permalink</a> \#**duration_s**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
\[Mac OS X\] Use Spotlight metadata to determine audio length.
## Examples
``` code
duration_s('foo.mp3') # => 235.705
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>466
467
468</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 466
def duration_s(path)
cmd("mdls -raw -name kMDItemDurationSeconds #{ sh_escape(path) }").to_f
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:escape_glob"
class="permalink">permalink</a> \#**escape_glob**(glob) ⇒ Object
<div class="docstring">
<div class="discussion">
Escape characters that have special meaning as a part of path global
patterns.
Useful when using `dir` with file names that may contain `{ } [ ]`
characters.
## Example
``` code
escape_glob('test [tmp]') # => 'test \\[tmp\\]'
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>277
278
279</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 277
def escape_glob(glob)
glob.gsub(/[\{\}\[\]]/) { |s| '\\' + s }
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:files"
class="permalink">permalink</a> \#**files**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Give only files matching the given glob.
This is the same as `dir` but only includes actual files (no directories
or symlinks).
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>265
266
267
268</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 265
def files(globs)
dir(globs).
select { |f| File.file?(f) }
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:find"
class="permalink">permalink</a> \#**find**(path, &block) ⇒ Object
<div class="docstring">
<div class="discussion">
Find matching files, akin to the Unix utility `find`.
If no block is given, it will return an array. Otherwise, it acts like
`Find.find`.
## Examples
Without a block:
``` code
find('~/Downloads/') # => [...]
```
Recursing and filtering using a regular expression:
``` code
find('~/Downloads/').grep(/\.pdf$/)
```
(**Note:** It's just Ruby, so any methods in `Array` and `Enumerable`
can be used.)
Recursing with a block:
``` code
find('~/Downloads/') do |path|
# ...
end
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>329
330
331
332
333
334
335
336
337</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 329
def find(path, &block)
expanded_path = expand(path)
if block.nil?
Find.find(expanded_path).to_a
else
Find.find(expanded_path, &block)
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:git_piston"
class="permalink">permalink</a> \#**git_piston**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
<div class="note deprecated">
**Deprecated.**
<div class="inline">
</div>
</div>
Pull and push the `git` repository at the given path.
Since this is deprecated, you might also be interested in
[SparkleShare](http://sparkleshare.org/), a great `git`-based file
syncronization project.
## Examples
``` code
git_piston('~/code/projectname')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>571
572
573
574
575</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 571
def git_piston(path)
full_path = expand(path)
stdout = cmd("cd #{ sh_escape(full_path) } && git pull && git push 2>&1")
log("Fired git piston on #{ sh_escape(full_path) }. STDOUT:\n\n#{ stdout }")
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:ignore_child_dirs"
class="permalink">permalink</a> \#**ignore_child_dirs**(arr) ⇒ Object
<div class="docstring">
<div class="discussion">
Given an array of directories, return a new array without any child
directories whose parent is already present in that array.
## Example
``` code
ignore_child_dirs(["foo", "foo/a", "foo/b", "bar"]) # => ["foo", "bar"]
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>749
750
751
752
753
754
755</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 749
def ignore_child_dirs(arr)
arr.sort { |x, y|
y.count('/') - x.count('/')
}.select { |d|
!arr.include?(File.dirname(d))
}
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:last_accessed"
class="permalink">permalink</a> \#**last_accessed**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
<div class="note deprecated">
**Deprecated.**
<div class="inline">
</div>
</div>
Alias of `accessed_at`.
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>526
527
528
529</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 526
def last_accessed(path)
# Not a normal `alias` so the deprecation notice shows in the docs.
accessed_at(path)
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:locate"
class="permalink">permalink</a> \#**locate**(name) ⇒ Object
<div class="docstring">
<div class="discussion">
\[Mac OS X\] Use Spotlight to locate all files matching the given
filename.
\[Ubuntu\] Use `locate` to locate all files matching the given filename.
## Examples
``` code
locate('foo.zip') # => ['/a/foo.zip', '/b/foo.zip']
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>346
347
348</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 346
def locate(name)
cmd("#{Maid::Platform::Commands.locate} #{ sh_escape(name) }").split("\n")
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:location_city"
class="permalink">permalink</a> \#**location_city**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Determine the city of the given JPEG image.
## Examples
``` code
loation_city('old_capitol.jpg') # => "Iowa City, IA, US"
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>451
452
453
454
455
456
457
458
459</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 451
def location_city(path)
case mime_type(path)
when 'image/jpeg'
gps = EXIFR::JPEG.new(path).gps
coordinates_string = [gps.latitude, gps.longitude]
location = Geocoder.search(coordinates_string).first
[location.city, location.province, location.country_code].join(', ')
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:media_type"
class="permalink">permalink</a> \#**media_type**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the Internet media type of the file.
In other words, the first part of `mime_type`.
## Examples
``` code
media_type('bar.jpg') # => "image"
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>671
672
673
674
675
676
677</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 671
def media_type(path)
type = MIME::Types.type_for(path)[0]
if type
type.media_type
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:mime_type"
class="permalink">permalink</a> \#**mime_type**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the MIME type of the file.
## Examples
``` code
mime_type('bar.jpg') # => "image/jpeg"
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>656
657
658
659
660
661
662</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 656
def mime_type(path)
type = MIME::Types.type_for(path)[0]
if type
[type.media_type, type.sub_type].join('/')
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:mkdir"
class="permalink">permalink</a> \#**mkdir**(path, options = {}) ⇒ Object
<div class="docstring">
<div class="discussion">
Create a directory and all of its parent directories.
The path of the created directory is returned, which allows for chaining
(see examples).
## Options
`:mode`
The symbolic and absolute mode can both be used, for example: `0700`,
`'u=wr,go=rr'`
## Examples
Creating a directory with a specific mode:
``` code
mkdir('~/Music/Pink Floyd/', :mode => 0644)
```
Ensuring a directory exists when moving:
``` code
move('~/Downloads/Pink Floyd*.mp3', mkdir('~/Music/Pink Floyd/'))
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>300
301
302
303
304
305</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 300
def mkdir(path, options = {})
path = expand(path)
log("mkdir -p #{ sh_escape(path) }")
FileUtils.mkdir_p(path, @file_options.merge(options))
path
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:modified_at"
class="permalink">permalink</a> \#**modified_at**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the modification time of a file.
In Unix speak, `mtime`.
## Examples
``` code
modified_at('foo.zip') # => Sat Apr 09 10:50:01 -0400 2011
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>539
540
541</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 539
def modified_at(path)
File.mtime(expand(path))
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:move"
class="permalink">permalink</a> \#**move**(sources, destination) ⇒ Object
<div class="docstring">
<div class="discussion">
Move `sources` to a `destination` directory.
Movement is only allowed to directories that already exist. If your
intention is to rename, see the `rename` method.
## Examples
Single path:
``` code
move('~/Downloads/foo.zip', '~/Archive/Software/Mac OS X/')
```
Multiple paths:
``` code
move(['~/Downloads/foo.zip', '~/Downloads/bar.zip'], '~/Archive/Software/Mac OS X/')
move(dir('~/Downloads/*.zip'), '~/Archive/Software/Mac OS X/')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>39
40
41
42
43
44
45
46
47
48
49
50
51</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 39
def move(sources, destination)
destination = expand(destination)
if File.directory?(destination)
expand_all(sources).each do |source|
log("move #{ sh_escape(source) } #{ sh_escape(destination) }")
FileUtils.mv(source, destination, @file_options)
end
else
# Unix `mv` warns about the target not being a directory with multiple sources. Maid checks the same.
warn("skipping move because #{ sh_escape(destination) } is not a directory (use 'mkdir' to create first, or use 'rename')")
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:newest_dupes_in"
class="permalink">permalink</a> \#**newest_dupes_in**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Convenience method that is like `dupes_in` but excludes the oldest dupe.
## Example
Keep the oldest dupe (trash the others):
``` code
trash newest_dupes_in('~/Downloads/*')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>411
412
413
414
415</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 411
def newest_dupes_in(globs)
dupes_in(globs).
map { |dupes| dupes.sort_by { |p| File.mtime(p) }[1..-1] }.
flatten
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:remove"
class="permalink">permalink</a> \#**remove**(paths, options = {}) ⇒ Object
<div class="docstring">
<div class="discussion">
Delete the files at the given path recursively.
**NOTE**: In most cases, `trash` is a safer choice, since the files will
be recoverable by retreiving them from the trash. Once you delete a file
using `remove`, it's gone! Please use `trash` whenever possible and only
use `remove` when necessary.
## Options
`:force => boolean`
Force deletion (no error is raised if the file does not exist).
`:secure => boolean`
Infrequently needed. See
[`FileUtils.remove_entry_secure`](https://www.ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_entry_secure)
## Examples
Single path:
``` code
remove('~/Downloads/foo.zip')
```
Multiple path:
``` code
remove(['~/Downloads/foo.zip', '~/Downloads/bar.zip'])
remove(dir('~/Downloads/*.zip'))
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>204
205
206
207
208
209
210
211</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 204
def remove(paths, options = {})
expand_all(paths).each do |path|
options = @file_options.merge(options)
log("Removing #{ sh_escape(path) }")
FileUtils.rm_r(path, options)
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:rename"
class="permalink">permalink</a> \#**rename**(source, destination) ⇒ Object
<div class="docstring">
<div class="discussion">
Rename a single file.
Any directories needed in order to complete the rename are made
automatically.
Overwriting is not allowed; it logs a warning. If overwriting is
desired, use `remove` to delete the file first, then use `rename`.
## Examples
Simple rename:
``` code
rename('foo.zip', 'baz.zip') # "foo.zip" becomes "baz.zip"
```
Rename needing directories:
``` code
rename('foo.zip', 'bar/baz.zip') # "bar" is created, "foo.zip" becomes "baz.zip" within "bar"
```
Attempting to overwrite:
``` code
rename('foo.zip', 'existing.zip') # "skipping move of..."
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>72
73
74
75
76
77
78
79
80
81
82
83
84</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 72
def rename(source, destination)
source = expand(source)
destination = expand(destination)
mkdir(File.dirname(destination))
if File.exist?(destination)
warn("skipping rename of #{ sh_escape(source) } to #{ sh_escape(destination) } because it would overwrite")
else
log("rename #{ sh_escape(source) } #{ sh_escape(destination) }")
FileUtils.mv(source, destination, @file_options)
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:size_of"
class="permalink">permalink</a> \#**size_of**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
Get the size of a file.
## Examples
``` code
size_of('foo.zip') # => 2193
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>548
549
550</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 548
def size_of(path)
File.size(path)
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:spotlight_content_types"
class="permalink">permalink</a> \#**spotlight_content_types**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
\[Mac OS X\] Use Spotlight metadata to determine which content types a
file has.
## Examples
``` code
spotlight_content_types('foo.zip') # => ['public.zip-archive', 'public.archive']
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>635
636
637</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 635
def spotlight_content_types(path)
mdls_to_array(path, 'kMDItemContentTypeTree')
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:sync"
class="permalink">permalink</a> \#**sync**(from, to, options = {}) ⇒ Object
<div class="docstring">
<div class="discussion">
Simple sync two files/folders using `rsync`.
The host OS must provide `rsync`. See the `rsync` man page for a
detailed description.
``` code
man rsync
```
## Options
`:delete => boolean` `:verbose => boolean` `:archive => boolean`
(default `true`) `:update => boolean` (default `true`)
`:exclude => string` `:prune_empty => boolean`
## Examples
Syncing a directory to a backup:
``` code
sync('~/music', '/backup/music')
```
Excluding a path:
``` code
sync('~/code', '/backup/code', :exclude => '.git')
```
Excluding multiple paths:
``` code
sync('~/code', '/backup/code', :exclude => ['.git', '.rvmrc'])
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 607
def sync(from, to, options = {})
# expand removes trailing slash
# cannot use str[-1] due to ruby 1.8.7 restriction
from = expand(from) + (from.end_with?('/') ? '/' : '')
to = expand(to) + (to.end_with?('/') ? '/' : '')
# default options
options = { :archive => true, :update => true }.merge(options)
ops = []
ops << '-a' if options[:archive]
ops << '-v' if options[:verbose]
ops << '-u' if options[:update]
ops << '-m' if options[:prune_empty]
ops << '-n' if @file_options[:noop]
Array(options[:exclude]).each do |path|
ops << "--exclude=#{ sh_escape(path) }"
end
ops << '--delete' if options[:delete]
stdout = cmd("rsync #{ ops.join(' ') } #{ sh_escape(from) } #{ sh_escape(to) } 2>&1")
log("Fired sync from #{ sh_escape(from) } to #{ sh_escape(to) }. STDOUT:\n\n#{ stdout }")
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:trash"
class="permalink">permalink</a> \#**trash**(paths, options = {}) ⇒ Object
<div class="docstring">
<div class="discussion">
Move the given paths to the user's trash.
The path is still moved if a file already exists in the trash with the
same name. However, the current date and time is appended to the
filename.
**Note:** the OS-native "restore" or "put back" functionality for
trashed files is not currently supported. (See [issue
\#63](https://github.com/benjaminoakes/maid/issues/63).) However, they
can be restored manually, and the Maid log can help assist with this.
## Options
`:remove_over => Fixnum` (e.g. `1.gigabyte`, `1024.megabytes`)
Delete files over the given size rather than moving to the trash.
See also `Maid::NumericExtensions::SizeToKb`
## Examples
Single path:
``` code
trash('~/Downloads/foo.zip')
```
Multiple paths:
``` code
trash(['~/Downloads/foo.zip', '~/Downloads/bar.zip'])
trash(dir('~/Downloads/*.zip'))
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 113
def trash(paths, options = {})
# ## Implementation Notes
#
# Trashing files correctly is surprisingly hard. What Maid ends up doing is one the easiest, most foolproof
# solutions: moving the file.
#
# Unfortunately, that means it's not possile to restore files automatically in OSX or Ubuntu. The previous location
# of the file is lost.
#
# OSX support depends on AppleScript or would require a not-yet-written C extension to interface with the OS. The
# AppleScript solution is less than ideal: the user has to be logged in, Finder has to be running, and it makes the
# "trash can sound" every time a file is moved.
#
# Ubuntu makes it easy to implement, and there's a Python library for doing so (see `trash-cli`). However, there's
# not a Ruby equivalent yet.
expand_all(paths).each do |path|
target = File.join(@trash_path, File.basename(path))
safe_trash_path = File.join(@trash_path, "#{ File.basename(path) } #{ Time.now.strftime('%Y-%m-%d-%H-%M-%S') }")
if options[:remove_over] &&
File.exist?(path) &&
disk_usage(path) > options[:remove_over]
remove(path)
end
if File.exist?(path)
if File.exist?(target)
rename(path, safe_trash_path)
else
move(path, @trash_path)
end
end
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:tree_empty%3F"
class="permalink">permalink</a> \#**tree_empty?**(root) ⇒ Boolean
<div class="docstring">
<div class="discussion">
Test whether a directory is either empty, or contains only empty
directories/subdirectories.
## Example
``` code
if tree_empty?(dir('~/Downloads/foo'))
trash('~/Downloads/foo')
end
```
</div>
</div>
<div class="tags">
Returns:
- <span class="type">(Boolean)</span>
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 714
def tree_empty?(root)
return nil if File.file?(root)
return true if Dir.glob(root + '/*').length == 0
ignore = []
# Look for files.
return false if Dir.glob(root + '/*').select { |f| File.file?(f) }.length > 0
empty_dirs = Dir.glob(root + '/**/*').select { |d|
File.directory?(d)
}.reverse.select { |d|
# `.reverse` sorts deeper directories first.
# If the directory is empty, its parent should ignore it.
should_ignore = Dir.glob(d + '/*').select { |n|
!ignore.include?(n)
}.length == 0
ignore << d if should_ignore
should_ignore
}
Dir.glob(root + '/*').select { |n|
!empty_dirs.include?(n)
}.length == 0
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:verbose_dupes_in"
class="permalink">permalink</a> \#**verbose_dupes_in**(globs) ⇒ Object
<div class="docstring">
<div class="discussion">
Convenience method for `dupes_in` that excludes the dupe with the
shortest name.
This is ideal for dupes like `foo.zip`, `foo (1).zip`, `foo copy.zip`.
## Example
Keep the dupe with the shortest name (trash the others):
``` code
trash verbose_dupes_in('~/Downloads/*')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>427
428
429
430
431</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 427
def verbose_dupes_in(globs)
dupes_in(globs).
map { |dupes| dupes.sort_by { |p| File.basename(p).length }[1..-1] }.
flatten
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:where_content_type"
class="permalink">permalink</a> \#**where_content_type**(paths, filter_types) ⇒ Object
<div class="docstring">
<div class="discussion">
Filter an array by content types.
Content types can be MIME types, internet media types or Spotlight
content types (OS X only).
If you need your rules to work on multiple platforms, it's recommended
to avoid using Spotlight content types.
## Examples
### Using media types
``` code
where_content_type(dir('~/Downloads/*'), 'video')
where_content_type(dir('~/Downloads/*'), ['image', 'audio'])
```
### Using MIME types
``` code
where_content_type(dir('~/Downloads/*'), 'image/jpeg')
```
### Using Spotlight content types
Less portable, but richer data in some cases.
``` code
where_content_type(dir('~/Downloads/*'), 'public.image')
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>701
702
703
704</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 701
def where_content_type(paths, filter_types)
filter_types = Array(filter_types)
Array(paths).select { |p| !(filter_types & content_types(p)).empty? }
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="method_details">
### <a href="/gems/maid/0.7.0/Maid%2FTools:zipfile_contents"
class="permalink">permalink</a> \#**zipfile_contents**(path) ⇒ Object
<div class="docstring">
<div class="discussion">
List the contents of a zip file.
## Examples
``` code
zipfile_contents('foo.zip') # => ['foo.exe', 'README.txt', 'subdir/anything.txt']
```
</div>
</div>
<div class="tags">
</div>
<span
class="showSource">\[<a href="#" class="toggleSource">View source</a>\]</span>
<table class="source_code">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><pre class="lines"><code>475
476
477
478
479
480</code></pre></td>
<td><pre class="code"><code># File 'lib/maid/tools.rb', line 475
def zipfile_contents(path)
# It might be nice to use `glob` from `Zip::FileSystem`, but it seems buggy. (Subdirectories aren't included.)
Zip::File.open(path) do |zip_file|
zip_file.entries.map { |entry| entry.name }.sort
end
end</code></pre></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="footer">
Generated on Mon Jul 4 19:46:58 2022 by
<a href="http://yardoc.org" target="_parent"
title="Yay! A Ruby Documentation Tool">yard</a> 0.9.28 (ruby-3.0.2).
</div>
</div>