Coverage for tatlin/lib/util.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 05:56 +0000

1# -*- coding: utf-8 -*- 

2# Copyright (C) 2024 Denis Kobozev 

3# 

4# This program is free software; you can redistribute it and/or modify 

5# it under the terms of the GNU General Public License as published by 

6# the Free Software Foundation; either version 2 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program; if not, write to the Free Software Foundation, 

16# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

17 

18import os 

19import os.path 

20import sys 

21from tatlin.lib.constants import RECENT_FILE_LIMIT 

22 

23from tatlin.lib.ui.dialogs import OpenDialog 

24 

25 

26def format_float(f): 

27 return "%.2f" % f 

28 

29 

30def resolve_path(fpath): 

31 if os.path.isabs(fpath): 

32 return fpath 

33 

34 if getattr(sys, "frozen", False): 

35 # we are running in a PyInstaller bundle 

36 basedir = os.path.join(sys._MEIPASS, "tatlin") # type:ignore 

37 else: 

38 # we are running in a normal Python environment 

39 basedir = os.path.dirname(os.path.dirname(__file__)) 

40 

41 return os.path.join(basedir, fpath) 

42 

43 

44def format_status(name, size_bytes, vertex_count): 

45 if size_bytes >= 2**30: 

46 size = size_bytes / 2**30 

47 units = "GB" 

48 elif size_bytes >= 2**20: 

49 size = size_bytes / 2**20 

50 units = "MB" 

51 elif size_bytes >= 2**10: 

52 size = size_bytes / 2**10 

53 units = "KB" 

54 else: 

55 size = size_bytes 

56 units = "B" 

57 

58 vertex_plural = "vertex" if int(str(vertex_count)[-1]) == 1 else "vertices" 

59 

60 return " %s (%.1f%s, %d %s)" % ( 

61 name, 

62 size, 

63 units, 

64 vertex_count, 

65 vertex_plural, 

66 ) 

67 

68 

69def get_recent_files(config): 

70 recent_files = [] 

71 

72 conf_files = config.read("ui.recent_files") 

73 if conf_files: 

74 for f in conf_files.split(os.path.pathsep): 

75 if f[-1] in ["0", "1", "2"]: 

76 fpath, ftype = f[:-1], OpenDialog.ftypes[int(f[-1])] 

77 else: 

78 fpath, ftype = f, None 

79 

80 if os.path.exists(fpath): 

81 recent_files.append((os.path.basename(fpath), fpath, ftype)) 

82 recent_files = recent_files[:RECENT_FILE_LIMIT] 

83 

84 return recent_files