Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preference to read preferences from local dir #28

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions OsmApi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,31 @@ our $auth_token;

BEGIN
{
my $prefs_filename = home()."/.osmtoolsrc";
my $prefs_eol;

$prefs = { "dryrun" => 1 };
my $prefs_eol = 1;
sub read_prefs_file {
$prefs = { "dryrun" => 1 };
$prefs_eol = 1;

open (PREFS, home()."/.osmtoolsrc") or die "cannot open ". home()."/.osmtoolsrc";
while(<PREFS>)
{
if (/^([^=]*)=(.*)/)
open (PREFS, $prefs_filename) or die "cannot open $prefs_filename";
while(<PREFS>)
{
$prefs->{$1} = $2;
if (/^([^=]*)=(.*)/)
{
$prefs->{$1} = $2;
}
$prefs_eol = substr ($_, -1) eq "\n";
}
$prefs_eol = substr ($_, -1) eq "\n";
close (PREFS);
}
close (PREFS);


read_prefs_file;
if ($prefs->{local}) {
$prefs_filename = "./.osmtoolsrc";
read_prefs_file;
}

# override user name and password from environment if given
$prefs->{username} = $ENV{OSMTOOLS_USERNAME} if (defined($ENV{OSMTOOLS_USERNAME}));
$prefs->{password} = $ENV{OSMTOOLS_PASSWORD} if (defined($ENV{OSMTOOLS_PASSWORD}));
Expand Down Expand Up @@ -76,13 +86,13 @@ BEGIN

foreach my $required("username","password","apiurl")
{
die home()."/.osmtoolsrc does not have $required" unless defined($prefs->{$required});
die "$prefs_filename does not have $required" unless defined($prefs->{$required});
}

if (!defined($prefs->{instance}))
{
$prefs->{instance} = sprintf "%010x", $$ * rand(100000000);
open(PREFS, ">>".home()."/.osmtoolsrc");
open(PREFS, ">>$prefs_filename");
printf PREFS "\n" unless $prefs_eol;
printf PREFS "instance=".$prefs->{instance};
close(PREFS);
Expand Down Expand Up @@ -113,6 +123,8 @@ BEGIN
{
$prefs->{'weburl'} = $1;
}

print STDERR "Read config from $prefs_filename\n" if ($prefs->{debug});
}

sub login
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,23 @@ These scripts do not have safety nets. Be sure that you feel confident to fix an
Configuration
-------------

You will have to create a file named .osmtoolsrc in your home directory containg your user name, password, and the URL of the OSM server to use. The URL must be complete up to the API version number and the slash afterwards, so:
You will have to create a file named `.osmtoolsrc` in your home directory containing your user name, password, and the URL of the OSM server to use. The URL must be complete up to the API version number and the slash afterwards, so:

username=fred
password=test
apiurl=http://api06.dev.openstreetmap.org/api/0.6/

If your username or password is not specified in your .osmtoolsrc file, these scripts will look for OSMTOOLS_USERNAME and OSMTOOLS_PASSWORD environment variables. As a last resort, you will be prompted for a user name or password on the command line (requires the Term::ReadKey module).
If your username or password is not specified in your `.osmtoolsrc` file, these scripts will look for `OSMTOOLS_USERNAME` and `OSMTOOLS_PASSWORD` environment variables. As a last resort, you will be prompted for a user name or password on the command line (requires the `Term::ReadKey` module).

By default, all tools will run in "dry run" mode, so no changes will be actually written and all write requests will be considered successful. Add the "dryrun=0" parameter to the file for live action.
By default, all tools will run in "dry run" mode, so no changes will be actually written and all write requests will be considered successful. Add the `dryrun=0` parameter to the file for live action.

By default, "dry run" also enables "debug" so you are shown the requests made. If you want to keep debug mode when setting dryrun=0, explicitly set debug=1. There's also debug_request_headers and debug_request_body to print out details about the HTML messages, and the same for responses.
By default, "dry run" also enables "debug" so you are shown the requests made. If you want to keep debug mode when setting `dryrun=0`, explicitly set `debug=1`. There's also `debug_request_headers` and `debug_request_body` to print out details about the HTML messages, and the same for responses.

### Local configuration

There's also an alternative way to configure osmtools by reading `.osmtoolsrc` from a local directory instead of your home directory. This allows you to have different configurations ready which is useful if you want to modify the scripts. You may want one configuration ready for a development server (for example, api06.dev.openstreetmap.org) to test your modifications and another one for a live server (for example, api.openstreetmap.org) to use the modified scripts on real data.

To enable reading preferences from a local directory, `.osmtoolsrc` in your *home* directory should have `local=1` set. In this case the rest of preferences in the home directory file is ignored and `.osmtoolsrc` in the current local directory is read instead.

SCRAPE SCRAPE SCRAPE
--------------------
Expand Down