forked from cihai/unihan-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_metadata.py
61 lines (42 loc) · 1.48 KB
/
package_metadata.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, \
with_statement, unicode_literals
import os
import sys
import re
package_file = os.path.join(os.path.dirname(__file__), "__init__.py")
file_content = open(package_file, "rt").read()
class Package_Metadata(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
attributes = [
'title', 'package_name', 'author', 'description', 'email',
'version', 'license', 'copyright'
]
@staticmethod
def get_attribute(attr, file_content):
regex_expression = r"^__{0}__ = ['\"]([^'\"]*)['\"]".format(attr)
mo = re.search(regex_expression, file_content, re.M)
if mo:
return mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (package_file,))
def refresh(self, attributes):
file_content = open(self.package_file, "rt").read()
for k in attributes:
attr_val = self.get_attribute(k, file_content)
if attr_val:
self[k] = attr_val
def __init__(self, package_file, attributes=None):
if attributes:
self.attributes = attributes
self.package_file = package_file
self.refresh(self.attributes)
p = Package_Metadata(package_file)
def print_metadata():
for k, v in p.items():
print('%s: %s' % (k, v))
if __name__ == '__main__':
print_metadata()
sys.exit()