[76] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # TODO explain |
---|
| 4 | # |
---|
| 5 | # -- Copyright 2007 IGN France / Geoportail project -- |
---|
| 6 | # |
---|
| 7 | |
---|
| 8 | import sys |
---|
| 9 | import os |
---|
| 10 | import re |
---|
| 11 | |
---|
| 12 | SUFFIX_JAVASCRIPT = ".js" |
---|
| 13 | |
---|
| 14 | def _pjcat2js_remove(rezDirectory,catName,targetDirectory): |
---|
| 15 | pjCatFilename = os.path.join(rezDirectory, catName) |
---|
| 16 | pjCat = open(pjCatFilename,'r') |
---|
| 17 | comment_re = re.compile("^#") |
---|
| 18 | srsdef_re = re.compile("^<([^>]*)>.* <>$") |
---|
| 19 | l = pjCat.readline() |
---|
| 20 | while len(l) != 0: |
---|
| 21 | if comment_re.search(l) is None: |
---|
| 22 | srsdef_mo = srsdef_re.match(l) |
---|
| 23 | srsdef_fn = os.path.join(targetDirectory, catName+srsdef_mo.group(1)+".js") |
---|
| 24 | if os.path.exists(srsdef_fn): |
---|
| 25 | os.remove(srsdef_fn) |
---|
| 26 | l = pjCat.readline() |
---|
| 27 | pjCat.close() |
---|
| 28 | |
---|
| 29 | def _pjcat2js_make(rezDirectory,catName,targetDirectory): |
---|
| 30 | pjCatFilename = os.path.join(rezDirectory, catName) |
---|
| 31 | pjCat = open(pjCatFilename,'r') |
---|
| 32 | comment_re = re.compile("^#") |
---|
| 33 | srsdef_re = re.compile("^<([^>]*)> *(.*) <>$") |
---|
| 34 | l = pjCat.readline() |
---|
| 35 | while len(l) != 0: |
---|
| 36 | if comment_re.search(l) is None: |
---|
| 37 | srsdef_mo = srsdef_re.match(l) |
---|
| 38 | srsdef_fn = os.path.join(targetDirectory, catName+srsdef_mo.group(1)+".js") |
---|
| 39 | srsdef = 'Proj4js.defs["'+catName+':'+srsdef_mo.group(1)+'"]="'+srsdef_mo.group(2)+'";' |
---|
| 40 | file(srsdef_fn,'w').write(srsdef) |
---|
| 41 | l = pjCat.readline() |
---|
| 42 | pjCat.close() |
---|
| 43 | |
---|
| 44 | def pjcat2js_clean(rezDirectory,targetDirectory): |
---|
| 45 | if not os.path.isdir(rezDirectory): |
---|
| 46 | return |
---|
| 47 | if not os.path.isdir(targetDirectory): |
---|
| 48 | return |
---|
| 49 | if os.path.abspath(rezDirectory) == '/': |
---|
| 50 | return |
---|
| 51 | if os.path.abspath(targetDirectory) == '/': |
---|
| 52 | return |
---|
| 53 | rezDirectory_name_len = len(rezDirectory) |
---|
| 54 | for root, dirs, filenames in os.walk(rezDirectory): |
---|
| 55 | if 'CVS' in dirs: |
---|
| 56 | dirs.remove('CVS') |
---|
| 57 | if '.svn' in dirs: |
---|
| 58 | dirs.remove('.svn') |
---|
| 59 | for filename in filenames: |
---|
| 60 | if not filename.endswith(SUFFIX_JAVASCRIPT) and not filename.startswith("."): |
---|
| 61 | filepath = os.path.join(root, filename)[rezDirectory_name_len+1:] |
---|
| 62 | filepath = filepath.replace("\\", "/") |
---|
| 63 | _pjcat2js_remove(rezDirectory,filepath,targetDirectory) |
---|
| 64 | |
---|
| 65 | def pjcat2js_run(rezDirectory,targetDirectory): |
---|
| 66 | if not os.path.isdir(rezDirectory): |
---|
| 67 | return |
---|
| 68 | if not os.path.isdir(targetDirectory): |
---|
| 69 | return |
---|
| 70 | if os.path.abspath(rezDirectory) == '/': |
---|
| 71 | return |
---|
| 72 | if os.path.abspath(targetDirectory) == '/': |
---|
| 73 | return |
---|
| 74 | rezDirectory_name_len = len(rezDirectory) |
---|
| 75 | for root, dirs, filenames in os.walk(rezDirectory): |
---|
| 76 | if 'CVS' in dirs: |
---|
| 77 | dirs.remove('CVS') |
---|
| 78 | if '.svn' in dirs: |
---|
| 79 | dirs.remove('.svn') |
---|
| 80 | for filename in filenames: |
---|
| 81 | if not filename.endswith(SUFFIX_JAVASCRIPT) and not filename.startswith("."): |
---|
| 82 | filepath = os.path.join(root, filename)[rezDirectory_name_len+1:] |
---|
| 83 | filepath = filepath.replace("\\", "/") |
---|
| 84 | _pjcat2js_make(rezDirectory,filepath,targetDirectory) |
---|
| 85 | |
---|