-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo_utils.py
More file actions
executable file
·109 lines (77 loc) · 3.05 KB
/
repo_utils.py
File metadata and controls
executable file
·109 lines (77 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
__author__ = 'chrissbarnett'
import os
import json
from repo_dir import get_directory
from metadata_type import get_metadata_type
from xml.dom.minidom import *
# this should be the absolute path to the local clone of your repository
# REPO_PATH = "/usr/local/metadata"
REPO_PATH = "/Users/cbarne02/Documents/code/git/edu.tufts"
FGDC_HEADER = ['<?xml version="1.0" encoding="utf-8" ?>',
'<!DOCTYPE metadata SYSTEM "http://www.fgdc.gov/metadata/fgdc-std-001-1998.dtd">']
def get_filename_from_type(f):
d = {'FGDC': 'fgdc.xml', 'ISO19139': 'iso19139.xml'}
mt, tree = get_metadata_type(f)
return d.get(mt), tree
def get_header(mt):
d = {'FGDC': FGDC_HEADER, 'ISO19139': None}
return d.get(mt)
def write_metadata(tree, meta_type, path, overwrite):
if not os.path.exists(path) or overwrite:
try:
with open(path, "w+") as d:
for line in get_header(meta_type):
d.write(line)
tree.write(d, xml_declaration=False, encoding="utf-8")
print "Metadata written at: " + path
except:
raise Exception("Error writing metadata")
else:
raise Exception("A metadata directory with that id already exists in the repository.")
def update_layers_json(layer_id, path, repo_path=REPO_PATH):
layers = get_layers_json(repo_path)
layers[layer_id] = os.path.relpath(path, repo_path)
set_layers_json(layers, repo_path)
def get_layers_json(repo_path=REPO_PATH):
lpath = os.path.join(repo_path, "layers.json")
layers = {}
if os.path.exists(lpath):
with open(lpath, 'r') as fr:
layers = json.load(fr)
return layers
def set_layers_json(layers, repo_path=REPO_PATH):
lpath = os.path.join(repo_path, "layers.json")
with open(lpath, 'w+') as fw:
json.dump(layers, fw, indent=4, sort_keys=True)
def write_to_repository(file_id, fs, repo_path=REPO_PATH, overwrite=False):
type_name = None
tree = None
try:
type_name, tree = get_filename_from_type(fs)
except Exception as e:
print file_id
print e.message
return
path = get_directory(file_id, repo_path)
fname = os.path.join(path, type_name)
if os.path.exists(fname):
print file_id
print path
if overwrite:
print "Warning: overwriting file!"
else:
raise Exception("A metadata directory with that id already exists in the repository.")
write_metadata(tree, type_name, fname, overwrite)
# create/update a json object at repository root mapping layer ids to directories
update_layers_json(file_id, path, repo_path)
return path
def find_from_layer_id(layer_id, repo_path=REPO_PATH):
layers = get_layers_json(repo_path)
if not layers.has_key(layer_id):
raise Exception("layer['" + layer_id + "'] not found!")
path = layers[layer_id]
path = os.path.join(repo_path, path)
if os.path.exists(path):
return path, os.listdir(path)
else:
raise Exception("layer['" + layer_id + "'] not found!")