本文整理汇总了Python中stetl.util.Util类的典型用法代码示例。如果您正苦于以下问题:Python Util类的具体用法?Python Util怎么用?Python Util使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Util类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init
def init(self):
self.ogr = ogr
# http://trac.osgeo.org/gdal/wiki/PythonGotchas
self.gdal = gdal
self.gdal.UseExceptions()
log.info("Using GDAL/OGR version: %d" % int(gdal.VersionInfo('VERSION_NUM')))
# GDAL error handler function
# http://pcjericks.github.io/py-gdalogr-cookbook/gdal_general.html
def gdal_error_handler(err_class, err_num, err_msg):
err_type = {
gdal.CE_None: 'None',
gdal.CE_Debug: 'Debug',
gdal.CE_Warning: 'Warning',
gdal.CE_Failure: 'Failure',
gdal.CE_Fatal: 'Fatal'
}
err_msg = err_msg.replace('\n', ' ')
err_class = err_type.get(err_class, 'None')
log.error('Error Number: %s, Type: %s, Msg: %s' % (err_num, err_class, err_msg))
# install error handler
self.gdal.PushErrorHandler(gdal_error_handler)
# Raise a dummy error for testing
# self.gdal.Error(1, 2, 'test error')
if self.source_options:
for k in self.source_options:
self.gdal.SetConfigOption(k, self.source_options[k])
# Open OGR data source in read-only mode.
if self.source_format:
self.data_source_p = ogr.GetDriverByName(self.source_format).Open(self.data_source, 0)
else:
self.data_source_p = self.ogr.Open(self.data_source, 0)
# Report failure if failed
if self.data_source_p is None:
log.error("Cannot open OGR datasource: %s with the following drivers." % Util.safe_string_value(self.data_source))
for iDriver in range(self.ogr.GetDriverCount()):
log.info(" -> " + self.ogr.GetDriver(iDriver).GetName())
raise Exception()
else:
# Open ok: initialize
self.layer = None
if self.sql:
self.layer_count = 1
self.layer_idx = -1
else:
self.layer_count = self.data_source_p.GetLayerCount()
self.layer_idx = 0
log.info("Opened OGR source ok: %s layer count=%d" % (Util.safe_string_value(self.data_source), self.layer_count))
示例2: execute_cmd
def execute_cmd(self, cmd):
env_vars = Util.string_to_dict(self.env_args, self.env_separator)
old_environ = os.environ.copy()
try:
os.environ.update(env_vars)
log.info("executing cmd=%s" % Util.safe_string_value(cmd))
subprocess.call(cmd, shell=True)
log.info("execute done")
finally:
os.environ = old_environ
示例3: read
def read(self, packet):
if not self.data_source_p:
log.info("End reading from: %s" % Util.safe_string_value(self.data_source))
return packet
if self.layer is None:
if self.sql and self.layer_idx == -1:
# PostgreSQL: Layer is gotten via Query
# http://trac.osgeo.org/postgis/wiki/UsersWikiOGR
self.layer = self.data_source_p.ExecuteSQL(self.sql)
self.layer_idx = 0
elif self.layer_idx < self.layer_count:
self.layer = self.data_source_p.GetLayer(self.layer_idx)
self.layer_idx += 1
if self.layer is None:
log.error("Could not fetch layer %d" % 0)
raise Exception()
log.info("Start reading from OGR Source: %s, Layer: %s" % (Util.safe_string_value(self.data_source), self.layer.GetName()))
else:
# No more Layers left: cleanup
packet.set_end_of_stream()
log.info("Closing OGR source: %s" % Util.safe_string_value(self.data_source))
# Destroy not required anymore: http://trac.osgeo.org/gdal/wiki/PythonGotchas
# self.data_source_p.Destroy()
self.data_source_p = None
return packet
# Return all features from Layer (ogr_feature_array) or next feature (ogr_feature)
if self.output_format == FORMAT.ogr_feature_array:
# Assemble all features
features = list()
for feature in self.layer:
features.append(feature)
packet.data = features
log.info("End reading all features from Layer: %s count=%d" % (self.layer.GetName(), len(features)))
packet.set_end_of_doc()
self.layer = None
else:
# Next feature
feature = self.layer.GetNextFeature()
if feature:
packet.data = feature
else:
log.info("End reading from Layer: %s" % self.layer.GetName())
packet.set_end_of_doc()
self.layer = None
return packet
示例4: write_end
def write_end(self, packet):
# Destroy not required anymore: http://trac.osgeo.org/gdal/wiki/PythonGotchas
# self.dest_fd.Destroy()
log.info("End writing to: %s" % Util.safe_string_value(self.dest_data_source))
self.dest_fd = None
self.layer = None
return packet
示例5: write
def write(self, packet):
# Are we all done?
if packet.data is None or self.dest_fd is None:
self.write_end(packet)
return packet
if self.layer is None:
log.info("No Layer, end writing to: %s" % Util.safe_string_value(self.dest_data_source))
return packet
# Assume ogr_feature_array input, otherwise convert ogr_feature to list
if type(packet.data) is list:
# Write feature collection to OGR Layer output
for feature in packet.data:
self.write_feature(feature)
self.write_end(packet)
else:
# Write single feature to OGR Layer output
if packet.end_of_stream or packet.end_of_doc:
self.write_end(packet)
return packet
self.write_feature(packet.data)
return packet
示例6: exec_cmd
def exec_cmd(self):
log.info("start ogr2ogr cmd = %s" % Util.safe_string_value(repr(self.cmd)))
self.ogr_process = subprocess.Popen(self.cmd,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
err_line = self.readline_err()
if err_line:
log.warning('ogr2ogr: %s ' % err_line)
示例7: __init__
def __init__(self, configdict, section, produces):
Input.__init__(self, configdict, section, produces)
# Create the list of files to be used as input
self.file_list = Util.make_file_list(self.file_path, None, self.filename_pattern, self.depth_search)
log.info("file_list=%s" % str(self.file_list))
if not len(self.file_list):
raise Exception('File list is empty!!')
self.file_list_done = []
示例8: __init__
def __init__(self, configdict, section):
FileInput.__init__(self, configdict, section, produces=FORMAT.string)
self.file = None
# Optional formatting of content according to Python String.format()
# Input file should have substitutable values like {schema} {foo}
# format_args should be of the form format_args = schema:test foo:bar
if self.format_args:
# Convert string to dict: http://stackoverflow.com/a/1248990
self.format_args = Util.string_to_dict(self.format_args, ':')
示例9: __init__
def __init__(self, configdict, section):
StringFilter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string)
# Formatting of content according to Python String.format()
# String should have substitutable values like {schema} {foo}
# format_args should be of the form format_args = schema:test foo:bar ...
self.format_args = self.cfg.get('format_args')
# Convert string to dict: http://stackoverflow.com/a/1248990
self.format_args_dict = Util.string_to_dict(self.format_args, ':')
示例10: execute_cmd
def execute_cmd(self, cmd):
env_vars = Util.string_to_dict(self.env_args, self.env_separator)
old_environ = os.environ.copy()
try:
os.environ.update(env_vars)
log.info("executing cmd=%s" % cmd)
result = subprocess.check_output(cmd, shell=True)
log.info("execute done")
return result
finally:
os.environ = old_environ
示例11: etree_elem2struct
def etree_elem2struct(packet, strip_space=True, strip_ns=True, sub=False, attr_prefix='', gml2ogr=True, ogr2json=True):
"""
:param packet:
:param strip_space:
:param strip_ns:
:param sub:
:param attr_prefix:
:param gml2ogr:
:param ogr2json:
:return:
"""
packet.data = Util.elem_to_dict(packet.data, strip_space, strip_ns, sub, attr_prefix, gml2ogr, ogr2json)
return packet
示例12: parse_args
def parse_args(args_list):
log.info("Stetl version = %s" % __version__)
argparser = argparse.ArgumentParser(description='Invoke Stetl')
argparser.add_argument('-c ', '--config', type=str, help='ETL config file in .ini format', dest='config_file',
required=False)
argparser.add_argument('-s ', '--section', type=str, help='Section in the config file to execute, default is [etl]',
dest='config_section', required=False)
argparser.add_argument('-a ', '--args', type=str,
help='Arguments or .properties files to be substituted for symbolic {argN}s in Stetl config file,\
as -a "arg1=foo arg2=bar" and/or -a args.properties, multiple -a options are possible',
dest='config_args', required=False, action='append')
argparser.add_argument('-d ', '--doc', type=str,
help='Get component documentation like its configuration parameters, e.g. stetl doc stetl.inputs.fileinput.FileInput',
dest='doc_args', required=False)
argparser.add_argument('-v', '--version',
action='store_true',
help='Show current version of stetl and exit',
required=False)
args = argparser.parse_args(args_list)
if args.config_args:
args_total = dict()
for arg in args.config_args:
if os.path.isfile(arg):
log.info('Found args file at: %s' % arg)
args_total = Util.merge_two_dicts(args_total, Util.propsfile_to_dict(arg))
else:
# Convert string to dict: http://stackoverflow.com/a/1248990
args_total = Util.merge_two_dicts(args_total, Util.string_to_dict(arg))
args.config_args = args_total
return args
示例13: __init__
def __init__(self, configdict, section, produces):
Input.__init__(self, configdict, section, produces)
# path to file or files: can be a dir or files or even multiple, comma separated
self.file_path = self.cfg.get('file_path')
# The filename pattern according to Python glob.glob
self.filename_pattern = self.cfg.get('filename_pattern', '*.[gxGX][mM][lL]')
# Recurse into directories ?
self.depth_search = self.cfg.get_bool('depth_search', False)
# Create the list of files to be used as input
self.file_list = Util.make_file_list(self.file_path, None, self.filename_pattern, self.depth_search)
log.info("file_list=%s" % str(self.file_list))
示例14: process_xml
def process_xml(self, packet):
while self.context is not None:
# while not packet.is_end_of_doc():
try:
event, elem = next(self.context)
except (etree.XMLSyntaxError, StopIteration):
# workaround for etree.XMLSyntaxError https://bugs.launchpad.net/lxml/+bug/1185701
self.context = None
if self.context is None:
# Always end of doc
# TODO: is this still useful for a non-input component?
packet.set_end_of_doc()
log.info("End of doc: %s elem_count=%d" % (self.cur_file_path, self.elem_count))
return packet
# Filter out Namespace from the tag
# this is the easiest way to go for now
tag = elem.tag.split('}')
if len(tag) == 2:
# Namespaced tag: 2nd is tag
tag = tag[1]
else:
# Non-namespaced tag: first
tag = tag[0]
if tag in self.element_tags:
if event == "start":
pass
elif event == "end":
packet.data = deepcopy(elem)
self.elem_count += 1
if self.strip_namespaces:
packet.data = Util.stripNamespaces(elem).getroot()
# Clear the element which has been read. Don't clear the root document,
# since the last element hasn't been processed yet.
elem.clear()
# If there is a next component, let it process
if self.next:
# Hand-over data (line, doc whatever) to the next component
packet.format = self._output_format
packet = self.next.process(packet)
return packet
示例15: GmlFeatureExtractor
#!/usr/bin/env python
#
# Extracts arrays of etree GML features from an GML etree document.
#
# Author: Just van den Broecke
#
from stetl.util import Util
from stetl.filter import Filter
from stetl.packet import FORMAT
log = Util.get_log('gmlfeatureextractor')
class GmlFeatureExtractor(Filter):
"""
Extract arrays of GML features etree elements from etree docs.
consumes=FORMAT.etree_doc, produces=FORMAT.etree_feature_array
"""
# XPATH Query base for extracting features by (non-namespaced thus local-name) tagname
xpath_base = "//*[local-name() = '%s']"
# Constructor
def __init__(self, configdict, section='gml_feature_extractor'):
Filter.__init__(self, configdict, section, consumes=FORMAT.etree_doc, produces=FORMAT.etree_feature_array)
log.info("cfg = %s" % self.cfg.to_string())
# Build the Xpath expresion from configures tagnames
self.feature_tags = self.cfg.get('feature_tags').split(',')