<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mitch&#039;s meanderings &#187; Programming</title>
	<atom:link href="http://mitchcontla.com/category/tech/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://mitchcontla.com</link>
	<description>thoughts on life, technology, and running; links, photos, and videos</description>
	<lastBuildDate>Wed, 19 May 2010 04:58:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Nautilus Script: Edit Remote Files (SSH) with Vim</title>
		<link>http://mitchcontla.com/2007/09/06/nautilus-script-edit-remote-files-ssh-with-vim/</link>
		<comments>http://mitchcontla.com/2007/09/06/nautilus-script-edit-remote-files-ssh-with-vim/#comments</comments>
		<pubDate>Thu, 06 Sep 2007 08:11:41 +0000</pubDate>
		<dc:creator>mitch</dc:creator>
				<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[gvim]]></category>
		<category><![CDATA[nautilus]]></category>
		<category><![CDATA[nautilus-script]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://mitch.contlafamily.com/2007/09/06/nautilus-script-edit-remote-files-ssh-with-vim/</guid>
		<description><![CDATA[My on again, off again love affair with KDE is currently off (but that&#8217;s another story). I&#8217;m once again enjoying the simplicity of Gnome, and the elegance of Nautilus.

The ability to graphically browse remote servers via SSH is great, but I found out that trying to edit a remote file chokes Vim. Apparently the netrw [...]]]></description>
			<content:encoded><![CDATA[<p>My on again, off again love affair with <a href="http://www.kde.org/" title="K Desktop Environment"><span class="caps">KDE</span></a> is currently off (but that&#8217;s another story). I&#8217;m once again enjoying the simplicity of <a href="http://www.gnome.org/" title="Gnome Desktop">Gnome</a>, and the elegance of <a href="http://www.gnome.org/projects/nautilus/" title="Nautilus file manager">Nautilus</a>.</p>

<p>The ability to graphically browse remote servers via <acronym title="secure shell">SSH</acronym> is great, but I found out that trying to edit a remote file chokes <a href="http://www.vim.org/" title="Vim editor">Vim</a>. Apparently the netrw module does not handle an <span class="caps">SSH </span><acronym title="Uniform Resource Identifier">URI</acronym>, but rather prefers <acronym title="secure copy">SCP</acronym>. So a quick nautilus-script to the rescue.</p>

<pre class="code python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
&nbsp;
files = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> unescape<span style="color: black;">&#40;</span>uri<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\s</span>'</span>, r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\</span> '</span>, <span style="color: #dc143c;">urllib</span>.<span style="color: black;">unquote</span><span style="color: black;">&#40;</span>uri<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getenv</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NAUTILUS_SCRIPT_SELECTED_URIS&quot;</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^file://'</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
        files.<span style="color: black;">append</span><span style="color: black;">&#40;</span>unescape<span style="color: black;">&#40;</span><span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^file://'</span>, r<span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^ssh://'</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
        files.<span style="color: black;">append</span><span style="color: black;">&#40;</span>unescape<span style="color: black;">&#40;</span><span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^ssh://([^/]+)(/[^/]+){2}/(.*)$'</span>, r<span style="color: #483d8b;">'scp://<span style="color: #000099; font-weight: bold;"></span>/<span style="color: #000099; font-weight: bold;"></span>'</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> files:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;gvim &quot;</span><span style="color: #483d8b;">&quot;&quot;</span> + <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>files<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;</pre>

<p>Still some work to do, but for now it&#8217;s simple and effective.</p>]]></content:encoded>
			<wfw:commentRss>http://mitchcontla.com/2007/09/06/nautilus-script-edit-remote-files-ssh-with-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sanitize File Names Using VBScript</title>
		<link>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/</link>
		<comments>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/#comments</comments>
		<pubDate>Fri, 19 May 2006 02:46:48 +0000</pubDate>
		<dc:creator>mitch</dc:creator>
				<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[regular-expressions]]></category>

		<guid isPermaLink="false">http://mitch.contla.net/2006/05/18/sanitize-file-names-using-vbscript/</guid>
		<description><![CDATA[We use an ASP based form to enable our customers to submit files created as part of a registration process. During the submission, the file is sent as an email attachment to our support staff, and a copy is written to a file server as a backup. The application uses a standard Win32 Save dialog [...]]]></description>
			<content:encoded><![CDATA[<p>We use an <acronym title="Active Server Pages">ASP</acronym> based form to enable our customers to submit files created as part of a registration process. During the submission, the file is sent as an email attachment to our support staff, and a copy is written to a file server as a backup. The application uses a standard <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_start_page.asp">Win32</a> Save dialog that suggests a default filename. In nearly every case, the file is submitted by the customer without changing the default name. If the email process fails, retrieving the backup copy is difficult at best.</p>

<p>I modified the form to create a unique filename using values from certain form elements. Since these values can, and often do, use characters that are invalid as part of a filename, I needed a way to &#8220;sanitize&#8221; the name during the submission process. I wrote the following <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/0a8270d7-7d8f-4368-b2a7-065acb52fc54.asp"><span class="caps">VBS</span>cript</a> code to replace these characters before attempting to save the file.</p>

<pre class="code vb">&nbsp;
   <span style="color: #b1b100;">Function</span> SanitizeFilename<span style="color: #66cc66;">&#40;</span>byVal strFilename, byVal strReplChar<span style="color: #66cc66;">&#41;</span>	
      
      <span style="color: #b1b100;">Set</span> objRegExp = <span style="color: #b1b100;">New</span> RegExp <span style="color: #808080;">' Create new RegExp object</span>
      
      <span style="color: #808080;">' Define regex pattern and set Global replacement property</span>
      objRegExp.<span style="color: #66cc66;">Pattern</span> = <span style="color: #ff0000;">&quot;[\x00-\x1f\x22\\/:\*\?&lt;&gt;\|]&quot;</span>
      objRegExp.<span style="color: #66cc66;">Global</span> = <span style="color: #b1b100;">True</span>
      
      <span style="color: #808080;">' Check strReplChar parameter for invalid length or character,</span>
      <span style="color: #808080;">' default to underscore</span>
      <span style="color: #b1b100;">If</span> <span style="color: #b1b100;">Len</span><span style="color: #66cc66;">&#40;</span>strReplChar<span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">Then</span> 
         strReplChar = objRegExp.<span style="color: #66cc66;">Replace</span><span style="color: #66cc66;">&#40;</span>strReplChar, <span style="color: #ff0000;">&quot;_&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">Else</span> 
         strReplChar = <span style="color: #ff0000;">&quot;_&quot;</span>
      <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
      
      <span style="color: #808080;">' Return clean filename</span>
      SanitizeFilename = objRegExp.<span style="color: #66cc66;">Replace</span><span style="color: #66cc66;">&#40;</span>strFilename, strReplChar<span style="color: #66cc66;">&#41;</span>
      
      <span style="color: #b1b100;">Set</span> objRegExp = <span style="color: #b1b100;">Nothing</span>
   <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
&nbsp;</pre>

<p>This function accepts two arguments, <code>strFilename</code> which receives the string created from the form values, and <code>strReplChar</code> which receives the character used for replacement. The function uses a <a href="http://en.wikipedia.org/wiki/Regex">regular expression</a> pattern to match the invalid characters (control characters through 31, double-quotes, and a handful of others). There is a check to make sure that the <code>strReplChar</code> argument is valid, but not much else. The <a href="http://msdn.microsoft.com/library/en-us/script56/html/9f1c25ba-46ce-46af-9f19-ac1d2bcf05d8.asp">RegExp Object</a> member <code>Replace</code> does the heavy lifting, before the function returns the modified string.</p>

<p>The <acronym title="Regular Expression">regex</acronym> pattern replaces invalid characters for Windows <a href="http://en.wikipedia.org/wiki/NTFS"><acronym title="New Technology File System">NTFS</acronym></a> (generally), but could easily be altered for other <acronym title="Operating System">OS</acronym> file systems. You could also use a more restrictive regular expression like:</p>

<div class="example">


<pre><code>
      [&amp;#094;\w\x20\&amp;%'`\-\@{}~!#\(\)&amp;_\&amp;#094;\+,\.=\[\]]
</code></pre>


</div>

<p>Then again, that may be a bit much. Wikipedia has a good <a href="http://en.wikipedia.org/wiki/Filename">filename</a> reference that includes a fairly comprehensive list of reserved words and allowable character sets. As always, if you need a good regex reference, try <a href="http://www.regular-expressions.info/">Regular-Expressions.info</a>.</p>

<p><strong><span class="caps">UPDATE</span>:</strong> The logic in my if/then statement to check the length and validity of the <code>strReplChar</code> was backwards. The example has been corrected.</p>]]></content:encoded>
			<wfw:commentRss>http://mitchcontla.com/2006/05/18/sanitize-file-names-using-vbscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
