SourceFiles.org - Use the Source, Luke
Home | Register | News | Forums | Guide | MyLinks | Bookmark

Related Sites

Latest News
  General News
  Reviews
  Press Releases
  Software
  Hardware
  Security
  Tutorials
  Off Topic


Back to files

ABOUT

The g-jutils are a collection of small but useful python utilities. I have chosen this somewhat strange name to prevent any other project from using the same (I'd say that's guaranteed :-) and of course it has my own initials. Don't ask me how to pronounce it. What all g-jutils have in common is the framework they all use: the GJutils_module <GJutils.py/>. This module contains objects that are designed to be of general use, and that should make it a bit easier to quickly write a small python utility of your own. Currently it contains four objects: one for color output, one for configuration files and two for command line parsing, one of them deprecated. The use and application of these objects is explained on the module_page <GJutils.py/>.

If you have written a small utility of your own that you think might be of value to others as well, mail it to me and I'll add it to the unofficial_g-jutils <unofficial/> repository. If I really like it I might even make it official, i.e. part of the g-jutils core package. Note that for that to happen you should at least consider the following guidelines. A g-jutil:

The last one is to prevent additional dependencies for the core package. Currently there are four official g-jutils. I will discuss each of them briefly. A coding discussion can be found on their own page, along with an individual download link. Please remember: you will always need the GJutils_module <GJutils.py/> somewhere in your python path for them to work.

The following command line options can be found in all g-jutils, depending on which objects are used:

  • [-q --quiet]: Be less verbose. By default this prevents the g-jutil's title from being printed after the command line is successfully parsed. The g-jutil itself can use this option to leave out other messages.
  • [-m --monochrome]: Disable output coloring while preserving the boldface attribute.
  • [-c --config CFG]: Use an alternative configuration file or directory. By default the standard linux scheme is followed: a dot followed by the g-jutil's name in the user's home directory.

G-RENAME

G-rename <g-rename/> brings the power of python's regular expressions to the bash prompt. It resembles the standard [mv] command, moving files or directories from the source argument to the destination argument. Only here the source argument is a python regular expression and the destination is a replacement string. If you are not familiar with python regular expressions it is useful to read this_howto <http://www.amk.ca/python/howto/regex/regex.html>; the actual renaming is done by the [re.sub] function described in the search_and_replace <http://www.amk.ca/python/howto/regex/regex.html#SECTION000620000000000000000> section.

Some exampes of what is possible. The most basic application is renaming a file, say 'foo', to another file, say 'bar':

-- g-rename foo bar

This will do what it says. However if there happens to be a file 'xfoox' in the same directory it will be renamed as well, with destination 'xbarx'. This can be prevented by prepending adding a carret (match at beginning) and a dollar (match at end) to the expression:

-- g-rename ^foo$ bar

This will rename foo only, but in this case you would have probably been better of with [mv]. Things becomes more interesting when braces are used to group certain parts of the expression. This makes it possible to modify a list of files or directories by refering to this group in the replacement string. Imagine a group of files, all ending with some numbers. The following command moves this numerical part to the beginning of the filename:

-- g-rename "(.*?)(\d*$)" "\2\1"

Note the need for quotation marks when using braces or baskslashes on the bash prompt. It is also possible to move files to different directories. The following command moves files to directories named by their extension:

-- g-rename "(.*)[.](.*)" "\2/\1.\2"

Any missing directories are created automatically. To catch double extensions such as [.tar.gz] simply append a question mark to the first group to make it less greedy.

This is only a brief introduction to give an idea of what is possible with this g-jutil. When you just keep in mind that [re.sub] is the underlying function you'll be able to use it at its full potential.

The following command line options are specific to g-rename:

  • [-r --recursive]: Recurse over subdirectories when finding matches for the regular expression. The expression is not used to exclude directories from visiting so this option is not very fast. Keep this in mind when you're in a hurry.
  • [-f --files]: Restrict the renaming operation to files.
  • [-d --directories]: Idem for directories.
  • [-i --ignorecase]: Use case-insensitive pattern matching. This effectively sets the expression's [re.IGNORECASE] flag.
  • [-c --count N]: Limit the number of replacements in a single file or directory. Useful when the expression matches multiple times and only the first occurence needs changing.

G-URLMON

G-urlmon <g-urlmon/> monitors a set of urls for changes. Whenever executed it downloads the urls specified on the command line and checks these files against the ones present in the configuration directory, which is ~/.g-urlmon by default. If the file is changed (or new) the changes are printed in a nicely colored unified diff format and the file is saved. When no urls are specified on the command line simply all files in the configuration directory are checked for changes. The downloading part is handled by the urllib2 <http://docs.python.org/lib/module-urllib2.html> module so a wide range of protocols is supported.

