Skip to content

Commit

Permalink
Merge pull request #42 from wri/master
Browse files Browse the repository at this point in the history
Update readme and explicitely numbered replacement fields
  • Loading branch information
Thomas Maschler authored Apr 11, 2017
2 parents c4a1c91 + 8f530bd commit 8754823
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 94 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@ Using arcpy_metadata

Creating the Metadata Editor

Edit existing metadata for Shapefiles, Rasters, FeatureClasses, RasterDatasets, MosaicDatasets or Layers
```python
import arcpy_metadata as md
metadata = md.MetadataEditor(path_to_some_feature_class) # currently supports Shapefiles, FeatureClasses, RasterDatasets and Layers
```


Edit or create an XML file directly
```python
import arcpy_metadata as md
metadata = md.MetadataEditor(metadata_file="path/to/metadata_file.xml") # currently supports Shapefiles, FeatureClasses, RasterDatasets and Layers
```


Choose your log level
```python
metadata = md.MetadataEditor(path_to_some_feature_class, loglevel="DEBUG") # use any of CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET, dafault is INFO

```

Get text items (returns string)

```python
Expand Down Expand Up @@ -73,7 +88,7 @@ last_update = metadata.last_update
last_update_year = metadata.last_update.year
```

Change date items (takes both date objects and formated string (yyyymmdd)
Change date items (excepts datetime objects and formated string (yyyymmdd, yyyy-mm-ddThh:mm:ss)

```python
from datetime import date
Expand Down Expand Up @@ -136,10 +151,6 @@ If you want to enable automatic updates of your metadata (feature classes only)
```python
metadata.finish(True)
```
This might overwrite some of your recent edits including the title.

The code is based on a set of core classes that provide set/append/prepend operations, and we would love pull requests that add classes or attributes to cover additional portions of metadata specs.


Supported items
---------------
Expand Down Expand Up @@ -229,6 +240,12 @@ Does not yet support all metadata items.
arcpy_metadata only works with 32-bit Python. We use arcpy.XSLTransform_conversion() to extract metadata from geodatabases. 64bit arcpy python bindings for background processing [do not support](http://desktop.arcgis.com/en/arcmap/latest/analyze/executing-tools/64bit-background.htm) tools inside the metadata conversion toolset.


How to contribute
-------------
Contributions are well come! Please fork and submit pull requests.
If you are missing a particular metadata attribute, you can easiy add them [here](https://github.com/ucd-cws/arcpy_metadata/blob/master/arcpy_metadata/elements.py). Don't forget to also add them to the [test cases](https://github.com/ucd-cws/arcpy_metadata/blob/master/tests/test_elements.py) to make sure everything works as expected


Acknowledgements
----------------
arcpy_metadata was initially a project of the [UC Davis Center for Watershed Sciences](https://watershed.ucdavis.edu) and has received significant contributions from the [World Resources Institute](https://www.wri.org). It was created as part of a larger project funded by the California Department of Fish and Wildlife [Biogeographic Data Branch](http://www.dfg.ca.gov/biogeodata/) and further developed for [Global Forest Watch](https://www.globalforestwatch.org). We thank our funders for their support and their commitment to high quality geospatial data.
Expand Down
24 changes: 12 additions & 12 deletions arcpy_metadata/metadata_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ def __init__(self, parent=None, tagname=None, path=None, child_elements=None):
self.current_items = []
for item in self.parent.elements.find(self.path):
if item.tag == self.tag_name:
new_path = "{}/{}".format(self.path, tagname)
new_path = "{0}/{1}".format(self.path, tagname)
child = MetadataParentItem(new_path, self.parent, child_elements, len(self.current_items))
self.current_items.append(child)

def new(self):
new_path = "{}/{}".format(self.path, self.tag_name)
new_path = "{0}/{1}".format(self.path, self.tag_name)
child = MetadataParentItem(new_path, self.parent, self.child_elements, len(self.current_items)+1)
self.current_items.append(child)

Expand Down Expand Up @@ -433,8 +433,8 @@ def __init__(self, parent, child_elements):

path = self.child_elements[element]["path"]

if "_{}".format(element) not in self.__dict__.keys():
setattr(self, "_{}".format(element), self._create_item(path))
if "_{0}".format(element) not in self.__dict__.keys():
setattr(self, "_{0}".format(element), self._create_item(path))
i = 0
else:
i += 1
Expand All @@ -449,22 +449,22 @@ def __setattr__(self, n, v):
if element_type == "attribute":
key = self.child_elements[n]["key"]
if v is None or v == "":
self.__dict__["_{}".format(n)].element.attrib[key] = ""
self.__dict__["_{0}".format(n)].element.attrib[key] = ""
else:
allowed_values = []
found = False
for value in self.child_elements[n]["values"]:
allowed_values.append(value[0])
if v == value[0]:
self.__dict__["_{}".format(n)].element.attrib[key] = value[1]
self.__dict__["_{0}".format(n)].element.attrib[key] = value[1]
found = True
if not found:
raise TypeError("Value must be in {}".format(allowed_values))
raise TypeError("Value must be in {0}".format(allowed_values))

elif isinstance(v, (str, unicode)):
self.__dict__["_{}".format(n)].element.text = v
self.__dict__["_{0}".format(n)].element.text = v
elif v is None:
self.__dict__["_{}".format(n)].element.text = ""
self.__dict__["_{0}".format(n)].element.text = ""
else:
raise RuntimeWarning("Input value must be of type String or None")
else:
Expand All @@ -478,8 +478,8 @@ def __getattr__(self, name):
if element_type == "attribute":
key = self.child_elements[name]["key"]
values = self.child_elements[name]["values"]
if key in self.__dict__["_{}".format(name)].element.attrib.keys():
v = self.__dict__["_{}".format(name)].element.attrib[key]
if key in self.__dict__["_{0}".format(name)].element.attrib.keys():
v = self.__dict__["_{0}".format(name)].element.attrib[key]
for value in values:
if v in value:
return value[0]
Expand All @@ -488,7 +488,7 @@ def __getattr__(self, name):

else:
#return self.__dict__["_{}".format(name)].value
return self.__dict__["_{}".format(name)].element.text # should be the same"
return self.__dict__["_{0}".format(name)].element.text # should be the same"

else:
return self.__dict__[name]
Expand Down
Loading

0 comments on commit 8754823

Please sign in to comment.