diff --git a/net-p2p/py-ed2k-tools/files/patch-ed2k__metutils.py b/net-p2p/py-ed2k-tools/files/patch-ed2k__metutils.py index 7cd01f6f60d3..7c4cfa1ac8ee 100644 --- a/net-p2p/py-ed2k-tools/files/patch-ed2k__metutils.py +++ b/net-p2p/py-ed2k-tools/files/patch-ed2k__metutils.py @@ -1,97 +1,116 @@ --- ed2k_metutils.py.orig 2003-05-06 11:53:14 UTC +++ ed2k_metutils.py -@@ -6,7 +6,6 @@ +@@ -6,24 +6,24 @@ # tested on macosx 10.2.4, python 2.2 import struct; -import types; import sys; # Some defines. -@@ -14,16 +13,16 @@ import sys; + TAG_TYPE_STRING = 2; TAG_TYPE_INTEGER = 3; ++TAG_TYPE_BLOB = 7 -TAG_HANDLE_FILENAME = chr( 1 ); -TAG_HANDLE_FILESIZE = chr( 2 ); -TAG_HANDLE_FILETYPE = chr( 3 ); -TAG_HANDLE_FILEFORMAT = chr( 4 ); -TAG_HANDLE_SOFAR = chr( 8 ); -TAG_HANDLE_GAP_START = chr( 9 ); -TAG_HANDLE_GAP_END = chr( 10 ); -TAG_HANDLE_TEMP_NAME = chr( 18 ); -TAG_HANDLE_PAUSED = chr( 20 ); -TAG_HANDLE_PRIORITY = chr( 24 ); +TAG_HANDLE_FILENAME = b'\x01' +TAG_HANDLE_FILESIZE = b'\x02' +TAG_HANDLE_FILETYPE = b'\x03' +TAG_HANDLE_FILEFORMAT = b'\x04' +TAG_HANDLE_SOFAR = b'\x08' +TAG_HANDLE_GAP_START = b'\x09' +TAG_HANDLE_GAP_END = b'\x0a' +TAG_HANDLE_TEMP_NAME = b'\x12' +TAG_HANDLE_PAUSED = b'\x14' +TAG_HANDLE_PRIORITY = b'\x18' class MetFile: """Class designed to hold the data of a .part.met file.""" -@@ -39,7 +38,7 @@ class MetFile: +@@ -39,7 +39,7 @@ class MetFile: # a .part file must exist, even if it's empty. The same doesn't apply for new overnet. self.version = 225; self.modDate = 0; - self.fileID = '\0' * 16; + self.fileID = b'\0' * 16 return; header_struct = " [x.part.met ...] | >" % sys.argv[ 0 ]; - print - print "This program will show the amount downloaded vs. the total size " - print "for the .part.met files listed on the command line." - print - print "This program assumes an 80 column display. You can tweak this " - print "by editing the script. Change the 'WIDTH' value." + print("invocation: %s < [x.part.met ...] | >" % sys.argv[0]) + print() + print("This program will show the amount downloaded vs. the total size") + print("for the .part.met files listed on the command line.") sys.exit( -1 ); total_size = total_down = 0; -@@ -34,7 +29,7 @@ if __name__ == "__main__": + +- sta = os.stat( sys.argv[ 1 ] )[ 0 ]; +- if stat.S_ISDIR( sta ): +- mets = [ "%s%s" % ( sys.argv[ 1 ], x ) for x in os.listdir( sys.argv[ 1 ] ) if x.endswith( ".met" ) ]; ++ if os.path.isdir(sys.argv[1]): ++ mets = [ "%s/%s" % (sys.argv[1], x) for x in os.listdir(sys.argv[1]) if x.endswith(".met") ] + else: + mets = sys.argv[ 1 : ]; for met_file in mets: - fh = open( met_file, "r" ); + fh = open(met_file, "rb") data = fh.read(); fh.close(); -@@ -43,7 +38,7 @@ if __name__ == "__main__": +@@ -43,7 +36,7 @@ if __name__ == "__main__": # We're interested in the name, the total size, and some kind of... anti-gapping. size = met_data.FindTags( TAG_HANDLE_FILESIZE )[ 0 ].value; - name = met_data.FindTags( TAG_HANDLE_FILENAME )[ 0 ].value; + name = met_data.FindTags(TAG_HANDLE_FILENAME)[0].value.decode() # Set the total downloaded to the file size. down = size; -@@ -71,19 +66,42 @@ if __name__ == "__main__": +@@ -71,19 +64,42 @@ if __name__ == "__main__": bar = "#" * ( WIDTH - 2 ); for gap in gaps: gap_start, gap_end = gaps[ gap ]; - char_gap_start = gap_start / bytes_per_char; - char_gap_end = gap_end / bytes_per_char; + char_gap_start = int(gap_start / bytes_per_char) + char_gap_end = int(gap_end / bytes_per_char) bar = bar[ : char_gap_start ] + " " * ( char_gap_end - char_gap_start ) + bar[ char_gap_end : ]; + + # Account for CJK characters occupy two fixed-width spaces. + def char_width(c: str) -> int: + if not c.isprintable(): return 0 + return 2 if unicodedata.category(c) == 'Lo' else 1 + + def visible_len(s: str) -> int: + return sum(char_width(c) for c in s) + + # Truncate string to specified limit. If truncation happens + # on double-width character (like it would have to be cut in + # half), append an extra space for nicer alignment. + def visible_substr_padded(s: str, l: int) -> str: + vislen = 0 + cut_here = 0 + padding = '' + for c in s: + vislen += char_width(c) + if vislen <= l: cut_here += 1 + if vislen == l: break + if vislen > l: padding = ' '; break + return s[:cut_here] + padding # Print out our summary. Limit the filenames. - sizestring = " - %s - %iK of %iK" % ( met_file.split( "/" )[ -1 ], down / 1024, size / 1024 ); + sizestring = " - %s - %.2fK of %.2fK" % (met_file.split("/")[-1], down / 1024, size / 1024) max_name_size = WIDTH - len( sizestring ); - if len( name ) < max_name_size: - name += " " * ( max_name_size - len( name ) ); + vislen = visible_len(name) + if vislen < max_name_size: + name += " " * (max_name_size - vislen) else: - name = name[ : max_name_size ]; - print "%s%s" % ( name, sizestring ); - print "[%s]" % bar; - print + name = visible_substr_padded(name, max_name_size) + print("%s%s" % (name, sizestring)) + print("[%s]" % bar) + print() del( met_data ); - print "Totals: %sK of %sK" % ( total_down / 1024, total_size / 1024 ); + print("Totals: %.2fK of %.2fK" % (total_down / 1024, total_size / 1024))