Skip to content

Commit

Permalink
ISSUE #8, moving load_rule outside CNV, into utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
castelao committed Mar 30, 2016
1 parent ca72f30 commit 70a23c7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
32 changes: 3 additions & 29 deletions seabird/cnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from seabird.exceptions import CNVError
# from seabird.utils import basic_logger
from seabird.utils import load_rule

logging.basicConfig(level=logging.DEBUG)


Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self, raw_text, defaults=None, logger=None):
self.defaults = defaults
self.attributes = {}
# ----
self.load_rule()
self.rule, self.parsed = load_rule(self.raw_text)

if not hasattr(self, 'parsed'):
return
Expand Down Expand Up @@ -90,34 +92,6 @@ def __getitem__(self, key):
return d
raise KeyError('%s not found' % key)

def load_rule(self):
""" Load the adequate rules to parse the data
It should try all available rules, one by one, and use the one
which fits.
"""
rules_dir = 'rules'
rule_files = pkg_resources.resource_listdir(__name__, rules_dir)
rule_files = [f for f in rule_files if re.match('^cnv.*yaml$', f)]
for rule_file in rule_files:
text = pkg_resources.resource_string(
__name__, os.path.join(rules_dir, rule_file))
rule = yaml.load(text)
# Should I load using codec, for UTF8?? Do I need it?
# f = codecs.open(rule_file, 'r', 'utf-8')
# rule = yaml.load(f.read())
r = rule['header'] + rule['sep'] + rule['data']
content_re = re.compile(r, re.VERBOSE)
if re.search(r, self.raw_text, re.VERBOSE):
logging.debug("Using rules from: %s" % rule_file)
self.rule = rule
self.parsed = content_re.search(self.raw_text).groupdict()
return

# If haven't returned a rule by this point, raise an exception.
logging.error("No rules able to parse it")
raise CNVError(tag='noparsingrule')

def raw_header(self):
r = self.rule['header'] + self.rule['sep']
content_re = re.compile(r, re.VERBOSE)
Expand Down
39 changes: 39 additions & 0 deletions seabird/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import re
import logging
import pkg_resources

# import codecs
import yaml

from seabird.exceptions import CNVError
## from seabird.utils import basic_logger
#logging.basicConfig(level=logging.DEBUG)


def make_file_list(inputdir, inputpattern=".*\.cnv"):
Expand Down Expand Up @@ -51,3 +59,34 @@ def press2depth(press, latitude):
depth = -((((-1.82e-15 * press + 2.279e-10) * press - 2.2512e-5) *
press + 9.72659) * press) / g
return depth




def load_rule(raw_text):
""" Load the adequate rules to parse the data
It should try all available rules, one by one, and use the one
which fits.
"""
rules_dir = 'rules'
rule_files = pkg_resources.resource_listdir(__name__, rules_dir)
rule_files = [f for f in rule_files if re.match('^cnv.*yaml$', f)]
for rule_file in rule_files:
text = pkg_resources.resource_string(
__name__, os.path.join(rules_dir, rule_file))
rule = yaml.load(text)
# Should I load using codec, for UTF8?? Do I need it?
# f = codecs.open(rule_file, 'r', 'utf-8')
# rule = yaml.load(f.read())
r = rule['header'] + rule['sep'] + rule['data']
content_re = re.compile(r, re.VERBOSE)
if re.search(r, raw_text, re.VERBOSE):
#logging.debug("Using rules from: %s" % rule_file)
#self.rule = rule
parsed = content_re.search(raw_text).groupdict()
return rule, parsed

# If haven't returned a rule by this point, raise an exception.
#logging.error("No rules able to parse it")
raise CNVError(tag='noparsingrule')

0 comments on commit 70a23c7

Please sign in to comment.