[Tutor] I love python / you guys :)
Luke Paireepinart
rabidpoobear at gmail.com
Mon Nov 16 13:25:34 CET 2009
Accidental off-list reply.
On Mon, Nov 16, 2009 at 5:36 AM, bibi midi <bibsmendez at gmail.com> wrote:
>
> In the same lines of if-you-can-think-of-anything-python-can-do-it i got
> inspired to ask a question for the gurus:
>
> When i use our company's LAN i set my proxy variable by hand in .bashrc.
> There are 4 files to insert proxy variable:
>
> in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
>
> The last one is actually rename e.g. mv to apt.conf to activate proxy and
> mv to apt.conf.bak to deactivate. The proxy variable is something like this
>
> export http_proxy=http://username:password@proxy:port
> ftp_proxy=$http_proxy
>
> To activate i uncomment them then source .bashrc. To deactivate i put back
> the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
> apt.conf see rename above. I deactivate because i have another internet
> connection option via 3G usb modem. But thats another story.
>
> I will do this myself in python so please show me the way. Surely this can
> be done.
>
> Sure it can.
This is actually fairly easy to do. I would just use regular expressions to
match the lines and comment/uncomment them depending on if you want to
enable/disable.
####################
Spoiler alert! don't read if you want to solve it yourself.
####################
#### Remove/Add comments to any line that matches a specified regular
expression.
## comments are assumed to be the '#' symbol.
#### Warning - completely untested code.
import re
regex = # remove leading spaces and the # comment symbol from a line, if it
exists.
def uncomment(filename, regex, newfile):
remove_comments_regex = ' *#*(.*$)'
for line in open(filename):
if re.match(regex, line): # if we have a matching line we should
remove the comment.
newfile.write(re.match(remove_comments_regex, line).groups()[0])
def comment(filename, regex, newfile):
for line in open(filename):
if re.match(regex, line):
#avoid dual-commenting
if line.strip().startswith("#"):
newfile.write(line)
else:
newfile.write('#' + line)
####################
End of spoiler
####################
As for renaming, look into the os module, there's an easy function for
renaming in there. Just add a check around the rename function for an
os.path.exists(...) so you can ensure the file exists before you attempt to
rename it, otherwise you might get an exception (or you could just try
renaming it and just catch the exception, either way.) Then just make a
.py script that can enable and one that can disable, and make sure tehre's
no harm if you run either one multiple times consecutively.
Hope that helps, and yes, we know you guys appreciate the help, that's why
we do it! We certainly don't get paid anything. It's nice to hear you say
it anyway though, so thanks!
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/b59758f5/attachment.htm>
More information about the Tutor
mailing list