G-urlmon does not have any command line options of its own.

G-REMIND

G-remind <g-remind/> is a reminder service focussed on flexibility. Its task is to show a list of upcoming events, a bit like bsd's [calendar] program. The flexibility comes from the possibility (read: necessity) to write small bits of python code to process the various event listings. This makes it possible to do things like printing an age for a birthday and calculating complicated dates like easter. Of course these things could all be part of g-remind so that you won't have to write your own code, but the module system makes it possible to also list those events that only you can think of.

The system works a bit like g-urlmon. The event listings are files in the configuration directory, which is ~/.g-remind by default. The events to be listed are specified on the command line and just as with g-urlmon all events are listed when none are specified. Every listing consists of two parts: a list of comma separated values and a block of python code, separated by at least two empty lines. From the first block the first line is used for column labels. These labels are used in the second block, which forms a python generator object that returns date-text tuples through the [yield]_statement <http://docs.python.org/ref/yield.html>. G-remind calls this generator for each parameter list in the first block, this way collecting a list of events from each file. The generated dates should start somewhere before today's date, available as [TODAY], and should be in chronological order so that g-remind can break out of the generator loop when the end date is exceeded. Look at the examples in the g-remind_modules <g-remind/modules/> repository to see all the above in action. Also take a look at the g-remind_code <g-remind/> to see which objects are available in the namespace that all modules run in. Remember that you are not restricted to this default namespace; the code is interpreted as normal python code so things like importing modules and reading and writing files are valid.

If you have written a module for g-remind that you think is useful for others as well, send me a mail and I'll add it to the repository <g-remind/modules/>. These modules don't necessarily have to contain new code; translations of existing modules and for example modules with national special days are also welcome attributions. Everytime a new version of g-jutils is released I will add a selection of modules from the repository to the package's example section.

The following command line options are specific to g-remind:

  • [-t --today DATE]: Use a starting date other than today. Useful mainly for testing the behaviour of newly written modules.
  • [-w --weeks N]: Change the number of weeks for which to list the events. By default events in a period of four weeks are shown.
  • [-o --output OUT]: Choose an output module out of 'text' and 'latex'. This changes the way in which the event linstings are printed out.
  • [-v --verbose]: For each event, print the name of the module that generated it. This was default behaviour in version 1.0.
  • [-n --noseparators]: Do not add week separators to the event listing. By default events are grouped in blocks of a week with a number counting the weeks from today.

G-TRAIN

G-train <g-train/> is a training shell, much unlike any of the previously existing ones. While most training tools focus on specific things like vocabulary, g-train can handle all kinds of training material. In fact, the main purpose I had in mind was geography. I believe a good way of learning the world by heart is by learning neighbour countries. Surely, the moment you can name the neighbour countries of every country in the world is the moment you can visualize the world with your eyes closed. I have not yet achieved this but I'm working on it.

The training material is a plain text file consisting of data blocks, separated by one or more empty lines. Each block starts with a single-word key, followed by a comma separated value list. In the case of geography, a data block may look like this:

-- country Netherlands
-- map Europe
-- neighbours Belgium, Germany
-- capital Amsterdam

Actually, this block is part of the 'world' data set that comes along with g-train. I have created this file using data from the CIA_World_Factbook <http://www.cia.gov/cia/publications/factbook/>. What makes g-train so special is that a single key (neighbours) may contain multiple values (Belgium and Germany). Also, each block may contain any number of keys. The above block contains four: country, map, neighbour and capital. They can be trained in any direction: from country to capital, from neighbours to country, etc. Applied on vocabulary training, multi-lingual data is possible:

-- english word
-- french mot
-- german wort
-- spanish palabra
-- italian parola
-- dutch woord

As always, any training material you would like to contribute is more than welcome! The shell provides commands to manage the possibly large amount of information. The first thing you need to do after entering the shell is to make a selection of the available data. Use the help function to find out how this is done. The training is started with 'train', for which you need to specify a question and an answer key. On exit the state is saved so the training can be continued later on, in case you did not manage to absorbe all that knowledge at once.

G-train does not have any command line options of its own.


Other Sites

Discussion Groups
  Beginners
  Distributions
  Networking / Security
  Software
  PDAs

About | FAQ | Privacy | Awards | Contact
Comments to the webmaster are welcome.
Copyright 2006 Sourcefiles.org All rights reserved.