ROS Code
 All Classes Namespaces Files Functions Variables
system.py
Go to the documentation of this file.
1 # System modules
2 import subprocess
3 import os
4 import sys
5 import logging
6 import pkg_resources
7 import clipboard
8 from .roserrors import *
9 
10 # Get The Current Platform
11 
12 
14  return sys.platform
15 
16 
17 def shellcommand(command):
18  """
19 
20  Execute a command in the host's terminal/shell/bash
21 
22  command:
23  Specify the command to be executed
24 
25  """
26 
27  subprocess.call(str(command))
28 
29 
30 def pipupdate():
31  """
32 
33  Update all currently installed pip packages
34 
35  """
36 
37  packages = [d for d in pkg_resources.working_set]
38  subprocess.call('pip install --upgrade ' + ' '.join(packages))
39 
40 
41 # Tools For Directories (If Exists, Make And Delete)
42 
43 
44 def dirtool(operation, directory):
45  operation = operation.lower()
46  if operation == 'exists':
47  return bool(os.path.exists(directory))
48  if operation == 'create':
49  if os.path.exists(directory):
50  raise RuntimeError(
51  'An Error Has Occured: Directory Already Exists (0007)')
52  else:
53  os.makedirs(directory)
54  elif operation == 'delete':
55  if os.path.exists(directory):
56  os.rmdir(directory)
57  else:
58  raise RuntimeError(
59  'An Error Has Occured: Directory Doesn\'t Exist (0009)')
60  else:
61  raise RuntimeError(
62  'An Error Has Occured: Invalid Operation Entered (0008)')
63 
64 
65 # Tools For Files (If Exists, Make And Delete)
66 
67 
68 def file(operation, path):
69  operation = operation.lower()
70  if operation == 'exists':
71  return bool(os.path.isfile(path))
72  if operation == 'read':
73  if os.path.isfile(path):
74  with open(path, 'r') as f:
75  return [line.strip() for line in f]
76  raise RuntimeError('An Error Has Occured: File Not Found (0012)')
77  elif operation == 'delete':
78  if os.path.isfile(path):
79  os.remove(path)
80  else:
81  raise RuntimeError('An Error Has Occured: File Not Found (0012)')
82  elif operation == 'create':
83  if not file('exists', path):
84  open(path, 'w').close()
85  else:
86  raise RuntimeError(
87  'An Error Has Occured: File Already Exists (0013)')
88  elif operation == 'clear':
89  if os.path.isfile(path):
90  open(path, 'w').close()
91  raise RuntimeError('An Error Has Occured: File Not Found (0012)')
92  else:
93  raise RuntimeError(
94  'An Error Has Occured: Invalid Operation Entered (0008)')
95 
96 
97 # Exit the current execution
98 
99 
100 def exitexecution(arguments=0):
101  sys.exit(arguments)
102 
103 
104 # Get The Character Limit
105 
106 
107 def charlimit():
108  return sys.maxsize
109 
110 
111 # Get The Highest Unicode Value
112 
113 
114 def unilimit():
115  return sys.maxunicode
116 
117 
118 # Get the version of Python
119 
120 
121 def pyversion(part=None):
122  if part is None:
123  return sys.version_info
124  return sys.version_info[part]
125 
126 
127 # Get the executable used by Python
128 
129 
130 def pyexec():
131  return sys.executable
132 
133 
134 # Print Python copyright information
135 
136 
138  return sys.copyright
139 
140 
141 # Set logging status dependant on if debug is enabled
142 
143 
144 def loglevel(leveltype=None, isequal=False):
145  """
146 
147  Set the logging level of ROS Code
148 
149  without arguments:
150  Gives you the logging level
151 
152  leveltype:
153  Choose the logging level. Possible choices are none (0), debug (10), info (20), warning (30), error (40) and critical (50)
154 
155  isequal:
156  Instead of setting the level, returns True if the level is equal to leveltype. Otherwise, returns False
157 
158  """
159  leveltype = leveltype.lower()
160  loglevels = {
161  "none": 0,
162  "debug": 10,
163  "info": 20,
164  "warning": 30,
165  "error": 40,
166  "critical": 50
167  }
168  if leveltype is None and isequal is False:
169  return logging.getEffectiveLevel()
170  if leveltype is not None and isequal is True:
171  if leveltype in loglevels.values():
172  return leveltype == logging.getEffectiveLevel()
173  elif leveltype in loglevels:
174  return loglevels[leveltype] == logging.getEffectiveLevel()
175  else:
176  raise WrongInput(
177  "Incorrect input provided. It should be none, debug, info, warning, error or critical"
178  )
179  if leveltype in loglevels.values():
180  logging.basicConfig(level=leveltype)
181  elif leveltype in loglevels:
182  logging.basicConfig(level=loglevels[leveltype])
183  else:
184  raise WrongInput(
185  "Incorrect input provided. It should be none, debug, info, warning, error or critical"
186  )
187 
188 
189 def logfile(targetfile="ros.log"):
190  """
191 
192  Set the file for ROS Code to log to
193 
194  targetfile:
195  Change the file to log to. By default it is ros.log
196 
197  """
198 
199  try:
200  str(targetfile)
201  except BaseException:
202  raise ConversionError("Cannot convert type " + str(type(targetfile)) +
203  "to str")
204  try:
205  logging.basicConfig(filename=str(targetfile))
206  except BaseException:
207  raise WrongInput("Invalid target file specified")
208 
209 
210 # Gets, sets, appends or preceeds the clipboard contents
211 
212 
213 def clipaction(action='get', text=None):
214  if action == 'get':
215  return clipboard.paste()
216  elif action == 'set':
217  clipboard.copy(str(text))
218  elif action == 'append':
219  clipboard.copy(str(clipboard.paste) + str(text))
220  elif action == 'preceed':
221  clipboard.copy(str(text) + str(clipboard.paste))
222 
223 
224 # Tools For Text Files
225 
226 
227 def text(operation, path, argument):
228  operation = operation.lower()
229  if operation == 'write':
230  if os.path.isfile(path):
231  fh = open(path, 'w')
232  fh.write(argument)
233  else:
234  raise RuntimeError('An Error Has Occured: File Not Found (0012)')
235  elif operation == 'append':
236  if os.path.isfile(path):
237  fh = open(path, 'a')
238  fh.write(argument)
239  else:
240  raise RuntimeError('An Error Has Occured: File Not Found (0012)')
def pycopyright
Definition: system.py:137
def charlimit
Definition: system.py:107
def loglevel
Definition: system.py:144
def text
Definition: system.py:227
def pyversion
Definition: system.py:121
def exitexecution
Definition: system.py:100
def pyexec
Definition: system.py:130
def getplatform
Definition: system.py:13
def pipupdate
Definition: system.py:30
def clipaction
Definition: system.py:213
def logfile
Definition: system.py:189
def shellcommand
Definition: system.py:17
def file
Definition: system.py:68
def dirtool
Definition: system.py:44
def unilimit
Definition: system.py:114