Parent: [831432] (diff)

Download this file

utils.py    109 lines (94 with data), 3.4 kB

  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
# This Source Code Form of OSSEval is subject to the terms of the GNU AFFERO
# GENERAL PUBLIC LICENSE, v. 3.0. If a copy of the AGPL was not
# distributed with this file, You can obtain one at http://www.gnu.org/licenses/agpl.txt
#
# OSSeval is powered by the SOS Open Source AGPL edition.
# The AGPL requires that you do not remove the SOS Open Source attribution and copyright
# notices from the user interface (see section 5.d below).
# OSSEval Copyright 2014 Bitergium SLL
# SOS Open Source Copyright 2012 Roberto Galoppini
# Author: Davide Galletti
from xml.dom import minidom
import urllib2
import OpenSourceProject
import base64
class Configuration():
api_key = OpenSourceProject.ohloh_api_key
max_number_of_records = OpenSourceProject.record_limit
class Forges(object):
ALL = 0
#SF = Sourceforge
SF = 1
#FC = Freecode was FM = Freshmeat
FC = 2
#FSF = Free Software Foundation
FSF = 5
#GH = Github
GH = 11
#GC = Google code
GC = 12
OHLOH = 100
CODEPLEX = 1000
APACHE = 1200
ECLIPSE = 1300
class xmlElements():
def __init__(self, xml):
self.xml = xml
def firstValue(self):
try:
return self.xml[0].firstChild.nodeValue
except:
return ""
class UrllibHelper():
@staticmethod
def urlopen(url_string, max_attempts = 3):
'''
Sometimes the download just fails for no apparent reason; retrying right after the
failure solves the issue; so this method retries max_attempts times with a default of 3
'''
success = False
n_attempts = 1
ret = ""
while not (success or n_attempts>max_attempts):
try:
response = urllib2.urlopen(url_string)
ret = response.read()
success = True
except Exception as ex:
print('Error downloading ' + url_string + " - Attempt n.:" + str(n_attempts) + " - " + str(ex))
n_attempts = n_attempts + 1
return ret
class StringList():
separator = " "
def __init__(self):
self.plain = []
self.base64_encoded = ""
def load_plain(self, strings):
self.plain = strings
separator = ""
for s in self.plain:
self.base64_encoded += separator + base64.b64encode(s)
separator = StringList.separator
return self
def load_base64(self, base64_encoded):
self.base64_encoded = base64_encoded
for s in self.base64_encoded.split(StringList.separator):
self.plain.append(base64.b64decode(s))
return self
def remove_empty_strings(self):
# self.load_plain(filter(lambda text: text.strip(), self.plain))
self.load_plain([text for text in self.plain if text.strip()])
class StringHelper():
@staticmethod
def removeNonAscii(thisString):
return "".join(filter(lambda x: ord(x)<128, thisString))
@staticmethod
def makeUnicodeSafe(thisString):
'''
It is probably equivalent to the above method
'''
while True:
try:
return unicode(thisString)
except UnicodeDecodeError as ex: #UnicodeDecodeError
thisString = thisString[0:ex.start] + thisString[ex.end:]