Skip to content

Commit 64f342e

Browse files
authored
Merge pull request #4 from laixintao/osx-support
move read_config() out of class
2 parents ee43dbd + a268716 commit 64f342e

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

MarkdownPicPicker.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import sys
66
import os
7+
78
try:
89
import pythoncom
910
except ImportError:
@@ -20,9 +21,10 @@
2021
__version__ = '0.2.2'
2122
__author__ = 'kingname'
2223

24+
from config import read_config
2325

24-
class MarkrdownPicPicker(object):
2526

27+
class MarkrdownPicPicker(object):
2628
CONFIG_PATH = 'config.ini'
2729

2830
def __init__(self, link_only=False):
@@ -45,8 +47,17 @@ def __init__(self, link_only=False):
4547
elif self.method == 'global_listen':
4648
self.keyboard_listen()
4749

50+
def _to_string(self):
51+
"""
52+
To test if the config reading is ok
53+
:return: None
54+
"""
55+
print("folder", self.picture_folder)
56+
print("suffix", self.picture_suffix)
57+
print("picture_bed", self.picture_bed)
58+
4859
def init_environment(self):
49-
self.read_config()
60+
self.__dict__.update(read_config())
5061
if not self.method \
5162
or not self.picture_folder \
5263
or not self.picture_suffix \
@@ -55,28 +66,8 @@ def init_environment(self):
5566
exit()
5667
if not os.path.exists(self.picture_folder):
5768
os.makedirs(self.picture_folder)
58-
59-
def read_config(self):
60-
if getattr(sys, 'frozen', None):
61-
config_path = os.path.join(os.path.dirname(sys.executable), self.CONFIG_PATH)
62-
else:
63-
config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.CONFIG_PATH)
64-
configs = ConfigParser()
65-
if not os.path.exists(config_path):
66-
print('can not find the config.ini, exit')
67-
exit()
68-
configs.read(config_path)
69-
self.method = configs['basic'].get('run_method', '')
70-
self.picture_folder = configs['basic'].get('picture_folder', '')
71-
self.picture_suffix = configs['basic'].get('picture_suffix', '')
72-
self.picture_bed = configs['basic'].get('picture_bed', '')
73-
7469
if self.picture_bed:
75-
self.uploader = QiniuUploader(configs['qiniu'])
76-
77-
if self.method == 'global_listen':
78-
self.short_key_one = configs['global_listen']['short_key_one']
79-
self.short_key_two = configs['global_listen']['short_key_two']
70+
self.uploader = QiniuUploader(self.uploader_info)
8071

8172
def keyboard_listen(self):
8273
if not pythoncom or not pyHook:
@@ -125,6 +116,7 @@ def save_picture(self):
125116
print('get picture from clipboard error because: {}'.format(e))
126117
return '', ''
127118

119+
128120
if __name__ == '__main__':
129121
arg = sys.argv[-1]
130122
if arg == '-linkonly':

config/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys, os
2+
from configparser import ConfigParser
3+
4+
5+
def read_config():
6+
_dict = {}
7+
if getattr(sys, 'frozen', None):
8+
config_path = os.path.join(os.path.dirname(sys.executable), 'config.ini')
9+
else:
10+
config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.ini')
11+
print(config_path)
12+
configs = ConfigParser()
13+
if not os.path.exists(config_path):
14+
print('can not find the config.ini, exit')
15+
exit()
16+
configs.read(config_path)
17+
_dict['method'] = configs['basic'].get('run_method', '')
18+
_dict['picture_folder'] = configs['basic'].get('picture_folder', '')
19+
_dict['picture_suffix'] = configs['basic'].get('picture_suffix', '')
20+
_dict['picture_bed'] = configs['basic'].get('picture_bed', '')
21+
22+
if _dict['picture_bed']:
23+
_dict['uploader_info'] = configs['qiniu']
24+
25+
if _dict['method'] == 'global_listen':
26+
_dict['short_key_one'] = configs['global_listen']['short_key_one']
27+
_dict['short_key_two'] = configs['global_listen']['short_key_two']
28+
29+
return _dict
30+
31+
32+
if __name__ == '__main__':
33+
print(read_config())
File renamed without changes.

markdownPicPicker/__init__.py

Whitespace-only changes.

pngpaste/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import datetime
2+
import os
3+
import sys
4+
import atexit
5+
from subprocess import call
6+
7+
8+
def capture():
9+
try:
10+
call(['/usr/local/bin/pngpaste', '-v'], stderr=open('/dev/null', 'w'))
11+
except OSError:
12+
print('please preinstall pngpaste use `brew install pngpaste` before use this script')
13+
sys.exit()
14+
15+
file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S.png')
16+
file_path = os.path.join('/tmp', file_name)
17+
atexit.register(lambda x: os.remove(x) if os.path.exists(x) else None, file_path)
18+
save = call(['/usr/local/bin/pngpaste', file_path])
19+
if save == 1:
20+
sys.exit()
21+
return file_path, file_name
22+
23+
24+
if __name__ == '__main__':
25+
print(capture())

0 commit comments

Comments
 (0)