-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadata_Record.py
111 lines (91 loc) · 3.83 KB
/
Metadata_Record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
LOCAL_DEBUG = False
#====================================================#
# Purpose: A generic data structure to hold #
# information found in each metadata record #
# Properties: title - The title of the book the #
# metadata record describes ( #
# Required field of all metadata #
# types) #
# SCN - The XCN of the book the metadata #
# record describes (Required field #
# of all metadata types) #
# Superclass: object #
# NOTE: Can be used as a concrete class but the #
# recommendation is to subclass and treat it #
# as an abstract class so that more specific #
# types of metadata might be contained (ex. #
# Marc_XML, etc...) #
#====================================================#
class Metadata_Record(object):
#----------------------------------------------------#
# Purpose: Initialize a Metadata_Recored with #
# default variables (constructor) #
# Parameters: self (implicit) - The instance of the #
# object the function #
# is invoked on #
# Return: N/A #
# See Also: __init__ documentation in Python #
# documentaion #
#----------------------------------------------------#
def __init__(self):
if LOCAL_DEBUG:
print '===================================================='
print 'Call Summary for __init__ (Metadata_Record)'
print '----------------------------------------------------'
print 'Self: ' + str(self)
print '===================================================='
self.title = None
self.SCN = None
def _generate_new_SCN(self):
from datetime import datetime
# Create and parse a timestamp as part of new
# SCN generation
now = datetime.now()
year = str(now.year)
month = str(now.month)
month = month.zfill(2)
day = str(now.day)
day = day.zfill(2)
hour = str(now.hour)
hour = hour.zfill(2)
minute = str(now.minute)
minute = minute.zfill(2)
second = str(now.second)
second = second.zfill(2)
microsecond = str(now.microsecond)
microsecond = microsecond.zfill(7)
# Generate the entire new SCN
newSCN = "(AutoGenerated)" + year + month + day + hour + minute + second + microsecond
return newSCN
#----------------------------------------------------#
# Purpose: To validate the object (check that it #
# contains both an SCN and title at minimum #
# and any other checks that need to occur #
# to make sure its valid) #
# Parameters: self (implicit) - The instance of the #
# object the function #
# is invoked ont #
# Return: boolean - If this instance of #
# Metadata_Record is valid #
#----------------------------------------------------#
def validate(self):
is_valid = True
if self.SCN is None:
if self.title is None:
is_valid = False
else:
# Generate the entire new SCN
newSCN = self._generate_new_SCN()
# Return the result
return is_valid
def __str__(self):
result_str = '{'
attrs = vars(self)
for k,v in attrs.iteritems():
if k.startswith('_'):
result_str += k[1:] + ':' + unicode(v) + ', '
else:
result_str += k + ':' + unicode(v) + ', '
result_str = result_str[:-2] + '}'
result_str = unicode(result_str)
return result_str