Permalink
Browse files

begin file command

  • Loading branch information...
ilmiont committed Nov 20, 2017
1 parent ad810ed commit d930424cde26c6c24d02b2bce03116a7d9ed5fe5
Showing with 93 additions and 4 deletions.
  1. +76 −0 com/file.py
  2. +14 −4 paths.py
  3. +3 −0 strings.py
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
#'''
#ipm
#/com/file.py
#By James Walker.
#Copyright ©James Walker 2017. Licensed under the MIT License.
#
#This file contains the "file" command for ipm to create new files.
import os, paths, strings
from comc import Command
from project import Project
#'''
#CLASS file(Command)
#ipm file creation command.
#'''
class file(Command):
#'''
#FUNCTION file.run(self)
#Implement the run method.
#'''
def run(self):
#Get filename to create
if len(self.args) < 1:
sys.exit(strings.FILE_NOPATH)
filename = self.args[0]
#Create the path to the file
path = os.path.join(self.wd, filename)
if not os.path.exists(path):
os.makedirs(path)
#Create the file - "a" to avoid overwriting if exists already
file = open(path, "a")
#Check whether to populate the file with a template
if len(self.args) >= 2:
self.populate(file, self.args[1])
#Close the file handle
file.close()
#'''
#FUNCTION file.populate(file, template)
#Populates the file handle <file> with the template <template>.
#'''
def populate(file, template):
#Configure variables
found = False
#Find the template
for path in paths.TEMPLATES_PLACES:
path = os.path.join(path, template)
if os.path.exists(path):
found = True
break
if not found:
print(strings.FILE_TEMPLATE_NO)
return False
#Print template path
print(strings.FILE_TEMPLATE + template)
#Load the template contents
template_file = open(path, "r")
template = template_file.read()
template_file.close()
#Find template tags
#'''
#End of file.
#'''
@@ -10,29 +10,39 @@
import app, os, sys
#ROOT LOCATIONS
INSTALL = ""
USER = ".ipm"
WD = os.getcwd()
TEMPLATES = "templates"
TEMPLATES = INSTALL += "templates"
#PREPARE <USER> FROM ENVIRONMENT
if sys.platform == "win32":
USER = os.getenv("APPDATA") + "\\" + USER
USER = os.path.join(os.getenv("APPDATA"), USER)
elif sys.platform == "linux":
USER = "~/" + USER
USER = os.path.join("~", USER)
else:
sys.exit("ipm aborting: unsupported platform path \"" + sys.platform + "\".")
#CHECK WHETHER ENVIRONMENT SPECIFIED
for arg in sys.argv:
if arg.startswith(app.ENV_ARG):
USER += arg.lstrip(app.ENV_ARG)
#CHECK <USER> EXISTS
if not os.isdir(USER):
try:
os.mkdir(USER)
except:
sys.exit("ipm aborting: could not make user directory \"" + USER + "\".")
#PLACES TO SEARCH FOR TEMPLATE NAMES
TEMPLATES_PLACES = [
os.path.join(INSTALL, TEMPLATES),
os.path.join(USER, TEMPLATES),
WD
]
#'''
#End of file.
#'''
@@ -12,6 +12,9 @@
ABORT_RUNTIME = "ipm: aborting, unhandled error occurred."
ABORT_NOCOMMAND = "ipm aborting: no command specified."
ABORT_NOFILE = "ipm aborting: ipm file not found."
FILE_NOPATH = "ipm aborting: no file path specified."
FILE_TEMPLATE = "ipm: found template file: "
FILE_TEMPLATE_NO = "ipm error: could not find template file in any path: "
INIT_START = "ipm: initialising new project in working directory..."
INIT_DONE = "ipm: initialised new project in working directory."
INIT_EXISTING = "ipm: working directory already initialised with project."

0 comments on commit d930424

Please sign in to comment.