Split up finance.py and make it a python module, also added tests for it.
[pyValueAverager.git] / finance / __init__.py
blobd15060aca97b498cadcf35aab7ed66ba1a56aba2
2 # Copyright (C) 2006 Google Inc.
3 # Copyright (C) 2008 David Euresti
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 try:
19 from xml.etree import cElementTree as ElementTree
20 except ImportError:
21 try:
22 import cElementTree as ElementTree
23 except ImportError:
24 from elementtree import ElementTree
26 import atom
27 import gdata
29 GFIN_NAMESPACE = 'http://schemas.google.com/finance/2007'
31 class PortfolioListEntry(gdata.GDataEntry):
32 _tag = 'entry'
33 _namespace = atom.ATOM_NAMESPACE
34 _children = gdata.GDataEntry._children.copy()
35 _attributes = gdata.GDataEntry._attributes.copy()
38 def PortfolioListEntryFromString(xml_string):
39 """Converts an XML string into a PortfolioListEntry object.
41 Args:
42 xml_string: string The XML describing a Document List feed entry.
44 Returns:
45 A DocumentListEntry object corresponding to the given XML.
46 """
47 return atom.CreateClassFromXMLString(PortfolioListEntry,
48 xml_string)
50 class PortfolioListFeed(gdata.GDataFeed):
51 """A feed containing a list of Portfolio Items"""
53 _tag = 'feed'
54 _namespace = atom.ATOM_NAMESPACE
55 _children = gdata.GDataFeed._children.copy()
56 _attributes = gdata.GDataFeed._attributes.copy()
57 _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
58 [PortfolioListEntry])
60 def PortfolioListFeedFromString(xml_string):
61 """Converts an XML string into a PortfolioListFeed object.
63 Args:
64 xml_string: string The XML describing a PortfolioList feed.
66 Returns:
67 A PortfolioListFeed object corresponding to the given XML.
68 """
69 return atom.CreateClassFromXMLString(PortfolioListFeed, xml_string)
72 class Symbol(atom.AtomBase):
74 _tag = 'symbol'
75 _namespace = GFIN_NAMESPACE
76 _children = atom.AtomBase._children.copy()
77 _attributes = atom.AtomBase._attributes.copy()
78 _attributes['symbol'] = 'symbol'
79 _attributes['fullName'] = 'fullName'
81 def __init__(self, extension_elements=None, value=None, scope_type=None,
82 extension_attributes=None, text=None):
83 self.value = value
84 self.type = scope_type
85 self.text = text
86 self.extension_elements = extension_elements or []
87 self.extension_attributes = extension_attributes or {}
89 class PositionData(atom.AtomBase):
91 _tag = 'positionData'
92 _namespace = GFIN_NAMESPACE
93 _children = atom.AtomBase._children.copy()
94 _attributes = atom.AtomBase._attributes.copy()
95 _attributes['shares'] = 'shares'
97 def __init__(self, extension_elements=None, value=None, scope_type=None,
98 extension_attributes=None, text=None):
99 self.value = value
100 self.type = scope_type
101 self.text = text
102 self.extension_elements = extension_elements or []
103 self.extension_attributes = extension_attributes or {}
106 class PositionListEntry(gdata.GDataEntry):
107 _tag = 'entry'
108 _namespace = atom.ATOM_NAMESPACE
109 _children = gdata.GDataEntry._children.copy()
110 _attributes = gdata.GDataEntry._attributes.copy()
112 _children['{%s}symbol' % GFIN_NAMESPACE] = ('symbol', Symbol)
113 _children['{%s}positionData' % GFIN_NAMESPACE] = ('positionData',
114 PositionData)
117 def PositionListEntryFromString(xml_string):
118 """Converts an XML string into a PositionListEntry object.
120 Args:
121 xml_string: string The XML describing a Position List feed entry.
123 Returns:
124 A PositionListEntry object corresponding to the given XML.
126 return atom.CreateClassFromXMLString(PositionListEntry, xml_string)
130 class PositionListFeed(gdata.GDataFeed):
131 """A feed containing a list of Position Items"""
133 _tag = 'feed'
134 _namespace = atom.ATOM_NAMESPACE
135 _children = gdata.GDataFeed._children.copy()
136 _attributes = gdata.GDataFeed._attributes.copy()
137 _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry',
138 [PositionListEntry])
141 def PositionListFeedFromString(xml_string):
142 """Converts an XML string into a PositionListFeed object.
144 Args:
145 xml_string: string The XML describing a PositionList feed.
147 Returns:
148 A PositionListFeed object corresponding to the given XML.
150 return atom.CreateClassFromXMLString(PositionListFeed, xml_string)