# HG changeset patch # User Benoît Allard # Date 1419861659 -3600 # Node ID 9ed24f48df016a415524db9b192b679df9a5dea3 # Parent bb1dd2a55643f02ed5d51474d839a23ee2902e3e parsers/CVRF: Move the date parsing method to parsers/XML diff -r bb1dd2a55643 -r 9ed24f48df01 farolluz/parsers/cvrf.py --- a/farolluz/parsers/cvrf.py Mon Dec 29 14:58:29 2014 +0100 +++ b/farolluz/parsers/cvrf.py Mon Dec 29 15:00:59 2014 +0100 @@ -27,16 +27,13 @@ """ from __future__ import print_function +# Allow .xml to be different from xml +from __future__ import absolute_import -import re import textwrap import xml.etree.ElementTree as ET -from datetime import datetime, timedelta -try: - from datetime import timezone -except ImportError: - from ..py2 import FixedTimeZone as timezone +from .xml import parseDate from ..common import CVRFNote, CVRFAcknowledgment, CVRFReference from ..document import (CVRF, CVRFPublisher, CVRFTracking, CVRFRevision, @@ -63,19 +60,6 @@ return tuple(int(i) for i in string.split('.')) -def parseDate(string): - m = re.match('(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:([+-])(\d{2}):(\d{2})|(Z))?', string) - if (m.group(7) is None) or (m.group(7) == 'Z'): - tzhours = 0 - tzmin = 0 - else: - tzhours = int(m.group(8)) - if m.group(7) == '-': - tzhours = - tzhours - tzmin = int(m.group(9)) - return datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)), int(m.group(6)), tzinfo=timezone(timedelta(hours=tzhours, minutes=tzmin))) - - def parseNote(elem): return CVRFNote( elem.attrib['Type'], diff -r bb1dd2a55643 -r 9ed24f48df01 farolluz/parsers/xml.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/farolluz/parsers/xml.py Mon Dec 29 15:00:59 2014 +0100 @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Description: +# Methods for parsing CVE XML documents +# +# Authors: +# Benoît Allard +# +# Copyright: +# Copyright (C) 2014 Greenbone Networks GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + +"""\ +Methods for parsing of CVE XML Documents + +Ref: http://scap.nist.gov/schema/vulnerability/0.4 +""" + +import re + +from datetime import datetime, timedelta + +try: + from datetime import timezone +except ImportError: + from ..py2 import FixedTimeZone as timezone + +def parseDate(string): + m = re.match('(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:([+-])(\d{2}):(\d{2})|(Z))?', string) + if (m.group(7) is None) or (m.group(7) == 'Z'): + tzhours = 0 + tzmin = 0 + else: + tzhours = int(m.group(8)) + if m.group(7) == '-': + tzhours = - tzhours + tzmin = int(m.group(9)) + return datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)), int(m.group(6)), tzinfo=timezone(timedelta(hours=tzhours, minutes=tzmin)))